Author Topic: 1D collision : Conservation of Momentum  (Read 255339 times)

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #15 on: February 16, 2009, 10:47:19 am »
The greenish x mark is the coordinate of the center of mass for those two objects.

The center of mass is moving at constant velocity, either before or after the collision.
Because there is no extra force acting on the system (system with the above two objects).

Xcm= m1*x1+m2*x2/(m1+m2);

If you switch to cm(selection box): observer at center of mass , you will find the greenish x mark will not move.


summer_zwx

  • Newbie
  • *
  • Posts: 1
Re: 1D collision : Conservation of Momentum
« Reply #16 on: February 18, 2009, 06:39:06 am »
Hi, your Java Applet applications are so great!
Could you please  e-mail me the source code?
I am doing a project for designing an interface and I can't figure out how to draw the dynamic arrows.

Thank you so much!

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #17 on: February 18, 2009, 08:49:55 am »
There is an attached file (java source code) for the first message. Did not you see it (source code)?

Question

  • Newbie
  • *
  • Posts: 10
Re: 1D collision : Conservation of Momentum
« Reply #18 on: February 21, 2009, 12:52:40 pm »
Sir, in the code there is this variable mcm( an int type) but I'm not sure what is it for, :- and the part where the collision is detected is at what code sir?

Oh yeah n one more thing sir, what technique did u use, in order to get the proper collision detection or what technique/s is involved to create this simulation, in the physics aspect as well( I don't want to know how to create the simulation from the EJS though-not yet), thx :)
« Last Edit: February 21, 2009, 12:59:50 pm by Question »

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #19 on: February 21, 2009, 02:46:30 pm »
The above simulation was not created with EJS. It was created more than 10 years ago with JDK1.0.2.

The coordinate of center of mass Xcm=(m1*X1+m2*X2)/(m1+m2)=(m1*X1+m2*X2)/mcm;
So mcm=m1+m2 is the sum of those two particles.
It is defined to make calculate property in the center of mass system much easier.
e.g. Vcm=(m1*V1+m2*V2)/(m1+m2)=(m1*V1+m2*V2)/mcm;

The radius of two circles are R1 and R2,  the distance between two center of the curcle d=|x2-x1|,
if d < R1+R2 then we know the circles should have collided with each other when d=R1+R2;
So we need to move time backward with ddt=(R1+R2-d)/|V2-V1|.

 X1=X1-V1*ddt;
 X2=X2-V2*ddt;
This is moment those two circle collide with each other.
Then, use the formula shown at the first message (or check out standard physics textbook) to calculate velocity for each circle. (V1', V2')
Then move the time forward to t:
X1=X1+V1'*ddt;
X2=X2+V2'*ddt;
 

Question

  • Newbie
  • *
  • Posts: 10
Re: 1D collision : Conservation of Momentum
« Reply #20 on: February 23, 2009, 08:51:11 pm »
Hello sir I don't get this line of code though

add("North",p);


what does the "North" signify?

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #21 on: February 23, 2009, 09:16:16 pm »
The above simulation was created more than 10 years ago with JDK1.0.2.

add("North",p);
means add p element to the north side of current Panel (another GUI element).

I would suggest you understand the physics model in the code.
I would suggest you study how to create GUI in java or use EJS to generate GUI for you if you want to create another by yourself!

Question

  • Newbie
  • *
  • Posts: 10
Re: 1D collision : Conservation of Momentum
« Reply #22 on: February 28, 2009, 11:39:48 am »
Salutations sir, well being a novice in this Java thingy I was jus curious from the code of this 1D collision is it possible to like save results of collision or in other words the data into a text file  ???and how  ???what do I have to import and at what part to put the codes, could u pleaz help, thx.

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #23 on: February 28, 2009, 12:06:06 pm »

private void saveData (java.io.FileWriter _output) throws java.lang.Exception {
  // include one line for each ASCII line in your file
  _output.write("A line of text
"); //
 in Windows, only
 in Unix
  _output.write("Another line of text
");
  }

  private boolean saveToFile (String _filename) {
  try {
  java.io.File file = new java.io.File(_filename);
  java.io.FileWriter fout = new java.io.FileWriter(file);
  saveData (fout);
  fout.close();
  return true;
  }
  catch (java.lang.Exception ioe) {
  System.err.println ("Error when trying to save"+_filename);
  ioe.printStackTrace(System.err);
  return false;
  }
  }



You can write
System.out.println(String_data);
to output String to java console as a quick way to get data.

If you need to read data from file
  private void readData (java.io.LineNumberReader _input) throws java.lang.Exception {
  String line = _input.readLine();
  while (line != null) {
  // Process the input line according to your needs:
  _println (line);
  line = _input.readLine();
  }
  } 

  private boolean readFromFile (String _filename) {
  try {
  java.io.Reader reader=null;
  if (_filename.startsWith("url:")) {
  java.net.URL url = new java.net.URL (_filename.substring(4));
  reader = new java.io.InputStreamReader(url.openStream());
  }
  else {
  java.io.File file = new java.io.File(_filename);
  reader = new java.io.FileReader (file);
  }
  java.io.LineNumberReader lineReader = new java.io.LineNumberReader(reader);
  readData (lineReader);
  reader.close();
  return true;
  }
  catch (java.lang.Exception ioe) {
  System.err.println ("Error when trying to read "+_filename);
  ioe.printStackTrace(System.err);
  return false;
  }
  }

  public void read () { readFromFile (filename); }

I believe you should be able to search the web for more information about "How to write data to a file"!

Question

  • Newbie
  • *
  • Posts: 10
Re: 1D collision : Conservation of Momentum
« Reply #24 on: March 09, 2009, 01:25:59 am »
Salutations sir, I'v got a problem again..I guess this line of code is where the collision is detected right, of the ball and the wall, rite?

if(X[5]-X[2]<size2){// m2 & wall
         if(coeff>0.){
            ddt=dt-(X[2]+size2-X[5])/V[2];
            if(ddt<dt){
               X[2]-=(V[2]-v0)*ddt;
               X[5]-=(V[5]-v0)*ddt;
               //V[2]=-coeff*V[2];
               V[2]=-V[2];
               if(frame==3)v0=(m1*V[1]+m2*V[2])/mcm;
               v0=V[frame];
               X[2]-=(V[2]-v0)*ddt;
               X[5]-=(V[5]-v0)*ddt;
            }
         }else if(V[1]==V[2]) V[1]=V[2]=0.;
         else V[2]=0;
      }
      V[3]=(m1*V[1]+m2*V[2])/mcm;
      v0=V[frame];
      X[3]=((m1*X[1]+m2*X[2])/mcm);
      drawIt();
   }

But when i changed the distance of the two walls further apart...

original code

g.fillRect((int)X[4]-size,yc-size4,size2,size8);
g.fillRect((int)X[5]-size,yc-size4,size2,size8);

to this

g.fillRect(0,yc-size4,size2,size8);
g.fillRect((int)X[5]+39,yc-size4,size2,size8);

..the balls don't move rite to the wall and before even reaching the wall they bounce back as if bouncing off the walls however proceed with the normal collision  ???.

Can u help me sir in explaining how do the balls actually "know" when they hit the wall and how should I lengthen the ability of the balls to travel, similar to how the balls in your collision 2D are capable of  :-. Thank you so much  :)

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #25 on: March 09, 2009, 10:28:48 am »
It has been a long time ago when I wrote the above code.
I guess x[2] is the center for ball 2, and x[5] is the coordinate for the wall.
If there distance x[5]-x[2]<size2 (assume x[5] is always large than x[2] )
Then the ball collide with the wall, so some code are used to calculated the collision process.

If you just want to change the wall position, you should change value for x[5] and keep the rest the same.

If you just change where you draw the wall, and you did not change the code to check the collosion: x[5].
The ball will be bounced back at the same place.
It is the same as you draw the wall at the wrong place.

Question

  • Newbie
  • *
  • Posts: 10
Re: 1D collision : Conservation of Momentum
« Reply #26 on: March 13, 2009, 06:02:34 pm »
Salutations again sir, okay now I get how it is able to detect the wall, what bothers me now is how do I get the rectangle to change its area size

I'v tried to put this code thinking I can chng the rectangle size(in red):

public class collision extends Applet implements Runnable{
   
   Dimension area= new Dimension(300,300);

but somehow when it starts or it runs the window frame is still the same before I modified
(End of first question)

Q2.
and I don't get wat does

this

area=size();code really mean
why is the size() method have the slash on the word size?-How do I know what the width of the frame or window is so that I know what area.width is wat value

Q3.
U said in order for the mass2 to detect the wall i have to change X[5] rite
(N i managed to get the blue circle to hit the border of the rectangle but had to change the X[5]=area.width-X[4];
to X[5]=area.width+5;)
But how about mass1?
I tried to change X[1] value
and X[4] value but the red circle keeps on bouncing back wen it didn't even hit the wall yet(in this case now I want it to hit the rectangle border)..therefore I plead from u sir could u plz shed some light on this, coz I'm at blur :-..i know that the division of X[4]by 6 has to with the plot in the frame similar to X[1] but how to make the red circle hit the the border of the rectangle I just don't know ??? It's the reason I asked about the area.width thingyit must have some relation rite?Thx again

Fu-Kwun Hwang

  • Administrator
  • Hero Member
  • *****
  • Posts: 3062
    • Enjoy the fun of physics with simulations
Re: 1D collision : Conservation of Momentum
« Reply #27 on: March 13, 2009, 06:31:36 pm »
In my code, the (width,height) of the applet is determined from parameter of the applet.
(You need to specify the width and height values in the applet tag-- Please check out html source for this page.)
area=size(); // get width and height value from applet created by browser. (browser use width/height value in applet tag to allocate area in the browser).
The above area include the area for the button/... at the top.
So I add
area.height-=yOffset;
to get the real area for the simulation region (exclude the top button/text input...).

Wall in the simulation was drawn with
      //draw wall 1,wall2
      g.setColor(Color.gray);
      g.fillRect((int)X[4]-size,yc-size4,size2,size8);
      g.fillRect((int)X[5]-size,yc-size4,size2,size8);
X4 and X5 is the x value for center of wall 1 and wall 2, and size2 is the width of the wall.
If you want to change the size, you should change 3th/4th parameters.
But you need to change the condition for collision according to the real width of the wall and radius of the circle.
Quote
U said in order for the mass2 to detect the wall i have to change X[5] rite
No. What I said was
Quote
If you just want to change the wall position, you should change value for x[5] and keep the rest the same.

I am will to help if you have problem with the physics involved.
You need to understand what is the condition to check for collision.
The best way is to write your own code. If you want to follow my code, you will need to understand how it work.

aizen

  • Newbie
  • *
  • Posts: 1
Re: 1D collision : Conservation of Momentum
« Reply #28 on: March 09, 2010, 06:04:11 pm »
Sir,
 Thankyou very much! This is a very big help to me and my students as well.

lookang

  • Hero Member
  • *****
  • Posts: 1772
  • http://weelookang.blogspot.com
    • lookang web
Re: 1D collision : Conservation of Momentum
« Reply #29 on: March 10, 2010, 09:41:39 am »
Agreed aizen!

The applets here are really helpful in learning.

The feature that is cool for me is:
the centre of mass., will add it here Ejs open source java applet 1D collision carts Elastic and Inelastic Collision http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=831.0 when time permits!

Thanks Prof Fu-Kwun Hwang!