效果演示:

Processing 代码:

// simple emulation of gravity

float x;
float y;    // y(t)
float speed = 0;
float g = 0.1;  // gravity

void setup(){
  size(640, 480);
  x = width/2;
  y = height/2;  // start from half height
}

void draw(){
  // draw the ball
  background(255);
  fill(0);
  noStroke();
  ellipse(x,y,10,10);
  
  // move and accelerate
  y += speed;
  speed += g;
  
  // bounce back up
  if(y>height){
    speed *= -0.95;  // add resistance to each bounce
    y = height;      // make sure that the ball can bounce back up
  }
}