Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GX Platformer Tutorial
#10
Part 5 - Collision Detection
So, if we want to prevent our player character from being able to walk through walls, we need to be able to detect when a collision has occurred.  

GX has built-in support for two different types of collision detection. The first, which we'll look at now, handles the scenario when an entity collides with a given tile position on the map.  The second, which we may cover in the future, handles the scenario when an entity collides with another entity.

To implement tile collision detection, we need to handle a new event (GXEVENT_COLLISION_TILE).  So, let's add a new Case statement for this event to our main event function GXOnGameEvent and create a new method for this logic:
Code: (Select All)
...
Sub GXOnGameEvent (e As GXEvent)
    Select Case e.event
        Case GXEVENT_UPDATE: OnUpdate e
        Case GXEVENT_COLLISION_TILE: OnTileCollision e
    End Select
End Sub
...
Sub OnTileCollision(e As GXEvent)
    If GXMapTile(e.collisionTileX, e.collisionTileY, 1) Then
        e.collisionResult = GX_TRUE
    End If
End Sub
...
GX is going to pass us the handle of the entity and information about the tile with which the entity is currently intersecting.  It does this by setting values in the GXEvent object that is passed to our event handler.  So far, we have only been concerned with the event type. For the tile collision event GX will also set the "entity" attribute to the handle of the entity involved in the collision as well as the position of the tile in the "collisionTileX" and "collisionTileY" attributes. The GXEvent object has an additional attribute which we can use to communicate back to the engine whether a collision was encountered, "collisionResult".

Since there is currently only one entity (the player), we need only check the map to see whether there is a tile present at the location by calling the GXMapTile function.

If we run the program now, we'll see that the player will not be able to walk past the walls on either side of the screen.
   
Ok, well that fixed one of our problems, but our player still seems to be walking through the air.  Now that we have our collision detection in place, we can turn on the gravity and our player won't fall through the floor. In GX, gravity is applied at the entity level.  We can apply this to our player by adding a call to the GXEntityApplyGravity method:
Code: (Select All)
...
' Create the player
Dim Shared player As Long
player = GXEntityCreate("img/character.png", 16, 20, 4)
GXEntityPos player, 120, 75
GXEntityApplyGravity player, GX_TRUE
...

This will now cause our player to fall down to the ground tiles:
   
You may also notice that the player is able to walk in front of the tree and barrel tiles.  This is because we are only checking for collisions on layer 1.

Now we can fall, but we're missing a key element of any good platformer... the jump.  We can add this by triggering a sudden upward velocity when the user presses the spacebar by adding the following to our OnUpdate method:
Code: (Select All)
...
    Else
        GXEntityVX player, 0
        GXEntityAnimateStop player
    End If
   
    If GXEntityVY(player) = 0 And GXKeyDown(GXKEY_SPACEBAR) Then
        GXEntityVY player, -120
    End If
End Sub
...
In addition to checking to see if the spacebar is pressed, we also want to make sure that the player's current Y velocity is 0.  We don't want him to be able to be able to start a jump when he is already jumping or falling.

Putting it all together, we can now walk and jump around our little map!


Next time we'll look at refining the controls and movement.


Attached Files
.zip   part5.zip (Size: 82.35 KB / Downloads: 540)
Reply


Messages In This Thread
GX Platformer Tutorial - by dbox - 03-05-2025, 02:01 AM
RE: GX Platformer Tutorial - by grymmjack - 03-05-2025, 01:28 PM
RE: GX Platformer Tutorial - by dbox - 03-05-2025, 07:19 PM
RE: GX Platformer Tutorial - by dbox - 03-06-2025, 01:45 AM
RE: GX Platformer Tutorial - by dbox - 03-07-2025, 08:58 PM
RE: GX Platformer Tutorial - by madscijr - 03-08-2025, 12:32 AM
RE: GX Platformer Tutorial - by dbox - 03-08-2025, 02:24 AM
RE: GX Platformer Tutorial - by dbox - 03-08-2025, 06:03 AM
RE: GX Platformer Tutorial - by dbox - 03-14-2025, 12:42 AM
RE: GX Platformer Tutorial - by dbox - 03-14-2025, 10:23 PM
RE: GX Platformer Tutorial - by dbox - 03-17-2025, 05:56 AM
RE: GX Platformer Tutorial - by dbox - 03-19-2025, 03:54 AM
RE: GX Platformer Tutorial - by dbox - 03-20-2025, 07:52 PM
RE: GX Platformer Tutorial - by Unseen Machine - 03-21-2025, 02:12 AM
RE: GX Platformer Tutorial - by dbox - 03-21-2025, 05:44 PM
RE: GX Platformer Tutorial - by dbox - 03-26-2025, 12:56 AM
RE: GX Platformer Tutorial - by dbox - 03-26-2025, 08:51 PM
RE: GX Platformer Tutorial - by vince - 10-01-2025, 10:48 AM
RE: GX Platformer Tutorial - by vince - 10-03-2025, 04:04 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)