Files
processing4/java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde
2013-03-18 22:12:27 -04:00

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();
}
}