mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 05:39:18 +01:00
39 lines
1.1 KiB
Plaintext
39 lines
1.1 KiB
Plaintext
// Gravitational Attraction (3D)
|
|
// Daniel Shiffman <http://www.shiffman.net>
|
|
|
|
// A class for an attractive body in our world
|
|
|
|
class Sun {
|
|
float mass; // Mass, tied to size
|
|
PVector location; // Location
|
|
float G; // Universal gravitational constant (arbitrary value)
|
|
|
|
Sun() {
|
|
location = new PVector(0,0);
|
|
mass = 20;
|
|
G = 0.4;
|
|
}
|
|
|
|
|
|
PVector attract(Planet m) {
|
|
PVector force = PVector.sub(location,m.location); // Calculate direction of force
|
|
float d = force.mag(); // Distance between objects
|
|
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
|
|
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
|
|
force.setMag(strength); // Get force vector --> magnitude * direction
|
|
return force;
|
|
}
|
|
|
|
// Draw Sun
|
|
void display() {
|
|
stroke(255);
|
|
noFill();
|
|
pushMatrix();
|
|
translate(location.x,location.y,location.z);
|
|
sphere(mass*2);
|
|
popMatrix();
|
|
}
|
|
}
|
|
|
|
|