Bouncy Ball

float dx=10;  // Inital x position
float dy=10; // Inital y position
float vx=10;  // Inital x velocity
float vy=0; //Inital v velocity, remember negative y is up
float t=.1;  // Time steps
float g=9.8; // Acceleration due to gravity, where each pixel = 1 meter
float vyf=0; // Final Velocity
float r=.85; // Coeficient of Restitution (Bounciness)

void setup()
{
  size(700, 400);
  smooth();
  background(100);
  frameRate (30/t);
}

void draw ()
{
  background(100);

  ellipse (dx, dy, 10, 10);
  vyf=vy+g*t;
  dy=dy+vy*t+.5*(vyf-vy)*t;
  vy=vyf;
  dx=dx+vx*t;
  if (dy>(height-5))
  {
    dy=height-5;
    vy=vy*(-r);
  }
  if (dy<5)
  {
    dy=5;
    vy=vy*(-r);
  }

  if (dx>(width-5))
  {
    dx=width-5;
    vx=vx*(-r);
  }
  if (dx<5)
  {
    dx=5;
    vx=vx*(-r);
  }
}

Comments