This is part of an ongoing series where we write a complete 2D game engine in C++ and SFML. A new tutorial is released every Monday. You can find the complete list of tutorials here and download the source code from the projects GitHub page.
Four weeks ago we started work on our collision system and in that time we’ve done a lot: we created collider components, the data structure to store the components, and the collision system to check for and resolve any collisions. All that’s left is to add the collider components to the tiles and player object and call the new collision system that we wrote last week. Which means that by the end of this tutorial we should finally have collisions working!
We’ll jump right in by implementing the collision system. We need to call three functions: Add, ProcessRemovals, and Resolve functions. We’ll call these in the ObjectCollection.
We’ll call the collidable systems Update in the ObjectCollections Update function, right after we update the objects.
Whenever we add an object to the object collection we’ll also want to add these objects to the collidables system. This can be done in the ProcessNewObjects function. This is the exact same process as our drawing system.
The last thing we need to do before our collision system is up and running is to process the removals. You’ve probably guessed already but we’ll do this in the ProcessRemovals function.
I’ve also added a check to see if objects have been removed from the object collection before we call the systems ProcessRemovals. This prevents unnecessary loops through the systems vectors checking for removals when there haven’t been any.Now our collision system is up and running but it has nothing to process. So the last step we need to do is add collidable components to anything that we want to collide, for now, that’s only the tiles and the player. I’ll start with the tiles by adding the component in the Parse function of TileMapParser.
We first check if the layer is named “Collisions”, so make sure the layer containing the collision tiles has this name in Tiled. For more information on layers and Tiled see my earlier tutorial. Doing it this way allows us more control over what tiles we collide with, and reduces the collision calls drastically than if we were to collide with every tile.
Once we know the tile is on the collision layer we add the collision component and calculate the size of the collider based on the size of the tile and set the collision layer to Tile. Lastly we need to add the box collider to the player. We’ll do this after we create the player object in SceneGame.
We add the collider, set the size (based on the size of the animation frames), and layer and then we should be good to go. If you run the game now and attempt to pass through the tiles hopefully you’ll be prevented from doing so. Success, our collision system works!You can stress test our new collision system by adding 100+ players to the game (in the SceneGame.OnCreate function) and setting their colliding layer to default so that they collide with each other. You’ll find that they resolve the collisions and look something like this:
100+ players colliding. The system works!
While this will give you a good idea of how the system resolves collisions (and its fun to watch them resolve), we’ll look at a better way to test it next week when we start writing our debug system.
As always, if you have any suggestions for what you would like covered or are having any trouble implementing a feature, then let me know in the comments and I’ll get back to you as soon as I can. Thank you for reading 🙂