Processing Program-Gravity with Objects

Ball ball1;
Ball ball2;

float accel = 0.1;//acceleration - only global variable

void setup() {
  size(800,800);
  ball1 = new Ball(50,0,3,16);
  ball2 = new Ball(100,50,5,32);
}

void draw() {
  background(100);
  ball1.display();
  ball2.display();
  ball1.reflect();
  ball2.reflect();
}

class Ball {
  float x;//horizontal position of ball
  float y;//vertical position of ball
  float xspeed;//horizontal speed of ball
  float yspeed;//vertical position of ball
  float w; //size of ball

  Ball(float tempx, float tempy, float tempxspeed, float tempw) {
    x = tempx;
    y = tempy;
    xspeed = tempxspeed;
    w = tempw;
    yspeed = 0;//initial yspeed always zero, not required as parameter
  }
  
  void display() {
    ellipse(x,y,w,w);
    x = x + xspeed;
    y = y + yspeed;
    yspeed = yspeed + accel;
  }
  
  void reflect() {
    if (y > height) {
      yspeed = yspeed * -0.95;
    }
    if (x < 0 || x > width) {
      xspeed = xspeed * -1.0;
    }
  }
}
ċ
GRAVITY_W_OBJECTS.pde
(1k)
Leah Segal,
Dec 1, 2013, 2:05 PM
Comments