On Thursday, Feb. 17, we did this illustration of the material of Chapter 3.
class Bob {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Bob(float m, float x, float y, float vx, float vy) {
mass = m;
location = new PVector(x, y);
velocity = new PVector(vx, vy);
acceleration = new PVector(0, 0);
}
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);
fill(175);
ellipseMode(CENTER);
ellipse(location.x, location.y, mass*16, mass*16);
}
}
class Spring {
PVector anchor;
float len;
float k = 0.1;
Spring(float x, float y, float l) {
anchor = new PVector(x, y);
len = l;
}
void connect(Bob b) {
PVector force = PVector.sub(b.location, anchor);
float d = force.mag();
float stretch = d - len;
force.normalize();
force.mult(-1 * k * stretch);
b.applyForce(force);
}
void display() {
fill(100);
rectMode(CENTER);
rect(anchor.x, anchor.y, 10, 10);
}
void displayLine(Bob b) {
stroke(255);
line(b.location.x, b.location.y, anchor.x, anchor.y);
}
}
Bob bob;
Spring spring;
void setup() {
size(640, 480);
bob = new Bob(1, 100, 100, 0, 0);
spring = new Spring(320, 50, 200);
}
void draw() {
push();
fill(188, 20);
noStroke();
rectMode(CORNERS);
rect(0, 0, width, height);
pop();
PVector gravity = new PVector(0, 1);
bob.applyForce(gravity);
spring.connect(bob);
bob.update();
bob.display();
spring.display();
}