Although this is not a full page of code, this is most likely the most suitable thing to include as a replacement (if i were to include a whole page of code to accurately show the depth of development then i would want to include my index file which is over 2,000 lines long so i feel that would be to much).
This code is the create moon function that i wrote, this this function i can create a moon with various sizes, masses and positions onto any object that ive already placed in my scene:
function createMoon(moonSize, moonMass, moonX, moonY, moonZ, targetPlanet){
var moonMat = moonVisMaterial[Math.floor(Math.random()*moonVisMaterial.length)];
var m = moonsPlanet.length;
var posX = targetPlanet.position.x;
var posY = targetPlanet.position.y;
var posZ = targetPlanet.position.z;
moonPlanetGeometry = new THREE.SphereGeometry(moonSize, 20, 15);
moonPlanetMaterial = moonMat;
moonPlanetMesh[m] = new THREE.Mesh(moonPlanetGeometry, moonPlanetMaterial);
moonPlanetShape[m] = new CANNON.Sphere(moonSize);
moonsPlanet[m] = new CANNON.RigidBody(moonMass, moonPlanetShape[m]);
moonsPlanet[m].position.set(posX + moonX,posY+moonY,posZ+moonZ);
moonsPlanet[m].velocity.set(0,0,8);
moonsPlanet[m].linearDamping = 0.0;
moonsPlanet[m].angularVelocity.set(Math.random()/2,Math.random(),Math.random()/2);
//collision filtergroup and maskgroups(who it can collide with)
moonsPlanet[m].collisionFilterGroup = objectsGrp;
moonsPlanet[m].collisionFilterMask = objectsGrp;
world.add(moonsPlanet[m]);
planetsMoonsContainer.add(moonPlanetMesh[m]);
moonsPlanet[m].preStep = function(){
moonsPlanet[m].force.set(0,0,0.01);
//get the vector point from the moon to the planet center
var moonsPlanet_to_planetsMoon = new CANNON.Vec3(targetPlanet.position.x -this.position.x,targetPlanet.position.y-this.position.y,targetPlanet.position.z-this.position.z);
//get distance from planet to moon
//var distance = moonsPlanet_to_planetsMoon.norm();
//force is pointing in the moon-plnaet direction
moonsPlanet_to_planetsMoon.normalize();
//Math.pow is to the power of (times the number by its self, however many times the 2nd integer states in this case 2)
moonsPlanet_to_planetsMoon.mult(/*6500/Math.pow(distance,2)*/60, this.force);
};
}
I am also going to include my collide function in this post to show a separate area of development that i have coded. The collide function handles everything from avatar scale changes(by calling my tweens) to the removal of the correct objects Cannon.js rigid body and Three.js mesh:
function colliderFunctions() {
//var p = planets.length;
avatarBody.addEventListener(“collide”, function(e) {
var scaleAmount = e.with.shape.radius / (avatar.scale.x * scaleValue);
if( e.with.shape.radius < newMass){
playHitSmaller();
//for testing
/*
if (ifColisionYet ==false){
scaleAmount = 3.5;
ifColisionYet = true;
}*/
scaleAvatarPlus.x = scaleAvatarPlus.y = scaleAvatarPlus.z += scaleAmount;
//scaleTween
tweenAvatarScalePlus.start();
//new avatar Phys Body
newMass = avRadius * avatar.scale.x;
//remove HitObj Phy
if( e.with == planets[planets.indexOf(e.with)]){
var objHit = planets.indexOf(e.with);
planetsContainerArea1.remove(planetsMesh[objHit]);
} else if (e.with == planetsMoon[planetsMoon.indexOf(e.with)]){
var objHit = planetsMoon.indexOf(e.with);
planetsMoonsContainer.remove(planetMoonMesh[objHit]);
} else if (e.with == moonsPlanet[moonsPlanet.indexOf(e.with)]){
var objHit = moonsPlanet.indexOf(e.with);
planetsMoonsContainer.remove(moonPlanetMesh[objHit]);
} else if (e.with == moons2Planet[moons2Planet.indexOf(e.with)]){
var objHit = moons2Planet.indexOf(e.with);
planetsMoonsContainer.remove(moon2PlanetMesh[objHit]);
} else if (e.with == mercuryPhysObj){
solarSystemContainer.remove(mercuryMesh);
} else if (e.with == venusPhysObj){
solarSystemContainer.remove(venusMesh);
} else if (e.with == earthPhysObj){
solarSystemContainer.remove(earthMesh);
} else if (e.with == marsPhysObj){
solarSystemContainer.remove(marsMesh);
} else if (e.with == jupiterPhysObj){
solarSystemContainer.remove(jupiterMesh);
} else if (e.with == saturnPhysObj){
solarSystemContainer.remove(saturnMesh);
} else if (e.with == uranusPhysObj){
solarSystemContainer.remove(uranusMesh);
} else if (e.with == neptunePhysObj){
solarSystemContainer.remove(neptuneMesh);
} else if (e.with == plutoPhysObj){
solarSystemContainer.remove(plutoMesh);
} else if (e.with == sunPhysObj){
solarSystemContainer.remove(sunMesh);
$(renderer.domElement).fadeOut(4000);
toggle();
cosmosCanStart == false
$(“#curtain”).fadeIn(500,function(){});
$(“#curtain #loadbar”).fadeIn(2500,function(){});
$(‘#curtain #loadbar #intro’).fadeIn(2500,function(){});
document.getElementById(“intro”).innerHTML = “Congratulations, you conquered the solar system! <br/><br/> Click here to try beat your score, or try on a different difficulty. <br/><br/> You achieved a highscore of: ” + highScore +” in: “+elapsedTime+” secounds <br/> “
$(‘#intro’).css({width: WIDTH, height: HEIGHT}).one(‘click’, function(e) {
location.reload();
});
cancelAnimationFrame(runScene);
}
world.remove(e.with);
} else if( e.with.shape.radius > newMass) {
playHitLarger();
if (Date.now() – lastCollide > 1000){
lives -= 1;
lastCollide = Date.now();
scaleAvatarMinus.x = scaleAvatarMinus.y = scaleAvatarMinus.z -= scaleAmount/difficulty;
tweenAvatarScaleMinus.start();
newMass = avRadius * avatar.scale.x;
world.remove(avatarBody);
avatarColliderMesh(newMass);
}
}
if (camStage !== 1 && newMass >= 3.5 && newMass <= 7 ){
tweenCam1.start();
playCameraPos();
tweenCamLookAt.start();
camStage = 1;
scaleValue = 35;
avatarLevel = 1200;
if(easy == true){
document.getElementById(“score”).style.color = ‘#47784D’;
}
} else if (camStage !== 2 && newMass >= 7 && newMass <= 13 ){
tweenCam2.start();
playCameraPos();
camStage = 2;
scaleValue = 20;
avatarLevel = 1500;
if(easy == true){
document.getElementById(“score”).style.color = ‘#B52BBD’;
}
} else if (camStage !== 3 && newMass >= 13 && newMass <= 17 ){
tweenCam3.start();
playCameraPos();
camStage = 3;
scaleValue = 10;
avatarLevel = 1800;
if(easy == true){
document.getElementById(“score”).style.color = ‘#FFFF19’;
}
} else if (camStage !== 4 && newMass >= 17 && newMass <= 25 ){
tweenCam4.start();
tweenCamLookAt2.start();
playCameraPos();
camStage = 4;
scaleValue = 4;
avatarLevel = 2000;
if(easy == true){
document.getElementById(“score”).style.color = ‘#FFFF19’;
}
} else if (camStage !== 5 && newMass >= 25){
tweenCam5.start();
tweenCamLookAt3.start();
playCameraPos();
camStage = 5;
scaleValue = 0.5;
avatarLevel = 2500;
if(easy == true){
document.getElementById(“score”).style.color = ‘#FFFF19’;
}
}
if(lives <= 0){
controlNodes.lives.innerHTML = lives;
$(renderer.domElement).fadeOut(4000);
toggle();
cosmosCanStart == false
$(“#curtain”).fadeIn(500,function(){});
$(“#curtain #loadbar”).fadeIn(2500,function(){});
$(‘#curtain #loadbar #intro’).fadeIn(2500,function(){});
document.getElementById(“intro”).innerHTML = “Oh no… You ran out of lives <br/><br/> Click here to try again! <br/><br/> You played for: “+elapsedTime+” secounds <br/>and achieved a highscore of: ” + highScore
$(‘#intro’).css({width: WIDTH, height: HEIGHT}).one(‘click’, function(e) {
location.reload();
});
cancelAnimationFrame(runScene);
}
});
}