04. Colliding Entities against TileShapeCollections

Introduction

This walk-through shows how to perform collision between entities and TileShapeCollection  instances. The two most common types tile-based collisions are:

  1. Solid collision – collision which prevents a character from passing through a tile, such as solid walls in a maze
  2. True/false tests – used to perform additional logic when  collisions occur, such as dealing damage to a character when touching spikes

We’ll be looking at both types.

Creating an Entity

For this guide, we’ll create an entity to test our collision code. This entity will need to be collidable (implements ICollidable) and needs logic for movement in response to input.

To create a new entity:

  1. In Glue, right-click on Entities.
  2. Select “Add Entity”

  3. Enter the name Player
  4. Check the Circle checkbox to give the entity a circle collision object
  5. Verify that ICollidable is checked – this should happen automatically when Circle is checked
  6. Click OK

The new entity should appear in the Entities folder in Glue.

Since the Player entity has its ImplementsICollidable set to true, any shape (Circle, Polygon, AxisAlignedRectangle) in the entity will be used in collision functions.

Adding Entity Movement

To make the entity move in response to keyboard input:

  1. Open the Player.cs  file in Visual Studio
  2. Modify the CustomActivity method as shown in the following code snippet:

Adding Player to GameScreen

Our entity is ready to go – we just need to add it to GameScreen.

To add the entity to GameScreen, drag+drop the entity onto the screen:

DragEntityOnScreen2

The Player will now appear on the game screen and can move with the arrow keys.

Performing Solid Collision

Solid collision can be used to prevent an entity from passing through solid objects on a tile map. On this example we’ll be colliding the Player instance against the Wall tiles which were used to create the wallCollision in the previous guide.

To add solid collision:

  1. Open GameScreen.cs in Visual Studio
  2. Modify CustomActivity so it performs solid collision as shown in the following snippet:

The Player will no longer be able to move through solid collision:

SolidCollision

Reacting to Non-Solid Collision

Non-solid collision is useful if your game requires colliding against tiles to perform custom logic, but those tiles should not block movement.

To perform non-solid collision, Modify CustomActivity in GameScreen.cs as shown in the following snippet:

Adding More TileShapeCollections

This guide uses the same TileShapeCollection to show how to perform solid and non-solid collision.

Full games may use multiple TileShapeCollection instances for different types of collisions. For example one TileShapeCollection may be used as solid collision (walls) and another TileShapeCollection may be used to deal damage to the player (lava).

If tiles with collision are placed on multiple layers in Tiled, then the .tmx file will be loaded with multiple TileShapeCollections in its Collisions property.

Specific collision logic can be performed for each TileShapeCollection. Each TileShapeCollection will have the same name as the layer that it is associated with. For example, the following code could be used to perform solid and damage-dealing collision:

 

The code above shows how to create multiple TileShapeCollection  instances using layer names from shapes placed on tiles. If your game requires using fewer layers, you can also select tiles out and add shape collections to layers manually. Keep in mind that this does not require placing rectangles on tiles in Tiled, and if you do rectangles then the code will automatically create shapes anyway.

If you do decide to use tile properties, you can use the AddCollisionFrom and AddMergedCollisionFrom. For example, the following code could be used to create collidable areas for a racing game which uses wall, dirt, grass, and road collision:

 

Conclusion

This guide shows how to work with TileShapeCollection  to perform solid collision (collision which prevents an entity from passing through the collision area) and non-solid collision (collision which is used to drive custom logic).

A typical game may include many TileShapeCollection  instances for different types of collision.

Although we didn’t cover it in this guide, the TileShapeCollection class uses axis-based partitioning for efficient collision. In most cases, even mobile platforms will be able to perform collision against TileShapeCollection  instances with tens of thousands of rectangles with very little impact on performance.

Developers interested in the details of this partitioning are encouraged to look at the TileShapeCollection  source code.