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

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: 2D Collision
« Reply #165 on: October 10, 2010, 05:26:43 pm »
You should have noticed that the velocity of those two balls are not small when the problem occurs.
Your time step is 1, however, dt=-17.4 so it is the problem. The time step 1 is too large.

I know that you are under a fix time step. 30FPS or whatever time step by your program.

You are doing one step of particle movement for each frame.
Here is my suggestion for your code
function actionLoop(e:Event):void {

calculateBallObjectNewPositions()
checkForCollisions()
updateBallPositions()

}

Your time step is 1 for the above 3 functions. Please change it to smaller value. e.g. change it to 0.1

You can change code like
ballObject.tempX = ballObject.tempX + ballObject.horizontalVelocity;
ballObject.tempY = ballObject.tempY + ballObject.verticalVelocity;
to
ballObject.tempX = ballObject.tempX + ballObject.horizontalVelocity*deltat;
ballObject.tempY = ballObject.tempY + ballObject.verticalVelocity*deltat;
and define deltat=0.1 (Because we change to smaller time step 0.1 so we need to calculate it 10 times)

then, change actionLoop
function actionLoop(e:Event):void {
for (var i:Number = 0; i < 10; i++) {
calculateBallObjectNewPositions()
checkForCollisions()
updateBallPositions()
}
}

Now, you are calculating it with 300FPS, even the program only update the view with 30FPS.

You can change 'deltat' to other values and change the number of outer loop (1/deltat) accordingly.
It is also possible to add code to adjust deltat according to velocity distribution (in some case, this might make it more efficient).

May be it only take you 1 hour to write the code, but you might need to spend 10 hours to debug the code (if you did not understand what your were doing or if you make a mistake without noticed).

#### mediakitchen

• Newbie
• Posts: 9
##### Re: 2D Collision
« Reply #166 on: October 10, 2010, 05:54:57 pm »
Thank you again. I have made the amends you suggested and it appears to work now.

http://www.mediakitchen.co.uk/physics/testFile5v2ballsDelta.html

Thank you for your patience.

Paul

#### lunayo

• Newbie
• Posts: 6
##### Re: 2D Collision
« Reply #167 on: November 07, 2010, 08:52:47 am »
Sir, how to calculate the momentum of ball hitting the wall? (Wall with various angle and various shape)

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: 2D Collision
« Reply #168 on: November 09, 2010, 02:46:24 pm »
You can use the same equation as above, set m2>>m1

The result will be
The component parallel to the wall will be the same
but the normal component of the velocity will change direction (with the same magnitude).

#### thepreditor10

• Newbie
• Posts: 2
##### Re: 2D Collision
« Reply #169 on: February 01, 2012, 09:34:26 pm »
i am finding this all a little confusing is there a simple way to explain it in layman's terms

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: 2D Collision
« Reply #170 on: February 01, 2012, 11:48:34 pm »
Please write down which part (where) you feel confusing!

#### thepreditor10

• Newbie
• Posts: 2
##### Re: 2D Collision
« Reply #171 on: February 02, 2012, 09:35:55 pm »
i find physics confusing in general i am trying to create a simulation not far off the same as shown here for my university project i have the ball moving using angle and velocity by doing

double scale_x=cos(angle);
double scale_y=sin(angle);
double v_x = (speed * scale_x);
double v_y = (speed * scale_y);

now i am looking to get the ball to bounce off the sides of the JPannel on collision also collision detection with two balls.
however i find it hard to understand how we are getting the balls to collide here and collision with the JPannel.

#### nsuhaili6

• Newbie
• Posts: 1
##### Re: 2D Collision
« Reply #172 on: May 07, 2012, 08:31:52 pm »
Hi prof,
your coding is for a circle 2-d collision right?
how am i going to simulate for irregular shape of object?
i want to simulate for water molecules model of this shape but in 2d..
-*-

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: 2D Collision
« Reply #173 on: May 08, 2012, 04:53:33 pm »
Do you mean three circles (as shown in the picture attached) as one object.

Then, you need to check collision between any two circles. And you need to re-derive the equation (with conservation of momentum and conservation of angular momentum and conservation of energy-- five equations for 2D case). It will be much more complicated than the above simulation.

#### Some1

• Newbie
• Posts: 2
##### Re: 2D Collision
« Reply #174 on: January 08, 2013, 07:45:36 am »
Hello!

I have utilised your algorithm in a personal project and now I need to write a documentation for it. One problem I am having is explaining to someone what the actual mass of the ball is measured in... I noticed you give the default value of m1=r1*r1; So the square of the ray of the circle but technically it can be anything.

My question is how much of m1 is a gram.
So:
1 gram = m1
m1 = ?

Thank you in hopes that you can answer my question and once again. I really appreciate your work. It has helped immensely.

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: 2D Collision
« Reply #175 on: January 08, 2013, 04:07:06 pm »
The mass used in the simulation can be 1g or 1kg or any other unit. It does not matter, the result will be the same (as long as all the particles used the same unit).

Because This is a two dimensional simulation, the mass is proportional to area (mass=density*area).
And the area of a circle is \$A=pi r^2\$, that is why the mass is proportional to r*r in a 2D simulation.
(mass will be proportional to \$r^3\$ for a 3-D simulation.).

#### bark46k

• Newbie
• Posts: 1
##### Re: 2D Collision
« Reply #176 on: September 26, 2013, 01:38:02 pm »
great stuff!

#### kacpero12345

• Newbie
• Posts: 1
##### Re: 2D Collision
« Reply #177 on: January 05, 2019, 12:22:20 am »
i have problem, if i set different masses balls clutch together
« Last Edit: January 05, 2019, 12:25:49 am by kacpero12345 »