NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
December 14, 2018, 03:16:56 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Youe can not help men permanently by doing for them what they could and should do for themselves. ..."Abraham Lincoln(1809-1865, US President 1861-1865"
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: 1 [2] 3   Go Down
  Print  
Author Topic: Collision 2D  (Read 148643 times)
0 Members and 2 Guests are viewing this topic. Click to toggle author information(expand message area).
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #30 on: January 13, 2009, 04:59:37 pm » posted from:Taipei,T'ai-pei,Taiwan

Applet shown at http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4
was created with JDK1.0.2. I wrote java code line by line.
So I can provide the java code in the format you want.
But this applet was created with EJS. I already provide the EJS source.
You can generate java source when you load it into EJS (if you really need it).
 But you also need to compile it with EJS libraries if you did not use EJS to generate the jar.
You should be able to do what you want with the provided code.

If you have problem with the physics or even java code. Write down your problem in detail, I will try to help.

Logged
ArdTraveller
Newbie
*
Offline Offline

Posts: 12


«
Embed this message
Reply #31 on: January 28, 2009, 09:33:31 pm »

sir for the earlier 2D Collision (the black and blue ball simulation) can u show me the code or help me, wat is the code to enable to change the mass of the balls(by putting slider-i know that we hv to import the JSlider right?), because the code puts them in a constant ratio of one, and can't be changed, thx Smiley.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #32 on: January 28, 2009, 10:32:57 pm »

Are you talking about the 2D Collision?

You will get the source code and you can modify whatever you want in the code.

By the way, the mass for those two objects can be changed with mouse click and drag.
Right click near the boundary of the circle to change it's size (and the mass at the same time). That is why the mass ratio textfield was changed to display mode only.
Logged
ArdTraveller
Newbie
*
Offline Offline

Posts: 12


«
Embed this message
Reply #33 on: February 06, 2009, 01:58:02 pm »

ok..thx sir, sir I'v been searching for articles that are about this collision thing, articles that could help me on my research about collisions of two objects, that has the proper explanation of the newtonian calcilatrions and collision detection...could u give me a link to the articles that are relevant to collision of two objects or collision simulation if u know where to get them..thx
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #34 on: February 09, 2009, 02:56:33 pm »

Hi Professor,

Sorry for my complete ignorance, I am newbie to this forum.

It seems no where I can find:
"There are VIEW or DOWNLOAD ejs source (xml) links just under the simulation.
Please view or download it by yourself.
You can also "load ejs as signed applet" to view the code in EJS and recompile it or generate java source code from it."

I am logged in, and able to download the .zip, but the .zip only contains .jar/.class file, neither am I able to find the (xml) links you mentioned to read the source code, could you please help me?
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #35 on: February 09, 2009, 03:40:18 pm »

Applet shown at http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4 was created with JDK1.0.2
There is no ejs source for that applet but you should be able to find the .java file if you unzip the downloaded file.
You can also download the .java from the attachement file under the above message.

Please check out Collision 2D if you want the EJS source code for collision 2d.
Unzip the downloaded jar file and you will find the xml source under users/ntnu/fkh
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #36 on: February 09, 2009, 03:54:51 pm »

Hi Professor Hwang, thanks for your quick reply, again applogize for my ignorance.

I am looking for the source of this current thread - Collision 2D. The problem is it seems I did not find the EJS source code. Attached is a screenshot for this thread, as you can see, I do not see a 'VIEW or DOWNLOAD ejs source (xml) links just under the simulation' link, neither did I find "load ejs as signed applet" to view the code in EJS.

I did download ejs_collision2D_e.jar(turns out to be a file called collision2D_e.zip), However in this .zip there is only two folder: _library -> ejsBasic.jar and collision2D_3->collision2D_e.jar and collision2D_html, there is no .xml.

I appreciate if you can help, I like to see a generated .java sounce for this example.


* Collision 2D.png (50.42 KB, 1920x1077 - viewed 447 times.)
« Last Edit: February 09, 2009, 03:56:31 pm by chaocai » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #37 on: February 09, 2009, 05:50:47 pm »

Sorry! Because we switched all the EJS simulations from version 3 to version 4 two weeks ago.
Now, all the simulations were distributed with .jar files.

However, due to some mistakes , the ejs_collision2D_e.jar was not available.
It is fixed now. You should be able to receive .jar instead of .zip now.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #38 on: February 09, 2009, 10:55:39 pm »

You can find the  EJS version XML source as attachement.
Load this file into EJS and you will be able to how the code was implemented.
If you really want to look at the .java files being generated, please check output/users/ntnu/fkh directory.
However, it will be easier to load the xml into EJS and find out how it was implemented.


*** There are 1 more attached files. You need to login to acces it!
Logged
ArdTraveller
Newbie
*
Offline Offline

Posts: 12


«
Embed this message
Reply #39 on: February 10, 2009, 11:33:03 am » posted from:Kuala Lumpur,Kuala Lumpur,Malaysia

Excuse me sir but with due respect you hvn't helped me to guide to possible articles related to collision of two objbects that would be related to ur collision 2D simulation, could you please help me sir, thx
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #40 on: February 10, 2009, 11:56:05 am » posted from:Kelowna,British Columbia,Canada

Thanks for the fixing. Yes, it is in .jar now that I can play as offline.

Thanks for supplying with the .xml, however when I tried to load and play in ejs 4.1, following error occur:
File successfully read Collision2D_e.xml
File saved successfully users/ntnu/fkh/Collision2D_e.xml
Generating simulation file Collision2D_e...
C:\My Documents\Downloads\EJS_4.1\workspace\source\users\ntnu\fkh\Collision2D_e.java:25: missing return value
  public int _getStepsPerDisplay() { return ; }
  ^
1 error
Compilation produced an error!

Could you let me know anything is wrong with my setting or the .xml? Again, I really look for the generated .java file to learn all the coding.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #41 on: February 10, 2009, 02:07:35 pm »

It is due to version change in EJS 4. Goto Model/Evolution Page and you wil find SPD at the left side.
Enter 1 into the field, save the file and compile it again. It should work fine.

This is due to a new parameter in version 4: StepsPerDisplay , no default value was assigned.

There are more than 400 simulations created with EJS 3.46.
I recompiled all the files to switched to version 4 several weeks ago with some code, but I did not have time to change the source one by one.
I will ask the Paco (author of ejs) to modify code to change it automatically.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #42 on: February 10, 2009, 02:30:36 pm »

Quote
Excuse me sir but with due respect you hvn't helped me to guide to possible articles related to collision of two objbects that would be related to ur collision 2D simulation, could you please help me sir, thx

May be I did not write it clearly or you did not read the previous messages carefully.

The 2D collision problem was transformed to 1D collision problem -- in the direction (red arrows) between center of mass for two collided circles when two circles just touch each other.

If there is no friction between two circles, the velocity components vertical red arrows will not change at all.

And all the calculation can be found in the previous messages e.g. http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.msg2935#msg2935

Collision in 1D is a standard textbook problem :Conservation of momentum and conservation of energy.

You should be able to find articles with google or any other search engine.
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #43 on: February 10, 2009, 03:21:41 pm » posted from:Kelowna,British Columbia,Canada

Hi Professor Hwang, thanks very much for the help on getting and setting up the collision2D to work with EJS, it now works with your instruction.

And I can have some real questions now. I am following your logic to create a simple game, attached is the .swf you can take a look. The goal is basically to bump the other player's ball out of the board, very preliminary stage at this moment. My PROBLEM is similar to someone that maybe 10% of the time there are some error with the collision, either it is not by physical law, i.e. a ball pops up suddenly in another possision, or they kind touch/collide but don't do anything. I believe follow all the code:
Code:
private function checkCollision(ball1:Chess, ball2:Chess):Void
{
var dx = ball2.px - ball1.px;
var dy = ball2.py - ball1.py;
var d = Math.sqrt(dx * dx + dy * dy);

//collsion happens
if(d <= ball1.radius + ball2.radius)
{
// calculate the component of velocity in the direction of (dx,dy)
var vp1 = ball1.vx * dx / d + ball1.vy * dy / d;
var vp2 = ball2.vx * dx / d + ball2.vy * dy / d;

// the collision should have occurred at t-d, move two ball backward
var dt = (ball1.radius + ball2.radius - d) / (vp1 - vp2);
ball1.px -= ball1.vx * dt;
ball1.py -= ball1.vy * dt;
ball2.px -= ball2.vx * dt;
ball2.py -= ball2.vy * dt;

// unit vector in the direction of the collision
var ax = dx / d;
var ay = dy / d;

// projection of the velocities in these axes
var va1 = ball1.vx * ax + ball1.vy * ay;
var vb1 = -ball1.vx * ay + ball1.vy * ax;
var va2 = ball2.vx * ax + ball2.vy * ay;
var vb2 = -ball2.vx * ay + ball2.vy * ax;

// new velocities in these axes (after collision): ed<=1,  for elastic collision ed=1
var ed = 1;
var vaP1 = va1 + (1 + ed) * (va2 - va1) / (1 + ball1.mass / ball2.mass);
var vaP2 = va2 + (1 + ed) * (va1 - va2) / (1 + ball2.mass / ball1.mass);

// undo the projections
ball1.vx = vaP1 * ax - vb1 * ay;
ball1.vy = vaP1 * ay + vb1 * ax;
ball2.vx = vaP2 * ax - vb2 * ay;
ball2.vy = vaP2 * ay + vb2 * ax;

// because we have move time backward dt, we need to move time forward dt
ball1.px += ball1.vx * dt;
ball1.py += ball1.vy * dt;
ball2.px += ball2.vx * dt;
ball2.py += ball2.vy * dt;

ball1.vx *= 0.65; //cc - friction/energy damping
ball1.vy *= 0.65;
ball2.vx *= 0.75;
ball2.vy *= 0.75;
}
}
The only difference is at the end(//cc - friction/energy damping), I added some friction/enery damping to simulate an imperfect kinetics. When you play, you will see the ball deccelerates quickly.


And here is the loop of checking multiple balls:
Code:
for(var i = 0; i < _balls.length; i++)
{
checkBoundary(_balls[i]);
var ballA:Baa= _balls[i];
for (var j:Number = 0; j < _chesses.length; j++)
{
if (i == j) continue;
var chessB:Chess = _chesses[j];
checkCollisionNew(chessA, chessB);
}
}

Would you be able to give a try and help me on identify what may went wrong?



* bump.swf (18.57 KB, 760x480 - viewed 1033 times.)
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #44 on: February 10, 2009, 04:14:51 pm »

for(var i = 0; i < _balls.length-1; i++)//change from _balls.length to _balls.length-1
      {
         checkBoundary(_balls);
         var ballA:Baa= _balls;
         for (var j:Number = i+1; j < _chesses.length; j++)//change from 0 to i+1
         {
// remove this line ;         if (i == j) continue;
            var chessB:Chess = _chesses[j];
            checkCollisionNew(chessA, chessB);
         }
      }

You do not need to take care of the collision twice!
Collision betweb ball 1 and ball 2 is the same collision between ball2 and ball1. Grin

And you will have to make sure that v*dt<< size of the ball in your code. Roll Eyes
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1787


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #45 on: February 10, 2009, 04:27:31 pm » posted from:Singapore,,Singapore

interesting
« Last Edit: February 10, 2009, 04:29:37 pm by lookang » Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #46 on: February 11, 2009, 01:00:40 am » posted from:Kelowna,British Columbia,Canada

Hi Professor, thanks for the suggestion again.

for(var i = 0; i < _balls.length; i++)//change from _balls.length to _balls.length-1
My code here is correct, since _balls array starts from index 0, so it checks from ball0 to ball[length - 1]. In your case, it would be correct if I make i <= _balls.length - 1. Roll Eyes 

for (var j:Number = i+1; j < _chesses.length; j++)//change from 0 to i+1
You are right about this. In fact, my program does look for outerloop i from 1st to the last ball, and inner loop j from the i ball to the last ball before exactly as your code. But I pasted this code in and wanted to do a double check. I don't think it would cause any program error except for the inefficiency.

About v*dt<< size of the ball , I am pretty sure but I will double check and get back on this.

So, all set aside, do you see any logic errors in my code, and have you had a chance to try my program? Again, I experience some really weird inconsistant issue, random popping and so on. Can you please try give some play? especially try to put balls close and do a hard bump, there may 10 - 25% chances error will happen and I am very confused.

It is here again, much appreciated:)
« Last Edit: February 11, 2009, 12:40:51 pm by chaocai » Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #47 on: February 11, 2009, 08:50:44 am »

The following code was copied from my original post at http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4
You did not following my original code -> you forgot to do the bold face parts !
------------------------------------------------------------------------
...
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:

Shocked Shocked Shocked
double dx = x2-x1, dy = y2-y1; // recalculated the direction of collision
// where x1,y1 are center of ball1, and x2,y2 are center of ball2
double distance = Math.sqrt(dx*dx+dy*dy); //calculate the distance again
Shocked Shocked Shocked
// Unit vector in the direction of the collision
double ax=dx/distance, ay=dy/distance;  // the result will be different if you did not do the above steps
...
------------------------------------------------------------------------
 Huh
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #48 on: February 11, 2009, 12:39:57 pm » posted from:Kelowna,British Columbia,Canada

Hi Professor, You are totally right, I missed this part I thought it was the same. So I added back, my code now looks like this:
Code:
private function checkCircleCollision(ball1:Chess, ball2:Chess, elapsed:Number):Void
{
var dx = ball2.px - ball1.px;
var dy = ball2.py - ball1.py;
var d = Math.sqrt(dx * dx + dy * dy);

//collsion happens
if(d <= ball1.radius + ball2.radius)
{
// calculate the component of velocity in the direction of (dx,dy)
var vp1 = ball1.vx * dx / d + ball1.vy * dy / d;
var vp2 = ball2.vx * dx / d + ball2.vy * dy / d;

// the collision should have occurred at t-d, move two ball backward
var dt = (ball1.radius + ball2.radius - d) / (vp1 - vp2);

ball1.px -= ball1.vx * dt;
ball1.py -= ball1.vy * dt;
ball2.px -= ball2.vx * dt;
ball2.py -= ball2.vy * dt;

// recalculat the direction of collision
dx = ball2.px - ball1.px;
dy = ball2.py - ball1.py;

// where x1,y1 are center of ball1, and x2,y2 are center of ball2, calculate the distance again
d = Math.sqrt(dx * dx + dy * dy);

// unit vector in the direction of the collision
var ax = dx / d;
var ay = dy / d;

// projection of the velocities in these axes
var va1 = ball1.vx * ax + ball1.vy * ay;
var vb1 = -ball1.vx * ay + ball1.vy * ax;
var va2 = ball2.vx * ax + ball2.vy * ay;
var vb2 = -ball2.vx * ay + ball2.vy * ax;

// new velocities in these axes (after collision): ed<=1,  for elastic collision ed=1
var ed = 1;
var vaP1 = va1 + (1 + ed) * (va2 - va1) / (1 + ball1.mass / ball2.mass);
var vaP2 = va2 + (1 + ed) * (va1 - va2) / (1 + ball2.mass / ball1.mass);

// undo the projections
ball1.vx = vaP1 * ax - vb1 * ay;
ball1.vy = vaP1 * ay + vb1 * ax;
ball2.vx = vaP2 * ax - vb2 * ay;
ball2.vy = vaP2 * ay + vb2 * ax;

// because we have move time backward dt, we need to move time forward dt
ball1.px += ball1.vx * dt;
ball1.py += ball1.vy * dt;
ball2.px += ball2.vx * dt;
ball2.py += ball2.vy * dt;

ball1.vx *= 0.65; //cc - friction/energy damping
ball1.vy *= 0.65;
ball2.vx *= 0.75;
ball2.vy *= 0.75;
}
}
:'(But now I face a really wired problem, its hard for me to explain and I try best, basically when two circles are very close and then try hit. The recalculation will position the two circle in an opposite way. Please try the new .swf below by taking this step:
- Drag the smallest middle left blue circle, aim it to the smallest top right red circle, it is about 45 degree/11 o'clock direction. Doing this will land the two red and blue circle just by each other.
- hit red to blue or blue to red hardest. You will see a WEIRD reverse pop. it almost seems like the fix reposition the two ball reversely.

I am sure the code this time should be correct, do you see what may cause this problem? (The problem only happens when two small circle are really close to each other.)
-*-


* bump.swf (18.5 KB, 760x480 - viewed 1050 times.)
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #49 on: February 11, 2009, 01:48:23 pm »

I tried but I was not able to re-produce what you want me to try. Why do not you change the code (at least as I suggested) to make it dasier to debug. It might be a bug somewhere in your code.

I used similar code at Browian motionparticle model for solid/liquid/gas states in 3D and several other simuations-- those simulations looks fine.

for the condition to check for collision:
Code:
if(d <= ball1.radius + ball2.radius)
You can add another condition to check if two velocity vectors are in the opposition direction(i.e. inner product is negative instead of positive!).

I am sorry that I would not be able to help again if you do not make it easier for me.
I do not have time to look at your code line by line again. You need to fix your bug by yourself. Sorry!
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #50 on: February 11, 2009, 02:04:28 pm » posted from:Kelowna,British Columbia,Canada

Yes, I understand, Professor. You have helped me a lot already, really appreciated. I will try my best to figure out the problem, its just quite frustrating.

Two quick small questions:
- You talked about inner product to make sure they are negative. In my case, it is actually always a ball to hit another still(not moving) ball, so dot product will be 0, assume checking the negative will not help in my case?
- I also find seems when problem occurs, here:
// the collision should have occurred at t-d, move two ball backward
var dt = (ball1.radius + ball2.radius - d) / (vp1 - vp2);

I will get Negative dt. Is this could ever be possible? I don't think dt can be negative...

Thanks alot

Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #51 on: February 11, 2009, 02:24:16 pm »

Because the code already make sure d <= ball1.radius + ball2.radius.

We need to move time back to the moment when two balls really collide, so dt should be >=0.
Code:
var dt = (ball1.radius + ball2.radius - d) / (vp1 - vp2);
dt should be positive or zero, unless you have the wrong sing when calculate vp1 - vp2.

It means that vp1-vp2 should be positive,too. You can add  Math.abs(vp1-vp2) to make sure it is positive.

About checking the inner product between two velocity vectors. It might be necessary after the first collisions, several balls are moving. Especially, when you have bug in the code so that balls collide with each other continuously.
Logged
chaocai
Newbie
*
Offline Offline

Posts: 8


«
Embed this message
Reply #52 on: February 12, 2009, 11:36:31 am »

Hi Professor, it's me again...lol...Lots of great suggestions from you, thanks so much.

Now, I really want to ask this question:
How to solve the "bullet shoots through paper" problem? I think it is quite a common situation and I believe some of my program errors are caused from speed too fast. Do you have a solution for such a problem, like a speedy ball hit and gets bounced from a thin paddle.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #53 on: February 12, 2009, 11:50:44 am »

You need to describe your problem more clearly. I am not fully understand your question!

You can check out Light reflected from arbitrary number of mirrors
if the so called thin paddle is fixed,and you can find the contact point and the slope of the contact point.

If the problem is coming from "velocity is too large" , you need to decrease the delta time dt(make it smaller.
Or move your particle step forward into smaller steps and check for collision if the velocity is too large ( e.g. dt*v=constant).

For example: you original time differert is dt. and x1=x1+vx1*dt; ...
Add code similar to the following
Code:
Define steps=10; dt2=dt/steps; // steps=(int)velocity;
for(int i=0;i x1=x1+vx1*dt2; x2= ... // step forward for all x,y variables
 checkcollision(x1,y1,x2,y2,vx1,vx2,vy1,vy2);
}

Logged
Question
Newbie
*
Offline Offline

Posts: 10


«
Embed this message
Reply #54 on: February 14, 2009, 04:53:29 pm »

Hello sir, can I know what article would be nesaccery 4 me to read on that is related to the simulation u created and what techniques are actually involved in creating the collision simulation, 4 example what kind of algorithms do u use n can u tell me them in specific detail, because apperantly newtonian physx is used right,

thx again bye
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #55 on: February 14, 2009, 06:29:13 pm »

I would sugegst you read standard physics textbook about collision 1D if you really want to understand it in depth.

You need to understand conservation of momentum and conservation of energy.
The velocity of two objecs after collision (V1',V2')can be calculated from velocity before collisions (V1,V2) and mass of two objects (m1,m2).

V1'= (m1-m2)*V1/(m1+m2) + 2*m2*V2/(m1+m2)= (m1V1+m2V2)/(m1+m2)+(V2-V1)*m2/(m1+m2)=Vcm+(V2-V1)*m2/(m1+m2);

V2'=2*m1*V1/(m1+m2)+(m2-m1)*V2/(m1+m2)=(m1V1+m2V2)/(m1+m2)+(V1-V2)*m1/(m1+m2)=Vcm+(V1-V2)*m1/(m1+m2);

Where Vcm=(m1V1+m2V2)/(m1+m2);
Before the collision, the velocity of m1 relative to Vcm is
V1-Vcm=V1-(m1V1+m2V2)/(m1+m2)=m2(V1-V2)/(m1+m2)
compare it with the second term in V1'

For collision 2D, the trick is to transform it into collision 1D problem:
In the direction of red arows, and another direction normal to red arrows (i.e. direction of green arrows).
If friction is not considered (which will cause both balls to rotate due to friction force),
Then green arrow velocity is not changed after the collision because there is no force in that direction.
And collision in red arrow direction is 1D collision problem, so you can use the above equation.
However, you need to calculate the component of velocity in red arrow direction. (This you need to understand the meaning of inner product -- mathematic operation).


Logged
FractalKiller
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #56 on: March 16, 2009, 02:56:11 am »

Hello!
It a good program and thank you for the source, but  when i want to build in my game it doesn't work well. There are 3 object with vspeed;hspeed;m;r variables. They change their positions base on the vspeed, and hspeed. There is a Control object which is check every object. There is the code for the control object. I make mistake in the code or there is something else mistake? (It's not a n language, it is a game maker program inside language, so maybe the mistake int the program.) The code is works, because if i change at the end the "obj2.vspeed=obj2.p*sc-obj2.n*cs;" to "obj.vspeed=obj2.p*sc-obj2.n*cs;" the balls bounce for each other but to wrong angle. So the detection is fine, but if i use the correct line (obj2.vspeed=obj2.p*sc-obj2.n*cs;) the ball go arcross each other, without any bounce. So that's my problem.
The Code:

  //Check every object about the wall bouncing

  db = instance_number(object1);
  for( i = 0; i< db; i+=1)
  {

  obj= instance_find(object1,i);

  maxT=maxx-obj.r;
  minT=minx+obj.r;
  if(obj.x>maxT){
  obj.x=2*maxT-obj.x;
  obj.hspeed=-obj.hspeed; }
  else if(obj.x<minT){
  obj.x=2*minT-obj.x;
  obj.hspeed=-obj.hspeed;}
  maxT=maxy-obj.r/2;
  minT=miny+obj.r/2;
  if(obj.y>maxT){
  obj.y=2*maxT-obj.y;
  obj.vspeed=-obj.vspeed; }
  else if(obj.y<minT){
  obj.y=2*minT-obj.y;
  obj.vspeed=-obj.vspeed;}


  obj.c = obj.c-1;
  //Collision with other object
  db2 = instance_number(object1);
  for( j = 0; j< db2; j+=1)
  {

  obj2= instance_find(object1,j);
  if (obj2!= obj)
  {
  r12 = sqrt((obj.x-obj2.x)*(obj.x-obj2.x)+(obj.y-obj2.y)*(obj.y-obj2.y));
  dmin = obj.r+obj2.r;
  if (r12<dmin){

  //if (obj.c<1) {
  cs=(obj2.x-obj.x)/r12; sc=(obj2.y-obj.y)/r12;
  obj.p=obj.hspeed*cs+obj.vspeed*sc;
  obj2.p=obj2.hspeed*cs+obj2.vspeed*sc;
  // back to collision time
  ddt=(dmin-r12)/(obj.p-obj2.p);
  if(ddt<dt) ddt=0;
  obj.x-=vspeed*ddt;
  obj.y-=hspeed*ddt;
  obj2.x-=vspeed*ddt;
  obj2.y-=hspeed*ddt;
  r12=sqrt((obj.x-obj2.x)*(obj.x-obj2.x)+(obj.y-obj2.y)*(obj.y-obj2.y));
  //calculate component of velocity
  cs=(obj2.x-obj.x)/r12; sc=(obj2.y-obj.y)/r12;
  obj.p=obj.hspeed*cs+obj.vspeed*sc;
  obj2.p=obj2.hspeed*cs+obj2.vspeed*sc;
  // normal components do not change
  obj.n=obj.hspeed*sc-obj.vspeed*cs;
  obj2.n=obj2.hspeed*sc-obj2.vspeed*cs;
  // }
  //if(obj.c<2){
  momentum=obj.m*obj.p+obj2.m*obj2.p;
  mcm = obj.m + obj2.m;
  Vrel=obj2.p-obj.p;
  obj.p=(momentum+eta*obj2.m*Vrel)/mcm;
  obj2.p=(momentum-eta*obj.m*Vrel)/mcm;
  ///px1=vp1*cs; py1=vp1*sc;
  ///px2=vp2*cs; py2=vp2*sc;
  obj.hspeed=obj.p*cs+obj.n*sc;
  obj2.hspeed=obj2.p*cs+obj2.n*sc;
  obj.vspeed=obj.p*sc-obj.n*cs;
  obj2.vspeed=obj2.p*sc-obj2.n*cs;
  //} else {obj.c = 0;}
  }  }  }
  }


FractaKiller

(Sorry for my English);
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #57 on: March 16, 2009, 08:12:37 am » posted from:Taipei,T\'ai-pei,Taiwan

1. I think  db = instance_number(object1); and db2 = instance_number(object1); should get the same value.
2. You can change the second loop for( j = 0; j< db2; j+=1)
to for( j = i; j< db2; j+=1)
3. It seems that you might have done the collision twice. I do not understand what is the meaning of obj.c
But,
Quote
  //if (obj.c<1) {
  cs=(obj2.x-obj.x)/r12; sc=(obj2.y-obj.y)/r12;
  obj.p=obj.hspeed*cs+obj.vspeed*sc;
  obj2.p=obj2.hspeed*cs+obj2.vspeed*sc;
  // back to collision time
  ddt=(dmin-r12)/(obj.p-obj2.p);
  if(ddt<dt) ddt=0;
  obj.x-=vspeed*ddt;
  obj.y-=hspeed*ddt;
  obj2.x-=vspeed*ddt;
  obj2.y-=hspeed*ddt;
  r12=sqrt((obj.x-obj2.x)*(obj.x-obj2.x)+(obj.y-obj2.y)*(obj.y-obj2.y));
  //calculate component of velocity
  cs=(obj2.x-obj.x)/r12; sc=(obj2.y-obj.y)/r12;
  obj.p=obj.hspeed*cs+obj.vspeed*sc;
  obj2.p=obj2.hspeed*cs+obj2.vspeed*sc;
  // normal components do not change
  obj.n=obj.hspeed*sc-obj.vspeed*cs;
  obj2.n=obj2.hspeed*sc-obj2.vspeed*cs;
  // }
I do not know why you add  if(ddt<dt) ddt=0;
You might want to add code to check collision only when two balls are approach each other, instead of leaving each other.

and
 
Quote
  //if(obj.c<2){
  momentum=obj.m*obj.p+obj2.m*obj2.p;
  mcm = obj.m + obj2.m;
  Vrel=obj2.p-obj.p;
  obj.p=(momentum+eta*obj2.m*Vrel)/mcm;
  obj2.p=(momentum-eta*obj.m*Vrel)/mcm;
  ///px1=vp1*cs; py1=vp1*sc;
  ///px2=vp2*cs; py2=vp2*sc;
  obj.hspeed=obj.p*cs+obj.n*sc;
  obj2.hspeed=obj2.p*cs+obj2.n*sc;
  obj.vspeed=obj.p*sc-obj.n*cs;
  obj2.vspeed=obj2.p*sc-obj2.n*cs;
  //}

The above two code are similar code for collision. The first one assume elastic collision.
The second is the code for possible in-elastic collision. If eta==1, The second code is the same as the first one.

If you let the code did the collision twice. Then, it will look like there is no collision happened.
Logged
notausedname
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #58 on: May 20, 2009, 12:48:13 am »

No questions here, but I would just like to thank you for these applets (this and the other, simpler billiards-themed ones). They've helped me immensely in implementing collision in my own project. It's nice to see people posting such high quality info to the public without expecting anything in return.
Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1787


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #59 on: May 20, 2009, 11:48:17 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

i can testify to being able to learn and implement my own projects as well. http://www.phy.ntnu.edu.tw/ntnujava/index.php?board=28.0

Fu-Kwun Hwang is a hero, a role model deserving of recognition for his contribution to mankind.

Is there a website where we can vote for this website? hmmmmm
Logged
Pages: 1 [2] 3   Go Up
  Print  
Youe can not help men permanently by doing for them what they could and should do for themselves. ..."Abraham Lincoln(1809-1865, US President 1861-1865"
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
2D Collision « 1 2 ... 5 6 »
Dynamics
Fu-Kwun Hwang 176 483800 Last post September 26, 2013, 12:38:02 pm
by bark46k
Elastic collision and in-elastic collision
Dynamics
Fu-Kwun Hwang 0 9961 Last post May 24, 2009, 04:10:14 pm
by Fu-Kwun Hwang
4 car collision going through two intersections
Request for physics Simulations
Kennyg10 1 7213 Last post August 06, 2009, 11:51:32 am
by Fu-Kwun Hwang
1 D collision carts Elastic and Inelastic Collision
dynamics
ahmedelshfie 6 13056 Last post April 27, 2010, 02:16:18 am
by ahmedelshfie
Collision 2D
dynamics
ahmedelshfie 1 5165 Last post September 24, 2010, 07:22:37 pm
by ahmedelshfie
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.145 seconds with 24 queries.since 2011/06/15