Science and Computers I -- PHY307/607

Lab 14. - Billiard Balls and Java

READ THIS FIRST In this lab you will play with a Java applet illustrating the motion of billiard balls. First we consider just one ball - while for a general shape of table this motion is chaotic we will see that for the square table it is not - the motion is regular and predictable. However this is no longer the case when we add a second ball ...

The code consists of 3 classes - a Ball class, a Simulation class and the New_Box class. The latter is responsible for initializing the applet, creating instances of the other classes and painting the screen. The Ball class encapsulates all the properties of a ball - its size and color. It also possesses a method draw() that is called by paint() which draws it to the screen. The Simulation class runs the simulation. Its class extends Runnable. This means that a separate Thread of execution is attached to this class which can be stopped and started in response to button clicks.

  1. Create a directory New_Box under your PHY307 directory. Download the source Java codes from the PHY307 lab page to this directory. Compile them and set their permissions correctly.
  2. Create a file New_Box.html from which to launch the applet in New_Box.class. In the applet tag specify a width of 250 and a height of 250. Set the permissions correctly for this file. Also, make sure you have the right permissions for the entire New_Box directory.
  3. Point Netscape at this new page. You should see a red ball of a certain size placed at a random point on the (white) table top. When you hit Go the ball moves in a random direction with a random speed. When it hits the sides it undergoes a perfectly elastic collision and bounces off. You should see that the motion of the ball repeats after some time - it is not chaotic.
  4. Try adding another ball -- first figure out where (in New_Box.java) a ball is explicitly created (its called ball1 there). Do something similar to create another ball - ball2. Make it blue and of size 20 pixels. Remember you must also define ball2 to be of type Ball near the top of the code in New_Box.java. Also, add code to explicitly draw ball2 to the screen (the draw() method).
  5. Next, figure out where (in Simulation.java) the ball's position is updated and put in some similar lines of code to update the position and velocity of ball2. You will need to change the parameter M from 4 to 8. Notice that the (x,y) positions and x,y components of ball2's speed have to be assigned to the array var[] in a way analogous to those of ball1 eg.
    
    var[4]=app.ball2.x;
    var[5]=app.ball2.vx;
    .....etc
    
    Also, you need to put code in to handle the collisions with the walls and to update the values of the position and speed with their new values given in the array new_var[]). Just mimic everything that is done to ball1 with similar code for ball2.
  6. Finally, locate the method f() in this class and inside the body of this method add four more lines
    
    dy[4]=y[5];
    dy[5]=0.0;
    dy[6]=y[7];
    dy[7]=0.0;
    
  7. Recompile and reload the applet. Does it work (i.e do you see two balls, of different sizes and colors bouncing around the screen?). What is unrealistic about this new simulation ? Is it actually chaotic ?
  8. To address these problems we need to add in a force between the two balls which stops them landing on top of each other (and ultimately will lead to a nonlinear interaction which allows for chaos). Inside Simulation.java add a new version of the method f() (delete the old one) which computes the forces on the balls. Add the following code
    
    private void f(double x, double y[], double dy[]){
    double dum,d1,d2,MASS1,MASS2,POWER=7.0,scale;
    MASS1=1.0;
    MASS2=4.0;
    
    dum=Math.sqrt((y[0]-y[4])*(y[0]-y[4])+(y[2]-y[6])*(y[2]-y[6]));
    scale=0.2;
    dum=dum/scale;
    if(dum<0.33) dum=0.33;
    d1=Math.pow(dum,POWER)*scale;
    
    dy[0]=y[1];
    dy[1]=(1.0/MASS1)*(y[0]-y[4])*(1/d1);
    dy[2]=y[3];
    dy[3]=(1.0/MASS1)*(y[2]-y[6])*(1/d1);
    dy[4]=y[5];
    dy[5]=(1.0/MASS2)*(y[4]-y[0])*(1/d1);
    dy[6]=y[7];
    dy[7]=(1.0/MASS2)*(y[6]-y[2])*(1/d1);
    
    return;
    }
    
    There is now a force of repulsion between the two balls that falls off rapidly with distance between them. Recompile and reload. You should now see the motion of either of the two balls is chaotic. Notice that on average the small ball is moving faster than the larger one - this ensures that the mean kinetic energy of the two balls is about the same - as required by the kinetic theory of gases !
    To get credit for this lab you need to
    1. Email me when it works !


    Back to the PHY307 Homepage

    This page maintained by Simon Catterall, last updated 7 December, 2000.