Sir, I'm implementing 2d collision on objective - c using opengl.
but it didn't work . if i didn't calculate and apply dt to ball 1 position (translation) , the ball is stuck together (but it is moving) .
but if i apply dt, the ball seems going messy when collideed. can you see what is going on with the code?
the variable dt can be minus when check through console, is it right?

[code]BBPoint coliddeSpeed = [(BBStaticMarble*)sceneObject speed];
BBPoint collideeTranslation = [(BBStaticMarble*)sceneObject translation];

CGFloat r1,r2;
CGFloat dx=collideeTranslation.x-translation.x, dy=collideeTranslation.y-translation.y;
CGFloat d=sqrt(dx*dx+dy*dy);
//First calculate the component of velocity in the direction of (dx,dy)
CGFloat vp1= speed.x*dx/d+speed.y*dy/d;
CGFloat vp2= collideeSpeed.x*dx/d+collideeSpeed.y*dy/d;
//Collision should have happened dt before you have detected r1+r2
CGFloat dt = (r1+r2-d)/(vp1-vp2);// the collision should have occurred at t-dt (Actually this is also an approximation).

//So you should move those two ball backward
translation.x -= speed.x*dt;
translation.y -= speed.y*dt;
collideeTranslation.x -=collideeSpeed.x*dt;
collideeTranslation.y-=collideeSpeed.y*dt;

dx=collideeTranslation.x-translation.x, dy=collideeTranslation.y-translation.y;
// where x1,y1 are center of ball1, and x2,y2 are center of ball2
CGFloat distance = sqrt(dx*dx+dy*dy);
// Unit vector in the direction of the collision
CGFloat ax=dx/distance, ay=dy/distance;
// Projection of the velocities in these axes
CGFloat va1=(speed.x*ax+speed.y*ay), vb1=(-speed.x*ay+speed.y*ax);
CGFloat va2=(collideeSpeed.x*ax+speed.y*ay), vb2=(-collideeSpeed.x*ay+collideeSpeed.y*ax);
// New velocities in these axes (after collision): ed<=1,  for elastic collision ed=1
CGFloat ed = 0.8;
CGFloat vaP1=va1 + (1+ed)*(va2-va1)/(1+self.mass/collideeMass);
CGFloat vaP2=va2 + (1+ed)*(va1-va2)/(1+collideeMass/self.mass);
// Undo the projections
speed.x=vaP1*ax-vb1*ay;  speed.y=vaP1*ay+vb1*ax;// new vx,vy for ball 1 after collision
collideeSpeed.x=vaP2*ax-vb2*ay; collideeSpeed.y=vaP2*ay+vb2*ax;// new vx,vy for ball 2 after collision

//Because we have move time backward dt, we need to move time forward dt.
translation.x+= speed.x*dt;
translation.y+= speed.y*dt;
collideeTranslation.x+=collideeSpeed.x*dt;
collideeTranslation.x+=collideeSpeed.y*dt;

[(BBStaticMarble*)sceneObject setSpeed:collideeSpeed];
[(BBStaticMarble*)sceneObject setTranslation:collideeTranslation];
[(BBStaticMarble*)sceneObject setIsMarbleMoving:YES]
[/code]