Thursday 29 January 2009

Quaternian Cameras and rotation

I have just had one of the most annoying coding sessions in recent days. It all started when I wanted to create a simple quaternian free roaming camera. Everything seemed to be working ok until I wanted to rotate the camera and have it move in the direction I had just rotated to.

Immediately the camera began moving on the exact opposite angle. So if I rotated the camera 90 degrees up, the camera direction vector would point 90 degrees down. Looking at the movement code showed no errors so what the heck was going on!

I finally decided that unless I was going mad.The error had to be with the camera direction vector itself. The direction was being set to view.forward and so I couldn't figure out how that could return an exact mirrored direction to the one that the camera was facing. It was only by chance that I noticed the direction was set not from the original matrix but from the inverted matrix. When I took the direction from the non inverted view matrix. suddenly my camera worked correctly.

The only puzzle now is why every example of a quaternian camera I've ever seen, seems to use the inverted view. Given I'm only ever adding the position to the normalized movement vector. I know that inverting the direction on that would definitely result in the camera moving in the wrong direction and at a mirrored angle.

Are other cameras using the inverted view and then simply un-inverting it again. Are they doing more math on the direction to change it from inverted to uninverted? Are they using an imaginary target and then creating a lookat matrix to that? Isn't all of that that silly? Doesn't taking the uninverted version in the first place (which will always point into the view) simply make more sense? What the heck am I missing?

No comments: