//Vehicle 2 //Fear and Agression, Coward and Agressive class Vehicle2{ int c_dia; // vehicle diameter color body_c; // color color cp, lp, rp; // current, left, and right pixel colors float left_sensor, right_sensor; // sensor intensities float redirect; // ratio of intensity between the two sensors String attitude; float max_speed = 3.0; float reaction = 0.1; // controls the acceleration, or reaction speed, of current locations temperature Vector2D acc; Vector2D vel; Vector2D loc; Vector2D sensor; Vector2D temp; Vector2D l = new Vector2D(random(.1*width, .9*width), random(.1*height, .9*height)); Vector2D v = new Vector2D(random(-max_speed,max_speed), random(-max_speed,max_speed)); // MUST BE GREATER THAN 1! Vector2D a = new Vector2D(0.0, 0.0); // CONSTRUCTOR Vehicle2(String att) { acc = a; vel = v; loc = l; attitude = att; c_dia = int((6*max_speed)/vel.magnitude()); } // main function to operate object void go() { update(); borders(); render(); } void update(){ if(abs(vel.magnitude()) < .01){ vel.setX(.000000001); vel.setY(.000000001); //println("dead"); } else{ // left sensor rp = get(int(loc.x() + .5*c_dia*vel.x()/vel.magnitude() + .25*c_dia*vel.y()/vel.magnitude()),int(loc.y() + .5*c_dia*vel.y()/vel.magnitude() - .25*c_dia*vel.x()/vel.magnitude())); right_sensor = brightness(rp); // right sensor lp = get(int(loc.x() + .5*c_dia*vel.x()/vel.magnitude() - .25*c_dia*vel.y()/vel.magnitude()),int(loc.y() + .5*c_dia*vel.y()/vel.magnitude() + .25*c_dia*vel.x()/vel.magnitude())); left_sensor = brightness(lp); if(attitude == "coward"){ redirect = ((right_sensor - left_sensor) / 255); body_c = #FFFF00; } else { redirect = ((left_sensor - right_sensor) / 255); body_c = #FF0000; } sensor = new Vector2D(-20*redirect*vel.y()/vel.magnitude(), 20*redirect*vel.x()/vel.magnitude()); cp = get(int(loc.x()), int(loc.y())); temp = new Vector2D((1 - brightness(cp) / 128) * reaction * -vel.x(), (1 - brightness(cp) / 128) * reaction * -vel.y()); vel.add(sensor); // adds in turning sensors acc.mult(0); // re-initialize acceleration vector acc.add(temp); // add speed sensor to acceleration vel.add(acc); // add acceleration to velocity loc.add(vel); // add velocity to location } } void borders(){ // Reverse the direction of the vehicle if it encounters a wall //println("(" + loc.x() + ", " +loc.y() + ")"); if (loc.x() > width - c_dia/2) vel.reverseX(); if (loc.x() < 0 + c_dia/2) vel.reverseX(); if (loc.y() > height - c_dia/2) vel.reverseY(); if (loc.y() < 0 + c_dia/2) vel.reverseY(); } void render(){ //if(mousePressed){ // sensor fields noStroke(); fill(0, 0, left_sensor, left_sensor); // LEFT arc(loc.x(), loc.y(), 4 * c_dia, 4 * c_dia, TWO_PI - PI/2 + atan2(vel.y(),vel.x()), TWO_PI + atan2(vel.y(),vel.x())); fill(right_sensor, 0, 0, right_sensor); // RIGHT arc(loc.x(), loc.y(), 4 * c_dia, 4 * c_dia, TWO_PI + atan2(vel.y(),vel.x()), PI/2 + atan2(vel.y(),vel.x())); // Left sensor point stroke(#FFFFFF); point(int(loc.x() + .5*c_dia*vel.x()/vel.magnitude() + .25*c_dia*vel.y()/vel.magnitude()),int(loc.y() + .5*c_dia*vel.y()/vel.magnitude() - .25*c_dia*vel.x()/vel.magnitude())); // Right sensor point point(int(loc.x() + .5*c_dia*vel.x()/vel.magnitude() - .25*c_dia*vel.y()/vel.magnitude()),int(loc.y() + .5*c_dia*vel.y()/vel.magnitude() + .25*c_dia*vel.x()/vel.magnitude())); //} // Body fill(body_c); stroke(#FFFFFF); ellipseMode(CENTER); ellipse(loc.x(), loc.y(), c_dia, c_dia); } }