Skip to content

Instantly share code, notes, and snippets.

@lazd
Created January 25, 2013 01:23
Show Gist options
  • Save lazd/4630739 to your computer and use it in GitHub Desktop.
Save lazd/4630739 to your computer and use it in GitHub Desktop.
THREE.JS: Rotate an object in 3D space given the position of the axes of a gamepad
var xRot = gamepad.y;
var yRot = gamepad.x;
// Rotate X relative to model axis
object.matrix.rotateX(xRot);
// Rotate Y relative to world axis
var axis = new THREE.Vector3(0,1,0);
var rotWorldMatrix = new THREE.Matrix4();
rotWorldMatrix.makeRotationAxis(axis.normalize(), yRot);
rotWorldMatrix.multiplySelf(object.matrix); // pre-multiply
object.matrix = rotWorldMatrix;
object.rotation.setEulerFromRotationMatrix(object.matrix, 'XYZ');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment