/** * free_fall.pde * * This program demonstrates the acceleration of an object falling freely * from a fixed height and stopping when it hits the ground. * * created: 4-oct-2011/sklar * */ int d = 20; // diameter of object float a = 9.8; // accleration due to gravity PVector v; // velocity of object PVector p; // position of object float t; // time boolean running; // flag indicating that simulation is running /** * setup() */ void setup() { size( 800,600 ); v = new PVector( 0, 0, 0 ); p = new PVector( (width-d)/2, 0, 0 ); t = 0; ellipseMode( CORNER ); running = true; } // end of setup() /** * draw() */ void draw() { if ( running ) { // print table of values println( "time = " + t + " velocity = " + v + " position = (" + p.x + "," + p.y + ")" ); // clear screen background( #ffffff ); // draw object stroke( #333333 ); fill( #000066 ); ellipse( (int)p.x, (int)p.y, d, d ); // update velocity v.y += a / frameRate; // update position p.y += v.y; // update time (in secconds) t += 1 / frameRate; // are we done? if ( p.y >= height - d ) { running = false; restart(); } } } // end of draw() /** * keyPressed() */ void keyPressed() { if (( key == 'q' ) || ( key == 'Q' )) { exit(); } else if (( key == 'r' ) || ( key == 'R' )) { restart(); } else if (( key == 's' ) || ( key == 'S' )) { running = ! ( running ); } } // end of keyPressed() /* * restart() */ void restart() { v.set( 0, 0, 0 ); p.set( (width-d)/2, 0, 0 ); t = 0; } // end of restart()