Rotate an object around a world axis

I am trying to rotate an object around the axis of the world. I found this question: How to rotate an object in the world of the three.js axis?

But this did not help to solve this problem:

var rotWorldMatrix; // Rotate an object around an arbitrary axis in world space function rotateAroundWorldAxis(object, axis, radians) { rotWorldMatrix = new THREE.Matrix4(); rotWorldMatrix.makeRotationAxis(axis.normalize(), radians); rotWorldMatrix.multiplySelf(object.matrix); // pre-multiply object.matrix = rotWorldMatrix; object.rotation.getRotationFromMatrix(object.matrix, object.scale); } 

multiplySelf and getRotationFromMatrix not defined (I get a console error). How to fix the problem?

Refresh

I tried using Quaternion , but it doesn't seem to work correctly. I am trying to rotate an object on user click, this is the function I wrote:

 function mouseUp(event) { var x= event.clientX; var y= event.clientY; var dx= (x - xBegin); var dy= (y - yBegin); var quaternion= new THREE.Quaternion(); quaternion.setFromAxisAngle(new THREE.Quaternion(dy,dx,0).normalize(),Math.sqrt(dx*dx+dy*dy)/250.0); object.quaternion.multiplyQuaternions(quaternion,object.quaternion); } 

It rotates correctly while the object is in vertical or horizontal position, but if, for example, it is 45 degrees from the x axis, it rotates very slowly and in the opposite direction of the click.

+1
javascript rotation
source share
1 answer

As long as the object does not have a rotated parent, you can rotate the object around the World axis like this:

 var q = new THREE.Quaternion(); // create once and reuse q.setFromAxisAngle( axis, angle ); // axis must be normalized, angle in radians object.quaternion.premultiply( q ); 

three.js r.78

+6
source share

All Articles