0

I try to make the solar system. My planets move in circles:

this.obj.position.x = (this.positionX*20) * Math.cos( this.ange );
this.obj.position.z = (this.positionX*20) * Math.sin( this.ange );
this.ange = this.ange + this.rotationAhisSpeed;

How do rotate the moon around the earth? I know the position of the earth, but how to calculate the cosine and sinus?

2 Answers 2

3

Making one object circle another object is fairly simple.

let earth = {
    x,
    y,
    z,
    orbitDistance,
    angle,
    angleSpeed
}

let moon = {
    x,
    y,
    z,
    orbitDistance,
    angle,
    angleSpeed
}

// Assume the object properties are filled out

earth.x = earth.orbitDistance * Math.sin( earth.angle );
earth.z = earth.orbitDistance * Math.cos( earth.angle );

// Starting from the Eath's position will make it the origin of the Moon's orbit
moon.x = earth.x + moon.orbitDistance * Math.sin( moon.angle );
moon.z = earth.z + moon.orbitDistance * Math.cos( moon.angle );

earth.angle += earth.angleSpeed;
moon.angle += moon.angleSpeed;
4
  • Look here please. Line 108 and the line 144. I tried to interpret your code, but my moon fall into the Suns. ((( stekolschikov.info/demo/SS
    – VINET
    Commented Feb 24, 2017 at 9:12
  • I've looked at your code, at lines 147-149. It seems like you're calculating the correct position but might not be correctly updating the position of the Moon's mesh.
    – Mr. Reddy
    Commented Feb 24, 2017 at 9:39
  • Probably did not understand, I changed position on lines 149,150,151
    – VINET
    Commented Feb 24, 2017 at 10:19
  • Perhaps it should be this.obj.position.x = earthX + 100 * Math.cos( this.ange );
    – Mr. Reddy
    Commented Feb 24, 2017 at 12:11
0

need to times that instead of plus

var count = 0;
render(){
var count = count + 1;
earth.position.x = (position.x * math.cos(count)) * 15;
earth.position.z = (position.z * math.sin(count)) * 15;
}
 sun.add(earth);

earth.updateMatrixWorld(); 
var worldMatrix = earth.matrixWorld;
var worldpos = new THREE.Vector3().setFromMatrixPosition(worldMatrix);`
earth.position.x = worldpos.x;
earth.position.y = worldpos.y;
earth.position.z = worldpos.z;

scene.add(earth);
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 18, 2022 at 3:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.