class Obstacle{ PVector velocity; PVector acceleration; float topspeed; PVector location; PVector point1; PVector point2; boolean toPoint2; Obstacle(int _x, int _y, int _x2, int _y2){ location = new PVector(_x, _y); point1 = new PVector(_x, _y); point2 = new PVector(_x2, _y2); toPoint2 = true; velocity = new PVector(0,0); topspeed = random(0.2, 1); } // Updates the location of the obstacle void update() { PVector _dir = new PVector(0,0); // Check the direction for the obstacle //if(toPoint2 && checkIfObstacleAtTarget(point2)) toPoint2 = false; //else if(!toPoint2 && checkIfObstacleAtTarget(point1)) toPoint2 = true; if(frameCount %5==0 && toPoint2 && checkIfObstacleAtTarget(point2)){ toPoint2 = false; } else if(frameCount %5==0 && !toPoint2 && checkIfObstacleAtTarget(point1)) toPoint2 = true; // Set the direction of the obstacle if(toPoint2) _dir = PVector.sub(point2, location); else _dir = PVector.sub(point1, location); // Our algorithm for calculating acceleration: //PVector dir = PVector.sub(target,location); // Find vector pointing towards mouse _dir.normalize(); // Normalize _dir.mult(1); // Scale acceleration = _dir; // Set to acceleration // Motion 101! Velocity changes by acceleration. Location changes by velocity. velocity.add(acceleration); velocity.limit(topspeed); location.add(velocity); } // Displays the obstacle void display() { // Draw obstacle path fill(192); stroke(192); line(point1.x, point1.y, point2.x, point2.y); arc(point1.x, point1.y, 4, 4, 0, TWO_PI); arc(point2.x, point2.y, 4, 4, 0, TWO_PI); pushMatrix(); translate(location.x, location.y); // Draw obsticales fill(255); noStroke(); arc(0,0,20,20,0,TWO_PI); popMatrix(); } boolean checkIfObstacleAtTarget(PVector target) { if(abs(location.x - target.x) <= 5 && abs(location.y - target.y) <= 5) return true; // if ((target.x - location.x <= 5) && (target.y - location.y <= 5)) return true; else return false; } }