dhpoware logo
home products source demos contact us
navbar navbar
box top left corner box top right corner
XNA 4.0 First Person Camera Demo.

This Windows XNA application implements a first person style interactive camera. A reusable FirstPersonCamera class is provided that supports such features as smooth mouse movement, walking, running, crouching, and jumping.

The room is parallax normal mapped using a custom .fx shader and the weapon is lit per pixel using the XNA 4.0 BasicEffect class.

To learn more about parallax normal mapping in XNA check out the XNA 4.0 Parallax Normal Mapping Demo.
To learn more about other camera techniques in XNA check out the XNA 4.0 Camera Demo.

1. This demo requires shader model 2.0 or higher support.
2. This demo is provided in source form only. No pre built binaries are supplied.


zip file Download source, assets, and XNA Game Studio 4.0 solution files.

Change History:

15 January 2011.
Initial release of demo for XNA Game Studio 4.0. The previous XNA Game Studio 3.0 version of the demo is no longer supported and is now only available upon request.

2 February 2009.
FirstPersonCamera.cs: Fixed bug in UpdatePosition() method where CurrentVelocity.Y was not being reset to zero when the camera had finished rising to the EyeHeightStanding Y position. This was causing the camera's eye position to be slightly higher than the EyeHeightStanding.Y position after the camera rose from a crouching posture. Thanks goes to David Reilly for spotting this bug.

16 November 2008.
Initial release of demo for XNA Game Studio 3.0. The previous XNA Game Studio 2.0 version of the demo is no longer supported and is now only available upon request.

24 August 2008.
FirstPersonCamera.cs: Updated the elapsed time calculation in the UpdateCamera() method. The elapsed time in seconds is now calculated based on whether the Game is running with a fixed time step.

30 January 2008.
Initial release of demo for XNA Game Studio 2.0. The original XNA Game Studio Express 1.0 Refresh version of the demo is no longer supported and is now only available upon request.

4 January 2008.
Demo.cs: Removed attenuation field from the Light structure. Added light radius field to the Light structure. Added a simple frame rate counter. Implementation is in the UpdateFrameRate() method. This method is called once per frame in the Draw() method.

parallax_normal_mapping.fx: Switched to a new light attenuation method based on a light radius. Changed vector outputs from the vertex shaders to float3. Fixed bug in vertex shaders where the worldInverseTransposeMatrix was not being cast to a float3x3. Updated the pixel shaders to use the float3 vector outputs from the vertex shaders.

15 December 2007.
FirstPersonCamera.cs: Added 4 new fields: forwardsPressed, backwardsPressed, strafeRightPressed, and strafeLeftPressed. These new fields track the state of the movement keys. Modified the GetMovementDirection() method to checked when the keys for moving forwards, backwards, strafe right, and strafe left are first pressed. When a movement key is first pressed the current velocity for movement in that direction is reset back to zero. This prevents the camera from slowly decelerating and then accelerating in the desired direction of movement.

Thanks goes to Pablo Alonso for contributing this fix.

11 December 2007.
Demo.cs: Added CAMERA_RUNNING_JUMP_MULTIPLIER constant. Changed CAMERA_RUNNING_MULTIPLIER from 2.5 to 2.0. In the Initialize() method the camera running velocity is now explicitly set.

FirstPersonCamera.cs: Removed Falling from the Posture enumeration. Removed the EyeHeightCrouching and EyeHeightJumping properties. Removed the eyeHeightJumping field. The max height when jumping is now determined only by the acceleration.Y and velocity.Y values. In the GetMovementDirection() method the jumping logic has been updated. When a jump is initiated the current velocity in the Y direction is now set to velocity.Y. Updated the UpdateVelocity() method to distinguish between jumping and crouching. Jumping will always have a negative acceleration applied to the velocity to simulate the effects of gravity. Crouching works the same as before.

1 December 2007.
New normal maps added.

Demo.cs: Complete rewrite. Now supports full screen and windowed modes. Added text support. Added ability to turn on and off mouse smoothing. Added ability to adjust the camera rotation speed.

NormalMappingUtils.cs: Replaced the NormalMappedVertex.CalcTangentSpaceVectors() method with the NormalMappedVertex.CalcTangent() method. Removed the bitangent field from the NormalMappedVertex structure.

parallax_normal_mapping.fx: Fixed a bug in the vertex shaders where the tbnMatrix was being incorrectly calculated. The vertex shaders now calculate the bitangent vectors. The vertex shaders now use the handedness stored in the tangent vectors when calculating the bitangent vectors. Removed the no texturing techniques. Added techniques for parallax normal mapping and normal mapping.

FirstPersonCamera.cs: Complete rewrite. Added sample usage instructions and code example in class comments. Head bobbing has been removed. Weapon sway and/or bobbing will replace head bobbing. This will be added in a future version of this class. Fixed several bugs in the camera physics code.

28 April 2007.
Demo.cs: Now uses the XNA default clockwise triangle winding order.

NormalMappingUtils.cs: Updated the NormalMappedRoom class to use XNA's default clockwise triangle winding order.

weapon.fbx: Reversed the polygon winding order.

10 March 2007.
FirstPersonCamera.cs: Added WeaponWorldMatrix() method.

Demos.cs: Refactored previous weapon transformation code to the WeaponWorldMatrix() method in the FirstPersonCamera class. Updated FLOOR_CLIP_BOUNDS constant to prevent the camera from getting too close to the walls. When the camera gets too close to the wall the player weapon looks too small in comparison to the brick textures on the walls.

8 March 2007.
Demo updated to support a player weapon attached to the first person camera.

FirstPersonCamera.cs: Added new field accumHeadingDegrees. Added new properties: Heading and Pitch. These return the camera's current Euler heading and pitch angles in degrees.

Demo.cs: Added new constants used to position the player weapon: WEAPON_X_OFFSET, WEAPON_Y_OFFSET, and WEAPON_Z_OFFSET. Added a new constant used to scale down the size of the player weapon: WEAPON_SCALE. Added new method UpdateWeaponWorldTransformation() to calculate the weapon's world matrix. This matrix positions and orients the player weapon relative to the first person camera's current world position.

box bottom left corner content box box bottom right corner
logo logo