Each frame, collisions are detected for rigid bodies. If two rigid bodies are deemed to be colliding with each other, we handle this collision by applying certain forces to both rigid bodies so that they are separated and no longer collide.
The separating axis theorem states that: As long as we are able to draw a line (axis) between two objects, they do not collide.
- We loop through each edge of both rigid bodies. We then draw an axis parallel to this edge.
- Both rigid bodies are then projected onto this axis using simple dot product operations.
- We then find out of these projections intersect each other. If for all edges, no axis is able to separate both rigid bodies, then both rigid bodies collide.
- We find the closest vertex of one rigid body and the closest edge of the rigid body to the axis.
- With the above steps, we can also calculate the depth of the collision.
Collision Response
In order to respond to collisions, we perform a few operations on the information we fetch from detecting collisions.
- Collision Vertex
- Collision Edge
- Axis
- Collision Depth
We calculate a force to apply to both points of the collision edge and the collision vertex. Here’s how:
- We start by calculating a depth vector of the collision. The collision depth we fetched earlier is a scalar. To get the depth vector we multiply the collision depth with the axis vector.
- We calculate two ratios according to the mass of each rigid body.
- The force multiplier for the collision vertex and the force multiplier for the collision edge as a whole.
- The force multiplier for point1 of the collision edge and the force multiplier for point2 of the collision edge.
- We then multiply these ratios to the penetration vector and apply the forces to the collision vertex and edge.