#### Archived

This topic is now archived and is closed to further replies.

# How to calculate a quaternion orientation?

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone, In my 3d engine, suppose I have an object at D3DXVECTOR3 position, and with orientation D3DXQUATERNION orientation. Now, I want this object to look at/or have an orientation towards another object, let''s say at D3DXVECTOR3 target. (I am keeping my position, orientation, etc. seperate and build them into a 4x4matrix) How do get the orientation quaternion? I can do it axis by axis through getting the angle object<->target Vec3Dot but this doesn''t seem to be the right way to do it. Any advice appreciated, Mark

##### Share on other sites
calculate a 3x3 matrix and convert to quaternion.

for a "look at" matrix, it's quite simple

Vector Dir  = (target - pos).UnitVector(); // direction of the cameraVector Up   = Vector(0, 1, 0); // you should specify this vector yourselfVector Side = Up.Cross(Dir).UnitVector(); // calculate the vector that goes across the cameraUp = Dir.Cross(Side); // re-normalise the camera's up vector

if you don't know what the Up vector should be, you can use the Up direction of the previous frame for the camera and plug it into the algo.

then you load the three vectors into a 3x3 matrix (side/up/dir vectors corresponds to the rows or columns of the matrix, depends if your matrix system is row major or column major) and convert to quaternion (should be a D3D support routine for that).

you may have a quaternion conversion function already, which takes two vectors and build a quaternion. Supplying the direction of the camera is not enough, you also need another reference vector to define the roll of the camera (like the up vector).

[edited by - oliii on September 21, 2003 2:53:49 PM]

##### Share on other sites
http://www.martinb.com/maths/algebra/vectors/angleBetween/index.htm

##### Share on other sites
Great! Thanks to both of you. The link is interesting.

Mark

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×