Examples+
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).
Highlighted Features
/**
* 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).
*/
// 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);
}
}
Related Examples
This example is for Processing 4+. If you have a previous version, use the examples included with your software. If you see any errors or have suggestions, please let us know.