In SAT collision detection how do I calculate the axes for projection?
We'll initially stick to 2D collisions detection for simplicity. (I'll discuss 3D at the end).
The axes used in SAT are the normal vectors of all of the edges of the two objects being considered.
Consider the box in this diagram;
The box has 4 edges: AB,BC,CD & DA (if using the clockwise convention). Each of these edges can be written as a vector;
- edge AB is the vector B-A. (That is vectorB minus vectorA)
- A & B are points, the expression (B-A) is the vector which encapsulates the distance and direction from point A to point B.
- We need the normal to this edge, A normal is vector at right angles to the edge. We can calculate this by swapping x and y of the edge and multiplying the y value by -1.
I'll do a numerical example;
A=(100,100) B=(150,175) edgeAB B-A=(150-100,175-100)=(50, 75) normalAB=(-75, 50)
For SAT we need also to find normalBC, normalCD and normal DA. We also need to find the normals to all the edges of the other object in the collision detection.
All these normals are the set of axes which we will project both objects' vertices onto.
In some tutorials you will be advised to normalise the axes. this is not necessary, although I find it does make visualising the process easier.
Note, in the diagram, normalAB is parallel with normalCD in this case the result of projection on these axes will be the same (overlap or non-overlap) will be the same so there is no need to project on to both of them. You can eliminate normal CD. Similarly normal BC and normal DA are parallel, so you can eliminate one of these.
For 3D you will need to find the normals for all the faces, you will also need to get the cross product of all the edges of one object with all the edges of the other.