Site Sections

Sunday, August 5, 2007

Quaternion

So today I was working on Indy Game and I was trying to get the slerping working (fun flybys - Check out the Hazy Mind site for more info) for the turning of the camera when going through doors. Anyways... Yea, Michigan Tech is not exactly the best school in the world for graphics. See, I didn't hear about Quaternions until just recently (actually until I read the Hazy Mind site myself). I've taken graphics courses and although they were not very good, they were all in OpenGL but not in the sense of learning OpenGL, but to instead learn the basics of rasterization, line/point theory, phong shading, and stuff like that... I mean we didn't even get into any model file formats, or level design or anything of real use when developing game engines.

Enough ranting... The reason I am writing this is because today when I was trying to get the slerping working I kept having this problem with my camera over-rotating. It would rotate to almost the place I wanted it but not quite right. It always seemed like it over shot its target axis that I thought I was defining in my Quaternion that I was passing as a target rotation to the Slerp function in my engine.

Here was my code:


HMCameraManager.ActiveCamera.Slerp(HMCameraManager.ActiveCamera.Rotation *
new Quaternion(0, 1, 0, 1), .45f, SlerpType.Revolve);


The idea here was that I was attempting to have the camera "Slerp" 90 degrees to re-adjust the camera for when a player went through a door. I am hoping that this gives the player a real chance to see that the world they are in is really in fact a 3D world even though they are restricted to a 2D plane of movement (except for the doors of course). Well it seems that I don't really understand Quaternions all that well... the X, Y, Z components are not the directly related to which axis to rotate to. Well, after about 4-5 hours of tinkering and a lot of trial and error, I fixed my problem, it really was quite simple and I hate that it took me so long.

Here was my fix - Don't laugh (ok, you can laugh a little):

HMCameraManager.ActiveCamera.Slerp(HMCameraManager.ActiveCamera.Rotation
* Quaternion.CreateFromAxisAngle(new Vector3(0, -1, 0),
MathHelper.ToRadians(90)), .45f, SlerpType.Revolve);

You can see here that my change involved a little more calculation to create a Quaternion that I know for a fact is a 90 degree rotation. Makes things a little simpler in my mind, even if it does look a little sloppier. If anyone else has a better suggestion, please feel free to let me know.

No comments: