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
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
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.