I'm sorry I did not understand. v What is the speed of the ball. vx1,vx2,vy1 or vy2. complate code is;

-(void) tophareket {

ballspeedx=0; ballspeedy=4; balltwospeedx=0; balltwospeedy=-4;

// top top1'e değerse

if(sqrt(pow((balltwo.center.x-ball.center.x),2)+pow((balltwo.center.y-ball.center.y),2))<38)

{

double r1=19, r2=19, m1=1, m2=1, x1=ball.center.x, y1=ball.center.y, x2=balltwo.center.x, y2=balltwo.center.y,

vx1=ballspeedx, vy1=ballspeedy, vx2=balltwospeedx, vy2=balltwospeedy;

double dx = x2-x1, dy = y2-y1;

// where x1,y1 are center of ball1, and x2,y2 are center of ball2

double distance = sqrt(dx*dx+dy*dy);

// Unit vector in the direction of the collision

double ax=dx/distance, ay=dy/distance;

// Projection of the velocities in these axes

double va1=(vx1*ax+vy1*ay), vb1=(-vx1*ay+vy1*ax);

double va2=(vx2*ax+vy2*ay), vb2=(-vx2*ay+vy2*ax);

// New velocities in these axes (after collision): ed<=1, for elastic collision ed=1

double vaP1=va1 + (1+1)*(va2-va1)/(1+1);

double vaP2=va2 + (1+1)*(va1-va2)/(1+1);

// Undo the projections

vx1=vaP1*ax-vb1*ay; vy1=vaP1*ay+vb1*ax;// new vx,vy for ball 1 after collision

vx2=vaP2*ax-vb2*ay; vy2=vaP2*ay+vb2*ax;// new vx,vy for ball 2 after collision

ballspeedx = vx1;

ballspeedy = vy1;

balltwospeedx = vx2;

balltwospeedy = vx2;

dx=x2-x1, dy=y2-y1; d=sqrt(dx*dx+dy*dy);

double vp1= vx1 *dx/d+vy1*dy/d;

double vp2= vx2*dx+vy2*dy/d;

double dt =(r1+r2-d)/(vp1+vp2);

x1-= vx1*dt;

y1 -= vy1*dt;

x2-=vx2*dt;

y2-=vy2*dt;

ball.center = CGPointMake(x1, y1);

balltwo.center = CGPointMake(x2, y2);

x1+= vx1*dt;

y1+=vy1*dt;

x2+=vx2*dt;

y2+=vy2*dt;

ball.center = CGPointMake(x1, y1);

balltwo.center = CGPointMake(x2, y2);

ball.center = CGPointMake(ball.center.x + ballspeedx, ball.center.y + ballspeedy);

balltwo.center = CGPointMake(topbir.center.x + balltwospeedx, topbir.center.y + balltwospeedy);

}

and I tried the java programs you provide, tenth the collision happened. I wonder where I made a mistake?

-(void) tophareket {

// top top1'e değerse

if(sqrt(pow((topbir.center.x-top.center.x),2)+pow((topbir.center.y-top.center.y),2))<41)

{

double alpha, m1=1, m2=1, r1=19, r2=19,

x1=top.center.x, y1=top.center.y, x2=topbir.center.x, y2=topbir.center.y,

vx1=topspeedx, vy1=topspeedy, vx2=topbirspeedx, vy2=topbirspeedy,;

double r12=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

double cs=(x2-x1)/r12; double sc=(y2-y1)/r12;

double vp1=vx1*cs+vy1*sc;

double vp2=vx2*cs+vy2*sc;

// back to collision time

double ddt=(r1+r2-r12)/(vp1-vp2);

if(ddt>0)ddt=0.;

x1-=vx1*ddt; y1-=vy1*ddt;

x2-=vx2*ddt; y2-=vy2*ddt;

r12= sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

//calculate component of velocity

cs=(x2-x1)/r12; sc=(y2-y1)/r12;

vp1=vx1*cs+vy1*sc;

vp2=vx2*cs+vy2*sc;

// normal components do not change

double vn1=vx1*sc-vy1*cs;

double vn2=vx2*sc-vy2*cs;

//calculate component of velocity in original X,Y coordinate

double px1=vp1*cs; double py1=vp1*sc;

double px2=vp2*cs; double py2=vp2*sc;

double nx1=vn1*sc; double ny1=-vn1*cs;

double nx2=vn2*sc; double ny2=-vn2*cs;

topspeedx = px1+nx1;

topspeedy = py1+ny1;

topbirspeedx = px2+nx2;

topbirspeedy = py2+ny2;

int i=2;

top.center = CGPointMake(top.center.x + i*topspeedx, top.center.y + i*topspeedy);

topbir.center = CGPointMake(topbir.center.x + i*topbirspeedx, topbir.center.y + i*topbirspeedy);

}