Real code, real problem comes

http://www.mediafire.com/?fdwtze33wwt
Source + Demo exe


[pascal]
procedure Tform1.colision;
var
n, i: Integer;
dist, dt: Double;
dx, dy: Double;
ax, ay: Double;
vp_n, vp_i: Double;
begin

for n := 0 to Length(ball) - 1 do
begin // n = currentball

// wall hit
//x
if (ball[n].x <= 0)
or (ball[n].x2 >= Form1.ClientWidth) then
begin
ball[n].vx := -ball[n].vx {* 0.9};
end;
//y
if (ball[n].y <= 0)
or (ball[n].y2 >= Form1.ClientHeight) then
begin
ball[n].vy := -ball[n].vy {* 0.9};
end;

// other balls hit
for i := 0 to Length(ball) - 1 do // i = other ball
if (i <> n) and not (ball[i].colided) and not (ball[n].colided) then
begin
*dx := ball[n].cx - ball[i].cx;
*dy := ball[n].cy - ball[i].cy;
*dist := Sqrt(Power(dx, 2) + Power(dy, 2));
if dist <= (ball[i].raio + ball[n].raio) then
begin
*ax := dx / dist;
*ay := dy / dist;
*vp_n := ball[n].vx * ax + ball[n].vy * ay;
*vp_i := ball[i].vx * ax + ball[i].vy * ay;
*dt := (ball[n].raio + ball[i].raio - dist) / (vp_n + vp_i);
*ball[n].x := ball[n].x - (ball[n].vx * dt);
*ball[n].y := ball[n].y - (ball[n].vy * dt);
*ball[i].x := ball[i].x - (ball[i].vx * dt);
*ball[i].y := ball[i].y - (ball[i].vy * dt);
ball[n].colided := True;
ball[i].colided := True;
form1.Caption := 'colidiu bola : ' + IntToStr(n) + ' , ' + IntToStr(i) + ' na distancia de ' + FloatToStr(dist) + ' ' + FloatToStr(ball[n].vx) + ' ' + FloatToStr(ball[n].vy) + ' ' + FloatToStr(ball[i].vx) + ' ' + FloatToStr(ball[i].vy);
ball[n].v2x := ball[n].v2x + ball[i].vx; // this will change, but need solve the oter problem first
ball[n].v2y := ball[n].v2y + ball[i].vy;
ball[i].v2x := ball[i].v2x + ball[n].vx;
ball[i].v2y := ball[i].v2y + ball[n].vy;
end;
end;
end;

for n := 0 to Length(ball) - 1 do
begin
if ball[n].colided then
begin
ball[n].vx := ball[n].vx + ball[n].v2x;
ball[n].vy := ball[n].vy + ball[n].v2y;
ball[n].v2x := 0;
ball[n].v2y := 0;
ball[n].colided := False;
end;
ball[n].Refresh;
end;
end;
[/pascal]

Based on http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0 i write some code to make distance of balls be ball1Ray + ball2Ray
i marked the lines in the code with a *

i think i writed it correctly, but it don't work, the code moves the ball to a wrong place =/ and not when the dist = ball1Ray + ball2Ray

:?