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);
}
Community
  • 1
  • 1
shoes
  • 15
  • 6
  • 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 '14 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 '14 at 20:58
  • first: `earthAxis.normalize();` then in your loop: `earth.rotateOnAxis( earthAxis, 0.02 );` – WestLangley Dec 10 '14 at 21:37
  • @WestLangley THANKS SO MUCH!! That works perfectly and it is nice and simple. – shoes Dec 10 '14 at 22:14

1 Answers1

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

WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • 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 '14 at 00:05
  • Please, in the future, avoid changing the question after it has been answered and accepted. It is better to make a new post. – WestLangley Dec 11 '14 at 00:45