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

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### 2D Collision
« on: January 29, 2004, 05:08:42 pm »
Registed user can get files related to this applet for offline access.
Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
If java program did not show up, please download and install latest Java RUN TIME

Due to so many requests from the users, source code for this applet is distributed with the ZIP files you will receive if you click get file for offline use button. Please do not ask me to send you the source code again!

You are welcomed to check out another Collision 2D created with EJS.

Please check out latest 2D collision created with EJS

Let's consider collisions in two dimension:
Press Start to begin the animation.
Two circular objects will move with pre-defined velocity (yellow arrow).
Click left mouse button to suspend the animation. Now, you can
Click near the tip of the velocity vector and drag the mouse button
left/right to change its value (the initial velocity of that object).
Click near the circular boundary and drag the mouse button to change its radius.

Right Click near the boundary of the circle to change it's size (also change it's mass, at the same time).

Click near the center of the circle and drag the mouse to change its location.

Click the mouse button again to resume the animation. When the two objects collide, the animation will be suspend.
Press the mouse button ( Do not release it) to see the velocity vectors just after the collision.

Press Reset button to reset parameters to default values.
eta is the coefficient of restitution eat Vf/Vi Vf=relative velocity just after collision
Vi=relative velocity just before collision
for elastic collision eta=1.
for perfectly inelastic collision eta=0.

You can select different frame of reference to view the relative motion of all the objects.
lab is a laboratory inertial frame. m1, m2 and CM are frame of reference with respect to
left circular object m1, right circular object m2 and center of mass for m1 and m2.

View collision in 1 dimension.

At the moment when they collide, the force between the two circle objects is along the line which connect the center of the two objects.

Now we have 1-D collision problem:
two object move in the direction along the line connect between their center, with velocitys along this directions. (Red vectors)

The vector perpendicular to the above components (green vectors) will not changed after collision.
(If there is no friction force between the two objects)

Please check out Rigid Body Collisions for 2D collision between rectangle shape objects.

Registed user can get files related to this applet for offline access.
Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
If java program did not show up, please download and install latest Java RUN TIME

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### How to convert 2D collision into 1D
« Reply #1 on: October 28, 2004, 03:19:44 pm »
[img:53443ef2ad]http://www.phy.ntnu.edu.tw/demolab/phpBB/pics/2_2_collision2d.png[/img:53443ef2ad]

1. Find the time when two ball collide : D= R1+ R2
where D=Distance between center of two balls , and R1/R2 is radius of each balls

2. Find the line connect center of two balls. and calculate the component of the velocitry for each ball alone the line connect those two balls.
For example : Velocity for each ball is Va and Vb (before collision)
Calculate Vap and Vbp. (and another normal component Van,Vbn)
3. It is become a 1D collision problem: ball A with velocity Vap collide with ball B with velocity Vbp . You should be able to calculate velocity after collision for each ball.
for example:
Vap2=((m1-m2)/(m1+m2))vap+ (2m2/(m1+m2))Vbp and
Vbp2=(2 m1/(m1+m2))Vap+((m2-m1)/(m2+m1))Vbp
The tangential component is not changed Van2=Van, and Vbn2=Vbn
4. Calculate the vector sum for velocity of each ball after collision
Va2=Vap2+Van2 and Vb2=Vb2p+Vbn2 (vector summation)

#### siput_tiung

• Newbie
• Posts: 3
##### topic4
« Reply #2 on: March 12, 2005, 12:41:59 pm »
hi,
is there any difference between balls or humans when collision occur?
i am trying to write a human model for collision cases... and still trying very2 hard to make it in realistic way. your applet seems like good for me and i'm impressed with it... perhaps it would be good to be applied to my project.. can u explain it in detail how's the calculation for it? dividing it into 3 condition,(1.before,2. during..,3.after.. )would be really2 great... thanks..

ref: http://www.phy.ntnu.edu.tw/ntnujava/viewtopic.php?t=19

Regards,
Bazli Karim
siput_tiung@yahoo.com

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### topic4
« Reply #3 on: March 12, 2005, 03:20:41 pm »
The above calculation assume it is an elastic collision between two balls.
i.e. No energy is loss during the collision.
And the ball would not change shape.

For collision betwen two human, we will change our body movment.

Would you explain in more detail why you need to calculate collision between human?
While two persons are walking or ...?

#### siput_tiung

• Newbie
• Posts: 3
##### topic4
« Reply #4 on: March 14, 2005, 11:07:58 am »
let's see... here i'm trying to do collision avoidance among human...

for example... human A is trying to go to point 1... and if there is another human blocking his way... then.. the human A must avoid it.. by changing to other direction and then, go to point 1..
here... i can detect the collision if there is human blocking another human.. but, i have problem with changing direction... to make it realistic..

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### topic4
« Reply #5 on: March 14, 2005, 03:33:38 pm »
Because you want to avoid collision among human.
So there is no collision among human.

You can check the velocity vector, if it is pointing to another person. Then someone is blocking his way.
Provide the horizontal displayment, you can calculate the least change of the velocity.
The person can change direction right at that point or change velocity gradually.

#### siput_tiung

• Newbie
• Posts: 3
##### topic4
« Reply #6 on: March 16, 2005, 03:41:52 pm »
thanks.. i got the idea now..

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### Java Code for collision in 2D
« Reply #7 on: May 11, 2005, 08:03:51 am »
Due to so many email questions asking for help. I re-wrote this post to include more detail calculation steps:

Assume the radius for two ball are r1 and r2, and the coordinates are (x1,y1) and (x2,y2).
Usually, we calculate the distance between two balls d=sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

The collision should have occurred if d equal r1+r2; // if(d==r1+r2)

However, there is a finite time step dt in the simulation calculation,so most often we would not be able to find the above condition (exact time when two ball real occurred).
So we check if d<= r1+r2; Then we know the collision should have occurred.

The real collision should have occurred before you detected that the distance between two center of the balls d is less than the sum of those two balls r1+r2. (d< r1+r2)

The correct way is to find the time when the two ball really collide with each other.
Suppose the center of those two ball are x1,y2 and x2,y2
dx=x2-x1, dy=y2-y1; d=sqrt(dx*dx+dy*dy);

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

Collision should have happened dt before you have detected r1+r2 and dt =(r1+r2-d)/(vp1-vp2);
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;

Now the distance between center of the two balls is d'=r1+r2;

The following code take care of collision:

double dx = x2-x1, dy = y2-y1;
// where x1,y1 are center of ball1, and x2,y2 are center of ball2
double distance = Math.sqrt(dx*dx+dy*dy);
// Unit vector in the direction of the collision
double ax=dx/distance, ay=dy/distance;
// Projection of the velocities in these axes
double va1=(vx1*ax+vy1*ay), vb1=(-vx1*ay+vy1*ax);
double va2=(vx2*ax+vy2*ay), vb2=(-vx2*ay+vy2*ax);
// New velocities in these axes (after collision): ed<=1,  for elastic collision ed=1
double vaP1=va1 + (1+ed)*(va2-va1)/(1+mass1/mass2);
double vaP2=va2 + (1+ed)*(va1-va2)/(1+mass2/mass1);
// Undo the projections
vx1=vaP1*ax-vb1*ay;  vy1=vaP1*ay+vb1*ax;// new vx,vy for ball 1 after collision
vx2=vaP2*ax-vb2*ay;  vy2=vaP2*ay+vb2*ax;// new vx,vy for ball 2 after collision

Quote
(ax,ay)  is the unit vector in the direction connected two objects.
draw a line connect two object, assign the angle between this line and x-axis is ?,
then  cos?=ax, sin ?=ay
So vap1*ax=vap1*cos? give x-component of vap1, and vap1*ay give y-component of vap1

Because vb1 and vb2 is in the direction perpendicular to the (ax,ay)
,then the angle between vb1 and x-axis is ?=?+?/2
so cos ?=cos(?+?/2)=-sin?=-ay, sin?=sin(?+?/2)=cos?=ax
to calculate x-component of vb1 we need to calculate vb1*cos ?=vb1*(-ay)=-vb1*ay
,and (y-component of vb1) = vb1*sin?=vb1*ax

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.

If you did not correct the time dt (before you have found collision occurred, you will find balls clutch together. and they don't even let go.

#### rsnd

• Newbie
• Posts: 1
##### topic4
« Reply #8 on: August 13, 2005, 11:33:14 am »
sorry for bothering Its probably a 3 year old thread!!!

Just wondering...what does vx0 represent in your code?

Thanks
Rsnd

#### Thomas

• Newbie
• Posts: 2
##### 2D and 3D Elastic Collision Solutions
« Reply #9 on: September 17, 2005, 11:27:54 pm »
Anybody looking into the physics of 2D and 3D- elastic collisions may also be interested in my pages http://www.plasmaphysics.org.uk/collision2d.htm and http://www.plasmaphysics.org.uk/collision3d.htm which represent direct and general solutions of the problems. I have also written corresponding Fortran and C++ codes for this ( see http://www.plasmaphysics.org.uk/programs/coll2d_for.htm , http://www.plasmaphysics.org.uk/programs/coll2d_cpp.htm , http://www.plasmaphysics.org.uk/programs/coll3d_for.htm , http://www.plasmaphysics.org.uk/programs/coll3d_cpp.htm ) which are free to use for everybody.

**************
Note that there was a bug in the collision detection routine of the 2D-versions of my programs (this led in certain cases to collisions being detected when in fact there were none). I have corrected this now. I have also made minor changes to the code in order to improve speed. Furthermore I have added a simplified version to the 2D-routines that does not not contain the collision detection routine but just returns the new velocities assuming that the input coordinates are already those of the collision.

Thomas, 11.Dec.05
*******************

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### topic4
« Reply #10 on: September 21, 2005, 08:30:44 am »
Is there a vx0 posted in this forum messages! Please explain what do you mean?

#### Janus20

• Professional Truth Mechanic
• Newbie
• Posts: 5
• Doing things correctly
##### DYNAMICS APPLET
« Reply #11 on: September 29, 2005, 10:56:41 am »
You have missed the spin component caused by the collision.

It may have been there before collision occured.

The spin neglect is an ENORMOUS ERROR. The information is dangerous due to its over simplification.

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### topic4
« Reply #12 on: October 18, 2005, 10:17:59 am »
Yes, the ball will also rotate if there are friction force between balls when collide.
This is just an applet try to help student to understand basic physics (how to transform a 2D problem into two 1D problem).

#### sujitkane

• Newbie
• Posts: 1
##### 2-D collision for circular discs
« Reply #13 on: October 19, 2005, 07:01:46 pm »
Hello Professor Hwang

I have been following these pages since its publication in january 2004, just happened to register today. I wonder how different will it be if we consider that the two colliding bodies are two circular discs. You might have heard of a board game called carrom , its very popular in India. Do take a look at it. Now we have tried to emulate the example you have put up here and applied it to the emulation of carrom. There are a few rough edges, but still the difference is not very clear. What changes will be necessary to make it work realistically?

Regards
Sujit Kane

#### Fu-Kwun Hwang

• Administrator
• Hero Member
• Posts: 3062
##### topic4
« Reply #14 on: October 20, 2005, 09:06:24 am »
I do not know anything about carrom. Is there a URL for that?

To add a rotation effect due to friction between two disks, the mathematics become more comples.

The friction between two disks is proportional to the normal force when two disks collide.
F(friction) = mu * F(normal)

The normal force is the rate of change of momentum (in the direction between two centers) when two disks collide.

The friction force might change the tangential component of the velocity if the friction force is not large enough ( sliding occurs)

The torque due to the friction will change the angular momentum for those two disks.

In 2D collision: vx,vy and w (angular momentum) are changed due to collision for each ball.
There are 6 variables need to be update for two balls, so you need to solve 6 equations.