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

JDK1.0.2 simulations (1996-2001) => Optics => Topic started by: Fu-Kwun Hwang on January 29, 2004, 10:21:25 pm



Title: Billiards and Physics
Post by: Fu-Kwun Hwang on January 29, 2004, 10:21:25 pm
This java applet show you how to use the law of reflection (for optics)in playing pool.
A ball bouncing off the bank of a pool table behaves like a light ray reflecting off a mirror.

There are two colored balls; red and blue.

How to play?
    Set up an initial velocity for the red ball and
      let the red ball knock the blue one into one of the six bags.

Click LEFT mouse button within the red ball and drag the mouse
    to set the initial velocity for the red ball.


    The ball starts to move when you release the mouse button.

Click the RIGHT mouse button to change the initial position for the red ball.

More information will be provided If you select level1 or level2.
Try it and find out the physics behind the game.
(For collision between two balls, please check out 2D Collision)

The following is a flash demostration.





Title: Email:
Post by: Fu-Kwun Hwang on January 29, 2004, 10:21:58 pm
Subject: RE: Billiards and Physics
Date: Mon, 5 Mar 2001 09:38:53 -0500
From: John Botsford <JBOTSFORD@equitable.ca>
To: 'Fu-Kwun Hwang' <hwang@phy03.phy.ntnu.edu.tw>

    John Botsford wrote:

   >
    >  The law of reflection can only be used in billiards to
    >  approximate the angle of reflection for a billiard ball.  Since
    >  the rails of a pool table are rubber, the ball will sink into
    >  the cushion to varying degrees depending on the velocity of the
    >  ball.  This amount that a ball sinks into the cushion changes
    >  the angle of reflection.  For instance, the faster a ball is
    >  moving when it hits the rail, the sharper the angle of
    >  reflection will be.  The law of reflection is a close
    >  approximation only if the ball is moving relatively slowly.  The
    >  other thing that affects the angle of reflection is the spin on
    >  a ball.  In the case of the cue ball, spin is often purposely
    >  mparted to the ball by striking it off center in order to change
    >  where the cue ball will end up.  As for object balls, spin is
    >  imparted to them by contact with other objects (balls and
    >  rails).  This spin also changes the angle of reflection.
    >


Title: reply:
Post by: Fu-Kwun Hwang on January 29, 2004, 10:22:37 pm
Yes. It is true that the law of reflection is only an approximation of what really happened!
     The effect of rubber and the spining effect also can be simulated
     if we know the spring constant and apply conservation of momentum,
     energy and angular momentum. But that would be too complicate to
    the students who just learn physics.
     The purpose of our animation is for teaching. That is why I
     simplify the problem and the result are still very similar to what
     really happened.  Thank you for your comments. Could I add your  comments to the end of the web pages?
     Regards!


Title: topic41
Post by: on January 30, 2004, 11:16:10 am
Subject:  Physics of Pool
Date:     Wed, 07 Oct 1998 14:13:44 -0400
From:     Adam Lapatin <alapatin@hampshire.edu>
To:       hwang@phy03.phy.ntnu.edu.tw
Hi, I just played around with your java applet for billiards and I was
wondering if you know of any good books and/or websites about the
physics of pool.  Any information you have about the subject would also
be extremely helpful ... if you haven't guesses I'm writing a report on
the physics of pool.

Thanks,

Adam


Title: topic41
Post by: on January 30, 2004, 12:42:08 pm
Subject: web page
Date: Mon, 5 Jul 1999 16:15:46 EDT
From: CLBlom@aol.com
To: hwang@phy03.phy.ntnu.edu.tw
Dear Fu-Kwun,

I appreciate your web pages:  Billards and Physics, and 2D Collision.
I am a high school science teacher, preparing to teach an "Integrated
Chemistry and Physics" class, and I anticipate using your web pages in the
classroom.  Thanks!

Carrie Doyle


Title: topic41
Post by: on January 30, 2004, 04:43:19 pm
Date: Tue, 16 Nov 1999 14:00:04 +0100
From: TURESSON SVEN-ERIK <da97tuse@ing.hj.se>
Subject: Re: Billiard
To: Fu-Kwun Hwang <hwang@phy03.phy.ntnu.edu.tw>

Thank you for helping. We got som new wiews that helped us to
determine our misstakes.
"Group 12 of the OOP-course at HJ"
/Sven-Erik


Title: topic41
Post by: lopa on September 29, 2005, 07:41:07 pm
Sir/mam
I want the java file of this applet
thanks


Title: hOW MUCH THIS WEBSITE SUCKS.
Post by: Blackninja on November 11, 2005, 12:53:17 am
With 25 years of experience, I find it very sad that teachers have to rely on a poor website like this to educate their fine pupils.  Unfortunately though, the future of the United States has to rely on such low quality teachers, websites that lack educational value, and endless amounts of work sheets.  Well, My main point is that this website is terrible and no student should be subjected to it.  Even though there are many html domains out there, this one however is a waste of valueable space, and should never see the light of day again.


Title: This applet is terrible material.
Post by: fatkid on November 11, 2005, 12:53:45 am
I was told by a peer to visit this site and try this applet to teach my students about reflection.  I was incredibly disappointed by the results.   The applet is poorly designed, does not teach about reflection very well, and I found it offensive.  I would really appreciate it if you focused more on quality than making offensive garbage like this so-called "Billiards" game.  Billiards has no place in the classroom and I am glad that this website is so obscure that no one will ever find it unless they stumble upon it by accident.  Please don't give me the excuse that you are a physicist, not a programmer, because we all have a responsibility to the children.  Making applets lacking in educational value shows a lack of social responsibility and is deplorable in this regard.  Please cut the crap and work harder.


Title: topic41
Post by: Fu-Kwun Hwang on November 11, 2005, 09:53:56 am
I do not know why you registered with several accounts(with different user name, for examples: the above two mesaages are posted by you and you also post some other message with different username).  

Are you realy a teacher?  Could you tell me why you post the following message (http://www.phy.ntnu.edu.tw/ntnujava/viewtopic.php?t=65) with another username on our forum:
[quote:296bbea393]Physics=Boring!!!!![/quote:296bbea393]
*** The above two messages and the quoted one are posted from the same IP within 10 minutes ***

If you are really a teacher:

I am sorry that you find our applets are useless for you students. I am also sorry that you feel physics is boring. I do enjoy the fun of physics very much!
I am willing to help you if you could tell me what kind of applet (in detail) you really want.  

I did what I could to help and many teachers do find our applets helpful for their teaching. That is why there are so many mirror sites around the world.

If I find something is not good enough , I would not just complained about the situation. I will really do something to help (our children).

 I will try to help you if you could tell me the design of an applet you think it will be good for helping student to understand the physics concept.  :roll:


Title: topic41
Post by: Fu-Kwun Hwang on November 11, 2005, 11:37:30 am
The following flash movie shows how this applet is related to reflection.
I did not aim the target directly.I can aim at any one of the image of the target.
Look at the trajectory of the balls and you will see why it is related to flection.
                           

                           


Title: topic41
Post by: Fu-Kwun Hwang on November 11, 2005, 12:35:20 pm
If the above two flash animations did not show up in your browser, please click flash1 and flash2


Title: Re: Billiards and Physics
Post by: tarabelo on February 22, 2007, 07:36:36 pm
Dear sir,

Your page is really interesting. I wonder if it'd be possible to get the source code of the applet. I am interested in making a new applet for my classroom with more possibilities, as an example, with more balls interacting.

Best regards

Dr. Tomas F. Pena


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on February 22, 2007, 07:49:11 pm
You should have received the source code now.


Title: Re: Billiards and Physics
Post by: tam on March 20, 2007, 11:18:49 pm
Dear Fu-Kwun Hwang,
I find out that your Billiard applet is very useful for learning reflection's concept .
I would like to have the source code if you don't mind; to learn more how to apply
the concept into programming.
Thanks for your kindness.
Tam


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 21, 2007, 08:51:57 pm
You should have received the source code now. However, it was written many years ago with JDK1.0.2.
You might need to change some of he code to be compiled with current version of JAVA.


Title: Re: Billiards and Physics
Post by: Dernhelm on April 03, 2007, 02:11:45 pm
Hello,

Very interesting! Could you, please, send me the source code of this applet?

Thank you very much.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on April 04, 2007, 09:50:49 pm
You should have received the source code now.


Title: Re: Billiards and Physics
Post by: whyzlo on April 05, 2007, 07:03:15 pm
Hi,

Is it possible to recieve source code? It would be very helpfull to me. Please  :).

Vaidotas,
Lithuania


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on April 06, 2007, 09:44:10 pm
Download the applets files again. You will find source code in the ZIP file.


Title: Re: Billiards and Physics
Post by: whyzlo on April 13, 2007, 02:52:44 pm
thank you good man  :)


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on May 20, 2007, 05:48:50 pm
Amazing Billiards in Super Slow Motion (http://www.youtube.com/watch?v=pZqkaJDaz2A)


Title: Re: Billiards and Physics
Post by: sport on May 28, 2007, 05:34:45 pm
Hello,

Could you, please, send me the source code of this applet?

Thank you very much.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on May 28, 2007, 09:19:05 pm
If you have read the above posted message, you should know source  code for this applet is included in the zip file if you have click "get applet files for offline use" button.


Title: Re: Billiards and Physics
Post by: sport on May 28, 2007, 09:30:51 pm
If you have read the above posted message, you should know source  code for this applet is included in the zip file if you have click "get applet files for offline use" button.

When I click in the button, it shows the following message:

Parse error: syntax error, unexpected T_STRING in /htdocs/demolab/smf/ntnujavazip.php on line 148


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on May 28, 2007, 11:41:15 pm
Sorry! It was a new bug when I modified the server code. It is fixed now. Please download it again.


Title: Re: Billiards and Physics
Post by: sport on May 29, 2007, 07:53:58 pm
Thank you!  :D


Title: Re: Billiards and Physics
Post by: neel.r on May 31, 2007, 10:05:53 pm
sir
could please mail me the source code of your applet(billiards)...
it would b very helpful for my project....
thank you very much...


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on June 01, 2007, 11:34:31 pm
Please read the previous posted messages. You will know where to find the source code.


Title: Re: Billiards and Physics
Post by: rajettan on July 19, 2007, 12:33:59 am
it indeed is marvellos.im a high school teacher from india.im preparing to teach collission as per your idea.thank you


Title: future
Post by: rajettan on July 19, 2007, 12:45:08 am
its true that we always see the past for light takes definite time to reach our eye.sunlight takes 8 minutes aprox to reach earth.that means we are seeing 8 minute old sun.can we argue in any way that we can see tha future.please help


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on July 20, 2007, 10:32:16 pm
we are seeing 8 minute old sun. So we can see the past instead of future.


Title: Re: Billiards and Physics
Post by: nadia_777 on December 29, 2007, 04:17:35 pm
Dear Mr Hwang I would like to know if you have written any article on billiard physics which comes with example code. I found the applet available really helpfull unfortunately some part of it seem quite complex and I was wondering if you further explanation is available somewhere in your website.

Thanks


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 29, 2007, 09:08:05 pm
I do not know what do you mean by "some part of it seem quite complex"? Which part?
Do you mean: how I processed the collision between two particles in two dimension?
If it is the case, please check out 2D Collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0)
You will find out how it was calculated and how the code was implemented (with source code).


Title: Re: Billiards and Physics
Post by: nadia_777 on February 15, 2008, 08:43:38 am
Dear Mr Hwang

I would like to understand how you approached about solving the collision problem. Did you use any sort of standard algorithm so that I go and read the right resource?

If not could you please explain to me how you solve the velocity after collision?

Thank you in advance.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on February 15, 2008, 04:17:52 pm
Please check 2D Collision (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=4.0) or  Collision 2D(EJS version) (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120)
for more information. You can find out the physics and the code from those two posts.


Title: Re: Billiards and Physics
Post by: nadia_777 on March 09, 2008, 08:06:39 am
Hi all,

I am trying to create a java pool game which monitors the users performance on how accurately they pocket balls. It is a one player game and I am having trouble with the algorithm related to motion and I used some of your code to implement that part but I am not getting the same result.   
Lets say that no collision occurs, when a force is applied upon the cue Ball, it travels along the right direction but it deviates from the trajectory more and more and end up in the wrong place. Could anyone tell me what the problem is? I am really. Thank you in advance.

Sample code from the Pool class
*************************
public void run()
 {
  Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  while (Thread.currentThread() == animThread)
  {
   delta = System.currentTimeMillis() - lastTime;
   lastTime += delta;
   if (ballMoved) AI(delta / 1000.);
   startTime += delay;
   try { animThread.sleep(Math.max(0, startTime - System.currentTimeMillis())); }
   catch (InterruptedException e) {  break; }
  }
 }
 
 public void travel(double dt)
 {
  for (int i = 0; i < Arr.size(); i++)
   Arr.getBallAt(i).collision(cueBall);

   cueBall.travel(dt);
 
  for (int i = 0; i < Arr.size(); i++)
   Arr.getBallAt(i).travel(dt);//ball.AI(dt);

  for (int i = 0; i < Arr.size(); i++)
   ballMoved = cueBall.check(Arr.getBallAt(i));

   repaint();
 }

Sample code for Ball Class
********************
public boolean travel(double dt)
{
  if (!ballMoved)
   return false;

  double velocity = Math.sqrt(vx * vx + vy * vy);
  if (velocity != 0. && !inHole)
  {
   dx = vx / velocity;
   dy = vy / velocity;
   velocity -= 20 * dt;
   if (velocity < 0.)
   {
    vx = 0.;
    vy = 0.;
   }
   else
   {
    vx = velocity * dx;
    vy = velocity * dy;
   }
  }
  x += vx * dt; //increment the x value
  y += vy * dt; //increment the y value
 
  if(isWithinLimits(x, y))
    { /*no action taken*/ }
  else //if the x and y coordinate value are larger than the limits
    //which are the table boundary
   changeDirection();
 
  if (inHole && vx == 0 && vy == 0)
  {
   if (x < diameter)
    x = -radius;
   else if (x > xmax)
    x = xmax + radius;
   else
    x = xmax;
   if (y < diameter)
    y = -radius;
   else if (y > ymax - diameter)
    y = ymax + radius;
   ballMoved = false;
  }
  return inHole || checkhole();
 }
 
 public boolean isWithinLimits(int xx, int yy)
 {
  if (xx > xmin && xx < xmax && yy > ymin && yy < ymax)
   return true;
  else
   return false;
 }
 public void changeDirection()
 {
  if (inHole)
  {
   vx = 0.;
   vy = 0.;
   return;
  }
 
  if (x < xmin)
  {
    x = 2*xmin-x;
    vx = -vx;
  }
  else if (x > xmax)
  {
    x = 2 * xmax - x;
    vx = -vx;
  }
 
  if (y > ymax)
  {
    y = 2 * ymax - y;
    vy = -vy;
  }
  else if (y < ymin)
  {
    y = 2*ymin-y;
    vy = -vy;
  }
 }//end of method
 
 


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 09, 2008, 03:10:56 pm
Please provide explanation for the following code:
public void travel(double dt)
 {
  for (int i = 0; i < Arr.size(); i++)
  Arr.getBallAt(i).collision(cueBall);

  cueBall.travel(dt);
 
  for (int i = 0; i < Arr.size(); i++)
  Arr.getBallAt(i).travel(dt);//ball.AI(dt);

  for (int i = 0; i < Arr.size(); i++)
  ballMoved = cueBall.check(Arr.getBallAt(i));

  repaint();
 }


Title: Re: Billiards and Physics
Post by: nadia_777 on March 09, 2008, 10:48:34 pm
public void travel(double dt)
{
  //Arr is an array that contains all the balls except the cue ball
  //so each time all the balls are checked if the cueball has collided with them
  for (int i = 0; i < Arr.size(); i++)
  Arr.getBallAt(i).collision(cueBall);

  //cueBall moves from its initial position
  cueBall.travel(dt);
 
  //the method travel is called on all the other balls and they will only move if their velocities is not 0
  for (int i = 0; i < Arr.size(); i++)
  Arr.getBallAt(i).travel(dt);

  for (int i = 0; i < Arr.size(); i++)
  ballMoved = cueBall.check(Arr.getBallAt(i));

  repaint();
}
 
 


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 10, 2008, 12:05:10 am
You were using the Euler's method to move the particle one step forward.
However, the error will accumulate unless it is a constant motion.
There is a constant friction force in your case a= 20;  (from your code:  velocity -= 20 * dt;)
The velocity is changing between time t to t+dt (it is not the same between time t and time t+dt).
However, because it is in a constant acceleration mode. so the velocity change linearly.
You should use the average velocity to calculate the displacement (i.e. use Midpoint method instead of Euler's method).

Your problem is mainly due to how you calculate the change in velocity.

   dx = vx / velocity;
   dy = vy / velocity;
   velocity -= 20 * dt;

I know you were trying to calculate the component, but the ratio of vx,vy will be different before and after you change the velocity. That is the reason why the moving direction was changed.

Because it is a constant force acting on the particle. So both vx and vy will change the same amount,
Just use vx-=a*dt; vy-=a*dt;  will be fine.
To make the calculation more accurate.
define vxs,vys before the above change:
i.e.
//save the previous velocity value
 vxs=vx;
 vys=vy;
//calculate the new velocity
 vx-= a*dt;
 vy-=a*dt;
// calculate the displacement
 x+= (vxs+vx)*dt/2;
 y+= (vys+vy)*dt/2;

The above method is also called as "Midpoint Method".


Title: Re: Billiards and Physics
Post by: nadia_777 on March 10, 2008, 02:38:30 am
I think I did the necessary changes but it got worse. The cue ball is moving in different directions, sometimes it stops and starts moving in a random direction.

    public boolean travel(double dt) {

        if (!ballMoved)
            return false;
        //for a 2D space the velocity operations is treated as a vector
        double velocity = Math.sqrt(vx * vx + vy * vy);
       
        //BACKUP the previous velocity value
         double vxs = vx;
         double vys = vy;
         
        if (velocity != 0. && !inHole)
        {
            dxx = vx / velocity;
            dyy = vy / velocity;
            velocity -= a * dt;
           
            if (velocity < 0.)
            {
                vx = 0.;
                vy = 0.;
            }
            else
            {
              /* Average Velocity = Displacement / Change in time
                 OR  (x2 - x1)/ (t2 - t1)
               */
               //CALCULATE THE NEW VELOCITY
               //before it was vx = velocity * dxx;// vy = velocity * dyy;
                vx -= a * dt;
                vy -= a * dt;
            }
        }
        //CALCULATE THE DISPLACEMENT
        //before it was x += vx * dt;  y += vy * dt;
        x += (vxs+vx) * dt/2; //add value to the x coordinate
        y += (vys+vy) * dt/2; // add value to the y coordinate
       
        if(isWithinLimits(x, y))
          { /*no action taken*/ }
        else if(inHole)
          {
                vx = 0.;
                vy = 0.;
          }
         //if the x and y coordinate value are larger than the limits
         //which are the table boundary
        else
            changeDirection();


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 10, 2008, 03:40:41 pm
1. You did not do the same as I suggested. You are still using the code as:
            dxx = vx / velocity;
            dyy = vy / velocity;
            velocity -= a * dt;
 Applied the change to vx,vy directly.

2. if(vx<0) vx=0; // this only work if vx>0 at t=0, what if vx<0 at t=0;


Title: Re: Billiards and Physics
Post by: nadia_777 on March 12, 2008, 08:42:13 am
It still not working and it seems to be worse than before in terms of how long the ball is supposed to travel after a certain force is used on it. Also it is not going in the right direction. Did I miss anything?

 public boolean travel(double dt) {

        if (!ballMoved)
            return false;
           
        //for a 2D space the velocity operations is treated as a vector
        double velocity = Math.sqrt(vx * vx + vy * vy);
       
        //BACKUP the previous velocity value
         double vxs = vx;
         double vys = vy;
         
        if (velocity != 0. && !inHole)
        {           
            if (velocity < 0.)
            {
                vx = 0.;
                vy = 0.;
            }
            else
            {
               //CALCULATE THE NEW VELOCITY
                vx -= a * dt;               
                vy -= a * dt;
            }
        }
        //CALCULATE THE DISPLACEMENT
        x += (vxs+vx) * dt/2;
        y += (vys+vy) * dt/2;

        if(isWithinLimits(x, y))
          { /*no action taken*/ }
        else if(inHole)
          {
                vx = 0.;
                vy = 0.;
          }
         //if the x and y coordinate value are larger than the limits
         //which are the table boundary
        else
            changeDirection();


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 12, 2008, 09:42:42 am
1. Oops!  Sorry, the drag force should not be a constant.
It will keep in the same direction if you changed it to

vx-=b*vx;
vy-=b*vy;

The b will determine how fast the particle will be stopped.
You can try it with 0.1 and adjusted as you need. May be make it as an adjustable parameter.

2. Because velocity = Math.sqrt(vx * vx + vy * vy);
So velocity will always be positive or zero.



Title: Re: Billiards and Physics
Post by: nadia_777 on March 12, 2008, 10:55:54 pm
I am not really good in physics so let me recapitulate what you said to the best of my abilities. You said that the drag force shouldn't be a constant then does that mean that the ball will only come to a halt when:
-vx = 0 and vy=0 but also when the force 'a' which initially had a value of 0.1  [double a = 0.1] gets its value reduced and reaches 0.0 at the same time that vx and vy get the value of 0?

Is there a formula to be used to gradually reduce the speed of a ball in motion and then stop the ball at a certain point and time?


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on March 21, 2008, 07:57:59 pm
If the force is proportional to velocity.

F= -b*v
From Newton's Law:
F= m dv/dt =-b*v
Then, dv/dt=-(b/m)*v

If the velocity V(t=0)=V0
Then the solution for the above differential equation is
V(t)=V0*e-bt/m

The velocity will not reach zero from mathematic point of view.
But it's magnitude will reduce to nearly 2% of its original value by the same time step t=4*(m/b) seconds.
if m/b=1s, V0=100m/s,  (the following values are estimated values, use exponential function to calculate it's exact value.)
at t=4s, V0=100*0.02=2m/s,
at t=8s, v0=2*0.02=0.04m/s,
at t=8s, v0=0.04*0.02=0.0008m/s
...



Title: Re: Billiards and Physics
Post by: RuDZiK on April 29, 2008, 06:24:54 pm
Dear Fu-Kwun Hwang!
I really interested in phisics generally, and your Billiard sample in particular. I was working on a project about ball collision, but it was quite difficult to find any simple examples, scientific literature or detailed articles about 2D-collision. I found your article very useful and clear for beginners, as well as for phisics/sciense interested people. Thank you for your help, I really moved forward in my work.

With best wishes

RuDZiK


Title: Re: Billiards and Physics
Post by: Arturas on July 23, 2008, 09:01:42 pm
Hello Fu-Kwun Hwang

Very interesting! could you, please, send me the source code of this applet?

Thank you very much.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on July 23, 2008, 09:57:22 pm
The source code was added as an attachement in the first message. Please download it by yourself.
However, it was written almost 10 years ago with JDK1.0.2.  You might need to re-write some code to work with current JDK version.


Title: Re: Billiards and Physics
Post by: rpaolet on September 04, 2008, 11:15:58 pm
Ciao Fu-Kwun Hwang,

i'm a italian student and i'm working on a exame of architecture.
Your forum is very interesting and i found your billiards code similar to my project,but i'd like to change some properties like:
-delete the second ball and  erase the holes on the table
-Insert the class swing.timer for add button start and stop to the GUI.

I'm trying to change the code but i didn't know the application doesnt' work.
Could you help me???

Thank you for your response and time.
ciao
Roberto


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on September 05, 2008, 12:00:03 am
Do you mean that you just want to have balls moving inside a rectangle box and collide with each others?


Title: Re: Billiards and Physics
Post by: rpaolet on September 05, 2008, 05:35:05 am
I'd like to have a ball in the rectangle box without holes that i can setup the position,the direction and the power(speed).
Then you can use the start/stop button for the moving.
Required:
-simulate the moving of the ball by use the java.swing.timer. class
-create a GUI
-rolling friction
-change the power(speed) when the ball is still moving

Thank you in advance.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on September 05, 2008, 09:29:16 pm
So you just need one ball in the rectangle?
What do you mean by rolling friction?
If the rolling friction is large enough then the ball's motion is moving without slipping.
It means that there is no energy loss.
What about the collision between the ball and the wall?
If it is elastic collision, then the energy is also conserved.

 I will create the simulation with easy java simulation. It is easier to create it this way.

Is this what you want? You can use mouse to drag and drop either particle position or velocity vector (click checkbox to show velocity). Change b value to add drag forces or clcik constant drag.


Title: Re: Billiards and Physics
Post by: rpaolet on September 06, 2008, 06:38:14 am
Yes the simulation is perfect, thank you very much for your help.
i don't understand some things;the code is written used the javax.swing.timer  and can modify or add of the code?

Thank you a lot for your collaboration.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on September 06, 2008, 04:52:18 pm
It was not written directly with java. I create it with a tool called Easy Java Simulation (http://www.phy.ntnu.edu.tw/ntnujava/index.php?board=11.0)

It is actually a java code generator. It is for people who is not so familiar with java but has strong background in physics/science.
You just define variables/ write equations to exppress relation between variables and equations to indicate how variables change with time. EJS will generate the java code for you. It also has graphic user interface so you can drag and drop graphic elements to create the view.
You can download the EJS source and find out how I did it(You will need to download and install EJS). You can also modify it and generate new code.

If you just want to see how it look like click "load ejs as signed applet" and allow browser to load EJS into your browser. You will be able to run EJS within browser, find out the model, moify it and generate new simulation.


Title: Re: Billiards and Physics
Post by: rpaolet on September 07, 2008, 06:22:10 am
Grazie, you have been very clearly.
I'm not sure that i can use this EJS for my exame,however thanks a lot because i didn't know the EJS suite and it's very fantastic!!!!!!

Ciao
Roberto


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on September 07, 2008, 11:15:43 am
If you are interested in java programming, then I will suggest you write java code by yourself.
However, if you are interested in science and modeling, it will be easier and more effective to use EJS. :-)
Check EJS web site and find out information about it. I have created many flash videos with step by step instruction to show new users how to use EJS to generate new simulations. However, it is displayed in Chinese interface. Sorry!


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on September 07, 2008, 05:46:20 pm
There is a bug in the code. It is corrected today.


Title: Re: Billiards and Physics
Post by: cuebux on November 03, 2010, 02:13:16 am
This is a good simulation. Is there some sort of equation that would help better understand the other factors a player would face in billiards like ball squirt, throw, bounce rates, deflection? Here is a video of the Meucci Black Dot Shaft Demonstration (http://www.cuebux.com/black-dot-shaft.html) showing squirt in action.

Perhaps a database driven application to allow input of collected data against different materials and test variables. Anyone want to partner up?
E.G Humidity, Sea Level, Air pressure etc.


Title: Re: Billiards and Physics
Post by: jarvo250 on December 21, 2010, 10:37:50 pm
I've been looking at this over the last couple of days as I have been trying to remove the, reset, clear and drop down options from the applet.

I'm not having much luck with it and constantly getting compile errors....Any chance there is  version without these options on??


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 21, 2010, 11:02:03 pm
The applet was created many years ago (more then 10 years) with JDK1.0.2
You might get error if you use latest jave to compile it (because java has severla major change over the year)

It should work fine if you use JDK1.0.2


Title: Re: Billiards and Physics
Post by: jarvo250 on December 22, 2010, 06:47:16 am
The applet was created many years ago (more then 10 years) with JDK1.0.2
You might get error if you use latest jave to compile it (because java has severla major change over the year)

It should work fine if you use JDK1.0.2


It compiles fine if I don't alter the code at all....I just want the levels, buttons and drop down part removed from the code. I am attempting to create a snooker game, and I am having real trouble getting started, and ideally would like to use this as a base to get going, but I don't want the options I mentioned. When I remove the codes I think are related to it, it never compiles correctly.....really tearing my hair out over this...


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 22, 2010, 10:00:31 am
I think the problem might be due to "selection list".
If you just remove all those GUI element, then the code can not find the selected level so there is an error.
Find the name for the selection list ,and find code related to that name, replace with proper value/code.
 


Title: Re: Billiards and Physics
Post by: jarvo250 on December 22, 2010, 07:51:46 pm
I think the problem might be due to "selection list".
If you just remove all those GUI element, then the code can not find the selected level so there is an error.
Find the name for the selection list ,and find code related to that name, replace with proper value/code.
 

Ah yes, thanks so much got it. Now how would I enlarge the area in which the ball bounce off. Its quite small at the minute, and would like to ideally make it bigger....


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 22, 2010, 10:02:49 pm
You do not need to re-compile it.
What you need to do is to change the width and height for the applet tag.


Title: Re: Billiards and Physics
Post by: ahmedelshfie on December 22, 2010, 10:14:15 pm
Quote
I think the problem might be due to "selection list".
If you just remove all those GUI element, then the code can not find the selected level so there is an error.
Find the name for the selection list ,and find code related to that name, replace with proper value/code.
Do you mean prof i delete this part from code
Quote
Reset","Clear","level1","level2","level3
And after recompile again i do it but what deleted is text in buttons and choose option but buttons stay not deleted only minimum size of buttons.
I tried delete buttons totally to no appear but always give error when i recompile again.  :-\

 


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 22, 2010, 11:14:33 pm
Code:
String rts,STR[]={"Reset","Clear","level1","level2","level3"};
p.add(cb=new Choice());
for(int i=2;i cb.addItem(STR[i]);

The above code add selection list .
If you remove the above code. It will produce error at the following code
      level=cb.getSelectedIndex();
because cb is not exist any more.
Code:
public void reset(boolean status){
level=cb.getSelectedIndex();
clear(status);
running=false;
m.stop();
s.stop();
}




Title: Re: Billiards and Physics
Post by: ahmedelshfie on December 22, 2010, 11:30:52 pm
I close also this code 
Code:
// level=cb.getSelectedIndex();
Recompile again but didn't change, i believe i didn't understand good your explain prof.


Title: Re: Billiards and Physics
Post by: jarvo250 on December 23, 2010, 12:00:11 am
I now have it to the size that I would like....however, the problem now is that I don't wont the balls to appear in random places upon start-up of the applet. I want to set the x and y coordinates of both balls, and still allow them to move freely when colliding.



EDIT: I HAVE NOW FIGURED THIS OUT, HOWEVER, HOW DO I ADD MORE BALLS!!!


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 23, 2010, 12:23:40 am
1. The position was random because there are assigned as
      x=Math.random()*wx;
      y=Math.random()*wy;

You need to change all those code to whatever you want.

2. There are only two balls in the code
movingballs m,s;

You need to add more and initialize each ball.
If you have more than two balls. You need a double loop to take care of collision between all balls.
e.g. ball A,B,C
You need to take care of collision between AB, BC and CA

You are welcomed to check out Browian motion (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=178.0) or particle model for solid/liquid/gas states in 3D (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=297.0)

3. If there is any error when compile the code, check out the error message (it will point out the line numeber where the error occur) and try to solve it according to what the message say.





Title: Re: Billiards and Physics
Post by: jarvo250 on December 23, 2010, 01:16:55 am
Ok so I have a new ball appearing, but it appears under an existing ball (one of the originals that was in the code). And I have it so that the white ball collides with it but the ball doesnt move.

I assume this is to do with that double loop you were on about.....where abouts is this in the code and what do I add.


Title: Re: Billiards and Physics
Post by: jarvo250 on December 23, 2010, 06:54:55 am
I have multiple balls on screen now, and each ball collides successfully. The only problem I have now is with the random positioning of the balls. I know how to change it so its not random. However, when I do change it two of the three balls go into the same place, and then the collisions are messed up. How can I set the position and allow the balls to collide.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 23, 2010, 07:05:14 pm
The code to process collision will fail if you have two balls at the same position. (This would not happened in real life).

If you have more than two ball , i.e. n ball
You need a double loop to process possible collision between any two balls.
The original code only process collision between two ball.
Change it a function e.g. collision(i,j) which will process collision between particle i and j.
The double will look like
for(int i=0;i<n;i++){
 for(int j=i+1;j<n;j++){
   collision(i,j);
 }
}

You are welcomed to check out Collision 2D (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0) to find more information.


Title: Re: Billiards and Physics
Post by: jarvo250 on December 23, 2010, 10:02:45 pm
The code to process collision will fail if you have two balls at the same position. (This would not happened in real life).

If you have more than two ball , i.e. n ball
You need a double loop to process possible collision between any two balls.
The original code only process collision between two ball.
Change it a function e.g. collision(i,j) which will process collision between particle i and j.
The double will look like
for(int i=0;i<n;i++){
 for(int j=i+1;j<n;j++){
   collision(i,j);
 }
}

You are welcomed to check out Collision 2D (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0) to find more information.

Am totally lost. There is already something similar to that in the code. Are you saying I need to do that again or not?

Honestly can't get it done....


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 23, 2010, 11:40:45 pm
You do not need to add more loop if you already have double loop in your code.

However, you have to do it correctly.
I do not know how you did it and I do not know how can I help?


Title: Re: Billiards and Physics
Post by: jarvo250 on December 23, 2010, 11:58:36 pm
I've managed to delete the existing double loop without effecting the applet.


Title: Re: Billiards and Physics
Post by: jarvo250 on December 24, 2010, 12:16:59 am
I have more than two balls colliding...i.e AB, BC, CA can all collide. The problem I have is with placing balls B and C at set coordinates. I have ball A working in set coordinates, but ball B and C share the same X and Y coordinates

i.e: x=Math.random()*wx;
     y=Math.random()*wy;

When i change the above to say, 100 and 200, both B and C balls move to that place. I need separate X and Y coordinates to be assigned but also work in the collision method.

I have new instance of a ball which allows me the third ball, but the new instance has the same value as B the second ball.


Title: Re: Billiards and Physics
Post by: Fu-Kwun Hwang on December 24, 2010, 09:54:42 pm
´╝ędefined movingballs(int xd,int yd,int sizei)
and add the following code inside
Code:
x=Math.random()*xd;
y=Math.random()*yd;

That is the reason why x,y are created randomly.

You can define something like 
movingballs(int xd,int yd,int xi,int yi,int sizei)
if you need to assign it to a new position (xi,yi)
and change the above code to
Code:
x=xi;
y=yi;

Then you can initialize new particle with new xi,yi value set.


Title: Re: Billiards and Physics
Post by: koclup1580 on December 29, 2012, 01:22:40 pm
oh good.