I am trying to create a delay before the cars come back to their initial y position after they avoid the truck in this animation. Currently, I have the timer in the avoidance () method but it doesn't seem to work no matter where I put it. Also, my blue car behaves differently to the collision than the green one and I see now logical reason for this?
/* collision detection and avoidance mechanism*/
// Declare the object variables for this program
Vehicle myVehicle1;
Vehicle myVehicle2;
Truck bigRedFireTruck;
Timer timer;
void setup() {
size(400, 200);
myVehicle1 = new Vehicle(color(0, 255, 0), 0, 100, 20, 10, 2);
myVehicle2 = new Vehicle(color(0, 0, 255), 0, 10, 20, 10, 1);
bigRedFireTruck=new Truck (color (255, 0, 0), width, height/2, 30, 14, 8, 1.5, 4);
timer = new Timer(200);
timer.startTimer();
}
void draw() {
background(225);
myVehicle1.drive();
if (myVehicle1.collDetect(bigRedFireTruck)) {
print("collision");
myVehicle1.avoidance(bigRedFireTruck);
}
myVehicle1.display();
myVehicle2.drive();
if (myVehicle2.collDetect(bigRedFireTruck)) {
print("collision");
myVehicle2.avoidance(bigRedFireTruck);
}
myVehicle2.display();
bigRedFireTruck.drive();
bigRedFireTruck.display();
}
// code for the superclass called Vehicle
class Vehicle {
// all of the fields for the class called Vehicle: declare the properties for the class called Vehicle
color c;
float xpos;
float ypos;
float vwidth;
float vheight;
float xspeed;
float yspeed;
float ydirection;
float yReturn;
// constructor for the class called Vehicle
Vehicle(color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempXspeed) { // CFSR: create the constructor of the Vehicle class and declare its temporary variables
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
vwidth=tempvwidth;
vheight=tempvheight;
xspeed = tempXspeed;
yspeed=0.0;
yReturn=tempYpos;
ydirection=1;
}
// methods for the class called Vehicle
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
yspeed= 0.0;
ypos = yReturn;
ydirection=1;
ypos = ypos+yspeed*ydirection;
if (ypos <0-vheight) {
}
}
void display() {
stroke(1);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, vwidth, vheight);
}
// collision detection method for the vehicle class: function that returns true or false based on whether the bigredtruck collides with another vehicle
boolean collDetect(Truck vcollision) {
if (xpos-vwidth/2<=(vcollision.xpos+vcollision.truckTotalWidth) && xpos+vwidth/2 >= vcollision.xpos && ypos-vheight/2<=(vcollision.ypos+vcollision.vheight/2) && ypos+vheight/2 >= (vcollision.ypos-vcollision.vheight/2))
{
return true;
}
else {
return false;
}
} // end of the collision detection method for the vehicle class
// avoidance mechanism method for the vehicle class
void avoidance(Truck tavoidance) {
if (timer.endTimer()) {
ypos = yReturn;
timer.startTimer();
}
if (ypos-tavoidance.ypos>0) {
ydirection=ydirection*1;
}
else {
ydirection=ydirection*-1;
}
yspeed=20;
ypos = ypos+yspeed*ydirection;
} // end the avoidance mechanism function
} // end the block for the class called Vehicle
class Truck extends Vehicle {
// declare all of the properties for the subclass called Truck that are not properties of the superclass Vehicle
int strokeWeightTruck; // declare the variable that will be used to set a strokeweight for the objects of the truck class different than the strokeweight of the vehicle class
float truckCabinWidth; // declare the variable that will be used to determine the width of the cabin part of the truck
float spaceBtwCabinVan; // declare the variable that will be used to determine the space between the cabin and the van parts of the truck
float truckTotalWidth; // declare the variable that will be used to determine the total width of the cabin
float truckCabinHeight; // declare the variable that will be used to determine the height of the cabin part of the truck
float truckVanXCenter; // declare the variable that will be used to determine the middle of the van part of the truck to draw its rectangle in rect(CENTER) mode;
float angle; // declare the variable that will control the position of the sin wave for the truck object(s) to move
float truckMiddleYPos; // declare the variable that will determine the Y position from which the sin motion occurs when the truck object(s) will move
float truckYRangeOfMotion; // declare the variable that will determine the radius of the sin motion when the truck object(s) will move
float speedOfSinWaveMovement; // declare the variable that will impart a change in the angle that controls the sin wave for the truck object(s) to move
Truck (color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempTruckCabinWidth, float tempXspeed, int tempstrokeWeightTruck) { // create the constructor of the Truck class and declare its temporary variables
super (tempC, tempXpos, tempYpos, tempvwidth, tempvheight, tempXspeed);
// initialize the parameters for the constructor of the subclass called Truck
strokeWeightTruck=tempstrokeWeightTruck;
truckCabinWidth=tempTruckCabinWidth;
// initialize the local variables for the display of the subclass called Truck
truckCabinHeight=tempvheight;
spaceBtwCabinVan=truckCabinWidth;
truckTotalWidth=truckCabinWidth+spaceBtwCabinVan+vwidth;
truckVanXCenter=truckCabinWidth+spaceBtwCabinVan+vwidth/2;
// initialize the local variables for the movement (driving) of the subclass called Truck
angle=0.0;
truckMiddleYPos=height/3;
truckYRangeOfMotion=height/3;
speedOfSinWaveMovement=0.04;
}
void drive() {
xpos = xpos - xspeed;
if (xpos < -vwidth) {
xpos = width;
}
ypos=truckMiddleYPos+sin(angle)*truckYRangeOfMotion;
angle+=speedOfSinWaveMovement;
}
void display () {
strokeWeight(strokeWeightTruck);
fill (c);
rectMode(CENTER);
rect (xpos+truckVanXCenter, ypos, vwidth, vheight);
rect (xpos+truckCabinWidth/2, ypos, truckCabinWidth, truckCabinHeight);
line (xpos+truckCabinWidth, ypos, xpos+(truckCabinWidth+spaceBtwCabinVan), ypos);
strokeWeight(1);
}
}
// code for the class called Timer
class Timer {
int resetTime;
int delayAvoidance;
Timer(int tempdelayAvoidance) {
delayAvoidance = tempdelayAvoidance;
}
// method to start the timer for the delay
void startTimer() {
resetTime=millis();
}
// method to determine whether the delay has passed
boolean endTimer () {
int passedTime=millis()-resetTime;
if (passedTime > delayAvoidance)
{
println( " X seconds have passed! " );
return true;
}
else {
return false;
}
}
}
/* collision detection and avoidance mechanism*/
// Declare the object variables for this program
Vehicle myVehicle1;
Vehicle myVehicle2;
Truck bigRedFireTruck;
Timer timer;
void setup() {
size(400, 200);
myVehicle1 = new Vehicle(color(0, 255, 0), 0, 100, 20, 10, 2);
myVehicle2 = new Vehicle(color(0, 0, 255), 0, 10, 20, 10, 1);
bigRedFireTruck=new Truck (color (255, 0, 0), width, height/2, 30, 14, 8, 1.5, 4);
timer = new Timer(200);
timer.startTimer();
}
void draw() {
background(225);
myVehicle1.drive();
if (myVehicle1.collDetect(bigRedFireTruck)) {
print("collision");
myVehicle1.avoidance(bigRedFireTruck);
}
myVehicle1.display();
myVehicle2.drive();
if (myVehicle2.collDetect(bigRedFireTruck)) {
print("collision");
myVehicle2.avoidance(bigRedFireTruck);
}
myVehicle2.display();
bigRedFireTruck.drive();
bigRedFireTruck.display();
}
// code for the superclass called Vehicle
class Vehicle {
// all of the fields for the class called Vehicle: declare the properties for the class called Vehicle
color c;
float xpos;
float ypos;
float vwidth;
float vheight;
float xspeed;
float yspeed;
float ydirection;
float yReturn;
// constructor for the class called Vehicle
Vehicle(color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempXspeed) { // CFSR: create the constructor of the Vehicle class and declare its temporary variables
c = tempC;
xpos = tempXpos;
ypos = tempYpos;
vwidth=tempvwidth;
vheight=tempvheight;
xspeed = tempXspeed;
yspeed=0.0;
yReturn=tempYpos;
ydirection=1;
}
// methods for the class called Vehicle
void drive() {
xpos = xpos + xspeed;
if (xpos > width) {
xpos = 0;
}
yspeed= 0.0;
ypos = yReturn;
ydirection=1;
ypos = ypos+yspeed*ydirection;
if (ypos <0-vheight) {
}
}
void display() {
stroke(1);
fill(c);
rectMode(CENTER);
rect(xpos, ypos, vwidth, vheight);
}
// collision detection method for the vehicle class: function that returns true or false based on whether the bigredtruck collides with another vehicle
boolean collDetect(Truck vcollision) {
if (xpos-vwidth/2<=(vcollision.xpos+vcollision.truckTotalWidth) && xpos+vwidth/2 >= vcollision.xpos && ypos-vheight/2<=(vcollision.ypos+vcollision.vheight/2) && ypos+vheight/2 >= (vcollision.ypos-vcollision.vheight/2))
{
return true;
}
else {
return false;
}
} // end of the collision detection method for the vehicle class
// avoidance mechanism method for the vehicle class
void avoidance(Truck tavoidance) {
if (timer.endTimer()) {
ypos = yReturn;
timer.startTimer();
}
if (ypos-tavoidance.ypos>0) {
ydirection=ydirection*1;
}
else {
ydirection=ydirection*-1;
}
yspeed=20;
ypos = ypos+yspeed*ydirection;
} // end the avoidance mechanism function
} // end the block for the class called Vehicle
class Truck extends Vehicle {
// declare all of the properties for the subclass called Truck that are not properties of the superclass Vehicle
int strokeWeightTruck; // declare the variable that will be used to set a strokeweight for the objects of the truck class different than the strokeweight of the vehicle class
float truckCabinWidth; // declare the variable that will be used to determine the width of the cabin part of the truck
float spaceBtwCabinVan; // declare the variable that will be used to determine the space between the cabin and the van parts of the truck
float truckTotalWidth; // declare the variable that will be used to determine the total width of the cabin
float truckCabinHeight; // declare the variable that will be used to determine the height of the cabin part of the truck
float truckVanXCenter; // declare the variable that will be used to determine the middle of the van part of the truck to draw its rectangle in rect(CENTER) mode;
float angle; // declare the variable that will control the position of the sin wave for the truck object(s) to move
float truckMiddleYPos; // declare the variable that will determine the Y position from which the sin motion occurs when the truck object(s) will move
float truckYRangeOfMotion; // declare the variable that will determine the radius of the sin motion when the truck object(s) will move
float speedOfSinWaveMovement; // declare the variable that will impart a change in the angle that controls the sin wave for the truck object(s) to move
Truck (color tempC, float tempXpos, float tempYpos, float tempvwidth, float tempvheight, float tempTruckCabinWidth, float tempXspeed, int tempstrokeWeightTruck) { // create the constructor of the Truck class and declare its temporary variables
super (tempC, tempXpos, tempYpos, tempvwidth, tempvheight, tempXspeed);
// initialize the parameters for the constructor of the subclass called Truck
strokeWeightTruck=tempstrokeWeightTruck;
truckCabinWidth=tempTruckCabinWidth;
// initialize the local variables for the display of the subclass called Truck
truckCabinHeight=tempvheight;
spaceBtwCabinVan=truckCabinWidth;
truckTotalWidth=truckCabinWidth+spaceBtwCabinVan+vwidth;
truckVanXCenter=truckCabinWidth+spaceBtwCabinVan+vwidth/2;
// initialize the local variables for the movement (driving) of the subclass called Truck
angle=0.0;
truckMiddleYPos=height/3;
truckYRangeOfMotion=height/3;
speedOfSinWaveMovement=0.04;
}
void drive() {
xpos = xpos - xspeed;
if (xpos < -vwidth) {
xpos = width;
}
ypos=truckMiddleYPos+sin(angle)*truckYRangeOfMotion;
angle+=speedOfSinWaveMovement;
}
void display () {
strokeWeight(strokeWeightTruck);
fill (c);
rectMode(CENTER);
rect (xpos+truckVanXCenter, ypos, vwidth, vheight);
rect (xpos+truckCabinWidth/2, ypos, truckCabinWidth, truckCabinHeight);
line (xpos+truckCabinWidth, ypos, xpos+(truckCabinWidth+spaceBtwCabinVan), ypos);
strokeWeight(1);
}
}
// code for the class called Timer
class Timer {
int resetTime;
int delayAvoidance;
Timer(int tempdelayAvoidance) {
delayAvoidance = tempdelayAvoidance;
}
// method to start the timer for the delay
void startTimer() {
resetTime=millis();
}
// method to determine whether the delay has passed
boolean endTimer () {
int passedTime=millis()-resetTime;
if (passedTime > delayAvoidance)
{
println( " X seconds have passed! " );
return true;
}
else {
return false;
}
}
}
1