Skip to content

Commit

Permalink
Added moon support; turn back on barycenter calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
seanpile committed Dec 10, 2016
1 parent fba9323 commit 3ed042c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
15 changes: 10 additions & 5 deletions scripts/CanvasDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const MIN_BODY_RADIUS = 5;
const MAX_BODY_RADIUS = 25;
const BASE_TIME_SCALE = 86400;
let timeScale = 5;
let numToRun = 1000;
let numToRun = 10000;

define(function () {

Expand Down Expand Up @@ -53,6 +53,10 @@ define(function () {
radius = 3;
color = "purple";
break;
case "mars":
radius = 5;
color = "red";
break;
default:
radius = 5;
color = "black";
Expand Down Expand Up @@ -91,14 +95,15 @@ define(function () {
}, this);

ctx.strokeStyle = "red";
ctx.lineWidth = 0.5;
ctx.beginPath();
ctx.moveTo(25, 0);
ctx.lineTo(-25, 0);
ctx.moveTo(50, 0);
ctx.lineTo(-50, 0);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.moveTo(0, 25);
ctx.lineTo(0, -25);
ctx.moveTo(0, 50);
ctx.lineTo(0, -50);
ctx.stroke();

last += step;
Expand Down
6 changes: 3 additions & 3 deletions scripts/SolarSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ define(["Vector"], function (Vector) {
// Calculate a new 'CoM' given all of the current bodies and update the
// coordinates of all current bodies.
SolarSystem.prototype.update = function (t, dt) {

//this.updateBarycenter();
this.updateBarycenter();
this.updateEphemeris(t, dt);

//console.log(this.bodies[2].velocity);
};

// Calcluate the barycenter of the system and adjust the coordinate
Expand All @@ -28,7 +29,6 @@ define(["Vector"], function (Vector) {

this.bodies.forEach(function (body) {
body.position = body.position.plus(newCenterOfMass);
body.velocity = body.velocity.plus(newCenterOfMass);
});
};

Expand Down
51 changes: 43 additions & 8 deletions scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,56 @@ requirejs(["SolarSystem", "CanvasDisplay", "Vector", "verlet-integrator"],

const AU = 149597870700.0; // m
const SUN_MASS = 1.989e30 / AU; // kg
const EARTH_MASS = 5.97e24 / AU; // kg
const MOON_MASS = 7.3476e22 / AU; // kg
const JUPITER_MASS = 1.898e27 / AU; // kg
const EARTH_TO_SUN = 149.6e9 / AU; // m
const MOON_TO_EARTH = 0.3633e9 / AU; // m
const UNIVERSAL_GRAVITY = 6.674e-11 / Math.pow(AU, 2); // N⋅m^2/kg^2

const EARTH = {
name: "earth",
mass: 5.97e24 / AU, // kg
distanceToSun: 149.6e9 / AU, // m
moons: [{
name: "moon",
mass: 7.3476e22 / AU, // kg
distanceToPrimary: 0.3633e9 / AU // m
}]
};

const MARS = {
name: "mars",
mass: 0.642e24 / AU, // kg
distanceToSun: 227.9e9 / AU
};

let integrator = new VerletIntegrator(UNIVERSAL_GRAVITY);
let solarSystem = new SolarSystem(integrator);

let sun = solarSystem.addBody("sun", SUN_MASS);
let earth = solarSystem.addBody("earth", EARTH_MASS);

earth.position = new Vector(0, EARTH_TO_SUN, 0);
earth.velocity = new Vector(-Math.sqrt(UNIVERSAL_GRAVITY * (SUN_MASS + EARTH_MASS) / EARTH_TO_SUN), 0, 0).times(1);
[EARTH, MARS].forEach(function (properties) {
let body = solarSystem.addBody(properties.name, properties.mass);
let random = Math.random();

//randomized locations on a unit circle

if (random <= 0.25) {
body.position = new Vector(0, properties.distanceToSun, 0);
body.velocity = new Vector(-Math.sqrt(UNIVERSAL_GRAVITY * (SUN_MASS + properties.mass) / properties.distanceToSun), 0, 0);
} else if (random <= 0.5) {
body.position = new Vector(-properties.distanceToSun, 0, 0);
body.velocity = new Vector(0, -Math.sqrt(UNIVERSAL_GRAVITY * (SUN_MASS + properties.mass) / properties.distanceToSun), 0);
} else {
body.position = new Vector(0, -properties.distanceToSun, 0);
body.velocity = new Vector(Math.sqrt(UNIVERSAL_GRAVITY * (SUN_MASS + properties.mass) / properties.distanceToSun), 0, 0);
}

if (properties.moons) {
properties.moons.forEach(function (props) {
let moon = solarSystem.addBody(props.name, props.mass);
moon.position = body.position.plus(body.position.times(1 / body.position.magnitude()).times(props.distanceToPrimary));
moon.velocity = body.velocity.plus(
body.velocity.times(1 / body.velocity.magnitude()).times(-Math.sqrt(UNIVERSAL_GRAVITY * (props.mass + body.mass) / props.distanceToPrimary)));
});
}
});

let canvas = document.getElementById("expanse-simulation");
let simulation = new Display(canvas, solarSystem);
Expand Down

0 comments on commit 3ed042c

Please sign in to comment.