Lesson8

Post Reply
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Lesson8

Post by admin »

Create the project BouncingBallApp.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

  1. package bouncingballapp;
  2. import org.opensourcephysics.controls.*;
  3. import org.opensourcephysics.frames.*;
  4. public class BouncingBallApp extends AbstractSimulation {
  5. // declares and instantiates a window to draw balls
  6. DisplayFrame frame = new DisplayFrame("x", "y", "Bouncing Balls");
  7. BouncingBall[] ball; // declares an array of BouncingBall objects
  8. double time, dt;
  9. public void initialize() {
  10. // sets boundaries of window in world coordinates
  11. frame.setPreferredMinMax(-10.0, 10.0, 0, 10);
  12. time = 0;
  13. frame.clearDrawables(); // removes old particles
  14. int n = control.getInt("number of balls");
  15. int v = control.getInt("speed");
  16. ball = new BouncingBall[n]; // instantiates array of n BouncingBall objects
  17. for(int i = 0;i<n;i++) {
  18. double theta = Math.PI*Math.random(); // random angle
  19. //instantiates the ith BouncingBall object
  20. ball = new BouncingBall(0, v*Math.cos(theta), 0, v*Math.sin(theta));
  21. frame.addDrawable(ball); //adds ball to frame so that it will be displayed
  22. }
  23. // decimalFormat instantiated in superclass and used to format numbers conveniently
  24. frame.setMessage("t = "+decimalFormat.format(time)); // appears in lower right hand corner
  25. }
  26. public void doStep() { // invoked every 1/10 second by timer in AbstractSimulation superclass
  27. for(int i = 0;i<ball.length;i++) {
  28. ball.step(dt);
  29. }
  30. time += dt;
  31. frame.setMessage("t="+decimalFormat.format(time));
  32. }
  33. public void startRunning() { // invoked when start or step button is pressed
  34. dt = control.getDouble("dt");
  35. } // gets time step
  36. public void reset() { // invoked when reset button is pressed
  37. control.setAdjustableValue("dt", 0.1); // allows dt to be changed after initializaton
  38. control.setValue("number of balls", 40);
  39. control.setValue("speed", 10);
  40. }
  41. public static void main(String[] args) { // sets up animation control structure using this class
  42. SimulationControl.createApp(new BouncingBallApp());
  43. }
  44. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

Code: Select all

package bouncingballapp;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.frames.*;
public class BouncingBallApp extends AbstractSimulation {
  // declares and instantiates a window to draw balls
  DisplayFrame frame = new DisplayFrame("x", "y", "Bouncing Balls");
  BouncingBall[] ball; // declares an array of BouncingBall objects
  double time, dt;
  public void initialize() {
    // sets boundaries of window in world coordinates
    frame.setPreferredMinMax(-10.0, 10.0, 0, 10);
    time = 0;
    frame.clearDrawables(); // removes old particles
    int n = control.getInt("number of balls");
    int v = control.getInt("speed");
    ball = new BouncingBall[n]; // instantiates array of n BouncingBall objects
    for(int i = 0;i<n;i++) {
      double theta = Math.PI*Math.random(); // random angle
      // instantiates the ith BouncingBall object
      ball[i] = new BouncingBall(0, v*Math.cos(theta), 0, v*Math.sin(theta));
      frame.addDrawable(ball[i]);           // adds ball to frame so that it will be displayed
    }
    // decimalFormat instantiated in superclass and used to format numbers conveniently
    frame.setMessage("t = "+decimalFormat.format(time)); // appears in lower right hand corner
  }
  public void doStep() { // invoked every 1/10 second by timer in AbstractSimulation superclass
    for(int i = 0;i<ball.length;i++) {
      ball[i].step(dt);
    }
    time += dt;
    frame.setMessage("t="+decimalFormat.format(time));
  }
  public void startRunning() { // invoked when start or step button is pressed
    dt = control.getDouble("dt");
  } // gets time step
  public void reset() { // invoked when reset button is pressed
    control.setAdjustableValue("dt", 0.1); // allows dt to be changed after initializaton
    control.setValue("number of balls", 40);
    control.setValue("speed", 10);
  }
  public static void main(String[] args) { // sets up animation control structure using this class
    SimulationControl.createApp(new BouncingBallApp());
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

Add the new file BouncingBall (java application) to the project.
Add osp.jar to the project.
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

  1. package bouncingballapp;
  2. import org.opensourcephysics.display.Circle;
  3. public class BouncingBall extends Circle { // Circle is a class that can draw itself
  4. final static double g = 9.8; // constant
  5. final static double WALL = 10;
  6. private double x, y, vx, vy; // initial position and velocity
  7. public BouncingBall(double x, double vx, double y, double vy) { // constructor
  8. this.x = x; // sets instance value equal to passed value
  9. this.vx = vx; // sets instance value equal to passed value
  10. this.y = y;
  11. this.vy = vy;
  12. setXY(x, y); // sets the position using setXY in Circle superclass
  13. }
  14. public void step(double dt) {
  15. x = x+vx*dt; // Euler algorithm for numerical solution
  16. y = y+vy*dt;
  17. vy = vy-g*dt;
  18. if(x>WALL) {
  19. vx = -Math.abs(vx); // bounce off right wall
  20. } else if(x<-WALL) {
  21. vx = Math.abs(vx); // bounce off left wall
  22. }
  23. if(y<0) {
  24. vy = Math.abs(vy); // bounce off floor
  25. }
  26. setXY(x, y);
  27. }
  28. }
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

Code: Select all

package bouncingballapp;
import org.opensourcephysics.display.Circle;
public class BouncingBall extends Circle { // Circle is a class that can draw itself
  final static double g = 9.8; // constant
  final static double WALL = 10;
  private double x, y, vx, vy; // initial position and velocity
  public BouncingBall(double x, double vx, double y, double vy) { // constructor
    this.x = x;   // sets instance value equal to passed value
    this.vx = vx; // sets instance value equal to passed value
    this.y = y;
    this.vy = vy;
    setXY(x, y); // sets the position using setXY in Circle superclass
  }
  public void step(double dt) {
    x = x+vx*dt; // Euler algorithm for numerical solution
    y = y+vy*dt;
    vy = vy-g*dt;
    if(x>WALL) {
      vx = -Math.abs(vx); // bounce off right wall
    } else if(x<-WALL) {
      vx = Math.abs(vx);  // bounce off left wall
    }
    if(y<0) {
      vy = Math.abs(vy); // bounce off floor
    }
    setXY(x, y);
  }
}
admin
Site Admin
Posts: 3118
Joined: Wed Dec 11, 2019 8:31 am
Has thanked: 4 times

Re: Lesson8

Post by admin »

The screenshot of BouncingBallApp program.
Attachments
BouncingBallApp.png
BouncingBallApp.png (19.64 KiB) Viewed 579 times
Post Reply