Skip to main content
GameDev.net gamedev.net
🔒 Locked

Collision Detection Problems

Started by skazzaks Mar 12, 2007 at 3:43 PM 5 replies 1.3k views
Original Post
skazzaks
skazzaks
I am making a 2D game and have been using the Separating Axis Theorem for simple AABB. When there is a collision, it uses the "move the objects out of penetration using the shortest vector possible" algorithm. However, if a character is falling and hits a platform, it should never determine that the shortest vector to translate the object out of collision is horizontal, though, right? Also, I do not know how to handle the case of multiple collisions. The way I have it now, if there are multiple collisions, it will only correctly handle the first. The first, however, might cause there to be another collision with another object. Let's say I am walking right and there is a "bump" in the road. When my character hits the bump, he slides right through it. At the point of contact, my collision detection detects that he will fall through the ground, so it stops that, and then the loop continues. Even if I allow for all of the collisions to be "resolved", I don't know how to tell if the resolution of that collision will cause another collision or not. Hopefully that makes sense :)
Cowboy Coder
Cowboy Coder
For your first problem, you need to detect which edge of the platform (top or side) is the one the object hits first. If it hit the top, move up. If it hit the side, move sideways

For if after getting a collision, resolving it causes new collisions, you need to detect these also. You need to be very careful not to move an object blindly.
skazzaks
skazzaks
So I think that I can use the same collision detection algorithm, but then instead of trying to find the Minimum Translation Vector, I should find the vector that pushes the object out to the point where it would have collided, right?

"For if after getting a collision, resolving it causes new collisions, you need to detect these also."

So after I detect a collision and resolve it, I need to once again run collision detection against all possible objects? Thanks!
skazzaks
skazzaks
Is there a common collision detection algorithm that is used for handling multiple collisions? I am looking around and can't find much and am having trouble trying to understand how to deal with resolving a second collision after resolving the first.
SimmerD
SimmerD
If you are trying to be super-accurate, then yes you need to re-detect hitting other objects, which can in theory, be quite expensive.

In practice, you can usually restrict the set of objects that you could possibly collide with this physics frame to just a few, and possibly handle just collisions between this small set for this frame.

What I do is to create a bounding box where my object is now, and where it would be if it moved as it wished with no collisions. The union of these two boxes is used to find triangles and/or objects that I may collide with this frame. As long as you are not doing super-high-speed bouncing, this works fine.
oliii
oliii
Yeah, you just test against all potential colliders. You intersect with something, SAT pushes you away, then you test against the other colliders too.

The main problem with the SAT isn't the collision detection, but the collision response. The SAT may push you in the wrong direction, occasionally making you 'bump' on edges, especially if the displacement is large (then you may even teleport on the other side of the object). The slide effect isn't clean.

A few things you can do that help.

(1) Do the test in Dynamic (using the velocity of objects). That will give you a better idea of what plane of collision you should use. That will also save the problem of fast moving object. It's not a very difficult test.

(2) If your map is made of stacked blocks (slopes, blocks, ...), flag which edge of the block is valid for collision response. For a floor tile, you would only use the top side for collision, and therefore your collision normal will always point up.

(3) Use a 'step up' algorythm. Basically, your player should be able to walk up small steps (or slopes). If you encounter a collision, test to see if you can still place the little guy a few pixels up. Have a look at http://jnrdev.72dpiarmy.com/

(3) If your map is made of interconnected segments, compute the normal at the vertices. When you detect a vertex collision, use the vertex normal as the direction of collision, and raytrace along that vector inside the collider (Your player's box, sphere, whatever), to find the depth of collision and the other point of contact.
Everything is better with Metal.
daniel_i_l
daniel_i_l
You can solve both of you're problems by not moving the objects physically but rather applying an impulse that changes the bodies momentum (and therefore its speed). For each frame, first use the SAT to detect collisions. Then go through all the collisions and store a resolving impulse for each body. Then go through all the bodies and apply the impulses.
This way the bodies arn't acually moved untill all collisions have been dealt with.
You can find information on the resolving impulse at Chris Heckers (google it) site.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.