Implimenting Collision Detection

Started by
12 comments, last by ronkfist 17 years, 9 months ago
Hi, What I am really wondering is how I should call the collision detection. I have a Node which contains the translation of the object and a bounding box. However I am not sure when I should call the collision detection 1. After the object moves (eg. In the update if it requires one) to check if there is a collision. If so send out a message 2. Check every object every frame (I think that this might be slower). However I am not sure what is best or faster or even if there is a better way. I know how to check if an object has collided, but I am not sure when to check.
Advertisement
I wish people would care to search around a bit. There is a forum on Maths on Physics . It exists for a purpose...
Quote:Original post by horizon981
I wish people would care to search around a bit. There is a forum on Maths on Physics . It exists for a purpose...


This is not the how but the when, which does not involve math and physics.

Thanks MOD I meant to put it in Game Programming, but I was for some reason in OpenGL!
Instead of writing such a complex system, you could consider using an existing one:
http://www.ode.org/
http://photoneffect.com/coldet/
[www.LifeIsDigital.net - My open source projects and articles.
Well, I want the experience, I could just use an existing one but my whole project is just to learn for now.
For every object, I associate a 'Bubble' (a sphere consisting of a centerpoint and radius) which is not necesarially centered on the object, but contains it.

For each object I check it against all other objects in the sector for collisions; simultaneously I resize its Bubble so that it is half the distance to the nearest other object.
Now on a subsequent movement, if the object in question is still within the boundaries of its own Bubble, it cannnot possibly be the initiating participant in a collision, and I can skip it's collision pass.
If it leaves the bounds, then it May be colliding and checking against all the other N-1 objects must occur. Collisions are checked and the object's Bubble has a new center and radius assigned.

This Bubble system basically means that the timing of collision check occurs as a function of both the object's speed, and proximity to other objects.
(an object far away for others has a large Bubble that it takes a long time to exit from, while a slow moving object won't exit very quickly either)
Well there are many ways you can do it depending upon how accurate you want the collision to be. If every object is moving then you might as well just check all objects every frame. Depending on how fast and small your objects are you may need to test cylinders (think a bounding sphere over time). The way you can optimize this is by sticking every object into a oct tree or some kind of spatial tree, this way you only test objects that might interesect and you don't have to test them all.
Ok well then that could be computationally difficult since i'd have 128 sprites(players, of course these objects would be complex but you dont have to check inside of the main AABB unless they colide) but i'd also have around 1280 projectiles at a time (say an average of 10 per player sometime more and less. These are all visible and may live for seconds at a time). So id have to check for a collision between 128 ships and 1280 shells so that would be (128 * 1280) * (128 *1280) or arround 2 million calculations a frame, right?

However what if I only need to check for a collision between the sprites and the land, and the shells and the ships instead of checking each object with all of the others. However I am not sure exactly how I could tell the difference between a sprite and a projectile, quickly. Would I want to have a list of pointers to all of the projectiles and ships to keep them seperate so it would be more like:
for each projectile, check a collision against each sprite and
for each sprite check collision again land
NOTE: sprites do not collide

However I can see a major problem with this solution. An projectile could be deleted from the node but it would still be in the list of projectiles. Would this be the best way to do what I want, if so i'd have to find a way to work out this bug! Is there even a better way?
Ok, I have one more question, were would I put a function to check if a collision has occured. Would I put it inside of the Node class and have a param that requires a pointer to the node to check against? It would then signal an event if a collision was detected. This is the only way that I can think of doing this.

Another class then would commence all of the calling to check for a collision.
I think its better to handle response immediatly after a collision detection, else you might get some strange results when multiple objects collide.

I do it something like this...

func checkCollisions
{

loop1
{
loop2
{
response = checkCollision(objFromLoop1, objFromLoop2)
if (response.collision)
{
response.execute()
}
}
}

}

This topic is closed to new replies.

Advertisement