...
| Code Block |
|---|
PVector vec1 = new PVector(1,2);
PVector vec2 = new PVector(3,4);
// read out components of vec1
println(vec1.x);
println(vec1.y);
println(vec1.z);
println(vec1);
println(vec2);
// set new components of vector
vec1.set(10,20);
println(vec1);
// vector addition when creating a new vector
println("-- Vec Add --");
PVector resVec = PVector.add(vec1,vec2);
println(resVec);
// adding a vector to an existing vector
vec1.add(vec2);
println(vec1);
// vector subtraction
println("-- Vec Sub --");
resVec = PVector.sub(vec1,vec2);
println(resVec);
vec1.sub(vec2);
println(vec1);
// vector multiplication
println("-- Vec Mult --");
resVec = PVector.mult(vec1,2);
println(resVec);
vec1.mult(2);
println(vec1);
// find vector magnitude
println("-- Vec Length --");
println(vec1.mag());
// find angle between two vectors
println("-- Vec Angle Between --");
println(degrees(PVector.angleBetween(vec1, vec2)));
// normalise a vector
println("-- Vec Normalise --");
vec1.normalize();
println(vec1);
println(vec1.mag()); |
Example 1: Gravity
Here we use vectors to create a force of attraction. The force's effect is accumulative, always adding to the velocity of our ellipse.
| Code Block |
|---|
PVector pos = new PVector(200,200);
PVector velocity = new PVector(1,0);
void setup() {
size(600,600);
fill(255);
}
void draw() {
background(0);
PVector direction = new PVector(mouseX, mouseY);
direction.sub(pos); // by subtracting the pos from mouse coordinates, we end up with a vector between the two points
//
direction.normalize(); // now we have a vector with the length of 1, this tells us the direction we want to push out ellipse in
direction.mult(.1); // we shorten the vector (magnitude), this gives us our speed
velocity.add(direction); //we add the direction to our velocity, so the changes accumulate over time
pos.add(velocity); // pos + velocity give us our new position!
ellipse(pos.x, pos.y,30,30);
}; |
Example 2: Bouncy ball
Here we use vectors to simulate the effect of bouncing.
| Code Block |
|---|
PVector pos = new PVector(200,200);
PVector velocity = new PVector(1,0);
float damper = 0.99;
void setup() {
size(600,600);
fill(255);
}
void draw() {
background(0);
velocity.mult(damper); // always slow the ball down to simulate resistance
pos.add(velocity); // pos + velocity give us our new position!
// if the ball is above or below the screen, invert the y value of the velocity to send it back where it came from
if (pos.y<0 || pos.y>height) {
velocity.y = -velocity.y;
pos.add(velocity);
}
// if the ball is left or right of the screen, invert the x value
if (pos.x<0 || pos.x>width) {
velocity.x = -velocity.x;
pos.add(velocity);
}
ellipse(pos.x, pos.y,30,30);
if(mousePressed) {
// show us our "line of power"
stroke(255,0,0);
line(pos.x,pos.y, mouseX,mouseY);
noStroke();
}
};
void mouseReleased() {
// when the mouse is released we give a ball a big push
PVector direction = new PVector(mouseX, mouseY);
direction.sub(pos); // by subtracting the pos from mouse coordinates, we end up with a vectore between the two points
direction.mult(.2); // we shorten the magnitude to reduce the power of the push
velocity.set(direction); //now we have our new velocity
};
|
Example 3: Car follower
The physics of a car a very different to a planet or a bouncy ball, yet most of the parameters are the same.
| Code Block |
|---|
PVector pos = new PVector(200,200);
PVector velocity = new PVector(1,0);
|