Acceleration with Vectors
Acceleration with Vectors by Daniel Shiffman.
Demonstration of the basics of motion with vector. A “Mover” object stores location, velocity, and acceleration as vectors The motion is controlled by affecting the acceleration (in this case towards the mouse) * For more examples of simulating motion and physics with vectors, see Simulate/ForcesWithVectors, Simulate/GravitationalAttraction3D
// A Mover object Mover mover; void setup() { size(640,360); mover = new Mover(); } void draw() { background(0); // Update the location mover.update(); // Display the Mover mover.display(); } /** * Acceleration with Vectors * by Daniel Shiffman. * * Demonstration of the basics of motion with vector. * A "Mover" object stores location, velocity, and acceleration as vectors * The motion is controlled by affecting the acceleration (in this case towards the mouse) */ class Mover { // The Mover tracks location, velocity, and acceleration PVector location; PVector velocity; PVector acceleration; // The Mover's maximum speed float topspeed; Mover() { // Start in the center location = new PVector(width/2,height/2); velocity = new PVector(0,0); topspeed = 5; } void update() { // Compute a vector that points from location to mouse PVector mouse = new PVector(mouseX,mouseY); PVector acceleration = PVector.sub(mouse,location); // Set magnitude of acceleration acceleration.setMag(0.2); // Velocity changes according to acceleration velocity.add(acceleration); // Limit the velocity by topspeed velocity.limit(topspeed); // Location changes by velocity location.add(velocity); } void display() { stroke(255); strokeWeight(2); fill(127); ellipse(location.x,location.y,48,48); } }
Functions Used
Sets the width of the stroke used for lines, points, and the border around shapes
Learn MoreCalled directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called
Learn MoreA class to describe a two or three dimensional vector, specifically a Euclidean (also known as geometric) vector
Learn MoreThe background() function sets the color used for the background of the Processing window
Learn More