[quote author=Fu-Kwun Hwang link=topic=4.msg1529#msg1529 date=1185264253]
Please following the code I did in my previous messages.
Post the code again if you still have the problem.
[/quote]

Hey professor i did it, but somethimes the balls don't colide right  ???

anything wrong with my code ?

[code]
  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[i].cx - ball[n].cx;
        dy := ball[i].cy - ball[n].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 * dx) + (ball[i].vy * ay);
          dt := ((ball[i].raio + ball[n].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].Refresh;
          ball[i].Refresh;
          //
          dx := ball[i].cx - ball[n].cx;
          dy := ball[i].cy - ball[n].cy;
          dist := Sqrt(Power(dx, 2) + Power(dy, 2));
          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;
          vn_n := ball[n].vy * ay - ball[n].vy * ax;
          vn_i := ball[i].vy * ay - ball[i].vy * ax;
          vn:= vp_n + (1+ed)*(vp_i - vp_n)/(1+ball[n].mass/ball[i].mass);
          vi:= vp_i + (1+ed)*(vp_n - vp_i)/(1+ball[i].mass/ball[n].mass);
          ball[n].vx:= vn*ax-vn_n*ay;
          ball[n].vy:= vn*ay+vn_n*ax;
          ball[i].vx:= vi*ax-vn_i*ay;
          ball[i].vy:= vi*ay+vn_i*ax;
          ball[n].Refresh;
          ball[i].Refresh;
          ball[n].colided:=True;
          ball[i].colided:=True;
        end;
      end;
  end;

  for n := 0 to Length(ball) - 1 do
  begin
    ball[n].Move;
  end;
end;
[/code]

2 balls
both mass = 1 and ed = 1