NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Dynamics => Topic started by: Fu-Kwun Hwang on November 19, 2004, 09:09:24 am



Title: Collision 2D
Post by: Fu-Kwun Hwang on November 19, 2004, 09:09:24 am
The following is a simulation for elastic collision which means that
1. the momentum is conserved: $m_1\vec{V_1}+m_2\vec{V_2}=m_1\vec{V'_1}+m_2 \vec{V'_2}$ where $m_1,m_2$ are mass for each object, $\vec{V_1},\vec{V_2}$ are velocity vectors before collision,$\vec{V'_1},\vec{V'_2}$ are velocity vectors after collision.
2. the total energy is conserved: $\frac{1}{2}m_1\vec{V_1}^2+\frac{1}{2}m_2\vec{V_2}^2=\frac{1}{2}m_1\vec{V'_1}^2+\frac{1}{2}m_2\vec{V'_2}^2$
For collision in 2D, there are four unknow, however, there are only three equations. Because the contact point is missing in the above equations.
To solve the above problem, we can break 2D collision problem into two 1D collision problem once we know the contact point (when collision happened).
We use x-y coordinate to describe the position of those two balls before and after the collision.
At the time when two balls just in contact with each other, we can choose a new coordinate system:
1. direction from center of mass of $m_1$ to center of mass of $m_2$: The interaction forces bwteen those two balls during collision is limited to this direction.  It become a 1D collision problem (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=5.0) in this direction.
2. direction which is perpendicular to the above direction: Assume there is no friction between those two balls, no rotation effect to be considered, then the velocity component for those two balls in this direction will not be changed when collision happened (Because the interaction force dose not have component in this direction).
 




 This simulation shows the physics of two ball collide in 2D (without any rotation motion).
    What can be changed:
  • The sliders on either side change the vertical position of each ball

  • mass(m) and initial horizontal velocity(vx) can be adjusted with slider bar

  • Use mouse click and drag to change the position (click near the center of the ball) or the velocity vector (click near the arrow)

  • check out the "paused when collide" checkbox to show more detail process during collision,
     Click mouse in the area to play it again



    Physics processes:
  1. when two ball meets, the program shows two components of the velocity for each ball (along the line connect two ball and perpendicular to it)

  2. Interaction (force) between balls is along the line connect two balls

  3. Velocitys perpendicular to connection line are not changed (No force/interaction in that direction)

  4. The rest of the problem is similar to 1D collision problem for ball with velocity along the line
  5.  
  6. Because this is a 2D simulation, so the radius is proportional to the square root of the mass to keep the same density (for those balls).



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'

If m1=m2  then V1'=V2, V2'-V1 ;
 it means that both particles just exchange velocity.
If m1>> m2 then V1' ≒ V1 ; V2'≒ 2V1+V2;
i.e.  If a heavy car hit you with velocity V1=50km/hr , you will fly out with velocity 2V1=100km/hr.
If m1<< m2 then V1' ≒ -V1+2V2 ; V2'≒ V2;
i.e.  If a small ball hit a wall (V2=0),then the ball will be bounced back (same magnitude,oppositive direction).

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).

(http://www.phy.ntnu.edu.tw/ntnujava/snapshotejs/4_smf_collision2D_20090118001718.gif)


Title: Re: Collision 2D
Post by: vijaywadnere on August 13, 2007, 04:34:03 pm
Hello Professor,

I am trying to develop a simple table/board game and was looking for some "good" physics to apply to the game - like -angle, velocity, friction, collision etc.

I got a fair idea from your other articles, but this one is close enough to what i was looking for.

Is it possible for you to share the source code for this one so that I can extract the required algoriths from it?

Thanks.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on August 13, 2007, 06:09:04 pm
-*-

Here is a modified version of the above simualtion:



Title: Re: Collision 2D
Post by: vijaywadnere on August 14, 2007, 11:56:08 am
Thank you  very much. :)

I'll try and see how I can get best out of it.  :)


Title: Re: Collision 2D
Post by: goldentrash on October 17, 2007, 03:12:11 am
Hello Profesor
I know it's a bit rude but..I have a little time to give my teacher working java application of 2d collision of 2 balls inside the square fence and I need the source code..please if you can,help me.I'm a novice in java..so that's why I have a problem.
thank you in advance!
daniel


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on October 17, 2007, 08:29:01 am
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.


Title: Re: Collision 2D
Post by: Chetmun on November 10, 2008, 07:39:34 am
Thank you for sharing your work on this 2D collision stuff. It was very clear and I was able to implement it quickly into my Java program. I have tested the technique with various parameters: 2 balls to multiple balls, different sizes, different initial trajectories, and different initial velocities. It worked well from the very beginning thanks to your clear example.

However, (didn't you know that was coming  ;)) I am having a bit of a problem. The balls seem to stick sometimes and even bounce off in odd directions. Sometimes the velocities increase at unexpected levels.

I put a test after the new locations and velocities have been calculated to see if the values were NaN's according to Java. In those cases I did not update the locations or velocities and the balls would overlap for a period of time.

I am assuming this is some kind of math precision issue. I have kept all variables at the level of Java's "double". Is there some kind of technique you employ to prevent this sort of behavior? Perhaps there's a flaw in my processing of all the balls. I "move" them all, then check each one against each other (only once, no redundancy) adjusting the locations and velocities according to your example.

Thanks in advance for any help.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on November 11, 2008, 08:55:27 pm
1. The situation will happen if you did not move the ball back to the time where two balls just collide. (If you just checked if those two ball collide with each other).
2. If you did the time correction. The situation you have descrbed might happened if the velocity was set too large or the time step is too large. And it also possible that you might have three balls collide at the same time (within your simulation time step).  However, the program only take care of two balls collision. Reduced the time step might help.

Because I do not know how it was implemented in your program and what are the initial condition for your case, I can not say any more.


Title: Re: Collision 2D
Post by: Chetmun on November 12, 2008, 01:18:15 am
Thanks for your reply. I am enclosing my code for your perusal. I think I have included everything I've seen from your posts. In other words, I believe I have "moved" the balls back in time properly. I see your point about the velocities possibly being too large. I will experiment with that. The initial directional velocities are randomly between 13 and 20 per time step. The radii are randomly between 15 and 50. Even though I have multiple balls moving simultaneously, the situation arises with 2-ball collisions and no other ball nearby. Perhaps you can spot something I missed.

Code:
private boolean collision(Circle c1, Circle c2)
{
double x1 = c1.getX(), y1 = c1.getY();
double x2 = c2.getX(), y2 = c2.getY();
double r1 = c1.getRadius(), r2 = c2.getRadius();

double dx = x2 - x1;
double dy = y2 - y1;
double d = Math.sqrt(dx*dx + dy*dy);

if(d <= r1 + r2)
{
double vx1 = c1.getXdir(), vy1 = c1.getXdir();
double vx2 = c2.getXdir(), vy2 = c2.getXdir();
double mass1 = c1.getMass(), mass2 = c2.getMass();

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

// collision should have happened dt before
double dt = (r1 + r2 - d) / (vp1 - vp2);

// move the circles backward in time
x1 -= vx1 * dt;
y1 -= vy1 * dt;
x2 -= vx2 * dt;
y2 -= vy2 * dt;

// new collision calculations at impact
dx = x2 - x1;
dy = y2 - y1;
d = Math.sqrt(dx*dx + dy*dy);

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

// projection of the velocities in these axes
double va1 =  vx1*ax + vy1*ay;
double vb1 = -vx1*ay + vy1*ax;
double va2 =  vx2*ax + vy2*ay;
double vb2 = -vx2*ay + vy2*ax;

// calculate new velocity after collision
double ed = 1;
double vaP1 = va1 + (1 + ed) * (va2 - va1) / (1 + mass1/mass2);
double vaP2 = va2 + (1 + ed) * (va1 - va2) / (1 + mass2/mass1);

// undo projections
vx1 = vaP1*ax - vb1*ay;
vy1 = vaP1*ay + vb1*ax;
vx2 = vaP2*ax - vb2*ay;
vy2 = vaP2*ay + vb2*ax;

// move time dt forward
x1 += vx1 * dt;
y1 += vy1 * dt;
x2 += vx2 * dt;
y2 += vy2 * dt;

// update locations and velocities
if (!Double.isNaN(x1)) c1.setX(x1);
if (!Double.isNaN(y1)) c1.setY(y1);
if (!Double.isNaN(x2)) c2.setX(x2);
if (!Double.isNaN(y2)) c2.setY(y2);
if (!Double.isNaN(vx1)) c1.setXdir(vx1);
if (!Double.isNaN(vy1)) c1.setYdir(vy1);
if (!Double.isNaN(vx2)) c2.setXdir(vx2);
if (!Double.isNaN(vy2)) c2.setYdir(vy2);

return true;
}
else
return false;
}


Title: Re: Collision 2D
Post by: Chetmun on November 12, 2008, 09:02:13 am
P.S. I did notice an error in my code regarding the retrieval of the y-velocities and have fixed that to no avail. I'm going to try moving the balls further ahead in time to where they are no longer colliding. I think that is the problem. Not sure how to calculate that but it seems the thing to do.


Title: Re: Collision 2D
Post by: Chetmun on November 12, 2008, 09:19:57 am
To interested parties, I changed the if(d <= r1 + r2) to while(d <= r1 + r2) and it seems to work a little better. The balls don't seem to stick together. However, I'm still getting some odd jumps in velocity and the deflection angles look wrong sometimes.


Title: Re: Collision 2D
Post by: lookang on November 12, 2008, 04:26:20 pm
if you are using EJS to do your simulation, i can help you to debug!

upload your xml file if suits :)



Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on November 12, 2008, 10:47:59 pm
Quote
The initial directional velocities are randomly between 13 and 20 per time step. The radii are randomly between 15 and 50.

It seems that your time step is too large (or velocity is too large).
Because you have particle moves 13-20 unit per time step, however, the radii are between 15-50.
So it is possible that the particle could move past half of the size of the particle.
However,  an approximation was made when I calculate the time particles really collide. (I have assumed that when we detect particle collide, the overlap distance is much small that their radii.
I think your problem will not occur again if you change velocity to 1.3-2.0


Title: Re: Collision 2D
Post by: Chetmun on November 13, 2008, 04:06:34 am
Thank you, I will experiment with a reduction in the velocities though I am not convinced that will help regarding the "jumpiness" I am seeing. I have taken the randomness out and am just manipulating 3 circles of equal size but I still see the circles move in unexpected ways occasionally upon collision, ie. the wrong angle or with a much larger velocity. I am curious about the approximation you mention. How is it an approximation? I wonder if making it more exact would help.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on November 13, 2008, 02:01:09 pm
Quote
         double vx1 = c1.getXdir(), vy1 = c1.getXdir();
         double vx2 = c2.getXdir(), vy2 = c2.getXdir();

Please check out how you calculate vy1 and vy2!


Title: Re: Collision 2D
Post by: Chetmun on November 17, 2008, 05:59:08 am
Yes, thank you. I mentioned I had fixed that in Reply #9. I'm slowing everything down and adding lots of extra drawing and displaying of various values along the way. Something is definitely not right. I'll get to the bottom of it and let you all know what I find! It's a little cumbersome, code-wise, due to the fact that the drawing for each ball is in the ball class and the timer, the moving, and the repainting are separate java methods.


Title: Re: Collision 2D
Post by: 280 on November 19, 2008, 12:33:44 am
Chetmun, currently I'm working on a project also involving 2d collision, your private Boolean collision function has helped a great deal in understanding how to start. However, things become cloudy when the Circle class functions come into play. It would help me a great deal if I could take a peak at your Circle Object code? Sorry if this is asking too much, pseudo code would suffice as well... In general I'm stumped on what exactly is needed to represent a circle traveling through space. Thank you.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on November 19, 2008, 04:00:43 pm
The above simulation was generated with EJS.
There is another verison of 2D Collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0) , you can download the file and find out the original java code (written with JDK1.0.2).


Title: Re: Collision 2D
Post by: ozgurvt on December 28, 2008, 11:20:55 pm
I couldn't understand what you are writing in the code, I hope you will explain the important parts with comments next time.

 


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on December 29, 2008, 08:30:25 am
Please check out 2D Collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0) for related discussion.

You need to understand the physics before you can understand the code.
Please write down in detail your understanding about collision process,
and the code you do not understand, otherwise, I will not be able to help.


Title: Re: Collision 2D
Post by: ArdTraveller on January 06, 2009, 06:46:33 pm
Sir, can I please have the source code for this applet,even cooler than the other 2D collision, thx


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 06, 2009, 08:23:47 pm
You can always download the EJS source cod in this forum if you login to the system.
Just click  "DOWNLOAD ejs source(xml)"  one line below the simulation.
You need to download EJS and load the source code into EJS.


Title: Re: Collision 2D
Post by: ArdTraveller on January 08, 2009, 09:59:02 am
I'v succesfully gotten to run the EJS and the 2D collision simulation can run but it's in XML so could you provide me with the Java source code like the one provided for the 1D collision coz I don know how to generate the Java source code from the EJS.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 08, 2009, 10:59:18 am
There is a setting in EJS that allow you to either keep java code or remove java code after ejs class files were generated.
(I believe the default is keep java code).
You can find java code in the generated directory (ejsfilename +".app")
It is under _simulation for EJS version 3, under workspace/output/users/... for EJS version 4.



Title: Re: Collision 2D
Post by: ArdTraveller on January 08, 2009, 11:49:11 pm
Well thank you sir but i still have a problem, the code needs a certain package because I can't compile and run it coz the main class which is Collision2D_e, extends this right ..org.opensourcephysics.ejs.AbstractModel and so with the other java programs so i don what to do it just can't run without this org package


Title: Re: Collision 2D
Post by: lookang on January 09, 2009, 08:33:35 am
1. launch EJS and open the file Collision2D.xml for the download link on first post
2. click on edit options button
3. a pop up on EJS options appears, first tab uncheck tickbox remove java files after compiling
4. the default is to remove for this file
5. use your computer explorer to goto F:\EasyJavaSimulation\EJS_4.1_081216\EJS_4.1\workspace\output\Collision2D_e.java    location may defer depend on what you expanded EJS.
6. i assume u are using EJS 4.1, for EJS 3.47, the idea is similar :)


try it!

i may be able to make a screenvideo by this weekend! watch this space for it!
Qn:How to watch this space?
under Additional Options... Notify me of replies. select it


Help U:
i have attached for you incase your unusually problem
"I can't compile and run it coz the main class which is Collision2D_e, extends this right ..org.opensourcephysics.ejs.AbstractModel and so with the other java programs so i don what to do it just can't run without this org package"

is bugging you.

Suggestion:
Try downloading a fresh copy from www.um.es/fem/Ejs/
may solve your problem about
"I can't compile and run it coz the main class which is Collision2D_e, extends this right ..org.opensourcephysics.ejs.AbstractModel and so with the other java programs so i don what to do it just can't run without this org package"

remember to attribute your derived work like one of these
This work is licensed under a Creative Commons Attribution 2.5 Taiwan License.  ;)


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 09, 2009, 05:56:24 pm
Quote
Well thank you sir but i still have a problem, the code needs a certain package because I can't compile and run it coz the main class which is Collision2D_e, extends this right ..org.opensourcephysics.ejs.AbstractModel and so with the other java programs so i don what to do it just can't run without this org package

EJS will generate all the java class files and pack it into jar for you.
I  do not understand why you need to compile it again.
If you really need to do it, you will find osp.jar,ejs.jar... under ejs/bin

Please write down what is your final goal? May be there is some other easy way to do it (Not the way you were doing right now.)


Title: Re: Collision 2D
Post by: ArdTraveller on January 12, 2009, 09:24:35 am
The thing is actually like this sir, i need a source code where i can use on my desktop and see it run on the desktop and actually just see the applet without having to create it into xml and jar and so on, just to see it run after the code is written in netbeans. The problem is the program has to import this org.colos.ejs.library._EjsConstants  package which I don know where it is and then in the code there is this line static public String _getEjsModel() { return "./Collision2D_e.xml";  which if by using EJS would run but if just written in netbeans would not.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 12, 2009, 01:29:58 pm
If you just want to run it on the desktop.
Download the above zip file (click download button) , unzip the file and double click the jar file.
The program will run nicely if you have java run time installed. You do not need the xml files.
You already have everything to run the java application.


Title: Re: Collision 2D
Post by: ArdTraveller on January 13, 2009, 02:15:43 pm
I was able to run it in jar sir, i just really need ur help of providing the source code like the one you provided for the 2D Collision at this url http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4 for this collision simulation, i was able to compile and run the one you gave for the 2D Collision, because the main function didn't have to extend any other package, if possible could you send it to this email, tera_xollaravo@yahoo.com, appreciate it much.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 13, 2009, 04:59:37 pm
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.



Title: Re: Collision 2D
Post by: ArdTraveller 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 :).


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on January 28, 2009, 10:32:57 pm
Are you talking about the 2D Collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0)?

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.


Title: Re: Collision 2D
Post by: ArdTraveller 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


Title: Re: Collision 2D
Post by: chaocai 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?


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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 (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0) 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


Title: Re: Collision 2D
Post by: chaocai 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 (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0). 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.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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.


Title: Re: Collision 2D
Post by: ArdTraveller on February 10, 2009, 11:33:03 am
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


Title: Re: Collision 2D
Post by: chaocai on February 10, 2009, 11:56:05 am
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.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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.
(http://www.phy.ntnu.edu.tw/ntnujava/snapshotejs/120_smf_collision2D_e_20090121175717.gif)
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.


Title: Re: Collision 2D
Post by: chaocai on February 10, 2009, 03:21:41 pm
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?



Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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. ;D

And you will have to make sure that v*dt<< size of the ball in your code. ::)


Title: Re: Collision 2D
Post by: lookang on February 10, 2009, 04:27:31 pm
interesting


Title: Re: Collision 2D
Post by: chaocai on February 11, 2009, 01:00:40 am
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. ::)     

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:)


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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:

:o :o :o
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
:o :o :o
// 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
...
------------------------------------------------------------------------
 ???


Title: Re: Collision 2D
Post by: chaocai on February 11, 2009, 12:39:57 pm
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.)
-*-


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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 motion (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=178.0) ,  particle model for solid/liquid/gas states in 3D (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=297.0) 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!


Title: Re: Collision 2D
Post by: chaocai on February 11, 2009, 02:04:28 pm
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



Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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.


Title: Re: Collision 2D
Post by: chaocai 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.


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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 (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=702.0)
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);
}



Title: Re: Collision 2D
Post by: Question 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


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang 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).

(http://www.phy.ntnu.edu.tw/ntnujava/snapshotejs/4_smf_collision2D_20090118001718.gif)


Title: Re: Collision 2D
Post by: FractalKiller 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);


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on March 16, 2009, 08:12:37 am
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.


Title: Re: Collision 2D
Post by: notausedname 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.


Title: Re: Collision 2D
Post by: lookang on May 20, 2009, 11:48:17 am
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


Title: Re: Collision 2D
Post by: Bluzkin on November 19, 2010, 02:08:33 am
Hello Fu-Kwun Hwang. This really helped me. For my finals I chose to make a physics simulation application (in Game Maker 8.0) including brownian motion.

I have tried to get the final formula for v1' with the first two equations but I just can't get it right. Could you maybe send me the the steps you took to get there? That would be great.

I also don't know how to calculate the component of a force (or other vector) in a certain direction so if you could help me with that I would really appreciate it.

Thanks in advance.

(I don't know if I double posted but I think my first post wasn't processed or something...)


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on November 19, 2010, 11:53:24 am
I assume you know how to find vector which connect between center of two particles.
if one is (x1,y1,z1),another one is (x2,y2,z1). then the vector $\vec{r}$ is (x2-x1,y2-y1,z2-z1).
Assume velocity of particle 1 is$vec{v}$= (vx1,vy1,vz1),
The velocity component in the vector $vec{r}$ direction can be calculated as $\vec{v_p}=(\vec{v}\cdot\vec{r})/r^2 \vec{r}$,where $r$ is the length of velocity $\vec{r}=\sqrt{(x2-x1)^2+(y2-y1)^2+(z2-z1)^2}$
 
i.e.
 $vx1'=((vx1*(x2-x1)+vy1*(y2-y1)+vz1*(z2-z1))/r^2) (x2-x1)$
 $vy1'=((vx1*(x2-x1)+vy1*(y2-y1)+vz1*(z2-z1))/r^2) (y2-y1)$
 $vz1'=((vx1*(x2-x1)+vy1*(y2-y1)+vz1*(z2-z1))/r^2) (z2-z1)$
 
And the velocity compendicular to $vec{r}$ is $\vec{v_n}=\vec{v}-\vec{v_p}$

P.S. The inner product between $\vec{A}$ and unit vector $\hat{n}$ will give you magnitude of projection of vector $\vec{A}$ in the direction of $\hat{n}=\vec{r}/r$, so the component in the direction $\hat{n}$ is $(\vec{A}\cdot\hat{n}) \hat{n}$


Title: Re: Collision 2D
Post by: Bluzkin on November 21, 2010, 12:40:39 am
Thanks! that really helped.

Now I just need to understand JS and have a look at the source code ;)


Title: Re: Collision 2D
Post by: lookang on April 11, 2011, 07:51:27 am
some more error when clicking 'VIEW  ejs xml source'
OOPS! EJS XML file (airplane.xml) does not exists!

download it working correctly though  :D


Title: Re: Collision 2D
Post by: Fu-Kwun Hwang on April 11, 2011, 05:51:32 pm
Thank you for reporting the problem. It is fixed now.