Collision detection

I’m currently trying to implement collision detection. Trying to work on it from a NeHe tutorial.
However it currently doesn’t work at all well and the balls speed up.

``````In More Detail:
*/
//a) Find X_Axis
//X_Axis = (center2 - center1);
//Unify X_Axis, X_Axis.unit();
xaxis[0] = xd;
xaxis[1] = yd;
xaxis[2] = zd;

//get velocities of both
U1[0] = move1[3];
U1[1] = move1[4];
U1[2] = move1[5];
U2[0] = move2[3];
U2[1] = move2[4];
U2[2] = move2[5];

//b) Find Projections
//U1x= X_Axis * (X_Axis dot U1)
//U1y= U1 - U1x
//U2x =-X_Axis * (-X_Axis dot U2)
//U2y =U2 - U2x
a =0.01;// (xaxis[0] * U1[0]) + (xaxis[1] * U1[1]) + (xaxis[2] * U1[2]);
U1x[0] = a * xaxis[0];
U1x[1] = a * xaxis[1];
U1x[2] = a * xaxis[2];
U1y[0] = U1[0] - U1x[0];
U1y[1] = U1[1] - U1x[1];
U1y[2] = U1[2] - U1x[2];
b = 0.01;//(-xaxis[0] * U1[0]) + (-xaxis[1] * U1[1]) + (-xaxis[2] * U1[2]);
U2x[0] = -xaxis[0] * b;
U2x[1] = -xaxis[1] * b;
U2x[2] = -xaxis[2] * b;
U2y[0] = U2[0] - U2x[0];
U2y[1] = U2[1] - U2x[1];
U2y[2] = U2[2] - U2x[2];
//c)Find New Velocities
//(U1x * M1)+(U2x*M2)-(U1x-U2x)*M2
//V1x= --------------------------------
//M1+M2
//(U1x * M1)+(U2x*M2)-(U2x-U1x)*M1
//V2x= --------------------------------
//M1+M2
/*
V1x=(U1x+U2x-(U1x-U2x))*0.5;						// Now Find New Velocities
V2x=(U1x+U2x-(U2x-U1x))*0.5;
V1y=U1y;
V2y=U2y;
*/
V1x[0] = (U1x[0]+U2x[0])-(U1x[0]-U2x[0])*0.5;
V1x[1] = (U1x[1]+U2x[1])-(U1x[1]-U2x[1])*0.5;
V1x[2] = (U1x[2]+U2x[2])-(U1x[2]-U2x[2])*0.5;
V2x[0] = (U1x[0]+U2x[0])-(U2x[0]-U1x[0])*0.5;
V2x[1] = (U1x[1]+U2x[1])-(U2x[1]-U1x[1])*0.5;
V2x[2] = (U1x[2]+U2x[2])-(U2x[2]-U1x[2])*0.5;
//In our application we set the M1=M2=1, so the equations get even simpler.
//d)Find The Final Velocities
//V1y=U1y
//V2y=U2y
//V1=V1x+V1y
//V2=V2x+V2y
V1y[0] = U1y[0];
V1y[1] = U1y[1];
V1y[2] = U1y[2];
V2y[0] = U2y[0];
V2y[1] = U2y[1];
V2y[2] = U2y[2];
V1[0] = V1x[0] + V1y[0];
V1[1] = V1x[1] + V1y[1];
V1[2] = V1x[2] + V1y[2];
V2[0] = V2x[0] + V2y[0];
V2[1] = V2x[1] + V2y[1];
V2[2] = V2x[2] + V2y[2];
mysphere[i]-&gt;setvelocity(V1);
mysphere[j]-&gt;setvelocity(V2);
``````

I have to set a and b to be 0.01 or the balls immediately disappear (go too fast).

Full code can be found here: - http://psi.homelinux.org/john/opengl/

Any help would be much appreciated.
Thanks.

And this has what to do with OpenGL, precisely?

I draw balls (using OpenGL), I bounce them inside a box, when they collide I need to detect this(done) and bounce them off each other at the correct angle (working on/trying to get help with). I am sure many people in this forum have had to do similar.

Originally posted by Korval:
And this has what to do with OpenGL, precisely?

What does your post have to do with OpenGL?

Don’t mind Korval, he tends to be harsh. Your question is off-topic, however, and will not be answered here. Try posting at www.flipcode.com, among other places.

Thanks, I just thought that most people in this forum would have had to deal with this sort of problem at one time or another.

I am an optimist, when people are like that I just tend to think I have caught them on a bad day, physics questions have been answered before : -http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/002966.html
which korval has answered

Originally posted by Korval:
It is important to have a full-function analytic solution for AI purposes, though. If you want the AI to hit something, it has to know what angle/power to use. And the fastest way to know that is to be able to solve this problem analytically.

amongst other things http://www.opengl.org/discussion_boards/ubb/Forum3/HTML/002834.html

Originally posted by Pedant:
I draw balls (using OpenGL)

So if you had drawn your balls using Direct X this would be a Direct X question, or it could have been a GDI question, or perhaps even a glide question.

Most collision detection questions get answered in the same way on this board - Opengl doesn’t have anything to do with it.

Where can I get cheap gas in the bay area? I figure most of you are programmers, and thus live around there, and you probably drive cars, too.

Originally posted by l33t:
Where can I get cheap gas in the bay area? I figure most of you are programmers, and thus live around there, and you probably drive cars, too.

Your question will tick people off unless you specify the gas has to support OpenGL.

After reading some of the other posts on this thread, I have no problem answering this question.

There’s a decent algorithm for this on Gamasutra. My implementation had a few problems.
http://www.gamasutra.com/features/20020118/vandenhuevel_03.htm

If this kind of thing interests you, have a look at ODE
http://opende.sourceforge.net/

PS: I guess you have a problem with energy conservation there. I cant really understand that code, so I cant say.

Nehe also has some tutorials on collision detection. its here
I think its lesson 30

-Zix

Originally posted by zix99:
[b]Nehe also has some tutorials on collision detection. its here
I think its lesson 30

-Zix[/b]

Originally posted by Pedant:
I’m currently trying to implement collision detection. Trying to work on it from a NeHe tutorial.

If you’re going to reply to an OT post, at least read it first.