A new set of PVector examples

This commit is contained in:
shiffman
2012-06-15 19:31:32 +00:00
parent ff0ae29cb6
commit 21196d8fd5
11 changed files with 548 additions and 0 deletions
@@ -0,0 +1,27 @@
/**
* 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);
smooth();
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 and ForcesWithVectors for OOP)
*/
PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape's acceleration
void setup() {
size(640,360);
smooth();
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,72 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Five moving bodies
Mover[] movers = new Mover[5];
// Liquid
Liquid liquid;
void setup() {
size(360, 640);
smooth();
reset();
// Create liquid object
liquid = new Liquid(0, height/2, width, height/2, 0.1);
}
void draw() {
background(0);
// Draw water
liquid.display();
for (int i = 0; i < movers.length; i++) {
// Is the Mover in the liquid?
if (liquid.contains(movers[i])) {
// Calculate drag force
PVector drag = liquid.drag(movers[i]);
// Apply drag force to Mover
movers[i].applyForce(drag);
}
// Gravity is scaled by mass here!
PVector gravity = new PVector(0, 0.1*movers[i].mass);
// Apply gravity
movers[i].applyForce(gravity);
// Update and display
movers[i].update();
movers[i].display();
movers[i].checkEdges();
}
fill(255);
text("click mouse to reset",10,30);
}
void mousePressed() {
reset();
}
// Restart all the Mover objects randomly
void reset() {
for (int i = 0; i < movers.length; i++) {
movers[i] = new Mover(random(1, 5), 40+i*70, 0);
}
}
@@ -0,0 +1,60 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
// Liquid class
class Liquid {
// Liquid is a rectangle
float x,y,w,h;
// Coefficient of drag
float c;
Liquid(float x_, float y_, float w_, float h_, float c_) {
x = x_;
y = y_;
w = w_;
h = h_;
c = c_;
}
// Is the Mover in the Liquid?
boolean contains(Mover m) {
PVector l = m.location;
if (l.x > x && l.x < x + w && l.y > y && l.y < y + h) {
return true;
}
else {
return false;
}
}
// Calculate drag force
PVector drag(Mover m) {
// Magnitude is coefficient * speed squared
float speed = m.velocity.mag();
float dragMagnitude = c * speed * speed;
// Direction is inverse of velocity
PVector drag = m.velocity.get();
drag.mult(-1);
// Scale according to magnitude
drag.setMag(dragMagnitude);
return drag;
}
void display() {
noStroke();
fill(127);
rect(x,y,w,h);
}
}
@@ -0,0 +1,64 @@
/**
* Forces (Gravity and Fluid Resistence) with Vectors
* by Daniel Shiffman.
*
* Demonstration of multiple force acting on bodies (Mover class)
* Bodies experience gravity continuously
* Bodies experience fluid resistance when in "water"
*/
class Mover {
// location, velocity, and acceleration
PVector location;
PVector velocity;
PVector acceleration;
// Mass is tied to size
float mass;
Mover(float m, float x, float y) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
}
// Newton's 2nd law: F = M * A
// or A = F / M
void applyForce(PVector force) {
// Divide by mass
PVector f = PVector.div(force, mass);
// Accumulate all forces in acceleration
acceleration.add(f);
}
void update() {
// Velocity changes according to acceleration
velocity.add(acceleration);
// Location changes by velocity
location.add(velocity);
// We must clear acceleration each frame
acceleration.mult(0);
}
// Draw Mover
void display() {
stroke(255);
strokeWeight(2);
fill(255, 200);
ellipse(location.x, location.y, mass*16, mass*16);
}
// Bounce off bottom of window
void checkEdges() {
if (location.y > height) {
velocity.y *= -0.9; // A little dampening when hitting the bottom
location.y = height;
}
}
}
@@ -0,0 +1,66 @@
/**
* Gravitational Attraction (3D)
* by Daniel Shiffman.
*
* Simulating gravitational attraction
* G ---> universal gravitational constant
* m1 --> mass of object #1
* m2 --> mass of object #2
* d ---> distance between objects
* F = (G*m1*m2)/(d*d)
*/
// A bunch of planets
Planet[] planets = new Planet[10];
// One sun (note sun is not attracted to planets (violation of Newton's 3rd Law)
Sun s;
// An angle to rotate around the scene
float angle = 0;
void setup() {
size(640, 360, P3D);
smooth();
// Some random planets
for (int i = 0; i < planets.length; i++) {
planets[i] = new Planet(random(0.1, 2), random(-width/2, width/2), random(-height/2, height/2), random(-100, 100));
}
// A single sun
s = new Sun();
}
void draw() {
background(0);
// Setup the scene
sphereDetail(8);
lights();
translate(width/2, height/2);
rotateY(angle);
// Display the Sun
s.display();
// All the Planets
for (int i = 0; i < planets.length; i++) {
// Sun attracts Planets
PVector force = s.attract(planets[i]);
planets[i].applyForce(force);
// Update and draw Planets
planets[i].update();
planets[i].display();
}
// Rotate around the scene
angle += 0.003;
}
@@ -0,0 +1,45 @@
// Gravitational Attraction (3D)
// Daniel Shiffman <http://www.shiffman.net>
// A class for an orbiting Planet
class Planet {
// Basic physics model (location, velocity, acceleration, mass)
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Planet(float m, float x, float y, float z) {
mass = m;
location = new PVector(x,y,z);
velocity = new PVector(1,0); // Arbitrary starting velocity
acceleration = new PVector(0,0);
}
// Newton's 2nd Law (F = M*A) applied
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
// Our motion algorithm (aka Euler Integration)
void update() {
velocity.add(acceleration); // Velocity changes according to acceleration
location.add(velocity); // Location changes according to velocity
acceleration.mult(0);
}
// Draw the Planet
void display() {
noStroke();
fill(255);
pushMatrix();
translate(location.x,location.y,location.z);
sphere(mass*8);
popMatrix();
}
}
@@ -0,0 +1,39 @@
// Gravitational Attraction (3D)
// Daniel Shiffman <http://www.shiffman.net>
// A class for an attractive body in our world
class Sun {
float mass; // Mass, tied to size
PVector location; // Location
float G; // Universal gravitational constant (arbitrary value)
Sun() {
location = new PVector(0,0);
mass = 20;
G = 0.4;
}
PVector attract(Planet m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
return force;
}
// Draw Sun
void display() {
stroke(255);
noFill();
pushMatrix();
translate(location.x,location.y,location.z);
sphere(mass*2);
popMatrix();
}
}
@@ -0,0 +1,37 @@
/**
* Normalize
* by Daniel Shiffman.
*
* Demonstration of normalizing a vector.
* Normalizing a vector sets its length to 1.
*/
void setup() {
size(640,360);
smooth();
}
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 50
mouse.mult(150);
translate(width/2,height/2);
// Draw the resulting vector
stroke(255);
line(0,0,mouse.x,mouse.y);
}
@@ -0,0 +1,37 @@
/**
* 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);
smooth();
}
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);
line(0,0,mouse.x,mouse.y);
}