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 and click OK

Adding Entity Collision

To add collision to our newly-created entity:

  1. Set the “ImplementsICollidable: property to “True”

  2. Expand the Player entity
  3. Right-click on the Objects folder and select “Add Object”
  4. Verify the FlatRedBall or Custom Type category is selected
  5. Select the Circle type – this will define our object’s collision area

  6. Click OK

Once an 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, the following code may be used to perform a variety of collisions. Notice that the solidCollision instance is created by passing in multiple tile names:

The code above shows how to create multiple TileShapeCollection  instances using tile names. Using names can be restrictive because each name can only appear one time in a tileset. Instead, properties can also be used to create TileShapeCollection  instances.

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.