How to get the "standard" rotation of three. Js when using quaternions?

A newbie to the stackoverflow member, a newbie to a 3D programmer and far from mathematical math wiz ... so I will try to formulate this question as clearly as I can, hoping it makes sense, and hoping for an answer that my mind will not take.

I wrote a very cool application using three.js that allows the user to fly through 3D space and explore the solar system. The flight model is freely based on the Fly.Controller example / extension in the three.js package, which taught me how to use quaternions to support all axis rotations relative to each other. The flying part works great.

Here's my dilemma: when using quaternions, how can I determine the "normal" (I don’t know what else to call) rotation values ​​to determine which direction I am facing? When using quaternions, the “rotation” structure inside the camera object remains equal to 0.0.0. Therefore, although I can fly freely in space from any angle, I cannot figure out how to determine in which direction I have encountered. Is there a built-in function three.js or another simple way to convert it?

I found several similar, confusing pointers on the Internet, but I can not decrypt and use anything for use in three.js. Thanks.

+8
rotation quaternions
source share
2 answers

Thanks for the quick reply - this was not quite what I was looking for, but I probably did not know how to clearly ask the question. My specific use case was that I wanted to draw a two-dimensional map represented by the relative positions of all the objects in my 3D scene, but I wanted to rotate the objects on the map based on camera yaw in the 3D scene - so I needed to know the “angle” that the quaternion-based camera came across so I can accordingly compensate for the rotation of 2D objects on the map. Seems to work very well. I just want not to have so many calculations, but at least Javascript is fast.

// Pass the obj.quaternion that you want to convert here: //********************************************************* function quatToEuler (q1) { var pitchYawRoll = new THREE.Vector3(); sqw = q1.w*q1.w; sqx = q1.x*q1.x; sqy = q1.y*q1.y; sqz = q1.z*q1.z; unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor test = q1.x*q1.y + q1.z*q1.w; if (test > 0.499*unit) { // singularity at north pole heading = 2 * Math.atan2(q1.x,q1.w); attitude = Math.PI/2; bank = 0; return; } if (test < -0.499*unit) { // singularity at south pole heading = -2 * Math.atan2(q1.x,q1.w); attitude = -Math.PI/2; bank = 0; return; } else { heading = Math.atan2(2*q1.y*q1.w-2*q1.x*q1.z , sqx - sqy - sqz + sqw); attitude = Math.asin(2*test/unit); bank = Math.atan2(2*q1.x*q1.w-2*q1.y*q1.z , -sqx + sqy - sqz + sqw) } pitchYawRoll.z = Math.floor(attitude * 1000) / 1000; pitchYawRoll.y = Math.floor(heading * 1000) / 1000; pitchYawRoll.x = Math.floor(bank * 1000) / 1000; return pitchYawRoll; } // Then, if I want the specific yaw (rotation around y), I pass the results of // pitchYawRoll.y into the following to get back the angle in radians which is // what can be set to the object rotation. //********************************************************* function eulerToAngle(rot) { var ca = 0; if (rot > 0) { ca = (Math.PI*2) - rot; } else { ca = -rot } return (ca / ((Math.PI*2)/360)); // camera angle radians converted to degrees } 
0
source share

This is a good question.

When an object is by default the default, it can be considered as focusing on its internal positive z axis. (An exception is a camera that is looking in the direction of its internal negative z axis.)

Therefore, when the object rotates, you can get the direction that the object collides by applying the quaternion of the object to a unit vector pointing in the direction of the positive z axis (negative z axis for the camera).

 var zVec = new THREE.Vector3( 0, 0, 1 ); zVec.applyQuaternion( object.quaternion ); 

This will return a unit vector pointing in the direction in which the object is “facing”.

If the object is a child of another rotated object, the situation is more complicated.

EDIT: Updated for r.58. Thanks @ishan.

+4
source share

All Articles