Quote Originally Posted by arthurprs
Quote Originally Posted by Robert Kosek
Hello,

Your problem is in procedure Tform1.colision; because you loop through the balls twice. You might have more than one problem, but this is the one I really see.

You only have two balls, and you know this, so why do you check the collision of the balls twice in nested for-loops? If ball 1 collided with ball 2, obviously ball 2 collided with 1! Yet, you loop through and check again. This is where the strange behavior comes in. To use a for loop with this sort of thing, you'll have to "register" collisions to avoid, uh, collision collisions.... How to make that less wacky and more understandable? Ah, prevent duplicate collisions. Sorry, for a second there I got stuck between cars in my train of thought.
...

Thx for the help! ;]

i will try what u say, but just one thing, i think the loop is necessary because the original source have 4 balls (you can see the dynamic array), isn't it ?


*from brazil so sorry my english
i have finnaly find the problem but don't know how solve it

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