Collision Handling


I am currently trying to figure out how I should do the implementation of simple collisions.
For now I am having enough when it supports circles and rectangles but I want to make it possible to add other shapes later.

Everything is actually made up of components managed by my EntityComponentSystem.

You can create Entities in the EntityComponentSystem and register ComponentSystems. Component Systems do have Update/Draw call getting passed the current iterated Entity. Entities can get components attached. ComponentSystems define the required components it needs from an entity, otherwise its not being updated / drawn by the component system.

Now I got some basic Components. For example: TransformComponent, ColliderComponent, SpriteComponent.

Transform Component contains Position, Rotation and Scalation.
Collider Component is abstract, it can be CircleCollider or RectangleCollider (for now).
Collider Component class has an abstract member function whereas the Circle or Rectangle Collider needs to implement it, returning true or false whether there is a collision or not.

Systems are for example, SpriteSystem, which required the TransformComponent because the SpriteSystem needs to know where to draw the sprite. Or CollisionSystem… and thats where I am currently having issues.

Additionally I have a Rigidbody Component which has a Velocity Vector. In my PhysicsSystem i have a member ApplyGravity which is a boolean value indicating whether it should apply gravity to the rigidbody. So Velocity vector keeps getting a gravity constant added in physics systems update function.

Now in collisionSystem I check each entity if it collides with another entity… that works but I dont know how I should handle those collisions… i tried setting up a LastGoodPosition vector which gets always updated when there is no collision of a specific entity and when there is a colliison, the current position of the entity gets set to LastGoodPosition (before collision has happend)… but depending on how fast the velocity was, the lastgoodposition is not exactly one pixel distance from actual object, no it depends on last update call … thats the problem so it happens quite often that the LastGoodPosition is actually not one pixel before object collision but 10 pixel away and yeah thats quite of not nice.
How would you handle that?

Thanks in advance

1 Like

If you are talking about the collision response, you should take a look to that:

The Handmade Hero tutorials cover some of this stuff. What they do is sweep the body to figure out where the collision would happen and then only move it that far. Heres a link to a video that may be the relevant one, if not just poke around.