Initial Commit

This commit is contained in:
plane000
2018-04-20 10:15:15 +01:00
parent 49150ccfe4
commit 62101e8e61
2870 changed files with 520122 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
/**
* Gravitational Attraction (3D)
* by Daniel Shiffman.
*
* Simulating gravitational attraction
* G ---> universal gravitational constant
* m1 --> mass of object #1
* m2 --> mass of object #2
* d ---> distance between objects
* F = (G*m1*m2)/(d*d)
*
* For the basics of working with PVector, see
* http://processing.org/learning/pvector/
* as well as examples in Topics/Vectors/
*
*/
// A bunch of planets
Planet[] planets = new Planet[10];
// One sun (note sun is not attracted to planets (violation of Newton's 3rd Law)
Sun s;
// An angle to rotate around the scene
float angle = 0;
void setup() {
size(640, 360, P3D);
// Some random planets
for (int i = 0; i < planets.length; i++) {
planets[i] = new Planet(random(0.1, 2), random(-width/2, width/2), random(-height/2, height/2), random(-100, 100));
}
// A single sun
s = new Sun();
}
void draw() {
background(0);
// Setup the scene
sphereDetail(8);
lights();
translate(width/2, height/2);
rotateY(angle);
// Display the Sun
s.display();
// All the Planets
for (Planet planet : planets) {
// Sun attracts Planets
PVector force = s.attract(planet);
planet.applyForce(force);
// Update and draw Planets
planet.update();
planet.display();
}
// Rotate around the scene
angle += 0.003;
}

View File

@@ -0,0 +1,43 @@
// Gravitational Attraction (3D)
// Daniel Shiffman <http://www.shiffman.net>
// A class for an orbiting Planet
class Planet {
// Basic physics model (position, velocity, acceleration, mass)
PVector position;
PVector velocity;
PVector acceleration;
float mass;
Planet(float m, float x, float y, float z) {
mass = m;
position = new PVector(x, y, z);
velocity = new PVector(1, 0); // Arbitrary starting velocity
acceleration = new PVector(0, 0);
}
// Newton's 2nd Law (F = M*A) applied
void applyForce(PVector force) {
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
// Our motion algorithm (aka Euler Integration)
void update() {
velocity.add(acceleration); // Velocity changes according to acceleration
position.add(velocity); // position changes according to velocity
acceleration.mult(0);
}
// Draw the Planet
void display() {
noStroke();
fill(255);
pushMatrix();
translate(position.x, position.y, position.z);
sphere(mass*8);
popMatrix();
}
}

View File

@@ -0,0 +1,36 @@
// 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 position; // position
float G; // Universal gravitational constant (arbitrary value)
Sun() {
position = new PVector(0, 0);
mass = 20;
G = 0.4;
}
PVector attract(Planet m) {
PVector force = PVector.sub(position, m.position); // 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(position.x, position.y, position.z);
sphere(mass*2);
popMatrix();
}
}