How not to do a camera class in XNA…

So a while back before Christmas I was messing around with trying to get a full 3D mouselook working – meaning that you look around with the mouse in 3D, but also have the freedom to roll the camera. This is really more ideal for an outer-space setting, as we as humans don’t normally roll our heads.

Anyway, I was trying to accomplish this by keeping track of several vectors – the camera position, look-at point, and a vector facing to the right of the camera. I was rotating them using a series of calculations (sin/cos, etc). I did get it working, more or less, but it still had some pretty big issues. For example, when you were sitting level and looked left or right, it worked fine. When you rotated the camera 90 degrees, and then moved the mouse left or right the camera would look up or down, instead of looking left/right. Either way, something was borked.

Today I had a bit of time between classes so I did some digging around and came across this wonderful little series of gems:
How To: Rotate and Move a Camera
How To: Make a First-Person Camera
How To: Make a Third-Person Camera

More importantly, I stumbled across this: Matrix.CreateFromYawPitchRoll Method. This is certainly a heck of a lot easier than what I was doing. Instead, now all I need to do is track my yaw, pitch, and roll, use the aforementioned function to rotate my points (the camera position, look-at, and up vector), and feed those into the Matrix.CreateLookAt method.

Not too shabby, eh? I’ll have to whip this up and see how it turns out.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s