Skip to content

Commit

Permalink
first commit mgvez#2
Browse files Browse the repository at this point in the history
  • Loading branch information
7ruth committed May 24, 2016
2 parents a352185 + 2a4fc95 commit a17dee3
Show file tree
Hide file tree
Showing 10 changed files with 884 additions and 29 deletions.
48 changes: 48 additions & 0 deletions build/app.js

Large diffs are not rendered by default.

Binary file added img/jsorrery-share-fb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/saturnrings.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 37 additions & 8 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<!doctype html>
<html xmlns:fb="http://ogp.me/ns/fb#">
<head>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="author" content="Martin Vézina, La Grange">
<script data-main="./js/app.js" src="./js/require.js"></script>

<script data-main="./build/app.js" src="./js/require.js"></script>
<title>jsOrrery - Javascript Solar System Simulator</title>

<meta property="og:title" content="jsOrrery - Solar System Simulator" />
<meta property="og:image" content="/img/jsorrery_likefb.jpg" />
<meta property="og:title" content="jsOrrery - Solar System Simulator" />
<meta property="og:image" content="http://mgvez.github.io/jsorrery/img/jsorrery-share-fb.jpg" />
<meta property="og:description" content="See the Solar System in your browser with jsOrrery, a Javascript / WebGL orbital mechanics simulator." />
<meta property="og:url" content=" " />
<meta property="og:url" content="http://mgvez.github.io/jsorrery/index.html" />


<link href='http://fonts.googleapis.com/css?family=Quattrocento' rel='stylesheet' type='text/css'>

Expand Down Expand Up @@ -120,7 +122,7 @@ <h3>Special thanks</h3>
<li>And of course, many thanks to <a href="http://www.mrdoob.com/">Mr Doob</a> for the excellent <a href="http://threejs.org/">three.js</a></li>
</ul>
</div>

<div id="preload">
<div class="orbit"><div class="planet"></div></div>
<div class="title">
Expand All @@ -129,7 +131,34 @@ <h2>solar system</h2>
</div>
</div>

<div id="credits" class="help" data-for="helpCredits">credits</div>

<div id="credits" class="help" data-for="helpCredits">CREDITS</div>

<script type="text/javascript">
/** ANALYTICS */
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-32778044-3');
ga('send', 'pageview');
/** TWITTER */
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

</script>

<script>
window.gapiLoaded = (function(ns){
ns.gapi = null;
return function(){
ns.gapi = window.gapi;
window.gapi.client.setApiKey('AIzaSyAFDXxg1NPaJdajDzmG29Lj648Ss9S1ORE');
window.gapi.client.load('urlshortener', 'v1', function(){});
}
})(window.jsorrery = window.jsorrery || {});
</script>
<script src="https://apis.google.com/js/client.js?onload=gapiLoaded"></script>

</body>
</html>
</html>
17 changes: 17 additions & 0 deletions js/jsorrery/CelestialBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ define(
},

setPositionFromDate : function(epochTime, calculateVelocity) {
<<<<<<< HEAD

=======
// console.log(epochTime);
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
epochTime = this.getEpochTime(epochTime);
this.position = this.isCentral ? new THREE.Vector3() : this.orbitalElements.getPositionFromElements(this.orbitalElements.calculateElements(epochTime));
this.relativePosition = new THREE.Vector3();
Expand All @@ -65,19 +69,32 @@ define(
return 0;
},

<<<<<<< HEAD
afterInitialized : function(){

this.previousRelativePosition = this.position.clone();

this.positionRelativeTo();

=======
afterInitialized : function(isSetRelativeTo){
// console.log(this.title);
if(isSetRelativeTo) {
this.previousRelativePosition = this.position.clone();
this.positionRelativeTo();
}
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
if(this.customInitialize) this.customInitialize();

if(this.customAfterTick) this.customAfterTick(ns.U.epochTime, ns.U.date);
},

positionRelativeTo : function(){
if(this.relativeTo) {
<<<<<<< HEAD
=======

>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
var central = ns.U.getBody(this.relativeTo);
if(central && central!==ns.U.getBody()/**/) {
this.position.add(central.position);
Expand Down
87 changes: 87 additions & 0 deletions js/jsorrery/Universe.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,18 @@ define(
this.scene.kill();
this.centralBody = null;
this.scene = null;
<<<<<<< HEAD
this.bodies = {};
=======
this.bodies = [];
this.bodiesByName = {};
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492

Labels.kill();
},

createBodies : function(scenario) {
<<<<<<< HEAD
var bodies = [];
this.bodies = {};

Expand All @@ -117,21 +123,63 @@ define(
initBodies : function(scenario){

_.each(this.bodies, function(body, name){
=======

var bodiesNames = Object.keys(scenario.bodies);
this.bodies = bodiesNames.map(function(name){
var config = scenario.bodies[name];
var body = Object.create(CelestialBody);
$.extend(body, config);
body.name = name;
return body;
});

this.centralBody = this.bodies.reduce(function(current, candidate) {
return current && current.mass > candidate.mass ? current : candidate;
}, null);

this.bodiesByName = this.bodies.reduce(function(byName, body) {
byName[body.name] = body;
return byName;
}, {});

this.bodies.sort(function(a, b){
return ((a.relativeTo || 0) && 1) - ((b.relativeTo || 0) && 1)
});

this.centralBody.isCentral = true;
// console.log(this.bodies);
},

initBodies : function(scenario){
this.bodies.forEach(function(body){
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
if((typeof scenario.calculateAll === 'undefined' || !scenario.calculateAll) && !body.isCentral){
body.mass = 1;
}
body.init();
body.setPositionFromDate(this.currentTime, true);
<<<<<<< HEAD

=======
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
}.bind(this));

this.setBarycenter();

//after all is inialized
<<<<<<< HEAD
_.each(this.bodies, function(body, name){
this.scene.addBody(body);
body.afterInitialized();
//console.log(body.name, body.isCentral);
=======
this.bodies.forEach(function(body){
// console.log(body.name, body.isCentral);

this.scene.addBody(body);
body.afterInitialized(true);
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
}.bind(this));

this.scene.setCentralBody(this.centralBody);
Expand All @@ -148,7 +196,12 @@ define(
pos : new THREE.Vector3(),
momentum : new THREE.Vector3()
};
<<<<<<< HEAD
_.each(this.bodies, function(b){
=======

this.bodies.forEach(function(b){
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
if(b === central) return;
massCenter.mass += b.mass;
massRatio = b.mass / massCenter.mass;
Expand All @@ -160,7 +213,11 @@ define(
massRatio = massCenter.mass / central.mass;
central.velocity = massCenter.momentum.multiplyScalar(massRatio * -1);
central.position = massCenter.pos.clone().multiplyScalar(massRatio * -1);
<<<<<<< HEAD
_.each(this.bodies, function(b){
=======
this.bodies.forEach(function(b){
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
if(b === central || (b.relativeTo && b.relativeTo != central.name)) return;
b.velocity.add(central.velocity);
//if central body's mass is way bigger than the object, we assume that the central body is the center of rotation. Otherwise, it's the barycenter
Expand All @@ -173,31 +230,51 @@ define(
},

repositionBodies : function(){
<<<<<<< HEAD
_.each(this.bodies, function(body, name){
body.reset();
body.setPositionFromDate(this.currentTime, true);
=======
// console.log(this.bodies);

this.bodies.forEach(function(body){
body.reset();
body.setPositionFromDate(this.currentTime, true);
// console.log(body.name);
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
}.bind(this));

Ticker.tick(false, this.currentTime);

this.setBarycenter();

//adjust position depending on other bodies' position (for example a satellite is relative to its main body)
<<<<<<< HEAD
_.each(this.bodies, function(body, name){
body.afterInitialized();
}.bind(this));
=======
this.bodies.forEach(function(body){
body.afterInitialized(false);
});
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
},

getBody : function(name) {
if(name === 'central' || !name) {
return this.centralBody;
}
<<<<<<< HEAD
return this.bodies[name];
=======
return this.bodiesByName[name];
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
},

calculateDimensions : function(){
var centralBodyName = this.getBody().name;
//find the largest radius in km among all bodies
<<<<<<< HEAD
var largestRadius = _.reduce(this.bodies, function(memo, body){
return memo < body.radius ? body.radius : memo;
}, 0);
Expand All @@ -206,6 +283,16 @@ define(
return (!body.isCentral && body.orbit && body.orbit.base.a > memo) ? body.orbit.base.a : memo;
}, 0);
var smallestSMA = _.reduce(this.bodies, function(memo, body){
=======
var largestRadius = this.bodies.reduce(function(memo, body){
return memo < body.radius ? body.radius : memo;
}, 0);
//find the largest semi major axis in km among all bodies
var largestSMA = this.bodies.reduce(function(memo, body){
return (!body.isCentral && body.orbit && body.orbit.base.a > memo) ? body.orbit.base.a : memo;
}, 0);
var smallestSMA = this.bodies.reduce(function(memo, body){
>>>>>>> 2a4fc95d538b0ca56dd4a26474eafb635732a492
return (!body.isCentral && body.orbit && (!body.relativeTo || body.relativeTo == centralBodyName) && (!memo || body.orbit.base.a < memo)) ? body.orbit.base.a : memo;
}, 0);
smallestSMA *= ns.KM;
Expand Down
96 changes: 96 additions & 0 deletions js/jsorrery/algorithm/GravityTicker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
define(
[
'jsorrery/NameSpace',
'jquery',
'three'
],
function(ns, $) {
'use strict';
/**
number of calculations of gravity per tick. Adding more calculation has the effect of checking the position of bodies more often at each tick, so that the forces are not a multiplication of their values of the beginning of the tick. Since each body moves at each second, their relative position is not the same at the beginning of tick as at the end. The force they produce is'nt either. If we want to be more precise we have to "move" each body a given number of time at each tick so the forces are calculated from their new position.
*/

var calculationsPerTick = 1;
var secondsPerTick = 1;
var deltaTIncrement = 1;
var deltaTIncrementSquared = 1;
var bodies = [];

/**
Calculates the forces that are created by each body toward one another
*/
var calculateGForces = function(){
var workVect=new THREE.Vector3(), i, j;
for(i=0; i<bodies.length; i++){
//loop in following bodies and calculate forces between them and this one. No need to do previous ones, as they were done in previous iterations
for(j=i+1; j<bodies.length; j++){
if(bodies[i].mass === 1 && bodies[j].mass === 1) continue;
workVect = getGForceBetween(bodies[i], bodies[j]);
//add forces (for the first body, it is the reciprocal of the calculated force)
bodies[i].force.sub(workVect);
bodies[j].force.add(workVect);/**/
}
}
};

/**
Get the gravitational force in Newtons between two bodies (their distance in m, mass in kg)
*/
var getGForceBetween = function(a, b){
var workVect=new THREE.Vector3(), dstSquared, massPrd, Fg;
workVect.copy(a.position).sub(b.position);//vector is between positions of body A and body B
dstSquared = workVect.lengthSq();
massPrd = a.mass * b.mass;
Fg = ns.G * (massPrd / dstSquared);//in newtons (1 N = 1 kg*m / s^2)
workVect.normalize();
workVect.multiplyScalar(Fg);//vector is now force of attraction in newtons/**/
return workVect;
};


var setDT = function (){
if(!calculationsPerTick || !secondsPerTick) return;
deltaTIncrement = secondsPerTick / calculationsPerTick;
deltaTIncrementSquared = Math.pow(deltaTIncrement, 2);
};

var Algorithm = {

tick : function(){
var t, i;
//We calculate the positions of all bodies, and thus the gravity, more than once per tick. Not efficient but more precise than approximating the whole forces to their value at the beginning of the cycle.
for(t=0; t < calculationsPerTick; t++){
calculateGForces();
for(i=0; i<bodies.length; i++){
bodies[i].moveBody(deltaTIncrement, deltaTIncrementSquared, i);
}
}
for(i=0; i<bodies.length; i++){
bodies[i].afterTick(secondsPerTick);
}/**/

return secondsPerTick;
},

setBodies : function(b){
bodies.length = 0;
$.each(b, function(name, body){
bodies.push(body);
});
},

setCalculationsPerTick : function(n){
calculationsPerTick = n || calculationsPerTick;
setDT();
},

setSecondsPerTick : function(s) {
secondsPerTick = s;
setDT();
}
}

return Algorithm;

}
);
Loading

0 comments on commit a17dee3

Please sign in to comment.