Saturday, 9 July 2011

Model realistic movement that is subject to friction

/*
Model realistic movement that is subject to friction
and therefore non-uniform acceleration/deceleration.
*/

float velocity = 50.0; //added to y coordinate
float friction = 0.99;

/*velocity is multiplied by friction, because friction
is less than 1, friction decreases the velocity with each frame
so the 'ball' slows down, until we re-boost it with a mouse press */

float y =0;

void setup() {
size(400, 400);
}



void draw() {
background(255);

fill(255, 0, 0);
ellipse(55, y, 45, 45);
velocity*=friction; //this decreases velocity with every frame.

y +=velocity;//add this to y;
//check for edges:
if ((y>height) ||(y<0)) {
velocity=-velocity;
}
println(velocity);
}

//reboost the 'ball'
void mousePressed() {
velocity =10;
}

No comments:

Post a Comment