# collision

• 07-12-2004
ichijoji
collision
Ok, I'm making a racing game. I want to make a collision system so the cars will bounce off of each other realistically. The best I've come up with is to trade the direction and speed between any two cars when they collide, but this doesn't look right. Does anybody have a better method?
• 07-13-2004
well, excluding coming up with a total physics system, you can do a basic reflection algorithm which excluding mass and all, is basically what you have.

Code:

``` // NOTE : all the following vectors are unit length // R is a vector that will store the reflected vector // L is a vector that is to be reflected off of a surface // N is the normal vector representing the surface //  a period represents dot product L *= -1; R =  2N(N . L) - L;```
someone with better drawing skills can give you a diagram.

other than that, google for it.
• 07-13-2004
VirtualAce
Strangely it also looks like the formula for specular lighting.
• 07-13-2004
vNvNation
How in depth do you want to go with this?

For one, BladeRunner's equations do not work because it implies that *no kinetic energy is lost*, but in reality cars don't bounce off of each other with the same kinetic energy they had coming in towards the impact site. Also, you need to be able to resolve the normal of the collision. If you are using spheres, this is the unit vector that connects the centers of the spheres. If you are colliding their aligned bounding boxes, then you need to check for:

vertex-vertex collisions
vertex-edge collisions
penetration collision

in case 1, the normal is ambiguous so you can just make it cross through the centers of the boxes

in case 2, the normal is the normal of the edge/plane that is being interacted with

in case 3, you should separate the boxes and then determine if the interaction was vertex-vertex or vertex-edge (objects should never be allowed to penetrate in a physics simulation).

edit

Quote:

Strangely it also looks like the formula for specular lighting.
No, that equation has nothing to do with lighting per-se...all the equations do is reverse the normal component of the vector, nothing more...it just so happens that the collisions between photons and other objects are perfectly elastic, and subsequently no kinetic energy is ever lost, and the angle of incidence is equal to the angle of reflection.
• 07-13-2004
VirtualAce
Same idea, though. Unit reflected vector is the specular component.
• 07-13-2004
Quote:

For one, BladeRunner's equations do not work because it implies that *no kinetic energy is lost*
*cough * *cough* I said that:

Quote:

excluding coming up with a total physics system, you can do a basic reflection algorithm which excluding mass and all, is basically what you have.
I know all about linear/angular momentum in this situation(for example, this is why a huge 18-wheeler semi truck will plow through a car, not vice-versa).

Quote:

(objects should never be allowed to penetrate in a physics simulation)
so much for bullets, thin/narrow objects(relative to what they are hitting) under the influence of heavy wind, needles ..... unless you are talking about when determining collisions they should never be allowed to penetrate?

btw vVNation, I wouldn't mind talking to you about a physics engine that I am currently in the beginning stages of(see if I have everything down so far right), and I would also talk about your opengl implementation.
• 07-13-2004
vNvNation
Inter-penetrations aren't allowed to happen, period. You'll run into plenty of problems if you do.

There's not a ton to talk about with the OpenGL implementation. Visit SGI's website, and look for their sample implementation. You can also find something called Mesa, which is another free OpenGL implementation. By reading through those thousands of lines of code, reading math books, and experimenting, I was able to create my own adaptations, and ultimately write about half of the implementation on my own. It really isn't that hard, but it is forced to ultimately run in software, not on your video card. This may represent a conundrum in your eyes, because you might be thinking "but OpenGL is supposed to be hardware accelerated, right?" Wrong. OpenGL is just an interface, nothing more.
• 07-13-2004