Pedant

10-14-2003, 02:35 PM

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]->setvelocity(V1);

mysphere[j]->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.

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]->setvelocity(V1);

mysphere[j]->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.