### Author Topic: 2D Collision  (Read 515776 times)

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #30 on: July 23, 2007, 04:35:51 pm »
You can find out the code related to what you need from previous messages.
You are not following the same steps I did.

#### arthurprs

• Newbie
• • Posts: 10 ##### Re: 2D Collision
« Reply #31 on: July 24, 2007, 02:29:19 am »
You can find out the code related what you need from previous messages.
You are not following the same steps I did.
ok.

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #32 on: July 24, 2007, 04:04:13 pm »
Please following the code I did in my previous messages.
Post the code again if you still have the problem.

#### arthurprs

• Newbie
• • Posts: 10 ##### Re: 2D Collision
« Reply #33 on: July 26, 2007, 10:44:45 am »
Please following the code I did in my previous messages.
Post the code again if you still have the problem.

Hey professor i did it, but somethimes the balls don't colide right anything wrong with my 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;

2 balls
both mass = 1 and ed = 1

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #34 on: July 26, 2007, 11:43:11 am »
You did not read my post carefully.
You did not do the following steps:

First calculate the component of velocity in the direction of (dx,dy)
vp1= vx1 *dx/d+vy1*dy/d;
vp2= vx2*dx+vy2*dy/d;

Collision should have happened dt before you have detected r1+r2

and dt =(r1+r2-d)/(vp1+vp2);// the collision should have occurred at t-dt (Actually this is also an approximation).

So you should move those two ball backward
x1-= vx1*dt;
y2 -= vy1*dt;
x2-=vx2*dt;
y2-=vy2*dt;

Next : do the collision parts. as the normal case.

And move the time forward:

Because we have move time backward dt, we need to move time forward dt.
so
x1+= vx1'*dt;
y1+=vy1'*dt;
x2+=vx2'*dt;
y2+=vy2'*dt;

Now the distance between two ball will be larger than r1+r2.

http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg15#msg15

#### estruiz

• Newbie
• • Posts: 2 ##### Re: 2D Collision
« Reply #35 on: July 27, 2007, 06:30:46 am »
Hi Professor

What I need to know if this is possible and if it is, at what speed does the car need to be traveling to be able to push back the truck?  Please email me if you need any more information at estruiz@msn.com

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #36 on: July 27, 2007, 10:20:00 am »
"the car is still heading in the same 14 degrees, but now going sideway almost at 90 degrees to the 14 degree direction."

I do not quite understand the above situation? And is it happened before the collision?

"The slope of the road heading south is S=1.45% south and the entire road slopes to the east at 2.75%.  "
I did not understand the above statement,either!

The car is sent east of the impact about 18 feet, But from center to edge of pavement is 14 feet, so the car is out of the pavement?

Without consider the above parameters. From the impact distance in the north-south direction.
I try to estimate the speed right after the impact for both car:
Assume the stopping distance are all due to friction of the road (which might not be true if car was flying after the impact)
The following are very rough estimation and base on  the above assumption:
acceleration for both car assume to be the same: a=-0.565*9.8=5.537 m/s^2

v=sqrt(2*a*s)=sqrt(2*9.8*0.565*s)
19 feet=5.79 m so the speed is about 8 m/s  So it took 1.4 s to fully stopped after the collision.
8 feet=2.43m so the speed is about 5.2m/s  So it took 0.9s to fully stopped after the collision.

Initial speed for the truck is 43mph=69km/hr=19.2m/s

Assume conservation of momentum during collision
3650*(v+5.2)=5251*(19.2+8) so v=33.9m/s = 122 km/hr= 75.9 mph (estimated speed for the car)

Because the mass for the car is smaller than the truck and the bounce off distance for the car is less than the truck, so the speed of the car is larger than the truck.
However, the above estimate are based on very simple model. I believe local police agent should have gather more data to provide a better estimation.

#### arthurprs

• Newbie
• • Posts: 10 ##### Re: 2D Collision
« Reply #37 on: July 28, 2007, 01:50:27 pm »
You did not read my post carefully.
You did not do the following steps:

First calculate the component of velocity in the direction of (dx,dy)
vp1= vx1 *dx/d+vy1*dy/d;
vp2= vx2*dx+vy2*dy/d;

Collision should have happened dt before you have detected r1+r2

and dt =(r1+r2-d)/(vp1+vp2);// the collision should have occurred at t-dt (Actually this is also an approximation).

So you should move those two ball backward
x1-= vx1*dt;
y2 -= vy1*dt;
x2-=vx2*dt;
y2-=vy2*dt;

Next : do the collision parts. as the normal case.

And move the time forward:

Because we have move time backward dt, we need to move time forward dt.
so
x1+= vx1'*dt;
y1+=vy1'*dt;
x2+=vx2'*dt;
y2+=vy2'*dt;

Now the distance between two ball will be larger than r1+r2.

http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.msg15#msg15

yes i did it in lines 30 and 31 =/

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #38 on: July 28, 2007, 09:39:48 pm »
You are checking collision between n particles
with loop like
for(int i=0;i for(j=0;j }
}
In the above case, collision between particles might be processed twice.
You can change the loop to
for(int i=0;i for(j=0;j  if(i!=j){// particle can not collide with itself
}
}

And I did not fully understand what do you mean by
"somethimes the balls don't colide right  Huh"!
You only tell me you think there is something wrong, But I did not know what is wrong!

}

#### arthurprs

• Newbie
• • Posts: 10 ##### Re: 2D Collision
« Reply #39 on: July 29, 2007, 03:49:09 pm »
You are checking collision between n particles
with loop like
for(int i=0;i<n;i++){
for(j=0;j<n;j++){
}
}
In the above case, collision between particles might be processed twice.
You can change the loop to
for(int i=0;i<n;i++){
for(j=0;j<i;j++){
if(i!=j){// particle can not collide with itself
}
}

And I did not fully understand what do you mean by
"somethimes the balls don't colide right  Huh"!
You only tell me you think there is something wrong, But I did not know what is wrong!

}

the problem is that in 10% of colisions the backtime to make (distance = r1 + r2) does not work right and the ball keep inside the other one

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #40 on: July 29, 2007, 10:11:46 pm »
It is always difficult and take time to find out the bug, especially you are working with another language.

Did you change the setting in the loop for your code?
Did you make sure all the particles are not overlap with each other in the initial state?
If you can run  your code step by step, you might be able to find out bugs in your code.
I was assuming the distance moved between each time step is smaller than the size of the particle.
It might be your time step is too large or the velocity is too big. In that case, try to use a smaller time step.

The original source code for this java applet is attached.

#### arthurprs

• Newbie
• • Posts: 10 ##### Re: 2D Collision
« Reply #41 on: July 30, 2007, 09:27:32 am »
It is always difficult and take time to find out the bug, especially you are working with another language.

Did you change the setting in the loop for your code?
Did you make sure all the particles are not overlap with each other in the initial state?
If you can run  your code step by step, you might be able to find out bugs in your code.
I was assuming the distance moved between each time step is smaller than the size of the particle.
It might be your time step is too large or the velocity is too big. In that case, try to use a smaller time step.

The original source code for this java applet is attached.

ohh i thinks is the velocity its 1.5 in one ball and 1.3 on another i will try #### estruiz

• Newbie
• • Posts: 2 ##### Re: 2D Collision
« Reply #42 on: July 31, 2007, 02:19:32 am »
From what I know the car went off the road and tried to get back on to his lane.  This caused the car to get out of control and went sideways at a 14 degree direction before the collision.

yes the slope of the street heading south is S=1.45% down and the cross slope of the street is actually S=2.86% to the east from the west edge of pavment.

yes the car went outside of the pavment onto dirt shoulder which has a slope of around S=13% EAST

#### Fu-Kwun Hwang

• Hero Member
•     • • Posts: 3062 ##### Re: 2D Collision
« Reply #43 on: July 31, 2007, 10:07:57 am »
I did not take into account the slope of the road in my previous estimation. Since the car went outside the pavement and onto dirt shoulder which has a slope. The velocity of the car might be less than the estimated velocity.  However, it was a very rough estimation from the information I knew.

#### tedhuntington

• Newbie
• • Posts: 1 ##### Re: 2D Collision
« Reply #44 on: August 31, 2007, 01:14:14 am »
The 2d collision description is easy to understand, thanks. What needs to be done to make it 3D? Is that a major difference? Do we just need to figure out the projection, perpendicular and one other component then?

thanks
Ted