2D Collision detection / reaction

So,

I’m implementing collision detection to my 2D game engine and I have some problems as of how to get going.

All my game objects are polygons that I’ve tweeked / implemented myself so the speed is not any problems. However, I wonder how I should detect the collisions…

At the moment I cmp polygon segments with each other i a nice (?) N2 mannor and generate a list of normals that I’ve intersected. The next step is the tricky part though since I don’t know how to react.

I mean, imagine a box approaching a different box horizontally until you detect collision. The collision I’ve detected only contain two normals - the top / bottom surfaces of the testpolygon.

I’ve tried to simply calculate the penetration depth of the collision and seperate the objects from each other according the velocity of the gameobject that collided, but there’s a major bug to that:

What if my gameobject is a person walking on a ground polygon and the person is affected by gravity - Then I detect collisions constantly (to keep the person from falloing though the floor) and when the person tries to move in any direction, he’s more or less stuck!

I feel that I have to calculate scalar-products with the normals of the surfaces that I’ve collided with, but then I have to calculate differently since I havn’t detected the surface directed towards the approaching box in the above example… Gaah! :wink:

This is where I hope that you guys come in and make my day a little brighter - Thanx!
/Markus

Are you using bounding rectangles or spheres before doing your polygon test? Sorry but its not obvious from your post.

I had the same problem though not with polygons in 2d (well, bounding rectangles count I guess but I used a simple overlap test); You have to switch your approach a little. Don’t check for falling if the character is not falling. So first check if there is contact with the ground polygon; if not then you know your falling so continue to check and add gravity etc… till you do reach the ground. Its kind of hard to put into words.

Err, so don’t inc delta y unless there is no contact with a ground polygon?

Yes, I do a bounds-test before going into the polygon cmp algorithm :wink: I was in a hurry when I wrote the post - My bad…

I feel a bit doubtsome to the “if contact, then don’t”-sollution since that isn’t as “general” as I want it to be. What if I jump towards a wall? Then I should stop moving horizontally, but still add gravity to make the caracter fall down… In my engine, the gravity is custom to the game you implement, thus you can have horizontal gravity in both directions AND/OR vertical gravity…

I don’t want to think about the number of “special cases” that I would have to define in order to make it work :wink: And if I made it work, I’d probably find a case that I didn’t cover sooner or later…

The only real general sollution must (should?) somehow involve the normals of the surfaces that I detect collision with… I just don’t see how at the moment :-/

Perhaps I could try to combine the “if contact, then don’t” concept and just ignore all incrementations of the object’s velocity that has a composant in the same direction as the collisoni sureface… that would allow forces to work on the objects without further trying to go “into” the surface that I’ve detected collision with…?

In need of more input :wink:
/Markus

[quote]What if I jump towards a wall? Then I should stop moving horizontally, but still add gravity to make the caracter fall down…
[/quote]
Get a line representing your movement during a frame. If there is a collision find the colliding point and the end point projected on to the colliding plane. Use the colliding point and projected point as a new line and check for collision recursivly until there is no collision.

Some like to do the gravity movement in a seperate step. That way you can possibly simplify you collision response.