// Example of motion control for first person driving // Written on Sept 30, 2005 by Jarek Rossigtnac for CS4451 import processing.opengl.*; // Camera control float fov=PI*0.5; color red = color(250,30,30); color green = color(30,200,30); color blue = color(20,20,170); color yellow = color(255,240,120); color brown = color(150,100,80); color lightgreen = color(100,250,100); color grey = color(150,150,150); float ha=0.0; float va=0.0; float a=0.0, x=-95.0, z=0.0; // current angles and positions float dp=0, odp=0, dpLimit=0.001; // forward velocity, new and old float da=0, oda=0, daLimit=0.00005; // change of angle, new and old float na; float nx; float nz; // copies for tracks boolean nonLinear=true; boolean showTracks=true; boolean dynamic=true; void setup() { size(600, 600, OPENGL); sphereDetail(6); stroke(200); perspective(PI/3.0,1.0,0.1,1000.0); println("initial matrix"); printMatrix(); } void draw() { background(150,180,255); lights(); pushMatrix(); translate(width/2,height/2,519.6152); scale(1.0,-1.0,-1.0); if (mousePressed) { dp= (height-mouseY)/float(height); da= (mouseX-width/2)/float(width)/50.0; if (nonLinear) { dp*=(height-mouseY)/float(height); da*=sq((mouseX-width/2)/float(width));}; if (dynamic) { if (abs(dp-odp)>dpLimit) {dp=odp+(dp-odp)*dpLimit/abs(dp-odp);}; if (abs(da-oda)>daLimit) {da=oda+(da-oda)*daLimit/abs(da-oda);};}; odp=dp; oda=da; }; a-=da; x-=sin(a)*dp; z+=cos(a)*dp; // FIRST PERSON VIEW pushMatrix(); rotateY(a); translate(-x,0,-z); render(); trees(); if (showTracks) {tracks();} popMatrix(); // TOP VIEW pushMatrix(); translate(-width/2+width/5,height/2-height/5,400.0); rotateX(-PI/2); scale(0.2); render(); translate(x,2,z); fill(blue); sphere(10); popMatrix(); popMatrix(); } void keyPressed() { switch (key) { case 't': showTracks= !showTracks; break; case 'n': nonLinear= !nonLinear; break; case 'd': dynamic= !dynamic; break; case 'h': dp=0; da=0; a=0.0; x=-95.0; z=0.0; break; }; }; void render(){ stroke(200); pushMatrix(); translate(0,-10,0); fill(brown); box(220,9.8,220); fill(yellow); box(200,10,200); fill(brown); box(190,10.1,190); fill(red); box(180,12,180); popMatrix(); }; void trees() { noStroke(); pushMatrix(); scale(1,4,1); fill(green); pushMatrix(); translate(-110,0,-107); for (int i=0; i<5; i++) {translate(40,0,0); sphere(2);}; popMatrix(); pushMatrix(); translate(-110,0,107); for (int i=0; i<5; i++) {translate(40,0,0); sphere(2);}; popMatrix(); fill(lightgreen); pushMatrix(); translate(-107,0,-110); for (int i=0; i<5; i++) {translate(0,0,40); sphere(2);}; popMatrix(); pushMatrix(); translate(107,0,-110); for (int i=0; i<5; i++) {translate(0,0,40); sphere(2);}; popMatrix(); popMatrix(); } void tracks(){ noStroke(); na=a; nx=x; nz=z; fill(blue); for (int j=0; j<10; j++) { for (int i=0; i<30; i++) {na-=da; nx-=sin(na)*dp; nz+=cos(na)*dp;}; pushMatrix(); translate(nx,-4.8,nz); sphere(0.10); popMatrix(); }; };