Files
processing4/java/examples/Books/Nature of Code/chp3_oscillation/ExtraOscillatingBody/Mover.pde
T

65 lines
1.1 KiB
Plaintext

// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Mover() {
location = new PVector(400,50);
velocity = new PVector(1,0);
acceleration = new PVector(0,0);
mass = 1;
}
void applyForce(PVector force) {
PVector f = PVector.div(force,mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display() {
stroke(0);
strokeWeight(2);
fill(127);
pushMatrix();
translate(location.x,location.y);
float heading = velocity.heading();
rotate(heading);
ellipse(0,0,16,16);
rectMode(CENTER);
// "20" should be a variable that is oscillating
// with sine function
rect(20,0,10,10);
popMatrix();
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
velocity.y *= -1;
location.y = height;
}
}
}