0
\$\begingroup\$

I was reading this tutorial on swept AABB collision detection, and I was confused by this piece of code:

// find the earliest/latest times of collision
float entryTime = std::max(xEntry, yEntry);
float exitTime = std::min(xExit, yExit);

The tutorial says this about this code:

entryTime will tell use when the collision first occurred and exitTime will tell us when it exited the object from the other side. This can be useful for certain effects, but at the moment, we just need it to calculate if a collision occurred at all.

So if entryTime is supposed to be the first time a collision occured, then why is max being used to determine the first time of collision? Shouldn't min be used instead? If max is used, then the highest value will be returned. The highest value would be the time of the last collision. Obviously this must be the correct way of doing this, but I'm not understanding it. If you are trying to find the first axis to collide, it would seem intuitive that you use min.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The smaller of the times xEntry and yEntry will be when the blue box crosses the first of two edges of the green box ("first" in the image below). The greater of the two times is when the blue box crosses the second edge ("second", below). The blue box isn't touching the green one until it crosses that second edge.

swept AABB example

In other words, the blue box touches the green one when it is between xEntry and xExit and also between yEntry and yExit. If it's between in x but not y, then it isn't touching, and vice-versa.

The reverse is true for the exit time: As soon as one edge is crossed, the boxes are no longer touching.

\$\endgroup\$
1
  • \$\begingroup\$ OK. So basically, both axes need to cross the near edge or you will collide to early. Thanks. This clears it up. \$\endgroup\$
    – Lysol
    Apr 14, 2017 at 1:06

You must log in to answer this question.

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