i have finnaly find the problem but don't know how solve itOriginally Posted by arthurprs
the balls get same V after the colision, so with same V balls continues coliding and getting more and more V
A -> with 0.5v
B <- with -0.9v
result in
A <- with -0.4v
B <- with -0.4v
=/
[pascal]
procedure Tform1.colision;
var
n, i: Integer;
dist: double;
begin
for n := 0 to Length(ball) - 1 do
begin // n = currentball
ball[n].v2x := 0;
ball[n].v2y := 0;
ball[n].colided := False;
// wall hit
//x
if (ball[n].x <= 0)
or (ball[n].x2 >= Form1.ClientWidth) then
begin
ball[n].vx := -ball[n].vx;
// todo: atrito 10%
end;
//y
if (ball[n].y <= 0)
or (ball[n].y2 >= Form1.ClientHeight) then
begin
ball[n].vy := -ball[n].vy;
// todo: atrito 10%
end;
// other balls hit
for i := 0 to Length(ball) - 1 do // i = other ball
if (i <> n) then
begin
dist := distance(ball[i].cx, ball[i].cy, ball[n].cx, ball[n].cy);
if dist < (ball[i].raio + ball[n].raio) then
begin
ball[n].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;
ball[n].v2y := ball[n].v2y + ball[i].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;
end;
ball[n].Refresh;
end;
end;
[/pascal]
EDITED
Bookmarks