TileShapeCollection
Introduction
TileShapeCollection is a class created specifically for tile based collision. Its features include:
Very fast collision - it has partitioning built-in.
Eliminates snagging - Internally the TileShapeCollection modifies contained AxisAlignedRectangles' RepositionDirections to eliminate snagging.
Full support in the FlatRedBall Editor for initial definition and collision relationships.
Simplified syntax - Adding collisions to TileShapeCollection is very easy to do in code, whether doing so manually in code or from a loaded TMX file.
TileShapeCollections and the Wizard
If you have created your project using the FlatRedBall Editor Wizard (either platformer or top-down), then your game already has TileShapeCollections. For example, the GameScreen should have a SolidCollision TileShapeCollection.
Creating TileShapeCollections in the FlatRedBall Editor
To add a TileShapeCollection:
Expand your GameScreen or Screen which should contain the TileShapeCollection. Typically collision is added to GameScreen.
Right-click on Objects and select Add Object
Verify FlatRedBall or Custom Type is selected
Select the TileShapeCollection option
Click OK
Now that you have created a TileShapeCollection, you can fill it using a variety of methods as shown before.
Example - Setting Collision from a Tile
The most common usage of TileShapeCollections is to add collision from a particular tile Type. Usually this is done through a Map object in the GameScreen. If your project has been set up using the wizard, then you should have a Map object in GameScreen. If so, you can:
Select the TileShapeCollection that you would like to fill from the map
Check the TileShapeCollection properties
Select the From Type option
Use the dropdown to select the type. These types will match the types defined in your map's Tileset (tsx) file
Example - Filling a TileShapeCollection Completely
When creating a game, you may want to add some placeholder collisions to test your logic and collision relationships. To create placeholder collisions:
Select your TileShapeCollection in Glue
Click the TileShapeCollection Properties
Select the Fill Completely option
Set the Tiles Wide and Tiles High to define the size of collision block you want
Click the Variables tab
Check the Visible checkbox
Now the TileShapeCollection will appear in your game and can be used for testing.
Defining Collision from a TMX File
Open Tiled
Click the wrench icon to edit the tileset
Select one (or more) tiles which should have collision
Add a Custom Property called SolidCollision. The type of the variable doesn't matter.
Save your tileset (tsx)
Place some tiles in your map
Save your TMX
Now in Glue we can associate the SolidCollision with the TileShapeCollection:
Select the TileShapeCollection in your GameScreen
Click the TileShapeCollection Properties tab
Select the From Property option
Use the dropdown to select your TMX file
Enter the property SolidCollision
Now if you run your game you will see collision wherever you placed your tiles.
TMX (LayeredTileMap) Example
Collision can be added to a TileShapeCollection from a loaded TMX file (which loads into a LayeredTileMap). The following code shows how to add collision from all tiles with the custom property HasCollision.
Code-Only Example
The following example shows how to create an AxisAlignedRectangle that moves around with the keyboard and collides against a TileShapeCollection. This project assumes a Glue project with:
A Screen called GameScreen
An AxisAlignedRectangle object named Rectangle in GameScreen
To add the TileShapeCollection:
Open the project in Visual Studio
Open GameScreen.cs
Add the following using statement:
Add the following at class scope:
Add the following to CustomInitialize:
To add movement and collision to your rectangle, add the following code to CustomActivity:
Finally you'll need to remove the TileShapeCollection. To do this, add the following to CustomDestroy:
Defining Custom Per-Tile Collision
By default each rectangle in a TileShapeCollection occupies the entire tile (16x16). Custom shapes can be defined in Tiled to create rectangles which occupy less than the entire tile, or even polygons for sloped collision. Partial tile collisions are defined in the TSX file, typically in the StandardTileset.tsx. To add collision on tiles:
Open the TSX file in Tiled
Select the tile which should have partial collision
Select View -> View and Toolbars -> Tile Collision Editor
Draw a rectangle or polygon on the tile
Set the type on the tile which has the collision. Note that the same type can be given to multiple tiles. Be sure to select the tile and not the shape. You may need to deselect and re-select the tile to force its properties to display rather than the newly-drawn polygon.
Repeat this process for any other tile which should have custom shapes.
Once you have added shapes to all of the tiles, and once you have set the types on the tiles, save the TSX file.
Paint the tiles in their desired locations. Note that tile collision can be previewed in Tiled by selecting the View -> Show Tile Collision Shapes option
Save the Tiled (TMX) file.
To use these shapes:
Select an existing TileShapeCollection or create a new TileShapeCollection. Typically this TileShapeCollection would be in the GameScreen.
Select the TileShapeCollection Properties tab
Select the From TMX Collision (use tileset shapes) option
Set Source TMX File/Object to Map
The TileShapeCollection will now include custom shapes as defined in Tiled.
Additional Information
PlatformerCharacterBase and TileShapeCollection - discusses how to use the PlatfromerCharacterBase with TileShapeCollection.
Last updated