xxxxxxxxxx
103
let scalefactor = 1;
let offsetx = 0;
let offsety = 0;// Verschiebung des Kosys in die Mitte
class Ball {
constructor (mass, radius, location, velocity, acceleration,color) {
this.mass = mass; // Masse
this.radius = radius; // Radius
this.location = location; // Ort
this.velocity = velocity; // Geschwindigkeit
this.color = color; // Farbe
this.acceleration = acceleration; // Beschleunigung
}
update() {
//let dt = 0.1;
let dt = 3600; // Die Anzahl der Sekunden in einem Schritt 1h = 3600sek
this.velocity.add(p5.Vector.mult(this.acceleration, 0.5*dt)); // ACHTUNG: Die 0.5 darfst du nicht ändern, sonst gehst du nicht in die Mitte des Intervalls. Vgl. mein Video von damals.
this.location.add(p5.Vector.mult(this.velocity, dt));
this.velocity.add(p5.Vector.mult(this.acceleration, 0.5*dt)); // Hier ebenso.
}
display() { // Anzeigen der Körper
fill(this.color)
circle(offsetx + scalefactor*this.location.x, offsety + scalefactor*this.location.y, 2*this.radius);
}
}
class Gravity {
constructor(gconst) {
this.gconst = gconst;
}
force_between(body1, body2) { // Berechnung der Gravitationskraft zwischen zwei Körpern
let distance = p5.Vector.sub(body2.location, body1.location);
// Kontrollausgaben
//console.log("distance: " + distance.mag());
//console.log("grav const: " + this.gconst);
//console.log("mass1: " + body1.mass);
//console.log("mass2: " + body2.mass);
//console.log("distance square: " + distance.magSq());
//console.log("ganzer Term (Betrag der Kraft): " + this.gconst*body1.mass*body2.mass/distance.magSq());
let force_magnitude = this.gconst*body1.mass*body2.mass/distance.magSq();
let force = p5.Vector.mult(p5.Vector.normalize(distance), force_magnitude);
//console.log("Betrag der Kraft: " + force.mag());
return force;
}
superposition(body, bodies) { // Berechnung der Beschleunigung aufgrund der Summe aller Graviationskräfte für einen Körper
bodies.forEach(element => {
if (body != element) {
body.acceleration.add(p5.Vector.div(this.force_between(body, element),body.mass));
}
});
}
}
let gravity = new Gravity(6.67*Math.pow(10, -11));//echte Gravitationskonstante
//let gravity = new Gravity(0.0000001);
let sonne;
let erde;
let mond;
function setup() {
createCanvas(windowWidth, windowHeight);
scalefactor = width/400000000000; // Änderung in m und nicht km, da dies die Grundeinheit ist.
offsetx = width/2;
offsety = height/2;
sonne = new Ball(1.988*Math.pow(10,30), 696340*0.00005, createVector(0,0), createVector(0,0), createVector(0,0), "yellow")
erde = new Ball(5.972*Math.pow(10,24), 6371*0.001, p5.Vector.add(sonne.location,createVector(-150000000000,0)), createVector(0,29722), createVector(0,0), "blue")
mond = new Ball(7.3483*Math.pow(10,22), 1737*0.001, p5.Vector.add(erde.location,createVector(-384400000,0)), createVector(0,29780+1022), createVector(0,0), "white")
}
//Dreisatz um reelle Abstände zu errechnen mit Ansatz: 800 Pixel = 400000000000 Meter
// Warum -?
// Hier teile ich die Radien durch 10000 bzw. 1000 da alle Körper aber vor allem die Sonne viel zu groß sind. Der Radius beeinflusst aber ja die Gleichungen nicht. Möglicher Ansatz: Zoomfunktion um raus zu zoomen, man könnte dann aber wahrscheinlich auch nicht alle drei Körper gleichzeitig sehen
function draw() {
background("black");
sonne.display()//anzeigen
erde.display()
mond.display()
gravity.superposition(sonne, [erde, mond]);
gravity.superposition(erde, [sonne, mond]);
gravity.superposition(mond, [sonne, erde]);
sonne.update()
erde.update()
mond.update()
}
// Mond innerhalb der Erde, da das Programm den Abstand der Mittelpunkte nimmt
//Lösung??