hi
sorry for bumping this old thread but ive some problems with ball collision (in C)
the - the conservation of kinetic energy is property but not the conservation of momentum :(
it's the simple ball collision 2D for old nokia mobiles
[code]static void redraw_screen(void)
{
/* ... */
ball_collision(&app.ball_a, &app.ball_b);
<redraw_screen>
}
[/code]
[code]
static void ball_collision(struct ball *ball_a, struct ball *ball_b)
{
float dx = ball_b->x - ball_a->x;
float dy = ball_b->y - ball_a->y;
float d = sqrt(dx*dx+dy*dy);

if(d<=(float)bmap_ball.height) // r1+r2 = bmap_ball.height
{
// first we calculate the velocity in the direction of (dx,dy)
float vp_a = (ball_a->vx*dx + ball_a->vy*dy)/d;
float vp_b = (ball_b->vx*dx + ball_b->vy*dy)/d;

// collision should have happened dt before
float dt =((float)bmap_ball.height-d)/(vp_a-vp_b);

// we should move those ball backward
ball_a->x -= ball_a->vx*dt;
ball_a->y -= ball_a->vy*dt;
ball_b->x -= ball_b->vx*dt;
ball_b->y -= ball_b->vy*dt;

dx = ball_b->x - ball_a->x;
dy = ball_b->y - ball_a->y;
d = sqrt(dx*dx+dy*dy);

// calculate the components of velocity
float ap = dx/d;
float an = dy/d;

vp_a = ball_a->vx*ap + ball_a->vy*an;
float vn_a = ball_a->vx*an - ball_a->vy*ap;

vp_b = ball_b->vx*ap + ball_b->vy*an;
float vn_b = ball_b->vx*an - ball_b->vy*ap;

// calculate new velocity for those balls after collision
ball_a->vx = vp_b*ap + vn_a*an;
ball_a->vy = vp_b*an - vn_a*ap;
ball_b->vx = vp_a*ap + vn_b*an;
ball_b->vy = vp_a*an - vn_b*ap;

// now we need to move time dt forward
ball_a->x += ball_a->vx*dt;
ball_a->y += ball_a->vy*dt;
ball_b->x += ball_b->vx*dt;
ball_b->y += ball_b->vy*dt;
}
}[/code]

and im stuck  ???

regards,
Bartek

oh, and mass1 = mass2 and eta =1