Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
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 –  MhdSyrwan Jan 7 '12 at 23:18
    
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. –  David Gouveia Jan 7 '12 at 23:55
    
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 ? –  MhdSyrwan Jan 8 '12 at 0:13
    
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. –  David Gouveia Jan 8 '12 at 0:24
1  
@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? –  Maik Semder Jan 8 '12 at 10:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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