# Collision Detection for Rotating Objects

This topic is 2627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to figure out how to handle collision detection when the objects involved can have rotational motion. I used the following method first:
moveif intersecting then     resolve intersection using the mtd and collision normal     calculate response velocitiesendif

Rotational motion was handled just fine, but I couldn't figure out how to support more than two objects. The problem was, one object's intersection could be resolved, but that resolution would put it inside of another object, that object would put it inside of another object (or even the first one it intersected with) and so on... There was also the problem of keeping track of what time the simulation was currently at inside the timestep.

I then moved on to a dynamic SAT test:
if will intersect then     move object to point of intersection     calculate response velocitiesendifmove the rest of the way

This had the potential to solve the multiple object problem, as well as the current time problem. However, a rotating object can now "dig" its way through solid objects. This could be fixed by checking for intersection and resolving it with the mtd, but then we are back to the original set of problems.

I have read a lot about both physics and collision detection recently, but unfortunately everything has been a bit vague on how you are supposed to setup the whole system. How do you solve this problem?

I should note that I did find this topic: http://www.gamedev.net/community/forums/topic.asp?topic_id=539913 It was helpful to a certain extent, but it didn't answer my question.

##### Share on other sites
Hi VBStrider,

I'm wrestling with exactly the same issue at the moment. I think the best (easiest) approach might be an iterative one, whereby you make several passes over the objects, each time resolving any new collisions that arose from your previous set of resolutions, until no objects interpenetrate. The problem is, this can create quite long chains of object resolution when many objects collide. I'm still researching the topic but there are probably many optimisations that can be made with this approach.

I don't bother to find the time of penetration in my physics code; for objects moving at reasonable speed I think it's ok to just discard the rest of the timestep's movement. It vastly simplifies the object update process, and tiny fractions of a timestep left over make no discernable difference, especially when you're moving objects about in order to resolve penetrations anyway.

If you find out any more concrete info on the topic I'd be interested to hear it.

##### Share on other sites
When resolving the intersection you should not change the position of your bodies instead change the velocity right before the intersection.

Assuming your intersection occurs at time t_c only integrate object positions to t_c - delta where delta is some small value. Then you should calculate the collision response, which changes the velocity of the two bodies and you can continue your simulation. Even if this sounds easy, the tricky part is that you must make sure that your collision response guarantees you that it prevents penetration.

Handling multiple contacts can also be handled this way. You simply perform the collision detection / response until you can integrate to the end of your frame. With many objects you might need to do some tricks to prevent microcollisions (i.e. collisions that happen a lot during one frame).

Hope this helps.

##### Share on other sites
I have changed the system according to your directions to look like this:
while dt > 0     if will intersect then          if time of first intersection is within dt then               integrate to time of first intersection               dt = dt - time of first intersection                              if objects are colliding then                    calculate response velocities               else                    integrate over dt                    dt = 0               endif          else               integrate over dt               dt = 0          endif     else          integrate over dt          dt = 0     endifloop

It can still dig through the other object though. Here is the demo: http://www.ifthensoftware.net/invisibleman/PhysicsTestbed.zip

##### Share on other sites
while dt > 0     if will intersect then          if time of first intersection is within dt then               integrate to time of first intersection               dt = dt - time of first intersection

Are you integrating here right to the time where the intersection occurs? You might get a problem due to limited precision in floating point arithmetic. E.g. the time you get from your collision detection is not right before the intersection, instead it might be the time right after the collision. Thats where you need the '- delta' of my previous post.

This looks odd to me:
               if objects are colliding then                    calculate response velocities               else                    integrate over dt                    dt = 0               endif

At this point you should already know, that the objects will collide. Therefore you do not need to check again, instead resolve it immediately.

It all should fit into something like this:
while dt > 0  tc = calc_next_collision_time  if tc < dt     integrate to tc - delta    correct velocities    dt = dt - tc  else    integrate to dt    dt = 0  end ifloop

Even though this looks simple, it can get quite messy to get it robust as you'll have to make sure floating point arithmetic is not fooling you.

This all assumes that bodies never penetrate each other. There is also another way of doing it, in which penetration is allowed but additional forces are applied to push bodies out of each other. Such an approach is used by Box2D and Chipmunk Physics. You might want to look into their references.

Sorry, I couldn't run your example as I don't have Windows flying around here right now. I'll have a look at it later.

##### Share on other sites
The objects could be touching, in which case the time of collision will be 0. That's what the second collision test is for.

The main problem is that the time of collision is only being calculated for linear motion, not angular motion. Because of this, the object can rotate into an object. Dynamic SAT does not support rotating objects, and so the object needs to be treated as though the rotation happens instantly at the end of the timestep. Checking for intersection causes the original problems.

There should be a well known solution to this problem... I see games use rotating objects all the time.

##### Share on other sites
Quote:
 Original post by VBStriderThe objects could be touching, in which case the time of collision will be 0. That's what the second collision test is for.

You could modify your collision detection method such that it tells you that no collision will occur for a given time (simplest method for this would be to return -1 in this case). This second check is not needed as you should only advance your simulation to a state where there is no collision if you do not have any correction methods for existing penetrations.

Yeah, getting rotational collisions properly to work is quite difficult. But try to get that done first. Testing computing collision response when you do not have a robust collision detection is not ideal.

For detecting collisions caused by rotations, you could use a bisection method to find the time of impact. This will involve frequent integrations and resetting of the states, but should be okay performance wise. There are a few threads here about collision detection of rotating bodies. It surely is possible, but is definitely harder than a dynamic SAT.

It works in a lot of games, but remember, that games in general do not simulae the real world, instead there are a lot of hacks to make games just look like reality.

##### Share on other sites
The second check calculates the dot product between the collision normal and the relative velocity. If the result is negative, they are colliding. Otherwise they are moving apart and there is no collision.

Thanks for the pointer! This is my current reading list (most likely in this order):
Physically Based Modeling: Principles and Practice
Section 9.5 of Real-Time Collision Detection, "Gilbert-Johnson-Keerthi Algorithm"
Ray Casting against General Convex Objects with Application to Continuous Collision Detection
Continuous Collision Detection and Physics
Impulse-based Dynamic Simulation of Rigid Body Systems

If anyone has recommendations for my reading list (new articles, read this instead of that, etc.) please let me know :)

##### Share on other sites
I have ran into a problem with using bisection to find the time of intersection. If one object has a great enough acceleration towards the object it collides with, the impulse will not keep it away for very long and it will end up colliding again within a very short amount of time. A single step ends up looking like this:
Integrate from t0 to t1.
Collision found at t0+dt1 and resolved.
Integrate from t0+dt1 to t1.
Collision found at t0+dt1+dt2 and resolved.
Integrate from t0+dt1+dt2 to t1.
Collision found at t0+dt1+dt2+dt3 and resolved.
...

This happened to me almost immediately, so it has to be a commonly known problem. However, the resources I have found have not mentioned it, much less how to solve it.

EDIT: Nevermind, the problem was misdiagnosed. It ended up being caused by an incorrectly calculated collision normal.

[Edited by - VBStrider on November 15, 2010 2:03:16 AM]