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.
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.
Subscribe to this blog via RSS.
C++ 30
Directx12 11
Mario kart 11
Commercial games development 15
Unity 15
Low-level programming (19) C++ (30) Text based adventure (5) Ex-machina (9) Game engine programming (11) Directx12 (11) Mario kart (11) Land of gold (5) Audio visual production (1) Commercial games development (15) Unity (15) Don't walk by (15)