# Find the Contact Normal of Rectangle Collision

It seems a lot of people have asked similar questions on this site, and every time it seems like a whole bunch of answers that don't work are given. Basically, I have two rectangles (AABBs) that collide, and I want to find out on what side they collided with each other.

I know the velocities of each object before they collide, and of course their positions before and when they've actually collided (if I use discrete collision checking that is, currently I'm using speculative). Many people say find the lowest penetration distance or take the largest speed (either horizontal or vertical) and that will give you the answer. But the collision is dependent on both of those values, I just don't know how they fit together to tell me where the collision is coming from. I don't think I need an exception for corner collisions.

I do not want to do my horizontal and vertical collisions separately as that just opens up even more problems.

So, any suggestions?

Instead of an AABB of size (w,h), position (x,y) and velocity v, and an AABB of size (w2,h2), position (x2,y2) and velocity v2, you could consider a fixed-position AABB of size (w+w2,h+h2), position (x,y) and a point of position (x2,y2) moving at velocity v2-v.

Then use AABB/ray collision resolution. You can restrict yourself to the 2D case by removing the code for the 3rd dimension. If the final t value is smaller than your current timestep, there is a collision.

Finally, the following tests will tell you where the collision took place and let you compute the normal:

if (t == t1) return Vector2D(-1.f,  0.f); /* left */
if (t == t2) return Vector2D( 1.f,  0.f); /* right */
if (t == t3) return Vector2D( 0.f, -1.f); /* bottom */
if (t == t4) return Vector2D( 0.f,  1.f); /* top */

• Yeah I had starting using that a while ago which was great for figuring out penetration depth and distance but not for finding the normal. I will try combining it with that link you provided, though. It looks promising! – Pinpickle Jan 22 '13 at 19:55
• @Pinpickle With AABBs the normal is just (-1,0) for left, (1,0) for right, etc. Are you looking for something else? – sam hocevar Jan 22 '13 at 20:50
• Yeah I am looking for something else, basically I'm trying to find which sides collided (and the contact normal is a result of that), I just thought asking for that would give me less ambiguity and less "find the shortest distance of penetration" answers. – Pinpickle Jan 22 '13 at 21:31
• @Pinpickle I added the contact normals I mentioned to the code. I think they're a lot simpler than what you seem to expect. – sam hocevar Jan 22 '13 at 21:37
• I think my first comment was a bit misleading, sorry! What I meant is I tried computing relative velocity and treating one rectangle as a point, what I haven't tried it ray casting. Like you said, the actual normal part of it is very simple. – Pinpickle Jan 23 '13 at 2:05

Check this

Use shorter compound.

• From what I can tell this relies on the rectangles alone and not their velocities, which is not the solution I need. – Pinpickle Jan 22 '13 at 19:57