mirror of
https://github.com/processing/processing4.git
synced 2026-01-30 03:41:15 +01:00
65 lines
1.4 KiB
Plaintext
65 lines
1.4 KiB
Plaintext
/**
|
|
* 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;
|
|
}
|
|
}
|
|
}
|
|
|
|
|