Collision Detection

Collision Detection

Jan 27, 2019. | By: Evan Diamond
1 min

The final and hardest part of the physics component is the collision detection.

I use AABB collision detection to see if any object with a physics component is colliding any object in the scene and providing the correct response depending on the collision.

Each physics component is given an array of all the sprite components in the scene which is passed into the collision function. This function goes through the all the blocks four times for up, down, left and right collisions. The function then sets a bool passed in by ref to true if it colliding on that side. Such as the in_left bool is set to true when the current object is colliding to the left of another. These bools are used in different mechanics that need to know about these collisions. The function itself also checks these bools for collisions to provide the correct response. The amount of types of collision responses got very extensive and most of them are relative to the current direction of gravity. This meant that these response were double but in different directions depending on gravity’s direction. The function returned the index of the block the user was currently colliding underneath with (or above if gravity was upwards) so that the block’s corresponding amount of friction could be found and given to the momentum functions.

Post Mortem

This collision detection system overall felt too time expensive .Each component had to iterate through all the objects 4 times every frame. In the future I would do all collision detection within the update of the game scene not each objects in their physics component. Also since the code base is using an component based desgin pattern for it’s gameObjects the physics component needed the sprite components all the blocks since thats were it’s colliders are stored. In future devlopment using a component based design pattern I will use a collider component to store the colliders otherwise collision only works if a game object also has a sprite.

About

Gamers developing new games.

Social Links

Our Bunker

University of the West of England,
Frenchay Campus, Bristol, BS16 1QY,
United Kingdom.