class MyVehicle { // field (properties) PVector pos; // position. PVector is a type which has three x, y, z properties float dir; // direction as angle (int from 0 - 360, while N is 0 acw pos) float speed; int colR, colG, colB; // 0-255 int raiseAlert = 0; // when other cars get closer than safe distance, raiseAlert is incremented int collisionTime =0; // tracks the number of frames collision occurs ArrayList trace; // a dynamic array to remember the previous positions of the vehicle // Constructor (way to initialize) MyVehicle(PVector p, float d, float s, int r, int g, int b) { pos = p; dir = d; speed = s; colR = r; colG = g; colB = b; trace = new ArrayList(); } // Methods void draw() { pushMatrix(); // saves the rotation/translation states of the world (think about this as a RESET POINT which you will invoke later) pushStyle(); // saves the attributes of the world translate(pos.x,pos.y,pos.z); // move to the vehicles coordinates fill(0); // fill color = 10 textFont(smallFont); // use font f (declared as global earlier) text("_"+raiseAlert,1,1,20,20); // text out rotateZ(dir); // rotate around positive Z axis (that is towards you, going out of the screen plane) fill (colR,colG,colB); // fill color = vehicle color //rectMode(CENTER); // rectMode(CENTER) draws the rectangle using a center point - see reference //rect(0,0,2,5); stroke(0); box(2, 5, 2); line(0,0,0,speed*3); // line length corresponds to speed fill(0,0,255,80); noStroke(); if (raiseAlert >0 ) sphere(10); //ellipse(0,0,raiseAlert*20,raiseAlert*20); //if there are other vehicles close by draw a circle popMatrix(); // invokes the saved state in pushMatrix again (think about this as a RESET) // draw the trace stroke(0,80); strokeWeight(3); noFill(); beginShape(); for(int i = 0; i < trace.size(); i++) { PVector v = (PVector) trace.get(i); vertex(v.x,v.y,v.z); } endShape(); popStyle(); // invoke saved attributes (RESET) raiseAlert = 0; // flush raiseAlert } void update( int w, int h, MyVehicle[] v, int ticks, MyImage terrain) { // if vehicle reaches an edge condition // change direction (reflection equation): dir = TWO_PI - dir if ((pos.x < 0) || (pos.x > w)) dir = TWO_PI - dir; if ((pos.y < 0) || (pos.y > h)) dir = PI - dir; // update the position pos.x = pos.x + (cos (PI/2 + dir)*speed/10); pos.y = pos.y + (sin (PI/2 + dir)*speed/10); pos.z = terrain.ht[min(max(int(pos.x),0),w-1)][min(max(int(pos.y),0),h-1)] + 2; colR = (int)terrain.getRVal(int(pos.x), int(pos.y)); colG = (int)terrain.getGVal(int(pos.x), int(pos.y)); colB = (int)terrain.getBVal(int(pos.x), int(pos.y)); // neighborhood evaluation -> look around for other cars for (int j = 0; j 0) && (distance < safeDistance)) { raiseAlert += 1; } } if (raiseAlert>0) {collisionTime++;} else {collisionTime=0;} if(collisionTime%7==1) { MyShape s = new MyShape(pos.x, pos.y, pos.z, dir, speed, colR, colG, colB, raiseAlert, collisionTime, trace); if(s!=null) myShapes.add(s); } //every 10th frame save position in trace if(ticks%10==0) trace.add(new PVector(pos.x, pos.y, pos.z)); //limit trace size to 40 if(trace.size() > 40) trace.remove(0); } }