xxxxxxxxxx
// Gravity.
// bam:9904
String title= "Gravity";
String author= "B.A.Martin";
String sub= "Press space-bar to advance time";
String subsub= "(or press keys 1, 2, 3, etc.)";
String hint= "'q' to quit\n'r' to reset\n's' to show answers";
// Time, distance, velocity, acceleration
int time=0;
float d=0, v=0, a=10, v0=0, vf=0, vavg=0, dlast=0;
float xo=135, xt=150, xv=200, xvavg=250, xd=300, xdrop=350;
// Building size
float xb=20, wide=100, tall=850, roof=60, bottom=roof+tall;
boolean hide=true;
color RED=color(255,0,0), GREEN=color(0,255,0), BLUE=color(0,0,255);
void setup() {
size( 600, 1000 );
}
void draw() {
scene();
titles();
action();
}
void action() {
// Labels above
fill(BLUE);
textSize(10);
text( "time", xt, roof-12 );
text( "velocity", xv, roof-12 );
text( "v-avg", xvavg, roof-12 );
text( "distance=\n vavg*t", xd, roof-12 );
text( "drop=\n d(t)-d(t-1)", xdrop, roof-12 );
// Show values for each second of falling
if (time==0) { show(0); return; }
dlast=0;
line( xb+wide+5, roof, xb+wide+280, roof );
for (int t=1; t<=time; t++ ) {
show( t );
}
}
// Display object and values at time t //
void show( float t ) {
vf= a * t;
vavg= (v0+vf) / 2;
d= vavg * t;
float y= roof + d +12;
//
fill(RED);
ellipse( xo, y-6, 8,8 ); // Red ball.
stroke(RED);
float speed= 5 * vf/10;
line( xo-3, y-6, xo, y-6-speed );
line( xo, y-6, xo, y-6-speed );
line( xo+3, y-6, xo, y-6-speed );
//
stroke(0);
fill(0);
line( xb+wide+5, y-12, xb+wide+260, y-12 );
fill(0);
textSize(12);
text( ""+(int)t, xt, y );
if (hide) {
text( "__ m/s", xv, y );
text( "__ m/s", xvavg, y );
text( "___ m", xd, y );
text( "__ m", xdrop, y );
} else {
text( ""+(int)vf, xv, y );
text( ""+(int)vavg, xvavg, y );
text( ""+(int)d, xd, y );
text( ""+(int)(d-dlast), xdrop, y );
}
dlast= d;
}
void scene() {
background( 200, 220, 255 );
fill(200,100,100);
rect( xb, roof, wide, tall ); // Building
stroke(0,150,0);
line( xb, bottom, width, bottom );
stroke(0);
man( xb+wide-20, roof+30 ); // Man on/under roof
}
void man( float x, float y ) {
float w=16, h=24, legs=12; // width of body
fill( 100, 255, 255 );
// +++ make legs longer!
rect( x, y, 4, -legs ); // legs
rect( x+w-4, y, 4, -legs );
rect( x, y-legs, w, -h ); // body
rect( x+w, y-h-legs, 14, 3 ); // arms
rect( x-3, y-h-legs, 3, 20 );
fill( 255,200,200 );
ellipse( x+w+18, y-h-legs+4, 8,4 ); // hand
fill( 255,200,200 ); // (pink)
float xx=x+w/2, yy=y-40;
ellipse( xx, yy, 14, 16 ); // head
fill( 255,100,100 ); // (pink)
// Expanding mouth!
float mouth= time/2;
fill(255, 0,255);
ellipse( xx,yy+4, mouth*2,mouth ); // mouth
//
noStroke();
ellipse( xx, yy, 2, 4 ); // nose
fill(255);
ellipse( xx-4, yy-3, 4, 4 ); // eyes
ellipse( xx+4, yy-3, 4, 4 );
fill(0,0,255);
ellipse( xx-4, yy-3, 2, 2 ); // pupils
ellipse( xx+4, yy-3, 2, 2 );
fill(255);
stroke(0);
}
void titles() {
fill(0);
textSize(24);
text( title, 10, 24 );
textSize(12);
text( sub, xt, 12 );
fill( 150 );
text( subsub, xt, 24 );
text( hint, width*3/4, 12 );
text( author, 20, height-20 );
report();
}
void report() {
int xr=width*3/4, y=120, n=0, h=15;
man( xr, roof+30); // Also show man.
fill(0,0,255);
// Report
text( "time: "+time, xr, y+h*n++ ); // Next line.
text( "distance: "+d, xr, y+h*n++ );
text( "velocity: "+vf, xr, y+h*n++ );
text( "acceleration: "+a, xr, y+h*n++ );
}
void keyPressed() {
if (key == 'q') exit();
else if (key == 'r') time=0; // reset.
else if (key == 's') hide= ! hide; // show/hide.
else if (key == ' ') ++time;
else if (key >= '0' && key <= '9') time= key-'0';
}