However, like all frameworks that aren't quite mature there is still a lot left to be desired by the XNA framework and its supporting IDE (XNA Express). Okay so most of my complaints are with the latter but I won't get into that now, instead I'm going to go over a quick and dirty way to create a first person camera perspective. Anyone who's played and FPS should have an idea of what I'm talking about.
NOTE: A lot of the code below is based on the How To Display a 3D Model, I posted about before. So I'd recommend checking that little tutorial before cutting your teeth on this one.
Now since I'm assuming you understand how to display 3D models in your code and have a working little demo of it I'm going to explain what you need to create your first person camera. The idea is very simple, in the 3D world you have objects, a camera (think of it as your eyes), and most importantly you have a point of reference for your view. Now I'm not very good at explaining the technical aspects of 3D graphics so just bare with me.
The idea of camera requires two things one of which is obviously the camera but in this case I'm referring to its position in 3D space. The second is its reference perspective which is a really fancy way of saying what direction in 3D space the camera is looking. This is important for this tutorial since its the reference position we'll be manipulating to create a pretty basic first person perspective. Still with me? Great!
So let's look at some code, more specifically the Draw routine in your XNA project, if you followed the tutorial you'll see something like this:
protected override void Draw( GameTime gameTime )
{
graphics.GraphicsDevice.Clear( Color.CornflowerBlue );
// Copy any parent transforms.
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo( transforms );
// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in myModel.Meshes)
{
// This is where the mesh orientation is set,
//as well as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationY( modelRotation ) *
Matrix.CreateTranslation( modelPosition );
effect.View = Matrix.CreateLookAt( cameraPosition,
Vector3.Zero,
Vector3.Up );
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians( 45.0f ),
aspectRatio, 1.0f, 10000.0f );
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
}
What we're interested in is the line that looks like the following:
effect.View = Matrix.CreateLookAt( cameraPosition,
Vector3.Zero,
Vector3.Up );
Basically we're going to replace the Vector3.Zero vector with a reference vector that will dictate which direction we're looking. Make sure that you put the reference point in front of the camera, so basically on a shallower Z axis value
// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3( 0.0f, 50.0f, 5000.0f );
//Put your reference position in front of the camera
Vector3 refPosition = new Vector3(0.0f,50.0f,4900.0f);
Now update the CreateLookAt call in your Draw routine
effect.View = Matrix.CreateLookAt( cameraPosition,
refPosition,
Vector3.Up );
Now all you need to do is setup some camera controls to update either the camera position or the reference position to create your first person experience. Basically all you need to do is subtract a movement scale value (this value dictates how quickly your perspective moves in the 3D world, think of it as look sensitivity) from the reference positions X axis to turn left and add a movement scale value to pan the camera right. In order to pan the camera up add your movement scale to the reference position's Y axis and subtract to look down. That's it! The CreateLookAt() call will keep your camera locked on your reference point giving you camera panning.
I should note that this isn't a true First person perspective since the camera pan to the right/left has a limitation, so basically this won't allow you to do a complete turn around. However you can break the reference plan and have it reverse your axis direction, basically right becomes left, left becomes right, etc.. You do this by having the camera position go past the reference positions, specifically on the Z axis, so basically have the camera's Z position be less than the reference position.
No comments:
Post a Comment