aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorrahiel <rahiel@protonmail.ch>2015-10-19 18:54:15 +0200
committerrahiel <rahiel@protonmail.ch>2015-10-19 18:54:15 +0200
commit6c266bc767354509185bb6d8e560800c4c961728 (patch)
treece7bc91ad2e682e628c36b235ec34f6a3b283eb8
parent1f8ee0b3f8843aa85cb5e22bbb37416df4a355e4 (diff)
cleanup code, force HTTPS
-rw-r--r--README.md24
-rw-r--r--calc.es6.js7
-rw-r--r--index.html1
-rw-r--r--sunsistemo.es6.js74
-rw-r--r--systems.es6.js95
5 files changed, 79 insertions, 122 deletions
diff --git a/README.md b/README.md
index 2f83433..f41e01d 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
-# Sunsistemo
+Sunsistemo
+==========
Sunsistemo is a numerical N-body simulator written in JavaScript.
[It runs completely in the browser](https://sunsistemo.js.org/). Given a set of
@@ -9,7 +10,7 @@ This was a project done for the course "Introduction to Computational Science"
as part of the
[Computational Science Master program](http://gss.uva.nl/future-msc-students/information-sciences/content27/computational-science.html).
-## Browser compatibility
+# Browser compatibility
Sunsistemo works great on:
* Chrome (and Chromium based browsers)
@@ -18,27 +19,24 @@ Sunsistemo works great on:
And it is slower on:
* Firefox
-## Installation
+# Installation
1. [Click here](https://sunsistemo.js.org/)
-2. Done!
+2. There is no step 2.
-## Credits
+# Credits
-Textures of the planets, moons and the sun were taken from
+* Textures of the planets, moons and the sun were taken from
<http://planetpixelemporium.com/planets.html> and are Copyright (c) by James
Hastings-Trew.
-
-The galaxy_starfield is from the
+* The galaxy_starfield is from the
[learningthreejs website](http://learningthreejs.com/blog/2013/09/16/how-to-make-the-earth-in-webgl/).
-
-The various balls are from
+* The various balls are from
<http://www.robinwood.com/Catalog/FreeStuff/Textures/TexturePages/BallMaps.html>.
-
-The glow effect of the sun is from
+* The glow effect of the sun is from
[Three.js Examples](https://stemkoski.github.io/Three.js/Simple-Glow.html) by
Lee Stemkoski.
-## License
+# License
[GPL-3.0+](https://github.com/sunsistemo/sunsistemo/blob/master/LICENSE.txt)
diff --git a/calc.es6.js b/calc.es6.js
index ed8291a..b49968a 100644
--- a/calc.es6.js
+++ b/calc.es6.js
@@ -1,10 +1,9 @@
// physical constants from http://physics.nist.gov/cuu/Constants/index.html
-let G = 6.67408E-11;
+export const G = 6.67408E-11;
let Vec3 = THREE.Vector3;
-import Body from "./systems.es6.js"
+import Body from "./systems.es6.js";
-// console.log(allTextures);
function accel(i, b) {
// Compute acceleration due to gravity on body i by all other bodies
@@ -139,5 +138,3 @@ export function mergeCollision(bodies, spheres, scene, b1, b2) {
spheres.splice(bodies.indexOf(bSmall),1);
console.log("merge!");
}
-
-
diff --git a/index.html b/index.html
index 48187ea..770019a 100644
--- a/index.html
+++ b/index.html
@@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
+ <script type="text/javascript">if(window.location.protocol=="http:"&&window.location.host=="sunsistemo.js.org")window.location.protocol="https";</script>
<title>Sunsistemo - N-body simulator</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
diff --git a/sunsistemo.es6.js b/sunsistemo.es6.js
index 87e4c19..51a1c98 100644
--- a/sunsistemo.es6.js
+++ b/sunsistemo.es6.js
@@ -11,15 +11,7 @@ let sphereP = 12;
let sunOn;
var steps;
-let system, bodies;
-let spheres;
-
-// let sysDict = {
-// "Random Bodies": systems.genBodies ,
-// "Solar System": systems.genSolarSystem,
-// "Total Angular Momentum": systems.genBodiesRot,
-// "Three Bodies": systems.gen3Bodies
-// };
+let system, bodies, spheres;
let menuList = [
{"label":"Empty", "function": systems.genBodies, "args": [0, true, false]},
@@ -33,32 +25,30 @@ let menuList = [
{"label":"Yin Yang", "function": systems.genYinYang, "args": []},
{"label":"Goggles", "function": systems.genYinYang, "args": []}
// {"label":"Solar System", "function": systems.genSolarSystem, "args": [true] }
-
];
gui(menuList);
simulate(menuList[0].function, [0, true, false]);
function gui(buttonList) {
- let buttonHeight = 50
- let buttonWidth = 200
- let body = d3.select("body")
- let menuDiv = body.selectAll("#gui")
- menuDiv.style()
+ let buttonHeight = 50;
+ let buttonWidth = 200;
+ let body = d3.select("body");
+ let menuDiv = body.selectAll("#gui");
+ menuDiv.style();
let menuSvg = menuDiv.append("svg")
.attr("width", buttonWidth + 50 + "px")
.attr("height", buttonHeight * menuList.length + 20 + "px")
.attr("class", "menuSvg");
var buttons = menuSvg.selectAll(".button")
- .data(menuList)
- // console.log(dict)
+ .data(menuList);
buttons.enter()
.append("g")
- .attr("transform", function(d,i){
- return ("translate(" + 10 + "," + ((i * buttonHeight) + 10) + ")")
- })
+ .attr("transform", function(d, i) {
+ return ("translate(" + 10 + "," + ((i * buttonHeight) + 10) + ")");
+ })
.attr("class", "button");
buttons.append("rect")
@@ -74,25 +64,24 @@ function gui(buttonList) {
.attr("dx", ".35em")
.attr("y", buttonHeight / 2)
.attr("dy", ".35em")
- .text(function(d){return d.label});
-
- buttons.on("mouseover", function(d){
- d3.select(d3.event.target.parentNode)
- .classed("highlight", true);
- })
- .on("mouseout", function(d){
- d3.select(d3.event.target.parentNode)
- .classed("highlight", false);
- })
-
+ .text(function(d) {return d.label;});
+
+ buttons.on("mouseover", function(d) {
+ d3.select(d3.event.target.parentNode)
+ .classed("highlight", true);
+ })
+ .on("mouseout", function(d) {
+ d3.select(d3.event.target.parentNode)
+ .classed("highlight", false);
+ })
.on("click", function(d){
- d3.selectAll(".selected")
- .classed("selected", false)
- clearSimulation()
- simulate(d.function, d.args)
- d3.select(d3.event.target.parentNode)
- .classed("selected", true);
- })
+ d3.selectAll(".selected")
+ .classed("selected", false);
+ clearSimulation();
+ simulate(d.function, d.args);
+ d3.select(d3.event.target.parentNode)
+ .classed("selected", true);
+ });
}
@@ -100,11 +89,11 @@ function clearSimulation() {
let simDiv = document.getElementById("sim");
while (simDiv.firstChild) simDiv.removeChild(simDiv.firstChild);
let statDiv = d3.select("#stats").remove();
+}
- }
function simulate(sysFunc, args){
system = sysFunc(...args);
- console.log(system)
+ console.log(system);
bodies = system.bodies;
if (system.hasOwnProperty("stepsPerFrame")) {
steps = system.stepsPerFrame;
@@ -112,7 +101,6 @@ function simulate(sysFunc, args){
else { steps = 1; }
[spheres] = init();
animate_leapfrog();
-
window.addEventListener('resize', onWindowResize, true);
}
@@ -150,7 +138,7 @@ function init() {
spheres.push(sphere);
}
- if (system.sunOn){
+ if (system.sunOn) {
let sun = spheres[0];
sun.material.emissive.set(0xfcd440);
@@ -176,11 +164,9 @@ function init() {
// overall light
let ambient = new THREE.AmbientLight(0xf0f0f0);
scene.add(ambient);
-
}
-
renderer = new THREE.WebGLRenderer();
renderer.setClearColor(0x000000);
renderer.setSize(window.innerWidth, window.innerHeight);
diff --git a/systems.es6.js b/systems.es6.js
index 2dcec65..ad547a1 100644
--- a/systems.es6.js
+++ b/systems.es6.js
@@ -3,15 +3,15 @@ let Vec3 = THREE.Vector3;
let textureSets = {
planets: ["earth", "jupiter", "mars", "mercury", "moon", "neptune",
- "pluto", "saturn", "uranus", "venus"],
+ "pluto", "saturn", "uranus", "venus"],
balls: ["tennisball", "softball"],
all: ["sun", "earth", "jupiter", "mars", "mercury", "moon", "neptune",
- "pluto", "saturn", "uranus", "venus", "clouds", "tennisball"]
- }
+ "pluto", "saturn", "uranus", "venus", "tennisball"]
+};
-// let textures = ["sun", "earth", "jupiter", "mars", "mercury", "moon", "neptune",
-// "pluto", "saturn", "uranus", "venus", "clouds", "tennisball"];
-// let allTextures = {};
+function getRandomTexture() {
+ return getRandomFromList(textureSets.all);
+}
class Body {
constructor(m, r, v, rad, texture, rot) {
@@ -62,6 +62,8 @@ let allTextures = { "sun": loadTextures("sun", true, false, false),
"softball": loadTextures("softball", true, true, false)
};
+let randomRot = () => Math.random() / 30;
+
export function genSolarSystem(sunOn) {
// Initial conditions of the solar system at 00:00:00 1 January 1970
@@ -180,9 +182,8 @@ export function genSolarSystem(sunOn) {
}
export function gen2Bodies(sunOn) {
- let rot = () => Math.random() / 30;
- let s1 = new Body(1E19, new Vec3(0, 0, 0), new Vec3(0, -90, 0), 12, "sun", new Vec3(0, 0, rot()));
- let s2 = new Body(1E18, new Vec3(200, 0, 0), new Vec3(0, 900, 0), 8, "earth", new Vec3(0, 0, rot()));
+ let s1 = new Body(1E19, new Vec3(0, 0, 0), new Vec3(0, -90, 0), 12, "sun", new Vec3(0, 0, randomRot()));
+ let s2 = new Body(1E18, new Vec3(200, 0, 0), new Vec3(0, 900, 0), 8, "earth", new Vec3(0, 0, randomRot()));
let bodies = [s1, s2];
return {
@@ -198,10 +199,9 @@ export function gen2Bodies(sunOn) {
}
export function gen3Bodies(sunOn) {
- let rot = () => Math.random() / 30;
- let s1 = new Body(1E19, new Vec3(0, 0, 0), new Vec3(0, 2, 0), 12, "sun", new Vec3(0, 0, rot()));
- let s2 = new Body(1E18, new Vec3(200, 0, 0), new Vec3(0, 900, 0), 8, "venus", new Vec3(0, 0, rot()));
- let s3 = new Body(1E18, new Vec3(-200, 0, 0), new Vec3(0, -900, 0), 8, "earth", new Vec3(0, 0, rot()));
+ let s1 = new Body(1E19, new Vec3(0, 0, 0), new Vec3(0, 2, 0), 12, "sun", new Vec3(0, 0, randomRot()));
+ let s2 = new Body(1E18, new Vec3(200, 0, 0), new Vec3(0, 900, 0), 8, "venus", new Vec3(0, 0, randomRot()));
+ let s3 = new Body(1E18, new Vec3(-200, 0, 0), new Vec3(0, -900, 0), 8, "earth", new Vec3(0, 0, randomRot()));
let bodies = [s1, s2, s3];
return {
@@ -224,11 +224,10 @@ export function genBodies(n, bodyTexture, sunOn, collisions) {
for (let i = 0; i < n; i++) {
let posVec = new Vec3(getRandomInt(-300,300), getRandomInt(-300,300), getRandomInt(-300,300));
let velVec = new Vec3(getRandomInt(-500,500), getRandomInt(-500,500), getRandomInt(-500,500));
- let rot = () => Math.random() / 30;
- let rotation = new Vec3(0, rot(), 0);
+ let rotation = new Vec3(0, randomRot(), 0);
bodies.push(new Body(1E16, posVec, velVec, 8, getRandomFromList(textureSets.planets), rotation));
- }
+ }
return {
bodies: bodies,
@@ -252,8 +251,7 @@ export function genBodiesRot(n, bodyTexture, sunOn, collisions) {
for (let i = 0; i < n; i++) {
let posVec = new Vec3(getRandomInt(-300,300), getRandomInt(-300,300), getRandomInt(-300,300));
let velVec = new Vec3(0,0,0);
- let rot = () => Math.random() / 30;
- let rotation = new Vec3(0, rot(), 0);
+ let rotation = new Vec3(0, randomRot(), 0);
velVec.crossVectors(posVec, angMomVec).multiplyScalar(Math.random() + .1);
bodies.push(new Body(1E14, posVec, velVec, 8, getRandomFromList(textureSet), rotation));
}
@@ -269,51 +267,29 @@ export function genBodiesRot(n, bodyTexture, sunOn, collisions) {
};
}
-export function genGoggles() { // I.B.5 goggles
- let m = 1 / 6.67408E-11;
- let s1 = new Body(m, new Vec3(-1, 0, 0), new Vec3(0.08330, 0.12789, 0), 0.05, "jupiter", new Vec3(0, 0.01, 0));
- let s2 = new Body(m, new Vec3(-s1.r.x, 0, 0), new Vec3(s1.v.x, s1.v.y, 0), 0.05, "tennisball", new Vec3(0, 0.01, 0));
- let s3 = new Body(m, new Vec3(0, 0, 0), new Vec3(-2 * s1.v.x, -2 * s1.v.y), 0.05, "earth", new Vec3(0, 0.03, 0));
-
- return {
- bodies: [s1, s2, s3],
- stepsize: 0.00001,
- stepsPerFrame: 500,
- camera: {x: 0, y: 0, z: 2},
- collisions: false
+function ThreeBodyPlanarPeriodicOrbit (x1d, y1d) {
+
+ let f = () => {
+ let m = 1 / calc.G;
+ let s1 = new Body(m, new Vec3(-1, 0, 0), new Vec3(x1d, y1d, 0), 0.05, getRandomTexture(), new Vec3(0, randomRot(), 0));
+ let s2 = new Body(m, new Vec3(-s1.r.x, 0, 0), new Vec3(s1.v.x, s1.v.y, 0), 0.05, getRandomTexture(), new Vec3(0, randomRot(), 0));
+ let s3 = new Body(m, new Vec3(0, 0, 0), new Vec3(-2 * s1.v.x, -2 * s1.v.y), 0.05, getRandomTexture(), new Vec3(0, randomRot(), 0));
+
+ return {
+ bodies: [s1, s2, s3],
+ stepsize: 0.00001,
+ stepsPerFrame: 500,
+ camera: {x: 0, y: 0, z: 2},
+ collisions: false
+ };
};
-}
-
-export function genYinYang() { // II.C.2a yin-yang I
- let m = 1 / 6.67408E-11;
- let s1 = new Body(m, new Vec3(-1, 0, 0), new Vec3(0.51394, 0.30474, 0), 0.05, "jupiter", new Vec3(0, 0.01, 0));
- let s2 = new Body(m, new Vec3(-s1.r.x, 0, 0), new Vec3(s1.v.x, s1.v.y, 0), 0.05, "tennisball", new Vec3(0, 0.01, 0));
- let s3 = new Body(m, new Vec3(0, 0, 0), new Vec3(-2 * s1.v.x, -2 * s1.v.y), 0.05, "earth", new Vec3(0, 0.03, 0));
- return {
- bodies: [s1, s2, s3],
- stepsize: 0.00001,
- stepsPerFrame: 500,
- camera: {x: 0, y: 0, z: 2},
- collisions: false
- };
+ return f;
}
-
-export function genButterFlyOne() { // I.A.1 butterfly I
- let m = 1 / 6.67408E-11;
- let s1 = new Body(m, new Vec3(-1, 0, 0), new Vec3(0.30689, 0.12551, 0), 0.05, "tennisball", new Vec3(0, 0.01, 0));
- let s2 = new Body(m, new Vec3(-s1.r.x, 0, 0), new Vec3(s1.v.x, s1.v.y, 0), 0.05, "softball", new Vec3(0, 0.01, 0));
- let s3 = new Body(m, new Vec3(0, 0, 0), new Vec3(-2 * s1.v.x, -2 * s1.v.y), 0.05, "earth", new Vec3(0, 0.03, 0));
-
- return {
- bodies: [s1, s2, s3],
- stepsize: 0.00001,
- stepsPerFrame: 500,
- camera: {x: 0, y: 0, z: 2},
- collisions: false
- };
-}
+export let genGoggles = ThreeBodyPlanarPeriodicOrbit(0.08330, 0.12789); // I.B.5 goggles
+export let genYinYang = ThreeBodyPlanarPeriodicOrbit(0.51394, 0.30474); // II.C.2a yin-yang I
+export let genButterFlyOne = ThreeBodyPlanarPeriodicOrbit(0.30689, 0.12551); // I.A.1 butterfly I
function loadTextures(textureName, textOn, bumpOn, specOn){
@@ -339,7 +315,6 @@ function loadTextures(textureName, textOn, bumpOn, specOn){
function getRandomFromList(list) {
-
return list[getRandomInt(0, list.length)];
}