// car accidents // Written June 2006 by Jarek Rossignac import krister.Ess.*; import java.util.ArrayList; float t=0, t0=0, d=2000.0; // time measured in miliseconds, aniamtion start time, duration boolean run = false; color red=color(256,100,100), green=color(100,256,100), blue=color(100,100,256), yellow=color(256,230,100), grey=color(100,100,100); int nr=6; car C [] = new car [nr]; int ns=6; street S [] = new street [ns]; vec I=new vec (1,0); //=============================== AudioChannel myChannel; int mode = -1; int playback_mode = 0; int selectedCar = -1; int step = 0; CubicBezier pathCurve; pt draggedCP = null; int cam = 2 * nr; ArrayList collisions = new ArrayList(); //=============================== void setup() { Ess.start(this); myChannel = new AudioChannel("crash.mp3"); framerate(40); size(1000, 800, P3D); createCars (); createStreets(); //loadAccident(1); } void createCars () { C[0] = new car(760,300,2*PI/3,red); C[1] = new car(690,520,3*PI/2,green); C[2] = new car(780,500,3*PI/2,blue); C[3] = new car(450,310,0,grey); C[4] = new car(940,220,PI,grey); C[5] = new car(600,60,PI/2,grey); } void createStreets () { S[0] = new street(650,50,650,750,90); S[1] = new street(740,50,740,750,90); S[2] = new street(50,270,950,270,90); S[3] = new street(200,50,200,750,90); S[4] = new street(310,620,440,450,70); S[5] = new street(290,610,460,720,70); } public void stop() { Ess.stop(); super.stop(); } void draw() { //=============================== if(run) checkForCollisions(); if(cam == 2 * nr) camera(); else if(cam % 2 == 0) { int cameraCar = cam / 2; pt lookFrom = C[cameraCar].F.make(); pt lookAt = lookFrom.make(); lookAt.x += 100; lookAt.turnAround(lookFrom, -(C[cameraCar].a)); camera(lookFrom.x, lookFrom.y, 2 * C[cameraCar].h / 3, lookAt.x, lookAt.y, 2 * C[cameraCar].h / 3, 0, 0, -1); } else { int cameraCar = cam / 2; pt lookFrom = C[cameraCar].F.make(); pt lookAt = lookFrom.make(); lookAt.x += 100; lookAt.turnAround(lookFrom, -(C[cameraCar].a)); vec behind = lookAt.vecTo(lookFrom); behind.unit(); behind.mul(2 * C[cameraCar].w); lookFrom.addVec(behind); camera(lookFrom.x, lookFrom.y, C[cameraCar].w, lookAt.x, lookAt.y, 2 * C[cameraCar].h / 3, 0, 0, -1); } lights(); //=============================== background (255); rectMode(CENTER); for (int i=0; id) { t=d; }; } // time during animation else { t = 0; }; for (int i=0; i 0) { println("yo"); fill(255, 255, 255, 127); noStroke(); ellipse(c.position.x, c.position.y, 2 * c.radius, 2 * c.radius); c.radius -= 10; } } //=============================== } void mousePressed() { if(mode == -1) return; if (mouseButton == LEFT) { //=============================== //Added so we cannot move cars or streets while we're defining a path if(selectedCar > -1) return; //=============================== boolean found=false; //=============================== for(int i = nr - 1; i >= 0; i--) { if(C[i].path[mode] != null) { // p1 for ith car vec distanceVec = C[i].path[mode].p1.vecTo(mouse()); if(distanceVec.norm() <= 3) { println("CP 1 of path of car " + i + " has been selected"); found = true; draggedCP = C[i].path[mode].p1; } // p2 for ith car distanceVec = C[i].path[mode].p2.vecTo(mouse()); if(distanceVec.norm() <= 3) { println("CP 2 of path of car " + i + " has been selected"); found = true; draggedCP = C[i].path[mode].p2; } // p3 for ith car distanceVec = C[i].path[mode].p3.vecTo(mouse()); if(distanceVec.norm() <= 3) { println("CP 3 of path of car " + i + " has been selected"); found = true; draggedCP = C[i].path[mode].p3; } } } //=============================== for (int i=nr-1; i>=0; i--) { if (!found) { if(C[i].selected()) { found=true; }; }; }; for (int i=ns-1; i>=0; i--) { if (!found) { if(S[i].selected()) { found=true; }; }; }; }//if (mouseButton == LEFT) else { if(selectedCar > -1) // there is a car selected for path design { switch(step) { case 1: //Special case: the second control point determines the tangent on the first endpoint //I will make it so that the position of the mouse simply specifies the distance from //the car along the long axis of the car vec carVec = C[selectedCar].B.vecTo(C[selectedCar].F); carVec.unit(); carVec.mul(dot(pathCurve.p0.vecTo(mouse()), carVec)); pt p1 = C[selectedCar].F.make(); p1.addVec(carVec); pathCurve.p1.setTo(p1); println("Point 1: (" + pathCurve.p1.x + ", " + pathCurve.p1.y + ")"); step++; break; case 2: pathCurve.p2.setTo(mouse()); println("Point 2: (" + pathCurve.p2.x + ", " + pathCurve.p2.y + ")"); step++; break; case 3: pathCurve.p3.setTo(mouse()); println("Point 3: (" + pathCurve.p3.x + ", " + pathCurve.p3.y + ")"); C[selectedCar].path[mode] = pathCurve.make(); println("Car " + selectedCar + "'s path has been finished."); step = 0; selectedCar = -1; break; default: println("ERROR 1"); break; } }//if(selectedCar > 0) else // there are no selected cars { for (int i=nr-1; i>=0; i--) { if(C[i].selected2()) { if(C[i].path[mode] != null) return; selectedCar = i; println("Car " + selectedCar + " has been selected for path design."); pathCurve = new CubicBezier(); pathCurve.p0.setTo(C[selectedCar].F); println("Point 0: (" + pathCurve.p0.x + ", " + pathCurve.p0.y + ")"); step = 1; break; } } }//else }//else } void mouseDragged() { if(mode == -1) return; if(draggedCP != null) { draggedCP.setTo(mouse()); return; } for (int i=0; i