Search code examples
javascriptthree.jsrotationquaternions

How to set rotation of object 90 degrees along world x-axis?


Basically I want to set the rotation of an object along the world axis by an arbitrary angle. I'm currently using the rotateAroundWorldAxis function (found in another thread) to try to do that.

function render() {

  // update mesh position
  rotateAroundWorldAxis(mesh, new THREE.Vector3(1, 0, 0), angle);

  renderer.render(scene, camera);

}

function rotateAroundWorldAxis(object, axis, radians) {

  rotWorldMatrix = new THREE.Matrix4();
  rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
  rotWorldMatrix.multiply(object.matrix); // pre-multiply
  object.matrix = rotWorldMatrix;
  object.rotation.setFromRotationMatrix(object.matrix);

}

The problem is I only want to set the angle (as in rotation.x = angle), not increment it (as in rotation.x += angle). Does anybody know how to adapt the above function (rotateAroundWorldAxis) to allow me to set the angle instead of incrementing it?

jsfiddle here


Solution

  • remove the third line in that function

    rotWorldMatrix.multiply(object.matrix); // pre-multiply

    This is the line that "adds" the two rotations together.