Rewrite the Example 5-9 (Gravity) using objects. Include two instances of a “Ball” (or whatever you call it) object.

Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 8-5: Rewrite the gravity example from Chapter 5 using objects with a Ball class. 
// Include two instances of a Ball object.

// Two ball objects
Ball ball1;
Ball ball2;

// Global gravity variable
float gravity = 0.1;  

void setup() { 
  size(200,200); 
  smooth();
  
  // Create ball objects
  ball1 = new Ball(50,0,16);
  ball2 = new Ball(100,50,32);
} 

void draw() { 
  background(255); 
  
  // Display ball objects
  ball1.display();
  ball2.display();

  // Move ball objects
  ball1.update();
  ball2.update();

} 
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Exercise 8-5: Rewrite the gravity example from Chapter 5 using objects with a Ball class. 
// Include two instances of a Ball object.

class Ball {

  float x;       // x location of square 
  float y;       // y location of square 
  float speed;   // speed of square 
  float w;       // size

  // Ball constuctor
  Ball(float tempX, float tempY, float tempW) {
    x = tempX;
    y = tempY;
    w = tempW;
    speed = 0;
  }


  void display() {
    // Display the square 
    fill(175); 
    stroke(0); 
    ellipse(x,y,w,w); 
  }

  void update() {
    // Add speed to location
    y = y + speed; 

    // Add gravity to speed
    speed = speed + gravity; 

    // If square reaches the bottom 
    // Reverse speed 
    if (y > height) { 
      speed = speed * -0.95;  
    } 
  }
}
  • Ulrich

    I took the liberty to improve the code by:

    if (y > height – w/2) {
    speed = speed * -0.95;
    }

    Hence, the balls bounce of the floor and not somewhere underneath it.

  • Kema

    When I type this in:

    // Two ball objects

    Ball ball1;

    Ball ball2;

    float gravity = 0.1;

    void setup() {

    size(200,200);

    smooth();

    //Create ball objects

    ball1 = new Ball(50,0,16);

    ball2 = new Ball(100,50,32);

    }

    void draw() {

    background(100);

    ball1.display();

    ball2.display();

    //Move ball objects

    ball1.update();

    ball2.update();

    }

    class Ball{

    float x=100; //x location of ball

    float y=0; //y location of ball

    float speed=0; //speed of ball

    float w; //size

    //Ball constructor

    Ball(float tempX, float tempY, float tempW){

    x=tempX;

    y=tempY;

    w=tempW;

    speed=0;

    }

    void display(){

    //Display the ball

    fill(175);

    stroke(0);

    ellipse(x,y,w,w);

    }

    void update(){

    // Add speed to location.

    y = y + speed;

    // Add gravity to speed.

    speed = speed + gravity;

    // If ball reaches the bottom

    // Reverse speed

    if (y > height) {

    // Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).

    // This is known as a “dampening” effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).

    speed = speed * -0.95;

    }

    }

    }

    It’s not working for me. Can someone please tell me what I’m doing wrong?