2
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

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 */
\$\endgroup\$
5
  • \$\begingroup\$ 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! \$\endgroup\$
    – Pinpickle
    Commented Jan 22, 2013 at 19:55
  • \$\begingroup\$ @Pinpickle With AABBs the normal is just (-1,0) for left, (1,0) for right, etc. Are you looking for something else? \$\endgroup\$ Commented Jan 22, 2013 at 20:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – Pinpickle
    Commented Jan 22, 2013 at 21:31
  • \$\begingroup\$ @Pinpickle I added the contact normals I mentioned to the code. I think they're a lot simpler than what you seem to expect. \$\endgroup\$ Commented Jan 22, 2013 at 21:37
  • \$\begingroup\$ 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. \$\endgroup\$
    – Pinpickle
    Commented Jan 23, 2013 at 2:05
-1
\$\begingroup\$

Check this

Use shorter compound.

\$\endgroup\$
2
  • \$\begingroup\$ From what I can tell this relies on the rectangles alone and not their velocities, which is not the solution I need. \$\endgroup\$
    – Pinpickle
    Commented Jan 22, 2013 at 19:57
  • \$\begingroup\$ @Pinpickle the link is now dead which is why link only answers are discoraged. \$\endgroup\$
    – user116458
    Commented Jun 17, 2018 at 16:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .