1
\$\begingroup\$

I am trying to implement my own third person camera using quaternions in DirectX. I first wanted to check that the camera rotates as expected so I started to rotate it 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 can put some code if someone asks but I first wanted to check there is no mistake in my approach. Thanks.

\$\endgroup\$
  • \$\begingroup\$ I've just tried putting the up vector colinear to the rotation axis which is maybe more realistic but I still get a similir strange result. \$\endgroup\$ – Itsbananas Dec 4 '15 at 20:40
  • \$\begingroup\$ I suspect the camera is being rotated (that is, its Up no longer matches the global Up) rather than being orbited around the axis in question and maintaining its facing. \$\endgroup\$ – Draco18s Dec 4 '15 at 22:20
  • \$\begingroup\$ When you say "global up" I think you mean the y-axis of the world frame. I do rotate it too. I also rotate the camera position vector ( which is the camera position point - lookAt, but since lookAt is always the world origin, the vector position has the same coordinate as the position point of the camera). All the coordinates are related to the world frame. \$\endgroup\$ – Itsbananas Dec 4 '15 at 22:56
  • \$\begingroup\$ Yes, the y-axis. I had a brain fart over what to call it. \$\endgroup\$ – Draco18s Dec 5 '15 at 2:35
  • \$\begingroup\$ This is the kind of question where a video or animated gif of what you're seeing would be invaluable for diagnosing the issue. Code helps too, of course. \$\endgroup\$ – DMGregory Dec 5 '15 at 6:08
0
\$\begingroup\$

Quaternions can encode both a rotation and a uniform scaling using non-normalized quaternion, this might be what is happening.

The axis of rotation (1, 1, 0) is not nomalized. Try with (0.707106781, 0.707106781, 0) which is the unit vector pointing in the same direction.

\$\endgroup\$
  • \$\begingroup\$ Well, I did try this in the early days of my debugging but I remember this did not change anything. The weird thing is that it was the problem because I've just solved it yesterday evening: it turns out that my function that normalizes my quaternion was only updating the [w,x,y,z] but did not update the rotation matrix. I feel stupid... \$\endgroup\$ – Itsbananas Dec 5 '15 at 12:50

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.