Simultaneous Multiple Object Collision Detection?

Started by
3 comments, last by slayemin 12 years, 1 month ago
Not prefixing because I want pseudocode rather than the C++ I'm working in. Also not looking for a library to import, as I'm trying to learn engine building technique. It's me, C++ and OpenGL/GLU/GLUT here, nothing else.

Collision detection has always been a big issue because of how expensive it is, I've heard. That I'm not worried about, a skim of the N+ tutorials has shown me that a grid system will work nicely for that. My problem is figuring out how to bounce off objects/kill overlap by applying halved inverse differences to three objects instead of two. My code is as follows:


for (a = 0 through maxPlayers) {
for (b = a through maxPlayers) {
if (a.collides(b)) {
bounceOffEachother(a,b);
}
}
}


...where bounceOffEachother will take the difference between them, find the axis of least resistance, and push them away from each other on that dimension to create a slide or push effect. Everything is cube shaped, so this works out well.

So, for trying to fix this, I wanted to get all collisions, then apply all the bounces...



for (a = 0 through maxPlayers) {
Player collidedWith[maxPlayers];
int collisionSlot = 0;

for (b = a through maxPlayers) {
if (a.collides(b)) {
if (collisionSlot < maxPlayers) {
collidedWith[collisionSlot] = b;
collisionSlot++;
}
}
}

for (c = 0 through collisionSlot) {
bounceOffEachother(a,c)
}

}


And yes yes it should be collidedWith[c] and I should be using playersPlaying[a] or something but this is pseudocode so I'm only doing what I have to for you to understand what I'm talking about. :P

So, my problem is, this technique still isn't working. I'll have my one dimensional bounces only apply to one collision still. Really, I'm not just using this for players, but also for walls, so if you hit two walls at once, only one of them has their collision create a bounce... meaning a row of walls doesn't work. So, any suggestions? I like having a two element bounceOffEachother function, but is that just not feasible for what I want to do?

Thanks guys!
Advertisement
Have you debugged this to see if when you hit two walls there are really two collisions in your collidedWith array? Because it might visually look it did, but it is very hard to actually to collide with two walls at once. You might be actually a fraction of a millimeter off of one when you are colliding with the other. If this is happening, what you should do is accumulate collisions over multiple iterations and only then apply the collision response. So you don't react immediately upon collision, you wait for a few iterations and accumulate all collisions on a certain object before bouncing. Hope this helps.
It does! At the least it will cut down my processing power issue with collision detection if I only check once every three cycles, and for some reason I wasn't thinking of that.

I've been playing with this more and it appears to be tied to which order my collisions are happening in - if I am moving across the tops of these blocks from the first ones checked towards the last ones checked, I don't fall, but the opposite direction commonly causes issues in bouncing/displacing. It also only happens when I have three cubes in a row, and I always fall in the middle one.

My best guess at this point is that I'm applying a displacement incorrectly and it's trying to bounce off the wrong block the wrong way at the wrong time. Read: I think I might just need more data. :P So I'm going to go hunting for it! Thanks again!
Another thing is, usually physics simulations use another cycle counter to say so. If you make your simulation to run in your main game loop, you will be frame-rate dependent. Your simulation will run faster or slower depending on the frame rate, and if the frame rate drops you might see objects go through each other. What you want to do in this case is have a fixed timestep on which you run physics simulation that runs completely separate from the main loop to ensure simulation consistency.

Cheers!
You're currently using an O(N^2) method for collision detection. This is fine when you don't have many moving objects in the world, but the amount of processing power required increases exponentially as you add more objects in. CPU's are pretty fast nowadays so you probably won't notice any problems until you start getting hundreds of objects (also, depends on how expensive your collision detection algorithm is). If you start getting latency issues, you might want to look into a Quad Tree. This will reduce your search to O(LogN) time. I wrote a quad tree for the first time a few days ago (compiled and worked the first time!!!) and it was suprisingly easy to code up (I figured it out on pen and paper before writing the code).

I also noticed that you algorithm isn't checking an object for self reference before doing collision detection checks. You might be doing that check in your collision detection function, but if you're not, you're going to always get an 'true' when checking for collision against yourself.

In my collision detection checks, I create a "collision record" for each collision I detected and then create a list of these collision records (to accumulate multiple collisions). Then, I run my lists of detected collisions into a collision handling class which then figures out how to process the collision based on the list of collision records (objects can be static (like a wall), moving and with mass (like an asteroid), or ethereal (like a bonus pickup item), or explosive (like a bomb or missile)).

This topic is closed to new replies.

Advertisement