Initial Commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Bouncing Ball with Vectors
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Demonstration of using vectors to control motion of body
|
||||
* This example is not object-oriented
|
||||
* See AccelerationWithVectors for an example of how to simulate motion using vectors in an object
|
||||
*/
|
||||
|
||||
PVector location; // Location of shape
|
||||
PVector velocity; // Velocity of shape
|
||||
PVector gravity; // Gravity acts at the shape's acceleration
|
||||
|
||||
void setup() {
|
||||
size(640,360);
|
||||
location = new PVector(100,100);
|
||||
velocity = new PVector(1.5,2.1);
|
||||
gravity = new PVector(0,0.2);
|
||||
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
|
||||
// Add velocity to the location.
|
||||
location.add(velocity);
|
||||
// Add gravity to velocity
|
||||
velocity.add(gravity);
|
||||
|
||||
// Bounce off edges
|
||||
if ((location.x > width) || (location.x < 0)) {
|
||||
velocity.x = velocity.x * -1;
|
||||
}
|
||||
if (location.y > height) {
|
||||
// We're reducing velocity ever so slightly
|
||||
// when it hits the bottom of the window
|
||||
velocity.y = velocity.y * -0.95;
|
||||
location.y = height;
|
||||
}
|
||||
|
||||
// Display circle at location vector
|
||||
stroke(255);
|
||||
strokeWeight(2);
|
||||
fill(127);
|
||||
ellipse(location.x,location.y,48,48);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Vector
|
||||
* by Daniel Shiffman.
|
||||
*
|
||||
* Demonstration some basic vector math: subtraction, normalization, scaling
|
||||
* Normalizing a vector sets its length to 1.
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
size(640,360);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
background(0);
|
||||
|
||||
// A vector that points to the mouse location
|
||||
PVector mouse = new PVector(mouseX,mouseY);
|
||||
// A vector that points to the center of the window
|
||||
PVector center = new PVector(width/2,height/2);
|
||||
// Subtract center from mouse which results in a vector that points from center to mouse
|
||||
mouse.sub(center);
|
||||
|
||||
// Normalize the vector
|
||||
mouse.normalize();
|
||||
|
||||
// Multiply its length by 150 (Scaling its length)
|
||||
mouse.mult(150);
|
||||
|
||||
translate(width/2,height/2);
|
||||
// Draw the resulting vector
|
||||
stroke(255);
|
||||
strokeWeight(4);
|
||||
line(0,0,mouse.x,mouse.y);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user