Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to derive "standard" rotations from three.js when using quaternions?

Newbie stackoverflow participant, newbie 3D programmer, and far from a math wiz... so I'll try to frame this question as clearly as I can, hoping it makes sense, and hoping for an answer that's not a mile over my head.

I've written a very cool app using three.js that lets the user fly through 3D space and explore a solar system. The flight model is loosely based on the Fly.Controller example/extension in the three.js package which taught me to use quaternions for keeping all the axis rotations sensible relative to each other. The flying part all works great.

Here's my dilemma: When using quaternions, how do I deduce the "normal" (I don't know what else to call it) rotation values to determine which direction I am facing? When using quaternions, the "rotation" structure inside the camera object stays at 0,0,0. So, while I can freely fly through space at any angle, I can't figure out how to determine what direction I'm actually facing. Is there a built in three.js function, or other easy way to convert this?

I've found some similar, confusing, pointers on the web, but nothing I can decipher and put to use in three.js. Thanks.

like image 740
J Sprague Avatar asked Jan 05 '13 02:01

J Sprague


People also ask

How do you calculate the rotation of a quaternion?

For rotation quaternions, the inverse equals the conjugate. So for rotation quaternions, q−1 = q* = ( q0, −q1, −q2, −q3 ). Inverting or conjugating a rotation quaternion has the effect of reversing the axis of rotation, which modifies it to rotate in the opposite direction from the original.

What is X Y z and W in quaternion?

A quaternion can represent a 3D rotation and is defined by 4 real numbers. x, y and z represent a vector. w is a scalar that stores the rotation around the vector.

Why are quaternions useful for rotations?

Quaternions are very efficient for analyzing situations where rotations in R3 are involved. A quaternion is a 4-tuple, which is a more concise representation than a rotation matrix. Its geo- metric meaning is also more obvious as the rotation axis and angle can be trivially recovered.


1 Answers

This is a good question.

When an object is in it's default orientation, it can be considered to be looking in the direction of it's internal, positive z-axis. (The exception is the camera, which is looking in the direction of its internal negative z-axis.)

Therefore, when an object is rotated, you can get the direction the object is facing by applying the object's quaternion 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 the object is "facing".

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

EDIT: updated for r.58. Thanks @eshan.

like image 178
WestLangley Avatar answered Oct 21 '22 11:10

WestLangley