Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GX Platformer Tutorial
#13
Part 8 - Collision Layer
In Part 5 we implemented our original collision detection.  It is based on simple logic that looks to see if our character is intersecting with any tile in layer 1.  If so, then we have a collision, and the player should not be allowed to move through that tile space.  This method works fine for simple maps with simple collision rules.  However, as you add complexity to your map you may run into scenarios where you want to separate the collision rules from the art placement and/or you may want to support different types of collision.  One common approach is to create a dedicated collision layer in your map.  Let's look at creating that for our game.  

We can open the map we were working on in part 6 from the File -> Open menu in the GX Map Maker.  Let's create a new layer by selecting the "+" button in the right corner of the Layers Panel:
   
This will create a new layer with the label "Layer 3".  Let's select that layer in the list to make it the active layer. 

In the bottom right corner of the tilesheet there is a tile that looks like a square with a dotted outline.  This was included to use in our collision layer.  You could actually use any tile you wanted, but it is useful to create one that allows you to see the tiles underneath.

With that tile selected, let's add collision tiles around the borders of our map like so:
   
Then save the changes with the File -> Save menu item.

Let's incorporate these changes into our game.  Copy the updated platformer.gxm file into map folder of the project.   Let's add a couple of constants:
Code: (Select All)
...
Const COLLISION_LAYER = 3
Const COLLISION_TILE = 72
..
Now let's rework our OnTileCollision method to use the new method:
Code: (Select All)
...
Sub OnTileCollision (e As GXEvent)
    Dim ctile
    ctile = GXMapTile(e.collisionTileX, e.collisionTileY, COLLISION_LAYER)
    If ctile = COLLISION_TILE Then
        e.collisionResult = GX_TRUE
    End If
End Sub
...
If we run our game now, we will see that we can now walk right past the little hill in the center of the screen since we did not add any collision tiles in that area.  Unfortunately, we can see the collision tiles on the screen.  No problem, we can remove this from view with a call to the GXMapLayerVisible method:
Code: (Select All)
...
' Create the map
GXMapLoad "map/platformer.gxm"
GXMapLayerVisible COLLISION_LAYER, GX_FALSE
...

Ok, now let's look at a more advanced scenario for collision detection.  Suppose we want our player to be able to walk in front of the little hill in the center but still allow him to jump on top of it.  Well, first we'll need a way to indicate that we should use a different kind of collision detection in our collision layer.

If we go back to our GX Map Maker, we can see that next to the collision tile we just used is another similar tile in position 71.  Let's select that tile and then, with our map layer (3) selected, place that collision tile at the top of our little hill:
   
Save the changes and copy the map into the game project.

Now, let's add a constant for the new collision tile and name it something wildly creative like "COLLISION_TILE2":
Code: (Select All)
...
Const COLLISION_TILE = 72
Const COLLISION_TILE2 = 71
...

Then we can update our OnTileCollision method with the new logic for this collision type:
Code: (Select All)
...
Sub OnTileCollision (e As GXEvent)
    Dim ctile
    ctile = GXMapTile(e.collisionTileX, e.collisionTileY, COLLISION_LAYER)

    If ctile = COLLISION_TILE Then
        e.collisionResult = GX_TRUE

    ElseIf ctile = COLLISION_TILE2 Then
        If GXEntityVY(e.entity) >= 0 Then
            If GXEntityY(e.entity) + GXEntityHeight(e.entity) < e.collisionTileY * GXTilesetHeight + 1 Then
                e.collisionResult = GX_TRUE
            End If
        End If
    End If
End Sub
...
Our logic here when intersecting with our new collision tile basically checks to see if the player entity is falling from above the tile, then prevent movement, otherwise, let them pass through.

With these changes we have all of the player movement and collision mechanics in place:


Attached Files
.zip   part8.zip (Size: 82.79 KB / Downloads: 393)
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: 3 Guest(s)