Today we'll be writing a FirstPerson Camera class. We're using OOP because chances are high that we'll want more than one Camera, and the ability to "attach" Cameras to various objects (imagine a Camera attached to the rocket grenade you just launched at some enemy). Even though the Camera is not a simple thing, it's something we will want early on just so we can explore 3D space in our demos. For this reason I am introducing it now. Let's just jump in the deep end - sink or swim !! .data fMouseSensitivity REAL4 120.0f ;<-- Provides some control over mouse sensitivity (larger=less sensitive) fCameraDeceleration REAL4 0.8f ;<-- Immitates friction by slowing the Camera velocity forward or back .code class Camera, ,C++ compatible void CheckKeys void CheckMouse void ChangeVelocity:fvel void ChangeHeading:fDegrees void ChangePitch:fDegrees void SetPerspective float MaxPitchRate float MaxHeadingRate float MaxForwardVelocity float PitchDegrees float HeadingDegrees float ForwardVelocity long qHeading ;<-- ptrs to Quaternion objects long qPitch ;<-- holding axial rotations long pFrustum ;<-- ptr to this Camera's CFrustum object (used for culling during rending) des Position Vec3 des DirectionVector Vec3 endclass Let's ignore the Class Methods for the moment and just examine the DataFields of the class object. "Pitch" refers to "tilt up and down", ie, rotation about the X axis. It follows then that MaxPitchRate determines the maximum change in Pitch angle per timestep. "Heading" refers to the "turn left and right", ie rotation about the Y axis. It follows then that MaxHeadingRate determines the maximum change in Heading angle per timestep. "ForwardVelocity" is the camera's current Speed Of Travel in whatever direction it happens to be facing. "Negative ForwardVelocity" simply means you are travelling Backwards from the direction you are facing. It follows then that MaxForwardVelocity determines the maximum Speed Of Travel forward/back. These limiting values prevent you from rotating your view at breakneck speed, and from accelerating beyond the speed of light, which gives the camera a little more "real world" feel (more on that shortly). PitchDegrees and HeadingDegrees contain rotation angles in floatingpoint degrees (0 to 360.00000). It would be far more efficient to use "radians" to express these angles, but I used regular degrees for the express purpose of maintaining the sanity of the novice reader of this tutorial. As it stands, the angles are converted to radians within the code before being applied... ForwardVelocity contains a value that ranges from minus MaxForwardVelocity to plus MaxForwardVelocity. Zero velocity means you are not moving. Simple enough? You might care to think of Velocity as Speed, since Speed measures "distance over time", and so does Velocity. The main difference is that Velocity is "Speed in a Straight Line", whereas Speed is based on distance/time regardless of change in direction. PitchDegrees, HeadingDegrees and Velocity are controlled by the Mouse and Keyboard. The actual camera rotation as applied in 3D space is stored as two "Quaternions", because they are less prone to "numerical drift" than matrices or other mechanisms are. The camera contains a pointer to a "CFrustum" object, which we'll talk about later. In order to USE this camera stuff yourself, theres no need to initialize anything. Just call SetPerspective from within your Render code to set the Camera view, then render all your objects. Finally, call CheckMouse and CheckKeys to update (if necessary) the camera view angles and stuff. In order to make our virtual camera feel a little more "real", we have two constants which control the mouse sensitivity and "camera deceleration". Deceleration is what slows the camera down when its moving, analagous to "air resistance in the direction of travel". Basically we simply reduce the current velocity by multiplying it by a fraction greater than 0 and less than 1. This means that when we stop accelerating, the camera continues moving for just a moment as it slows to a stop. ChangeVelocity, ChangeHeading and ChangePitch methods are called in response to user input either by mouse or by keyboard to change the current camera values. I don't think it's warranted to look too closely at the Camera code yet, lets implement the darn thing, prove it works, then study the code ourselves, shall we? I will publish an extension to this document if I receive enough requests to do so, which can be made to user EvilHomer2k, in the GameCoding forum of the public messageboard http://board.win32asmcommunity.net