aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJaro Camphuijsen <jjecamphuijsen@gmail.com>2015-10-15 18:34:37 +0200
committerJaro Camphuijsen <jjecamphuijsen@gmail.com>2015-10-15 18:41:29 +0200
commitd2b940f87f642384bb98386ed46db4871276d7f9 (patch)
tree773dc548417409481242eb6653971a1af7948ad7
parent2fe19ed29d36d93317dcdb37ae0ed607d6bb0cef (diff)
reorganized code and added simulation function for issue #5
-rw-r--r--calc.es6.js155
-rw-r--r--sunsistemo.es6.js48
-rw-r--r--systems.es6.js133
3 files changed, 190 insertions, 146 deletions
diff --git a/calc.es6.js b/calc.es6.js
index 000aa3c..ed8291a 100644
--- a/calc.es6.js
+++ b/calc.es6.js
@@ -2,133 +2,9 @@
let G = 6.67408E-11;
let Vec3 = THREE.Vector3;
-let planets = ["earth", "jupiter", "mars", "mercury", "moon", "neptune",
- "pluto", "saturn", "uranus", "venus"];
-let balls = ["tennisball", "softball"];
-let textures = ["sun", "earth", "jupiter", "mars", "mercury", "moon", "neptune",
- "pluto", "saturn", "uranus", "venus", "clouds", "tennisball"];
-let allTextures = {};
-
-allTextures = { "sun": loadTextures("sun", true, false, false),
- "mercury": loadTextures("mercury", true, true, false),
- "venus": loadTextures("venus", true, true, false),
- "earth": loadTextures("earth", true, true, true),
- "mars": loadTextures("mars", true, true, false),
- "moon": loadTextures("moon", true, true, false),
- "jupiter": loadTextures("jupiter", true, false, false),
- "saturn": loadTextures("saturn", true, false, false),
- "uranus": loadTextures("uranus", true, false, false),
- "neptune": loadTextures("neptune", true, false, false),
- "pluto": loadTextures("pluto", true, true, false),
- "tennisball": loadTextures("tennisball", true, true, true),
- "softball": loadTextures("softball", true, true, false)
-};
-
-
-class Body {
- constructor(m, r, v, rad, texture, rot) {
- this.m = m;
- this.r = r;
- this.v = v;
- this.rad = rad;
- this.texture = texture;
- this.rot = rot;
- }
-
- getTexture() {
- return allTextures[this.texture]["texture"];
- }
-
- getBumpMap() {
- return allTextures[this.texture]["bumpMap"];
- }
- getSpecularMap() {
- return allTextures[this.texture]["specularMap"];
- }
-
- set(r, v) {
- this.r = r;
- this.v = v;
- return this;
- }
-
- clone() {
- return new Body(this.m, this.r, this.v, this.rad, this.texture, this.rot);
- }
-}
+import Body from "./systems.es6.js"
-export default Body
-
-function loadTextures(textureName, textOn, bumpOn, specOn){
- let fullTexture = {};
- if (textOn) {
- let texture = THREE.ImageUtils.loadTexture("textures/" + textureName + "map.jpg" );
- texture.minFilter = THREE.LinearFilter;
- fullTexture.texture = texture;
- }
- if (bumpOn) {
- let bumpMap = THREE.ImageUtils.loadTexture("textures/" + textureName + "bump.jpg");
- bumpMap.minFilter = THREE.LinearFilter;
- fullTexture.bumpMap = bumpMap;
- }
- if (specOn) {
- let specularMap = THREE.ImageUtils.loadTexture("textures/" + textureName + "specular.jpg");
- specularMap.minFilter = THREE.LinearFilter;
- fullTexture.specularMap = specularMap;
- }
-
- return fullTexture;
-}
-
-function getRandomFromList(list) {
-
- return list[getRandomInt(0, list.length)];
-}
-
-export function genBodies(n, bodyTexture) {
-
- if (!bodyTexture){allTextures = [];}
- let bodies = [];
-
- 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);
-
- bodies.push(new Body(1E16, posVec, velVec, 8, getRandomFromList(planets), rotation));
- }
-
- return {
- bodies: bodies,
- stepsize: 0.001,
- camera: {x: 0, y: 0, z: 400}
- };
-}
-
-export function genBodiesRot(n, bodyTexture) {
- if (!bodyTexture){allTextures = [];}
- let bodies = [];
-
- let angMomVec = new Vec3(0,4,0);
-
- bodies.push(new Body(1E18, new Vec3(0, 0, 0), new Vec3(0, 0, 0), 16, "sun",
- new Vec3(0, 0.01, 0)));
- 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);
- velVec.crossVectors(posVec, angMomVec).multiplyScalar(Math.random());
- bodies.push(new Body(1E14, posVec, velVec, 8, getRandomFromList(planets), rotation));
- }
-
- return {
- bodies: bodies,
- stepsize: 0.001,
- camera: {x: 0, y: 0, z: 400}
- };
-}
+// console.log(allTextures);
function accel(i, b) {
// Compute acceleration due to gravity on body i by all other bodies
@@ -241,6 +117,27 @@ export function elasticCollision(b1, b2) {
b2.v = v2new;
}
-export function getRandomInt(min, max) {
- return Math.floor(Math.random() * (max - min)) + min;
-} // from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
+export function mergeCollision(bodies, spheres, scene, b1, b2) {
+ let bBig, bSmall;
+ if (b1.m > b2.m) {
+ bBig = b1, bSmall = b2;
+ console.log("merge!");
+
+ }
+
+ else {
+ bBig = b2, bSmall = b1;
+ console.log("merge!");
+
+ }
+ // console.log(bBig.m)
+ bBig.m += bSmall.m;
+ bBig.rad += 2;
+
+ bodies.splice(bodies.indexOf(bSmall),1);
+ scene.remove(spheres[bodies.indexOf(bSmall)]);
+ spheres.splice(bodies.indexOf(bSmall),1);
+ console.log("merge!");
+}
+
+
diff --git a/sunsistemo.es6.js b/sunsistemo.es6.js
index 2f56d3f..bafdb54 100644
--- a/sunsistemo.es6.js
+++ b/sunsistemo.es6.js
@@ -2,26 +2,42 @@ let scene, camera, light, renderer;
let controls, stats;
import Body from "./calc.es6.js";
-import * as calc from "./calc.es6.js";
import * as systems from "./systems.es6.js";
+import * as calc from "./calc.es6.js";
let bodyTexture = true;
-let numBodies = 300;
+let numBodies = 100;
let sphereP = 32;
-let system = calc.genBodiesRot(numBodies, bodyTexture);
-// let system = systems.genSolarSystem();
-let bodies = system.bodies;
-let [spheres] = init();
-
+let system
+// let system = systems.genBodiesRot(numBodies, bodyTexture);
+let bodies;
+let spheres;
+let sysDict = {
+ "Random Bodies": systems.genBodies ,
+ "Solar System": systems.genSolarSystem,
+ "Total Angular Momentum": systems.genBodiesRot,
+ "Three Bodies": systems.gen3Bodies
+};
var steps;
-if (system.hasOwnProperty("stepsPerFrame")) {
- steps = system.stepsPerFrame;
-} else {
- steps = 1;
-}
-animate_leapfrog();
+simulate("Random Bodies")
+
+
+function simulate(sysID){
+ let sysFunc = sysDict[sysID];
+ system = sysFunc(numBodies, bodyTexture)
+ bodies = system.bodies;
+ if (system.hasOwnProperty("stepsPerFrame")) {
+ steps = system.stepsPerFrame;
+ } else {
+ steps = 1;
+ }
+ [spheres] = init();
+ animate_leapfrog();
+
+ window.addEventListener('resize', onWindowResize, true);
+}
function init() {
@@ -49,7 +65,7 @@ function init() {
let material = new THREE.MeshPhongMaterial();
material.map = b.getTexture();
material.bumpMap = b.getBumpMap();
- material.bumpScale = 0.2;
+ material.bumpScale = 0.05;
material.specularMap = b.getSpecularMap();
let sphere = new THREE.Mesh(geometry, material);
sphere.position.set(b.r.x, b.r.y, b.r.z);
@@ -119,6 +135,8 @@ function animate() {
if ((i !== j) && (calc.touch(bodies[i],bodies[j]))) {
spheres[i].texture
calc.elasticCollision(bodies[i],bodies[j]);
+ // calc.mergeCollision(bodies, spheres, scene, bodies[i], bodies[j]);
+
}
}
}
@@ -136,8 +154,6 @@ function onWindowResize() {
renderer.setSize(window.innerWidth, window.innerHeight);
render();
}
-window.addEventListener('resize', onWindowResize, true);
-
function render() {
renderer.render(scene, camera);
diff --git a/systems.es6.js b/systems.es6.js
index 500a45b..2beb90b 100644
--- a/systems.es6.js
+++ b/systems.es6.js
@@ -1,7 +1,61 @@
-import Body from "./calc.es6.js";
import * as calc from "./calc.es6.js";
let Vec3 = THREE.Vector3;
+let planets = ["earth", "jupiter", "mars", "mercury", "moon", "neptune",
+ "pluto", "saturn", "uranus", "venus"];
+let balls = ["tennisball", "softball"];
+let textures = ["sun", "earth", "jupiter", "mars", "mercury", "moon", "neptune",
+ "pluto", "saturn", "uranus", "venus", "clouds", "tennisball"];
+// let allTextures = {};
+
+class Body {
+ constructor(m, r, v, rad, texture, rot) {
+ this.m = m;
+ this.r = r;
+ this.v = v;
+ this.rad = rad;
+ this.texture = texture;
+ this.rot = rot;
+ }
+
+ getTexture() {
+ return allTextures[this.texture]["texture"];
+ }
+
+ getBumpMap() {
+ return allTextures[this.texture]["bumpMap"];
+ }
+ getSpecularMap() {
+ return allTextures[this.texture]["specularMap"];
+ }
+
+ set(r, v) {
+ this.r = r;
+ this.v = v;
+ return this;
+ }
+
+ clone() {
+ return new Body(this.m, this.r, this.v, this.rad, this.texture, this.rot);
+ }
+}
+
+export default Body
+
+let allTextures = { "sun": loadTextures("sun", true, false, false),
+ "mercury": loadTextures("mercury", true, true, false),
+ "venus": loadTextures("venus", true, true, false),
+ "earth": loadTextures("earth", true, true, true),
+ "mars": loadTextures("mars", true, true, false),
+ "moon": loadTextures("moon", true, true, false),
+ "jupiter": loadTextures("jupiter", true, false, false),
+ "saturn": loadTextures("saturn", true, false, false),
+ "uranus": loadTextures("uranus", true, false, false),
+ "neptune": loadTextures("neptune", true, false, false),
+ "pluto": loadTextures("pluto", true, true, false),
+ "tennisball": loadTextures("tennisball", true, true, true),
+ "softball": loadTextures("softball", true, true, false)
+};
export function genSolarSystem() {
// Initial conditions of the solar system at 00:00:00 1 January 1970
@@ -128,3 +182,80 @@ export function gen3Bodies() {
camera: {x: 0, y: 0, z: 400}
};
}
+
+export function genBodies(n, bodyTexture) {
+
+ if (!bodyTexture){allTextures = [];}
+ let bodies = [];
+
+ 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);
+
+ bodies.push(new Body(1E16, posVec, velVec, 8, getRandomFromList(planets), rotation));
+ }
+
+ return {
+ bodies: bodies,
+ stepsize: 0.001,
+ camera: {x: 0, y: 0, z: 400}
+ };
+}
+
+export function genBodiesRot(n, bodyTexture) {
+ if (!bodyTexture){allTextures = [];}
+ let bodies = [];
+
+ let angMomVec = new Vec3(0,4,0);
+
+ bodies.push(new Body(1E18, new Vec3(0, 0, 0), new Vec3(0, 0, 0), 16, "sun",
+ new Vec3(0, 0.01, 0)));
+ 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);
+ velVec.crossVectors(posVec, angMomVec).multiplyScalar(Math.random());
+ bodies.push(new Body(1E14, posVec, velVec, 8, getRandomFromList(planets), rotation));
+ }
+
+ return {
+ bodies: bodies,
+ stepsize: 0.001,
+ camera: {x: 0, y: 0, z: 400}
+ };
+}
+
+
+function loadTextures(textureName, textOn, bumpOn, specOn){
+ let fullTexture = {};
+ if (textOn) {
+ let texture = THREE.ImageUtils.loadTexture("textures/" + textureName + "map.jpg" );
+ texture.minFilter = THREE.LinearFilter;
+ fullTexture.texture = texture;
+ }
+ if (bumpOn) {
+ let bumpMap = THREE.ImageUtils.loadTexture("textures/" + textureName + "bump.jpg");
+ bumpMap.minFilter = THREE.LinearFilter;
+ fullTexture.bumpMap = bumpMap;
+ }
+ if (specOn) {
+ let specularMap = THREE.ImageUtils.loadTexture("textures/" + textureName + "specular.jpg");
+ specularMap.minFilter = THREE.LinearFilter;
+ fullTexture.specularMap = specularMap;
+ }
+
+ return fullTexture;
+}
+
+
+function getRandomFromList(list) {
+
+ return list[getRandomInt(0, list.length)];
+}
+
+export function getRandomInt(min, max) {
+ return Math.floor(Math.random() * (max - min)) + min;
+} // from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random