Monday, October 10, 2016

collision detection - How do you prevent AABBs from passing through each other?


I know what an AABB is, but I don't know how you would check for collision with an object that is going really fast. If an object goes fast enough, it can skip over other bounding boxes without it detecting a collision.


Example:
High speed object


So, for an object with a bounding box to move from (0, 0) to (525, 670) in one update, would I have to somehow iterate over each point from the starting point to the end? Because I need objects to be able to go fast and still be tested for collision on other bounding boxes. I hope that makes sense.



Answer




The term you are looking for is "Continuous Collision Detection" (CCD), something also refered to as "Anti-Tunneling" and less commonly "Broad-phase collision detection".


Some CCD implementations rely on ray-casting approaches that cast a ray in the direction of the fast traveling object to check if there is an object in the way and try to estimate whether the next frame update could already bypass this "hit" object (based on current speed). If so the expected time of impact is calculated and the movement restricted accordingly.


Another approach often used by the physics engines (e.g. "PhysX" or "Bullet") is to not only check for collisions at the position the object currently is in (which might already be beyond the object it should have collided with), but to also do various checks between the position from the previous frame and the current one. Bullet does so by using a "swept sphere" which is used to quickly check for potential collisions along that path (a sphere is used since it is even faster to check than a box).


It is advised to only turn on CCD for fast objects since it rather performance-heavy. Therefore, in some engines you have to defined speed threshold and only once they are hit, CCD comes into play (e.g. see this Bullet setup guide).


PS: I know that the mentioned physics engines are C++ and you are asking for Java, but the principles still apply.


BTW: Also have a look at this answer, as well as this more generic / theoretical SO question on potential algorithms.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...