Files
processing4/java/examples/Topics/Simulate/ForcesWithVectors/Liquid.pde
2012-07-17 19:08:14 +00:00

61 lines
1.1 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"
*/
// 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);
}
}