class Mover { PVector location; PVector velocity; PVector acceleration; float topspeed; float angle; Mover() { location = new PVector(random(0,width),random(0,height)); velocity = new PVector(0,0); topspeed = 2.5; } // Updates the location of the mover void update(PVector _dir) { // 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); PVector target = PVector.add(location, _dir); // Rotat mover PVector temp_vector = PVector.sub(new PVector(width, location.y),location); // Make temp vector to be able to calculate angle angle = degrees(PVector.angleBetween(temp_vector, _dir)); // Get angel from direction and temp vector if(target.y < location.y) angle = angle * -1; // If target is over mover invert angle /* For debugging stroke(0); line(location.x,location.y,temp.x,temp.y); line(location.x,location.y,target.x,target.y); println(angle); println(location.x+", "+location.y); println(temp.x+", "+temp.y);*/ } // Displays the mover void display() { int x1 = -10; int y1 = -5; int x2 = -10; int y2 = 5; int x3 = 10; int y3 = 0; pushMatrix(); translate(location.x, location.y); rotate(radians(angle)); fill(20); stroke(255); triangle(x1, y1, x2, y2, x3, y3); popMatrix(); } // Detects what should hapen if contact with edge void checkEdges() { if (location.x > width) { location.x = 0; } else if (location.x < 0) { location.x = width; } if (location.y > height) { location.y = 0; } else if (location.y < 0) { location.y = height; } } // Check if car has reach the target boolean checkIfCarAtTarget(PVector target){ if(abs(location.x - target.x) <= 5 && abs(location.y - target.y) <= 5){ return false; }else return true; } }