Collisions in games are used in most games. Collisions are often used for the following functionality:
- Solid collision to keep players, enemies, and bullets from moving between rooms or out of bounds
- Damage collision to deal damage to players, slow movement, or heal players
This tutorial will show how to work with the solid collision, and how to add new types of collision to your game.
By default our game already has SolidCollision defined in the GameScreen – this was added earlier in the first tutorial by one of the options. Until now the SolidCollisions have been invisible, and we haven’t created anything to collide against so there hasn’t been any functionality yet either.
We can verify that the SolidCollision shape collection is actually being created by making it visible. To do this:
- Expand the GameScreen in Glue
- Select the SolidCollision object. Note that it is blue, indicating that it is visible to children screens such as Level1
- Click the Variables tab
- Check the Visible property
If you run the game now, you will see that all solid collision tiles are outlined in white. This is the SolidCollision object.
You may want to keep collision visibility set to true to help diagnose problems in your game, but you should turn it to false when your game is ready to distribute.
Creating Additional TileShapeCollections
As mentioned above, the SolidCollision shape collection is a common object, so Glue provides quick defaults for this type. For the rest of this tutorial we will cover how to create a new TileShapeCollection.
At a high level, the steps are:
- Decide on a tile to use for collision
- Set the Type on the tile
- Create a new TileShapeCollection in GameScreen
- Configure the TileShapeCollection to be created from the tile type specified earlier
Setting the Tile Type
The first step is to decide which tile you would like to use for your collision. As we mentioned before, all gameplay tiles (such as collision) should be on the TiledIcons. To mark a tile as collidable:
- Open Tiled
- Click the TiledIcons tileset
- Click the Edit icon to edit the TiledIcons tileset
- Select a Tile that you would like to use for collision
- Enter a Type for that tile.
Don’t forget to save your tileset file
Now that the type has been set and the tileset has been saved, you can place the tile in your level. Make sure to place it on the GameplayLayer in case your game has multiple layers.
Be sure to save your map after adding tiles.
Creating a New TileShapeCollection
Now that we have a new tile type in our game, we can add another TileShapeCollection. To do this:
- In Glue, select the GameScreen
- Click the Quick Actions tab
- Select the Add Object to GameScreen button
- Type or look for TileShapeCollection in the window and select this option
- Enter the name for your tileset. Usually this should match the type of your tile.
- Click OK
Configuring the TileShapeCollection
TileShapeCollections usually come from specific tiles in tile maps. We’ll set up the TileShapeCollection created in the previous section here. To do this:
- Select the newly-created TileShapeCollection
- Click the TileShapeCollection Properties tab
- Select the From Type option
- Select Map as the source TMX
- Select the matching type in the Type dropdown
Notice that Remove Tiles is checked by default. Uncheck this option if you would like to see the tiles in game.
We should also turn on collision visibility to make sure it is created as we expect.
If you run your game, you will see the collision in game. The tiles will be removed if the Remove Tiles checkbox was left checked.
Creating TileShapeCollections in Code
As mentioned earlier, TileShapeCollection instances can also be created in code. For more information on creating collision in code from Tiles, see these links: