Public Group

# Rotating the camera with quaternions

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

## Recommended Posts

Hello,

I am trying to implement my own third person camera using quaternions in DirectX. I first wanted to check the rotations are fine so I started to rotate around the world y-axis (0,1,0) and then x-axis (1,0,0) and it worked fine. But when rotating around the axis (1,1,0) the effect is not what I expected. Here is a description of what I do.

The scene is composed by a cube of side 2, centered at the world origin. The camera is originally placed at (0,0,-10), looking at the origin (0,0,0) and has the up vector (0,1,0). To test the rotation, at each frame I increase the angle of rotation theta by a constant amount dTheta. I build my quaternion from the axis of rotation (1,1,0) and the angle theta, transform the quaternion to its 3-by-3 matrix form and multiply both the position vector and up vector. I call the D3DXMatrixLookAtLH function to build the view matrix with the new position, the lookAt point (the origin of the world frame) and the new up vector.

What I get is my cube rotating in a very strange way (well, I know it is the camera that is rotating, but you know what I mean, everything is relative). Moreover it seems the distance between the cube and the camera changing because, as the cube rotates, its size oscillates.

I have also tried to use a vector colinear to the axis of rotation for the up vector of the camera but the result is similar: the rotation is not conform to what one can expect et there is still the zoom in / zoom out effect.

I can put some code if someone asks but I first wanted to check there is no mistake in my approach. Thanks.

Edited by Itsbananas

##### Share on other sites

Are you sure you are converting the quaternion to matrix in the way you expect? It's not hard to produce a matrix that rotates the world instead of the camera (code would be helpful to see).

##### Share on other sites

Hello swiftcoder and thank you for your response.

Well, after five days of research, I feel dumb... It turns out that my function that normalizes the quaternion only normalizes [w,x,y,z] but does not recompute the rotation matrix. I feel very.... dumb. It works fine now.

If it can helps other people for their own implementation, I can put my code here. Just ask if you want.

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 10
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013149
×