xxxxxxxxxx
// Ballistic.
// bam:9906
String title= "Ballistic";
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; 'r' to reset\n's' to show answers";
// Time, distance, velocity, acceleration
int time=0;
float d=0, v=100, a=10, v0=100, vf=0, vavg=0, dlast=0;
float xo=35, xt=150, xv=200, xvavg=250, xd=300, xdrop=350;
//
//-- float xb=20, wide=100, tall=850, roof=60, bottom=roof+tall;
float xb=20, wide=100, tall=850, roof=60, bottom=600;
boolean hide=false, pause=false;
color RED=color(255,0,0), GREEN=color(0,255,0), BLUE=color(0,0,255);
color YELLOW=color(255,255,0), CYAN=color(0,255,255), MAGENTA=color(255,0,255);
color BROWN=color(127,0,0);
void setup() {
size( 600, 650 );
}
void draw() {
scene();
titles();
action();
pause= true; // Wait for next event.
}
void action() {
dlast=0;
// Labels above & below
labels( roof );
labels( bottom+20 );
// Show values for each second of rising/falling
line( xb+wide+5, bottom-d, xb+wide+280, bottom-d );
for (int t=0; t<=time; t++ ) {
show( t );
}
}
void labels( float yy ) {
fill(BLUE);
textSize(10);
text( "time", xt, yy );
text( "velocity", xv, yy );
text( "v-avg", xvavg, yy );
text( "distance=\n vavg*t", xd, yy );
text( "drop=\n d(t)-d(t-1)", xdrop, yy );
}
// Display object and values at time t //
void show( float t ) {
vf= v0 - a * t;
vavg= (v0+vf) / 2;
d= vavg * t;
//-- float y= roof + d +12;
float y= bottom - d;
float xx= vf>0 ? 0 : 12; // Right shifted after apogee
float xxx= vf>0 ? xo+t*3 : xo+t*3 + 10;
float yy= vf>0 ? y : y+12; // above or below line.
float speed= -5 * vf/10; // speed indicator lines
//
stroke(BLUE);
//-- line( xb+wide+25, y, xb+wide+260, y );
line( xxx+20, y, xxx+260, y );
if (vf>0) {
//-- text( ""+(-speed)+" m/s", xo-100, y-6 );
stroke(RED);
fill(YELLOW); // Yellow rocket.
triangle( xxx-6, y, xxx, y+speed, xxx+6, y );
fill(BLUE);
} else {
//-- text( ""+(-speed)+" m/s", xo-50, y+6 );
noStroke();
fill( 200-vf/20,0,0, 200-vf/3); // Brown (transparent) debris.
ellipse( xxx,y, 10-vf/5, 10-vf/20 );
stroke(BROWN);
fill(RED);
}
if (abs(vf) < 0.5) { text( "APOGEE!", xo, y-12 ); }
if (vf<0 && y>=bottom) { text( "SPLASHDOWN.", xo, bottom-6 ); }
// Leading blast or trailing smoke
line( xxx, y, xxx-3, y-speed*3/4 );
line( xxx, y, xxx, y-speed*3/4 );
line( xxx, y, xxx+3, y-speed*3/4 );
//
values( t, xx, yy );
}
void values( float t, float xx, float y ) {
textSize(12);
text( ""+(int)t+"s", xx+xt, y );
if (hide) {
text( "__ m/s", xx+xv, y );
text( "__ m/s", xx+xvavg, y );
text( "___ m", xx+xd, y );
text( "__ m", xx+xdrop, y );
} else {
text( ""+(int)vf, xx+xv, y );
text( ""+(int)vavg, xx+xvavg, y );
text( ""+(int)d, xx+xd, y );
text( ""+(int)(d-dlast), xx+xdrop, y );
}
dlast= d;
}
void scene() {
background( 255, 225, 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 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( width*3/4, roof-12 );
}
void report( float x, float y ) {
//-- int xr=width*3/4, y=60, n=0, h=16;
//-- man( xr, roof+30); // Also show man.
int n=0, h=16;
fill(MAGENTA);
textSize( h );
text( "time: "+time, x, y+h*n++ ); // Next line.
text( "distance: "+d, x, y+h*n++ );
text( "velocity: "+vf, x, y+h*n++ );
text( "acceleration: "+a, x, 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';
pause=false;
}