0

I am trying to rotate earth about it's tilted axis in three js. I found this solution, and I am trying to use it on my code, but it doesn't do anything.

When I execute this code the planet just sits there and doesn't rotate at all. I don't really have a firm grasp of what a quaternion is or how it works, so I am not sure what is going wrong.

function rotateAroundAxis(object, axis, radians) {
    var vector = axis.normalize();
    var quaternion = new THREE.Quaternion().setFromAxisAngle(vector, radians);
    object.rotation = new THREE.Euler().setFromQuaternion( quaternion );
}

earthAxis = new THREE.Vector3(Math.cos(23.4*Math.PI/180), Math.sin(23.4*Math.PI/180), 0);

function render() {
    stats.update();
    step += 0.02;
    rotateAroundAxis(earth, earthAxis, step);
}
4
  • Just some sanity checking - you're calling render in a requestAnimationFrame loop right? What is the source of the rotateAroundAxis function?
    – Andy Ray
    Dec 10, 2014 at 20:47
  • Yes I am and I updated the original post with the rotatearoundaxis function. I can post the entire code, but it is pretty long.
    – shoes
    Dec 10, 2014 at 20:58
  • first: earthAxis.normalize(); then in your loop: earth.rotateOnAxis( earthAxis, 0.02 ); Dec 10, 2014 at 21:37
  • @WestLangley THANKS SO MUCH!! That works perfectly and it is nice and simple.
    – shoes
    Dec 10, 2014 at 22:14

1 Answer 1

3

First, you need to tilt your sphere's geometry by 23.4 degrees by applying a transformation to it.

var radians = 23.4 * Math.PI / 180; // tilt in radians
mesh.geometry.applyMatrix( new THREE.Matrix4().makeRotationZ( - radians ) );

Then, to rotate your earth on its axis in object space, first normalize the axis you are rotating around.

earthAxis = new THREE.Vector3( Math.sin( radians ), Math.cos( radians ), 0 ).normalize();

Then in your render function, do this:

earth.rotateOnAxis( earthAxis, 0.01 ); // axis must be normalized

three.js r.69

2
  • So this works and it rotates it about the vector, but from the upright position. What I need is to tilt the earth by 23.4 degrees then rotate about that which this does not seem to do.
    – shoes
    Dec 11, 2014 at 0:05
  • Please, in the future, avoid changing the question after it has been answered and accepted. It is better to make a new post. Dec 11, 2014 at 0:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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