I have a Vector3 represents an Orientation in 3D , how to convert it to the corresponding Quaternion ? Is there any quick way ?

EDIT : I want to add an angular velocity vector to the Orientation Quaternion of the body


If your Vector3 stores an euler angle (yaw, pitch, roll) you can use the following static method directly:

Quaternion quaternion = Quaternion.CreateFromYawPitchRoll(vector.X, vector.Y, vector.Z);

On the other hand, if you're not storing an euler angle, but just the direction (or forward vector) of the camera/object, then bear in mind that generally a single 3D vector is not enough to store a complete orientation in 3D space unless you restrict one of the axes of rotation.

A typical case is where your camera/object can't roll or flip around and the Up vector is always pointing in the upper direction, e.g. FPS camera. If that's the case, then you should create a rotation matrix from your forward vector, and then use the following static method:

Quaternion quaternion = Quaternion.CreateFromRotationMatrix(matrix);

As for creating that rotation matrix, I think something like this should work (I hope so, I'm a little rusty with cross products):

Matrix matrix = Matrix.Identity;
matrix.Forward = vector;
matrix.Right = Vector3.Normalize(Vector3.Cross(matrix.Forward, Vector3.Up));
matrix.Up = Vector3.Cross(matrix.Right, matrix.Forward);

I think this may cause problems though if you try to look directly up or down, or close to those orientations, so you may need to handle those cases separatedly.

  • \$\begingroup\$ in my problem i have an angular velocity vector and i want to add it to the quaternion,so i will add the converted vector(quaternion after conversion) to the orientation Quaternion \$\endgroup\$ – MhdSyrwan Jan 7 '12 at 23:18
  • \$\begingroup\$ Then it's probably represented as an euler angle so just use the first method, and then use Quaternion.Concatenate to add it to the old quaternion (at least it's what I gather from the documentation). But try not to mix up which coordinate corresponds to yaw/pitch/roll. \$\endgroup\$ – David Gouveia Jan 7 '12 at 23:55
  • \$\begingroup\$ but,i think it's Quaternion.CreateFromYawPitchRoll(vector.Y, vector.X, vector.Z); not Quaternion.CreateFromYawPitchRoll(vector.X, vector.Y, vector.Z); isn't that right ? \$\endgroup\$ – MhdSyrwan Jan 8 '12 at 0:13
  • \$\begingroup\$ Yes it should be since Yaw = Y-axis rotation, Pitch = X-axis rotation, Roll = Z-axis rotation. Depends on how you (or some library you're using) set up those angular velocity values. \$\endgroup\$ – David Gouveia Jan 8 '12 at 0:24
  • 1
    \$\begingroup\$ @MhdSyrwan the velocity-vector may also be an axis-angle vector, which is basically a rotation axis and the angle encoded in the vector-length. Where does the velocity-vector come from? Did you compute it yourself or does it come from any library? \$\endgroup\$ – Maik Semder Jan 8 '12 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.