[quote author=Fu-Kwun Hwang link=topic=4.msg7113#msg7113 date=1284137411]
I do not understand the way you defined those angle?
I would suggest you use (x,y,z) coordinate.
[img]http://www.phy.ntnu.edu.tw/demolab/phpBB/pics/2_2_collision2d.png[/img]


Assume \vec{r_a} and \vec{r_b} are center of those two balls. And the radius are R_a,R_b.
Let \vec{d}= \vec{r_a}-\vec{r_b} which is the relative vector between two centers.
Collision occurred if the length math_failure (math_unknown_error): |\vec{d}|<R_a+R_b :
The next step is to find two components of velocity vectors \vec{V_a}=(V_{ax},V_{ay},V_{az}),\vec{V_b}=(V_{bx},V_{by},V_{bz}):
  1. velocity components parallel to \vec{d}=(d_x,d_y,d_z)
  \vec{V}_{ap}=\vec{V_a}\cdot \vec{d}/|\vec{d}|,\vec{V}_{bp}=\vec{V_b}\cdot \vec{d}/|\vec{d}|
  The time step to move backward dt=\frac{R_a+R_b-|\vec{d}|}{|\vec{V}_{ap}-\vec{V}_{bp}|}

Hints: how to calculate inner product \vec{V_a}\cdot \vec{d}=V_{ax}*d_x+ V_{ay}*d_y+V{az}*dz

  2. velocity components perpendicular to \vec{d}
\vec{V_{an}}=\vec{V_a}-\vec{V_{ap}}, \vec{V_{bn}}=\vec{V_b}-\vec{V_{bp}}
The above two component will not change before and after collision


You can check out previous posted message. e.g. [url=http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg9#msg9]How to convert 2D collision into 1D[/url]

Just add one more component (z) if you want to transfer from 2D(x,y)to 3D(x,y,z).

It will be much easier if understand the meaning of inner product between two vectors- Help you find out projection of one vector into another vector.
[/quote]


Thank you very much, but I still have a problem.
I'm not sure that the bounce is correct...

This is my code:
[code]
                float[] center1=p1.getCenter();
float[] center2=p2.getCenter();
float[] velocity1=p1.getVelocity();
float[] velocity2=p2.getVelocity();
short r1=p1.getRadius();
short r2=p2.getRadius();


float dx12 = (center1[0]-center2[0]);
float dy12 = (center1[1]-center2[1]);
float dz12 = (center1[2]-center2[2]);

float dx21 = (center2[0]-center1[0]);
float dy21 = (center2[1]-center1[1]);
float dz21 = (center2[2]-center1[2]);


float distance = (float) Math.sqrt(dx12*dx12+dy12*dy12+dz12*dz12);

if (distance<((r1+r2))){

float[] Va=p1.getVelocityComponent();
float[] Vb=p2.getVelocityComponent();

float[] D={dx12,dy12,dz12};
float[] Db={dx21,dy21,dz21};

float[] Vap= new float[3];
Vap[0]=Va[0]*D[0]/distance;
Vap[1]=Va[1]*D[1]/distance;
Vap[2]=Va[2]*D[2]/distance;


float[] Vbp= new float[3];
Vbp[0]=Vb[0]*Db[0]/distance;
Vbp[1]=Vb[1]*Db[1]/distance;
Vbp[2]=Vb[2]*Db[2]/distance;

float diff=(float) Math.sqrt((Vap[0]-Vbp[0])*(Vap[0]-Vbp[0])+(Vap[1]-Vbp[1])*(Vap[1]-Vbp[1])+(Vap[2]-Vbp[2])*(Vap[2]-Vbp[2]));


float dt=(r1+r2-distance)/(diff);


float[] Van = new float[3];
Van[0]=Va[0]-Vap[0];
Van[1]=Va[1]-Vap[1];
Van[2]=Va[2]-Vap[2];

float[] Vbn = new float[3];
Vbn[0]=Vb[0]-Vbp[0];
Vbn[1]=Vb[1]-Vbp[1];
Vbn[2]=Vb[2]-Vbp[2];


center1[0]-=Va[0]*dt;
center1[1]-=Va[1]*dt;
center1[2]-=Va[2]*dt;

center2[0]-=Vb[0]*dt;
center2[1]-=Vb[1]*dt;
center2[2]-=Vb[2]*dt;


dx12 = (center1[0]-center2[0]);
dy12 = (center1[1]-center2[1]);
dz12 = (center1[2]-center2[2]);

dx21 = (center2[0]-center1[0]);
dy21 = (center2[1]-center1[1]);
dz21 = (center2[2]-center1[2]);



distance = (float) Math.sqrt(dx12*dx12+dy12*dy12+dz12*dz12);
// Unit vector in the direction of the collision
double ax=dx12/distance, ay=dy12/distance, az=dz12/distance;


float[] Vap1= new float[3];
Vap1[0]=(float) (Va[0]*ax);
Vap1[1]=(float) (Va[1]*ay);
Vap1[2]=(float) (Va[2]*az);


float[] Vbp1= new float[3];
Vbp1[0]=(float) (Vb[0]*(-ax));
Vbp1[1]=(float) (Vb[1]*(-ay));
Vbp1[2]=(float) (Vb[2]*(-az));

float[] Van1 = new float[3];
Van1[0]=Va[0]-Vap1[0];
Van1[1]=Va[1]-Vap1[1];
Van1[2]=Va[2]-Vap1[2];

float[] Vbn1 = new float[3];
Vbn1[0]=Vb[0]-Vbp1[0];
Vbn1[1]=Vb[1]-Vbp1[1];
Vbn1[2]=Vb[2]-Vbp1[2];

float mass1=p1.getMass();
float mass2=p2.getMass();


float[] vaP1=new float[3];
vaP1[0]=(float) (Vap1[0] + (1+ed)*(Vbp1[0]-Vap1[0])/(1+mass1/mass2));
vaP1[1]=(float) (Vap1[1] + (1+ed)*(Vbp1[1]-Vap1[1])/(1+mass1/mass2));
vaP1[2]=(float) (Vap1[2] + (1+ed)*(Vbp1[2]-Vap1[2])/(1+mass1/mass2));

float[] vaP2=new float[3];
vaP2[0]=(float) (Vbp1[0] + (1+ed)*(Vap1[0]-Vbp1[0])/(1+mass2/mass1));
vaP2[1]=(float) (Vbp1[1] + (1+ed)*(Vap1[1]-Vbp1[1])/(1+mass2/mass1));
vaP2[2]=(float) (Vbp1[2] + (1+ed)*(Vap1[2]-Vbp1[2])/(1+mass2/mass1));

velocity1[0]=vaP1[0]-Van[0];
velocity1[1]=vaP1[1]-Van[1];
velocity1[2]=vaP1[2]-Van[2];

velocity2[0]=vaP2[0]-Vbn[0];
velocity2[1]=vaP2[1]-Vbn[1];
velocity2[2]=vaP2[2]-Vbn[2];


center1[0]+=velocity1[0]*dt;
center1[1]+=velocity1[1]*dt;
center1[2]+=velocity1[2]*dt;

center2[0]+=velocity2[0]*dt;
center2[1]+=velocity2[1]*dt;
center2[2]+=velocity2[2]*dt;

[/code]

There is some problems with to compute the diff, because sometimes it is NaN...
There is any error in my code?