Getting started with 3D XNA
Written by David Conrad   
Friday, 30 July 2010
Article Index
Getting started with 3D XNA
Building the cube
Buidling the faces
More faces
The effect
Projection and view transformations
Update and rotation

Banner

Update

To see something a little more interesting we can make the cube rotate by adding some code to the Update method to change the World matrix. We can set the World matrix so that the cube is rotated through a small angle about the Z axis and moved back into the screen 5 units so that it is more within the camera's view:

private float angle=0;
protected override void Update(
GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).
Buttons.Back== ButtonState.Pressed)
this.Exit();

 angle = angle + 0.005f;
if (angle > 2 * Math.PI) angle = 0;
Matrix R = Matrix.CreateRotationY(angle) *
Matrix.CreateRotationX(.4f);
Matrix T =
Matrix.CreateTranslation(0.0f, 0f, 5f);
effect.World = R * T;
base.Update(gameTime);
}

It is important which order these transformations are carried out in because rotations are always about the origin. So if you rotate and then move what you see is a cube rotated about its centre. If you move and rotate then what you see is a cube orbiting the origin. Try it out to see how it works. What you should see is a rotating white cube.

cube2

A rotating white cube

To make the cube look more interesting we should now start to assign textures to the faces so that the cube has some material properties but this would take us into the next stage in developing a 3D world.

An alternative is to modify the lighting so that the white cube looks different as it rotates. Change the AmbientColor property in the Initialize method to read:

effect.AmbientColor = 
new Vector3(0.0f, 1.0f, 0.0f);

The colour is specified as Red, Green, Blue components and so in this case the surfaces will be coloured green.

If you now run the program you will see a rotating green cube with a directional white light coming from the left. As the cube rotates you will see the side on the left reflect the white directional light. 

cube3

That’s all there is too it and now you can go on to animate a whole range of objects. You can also move the camera as well as the objects to implement “fly bys”. The BasicEffect object is limited but you can learn a lot using it before you eventually need to move on to your own custom shaders but that’s another story.

Explore the basic program to learn how things work. Try modifying the lighting. Move so that the direction lighting is from the top. Change the color. Add a specular color. Try moving the world view to see the cube move. Change the rotation to be about a diagonal axis. Another basic skill is working with more than one object so add multiple cubes at different locations.

All of these are fairly easy but they do require a good grasp of the geometry, the co-ordinate system and vectors.

To access the code for this project, once you have registered,  click on CodeBin.

Banner


QuickSort Exposed

The QuickSort is the most elegant of algorithms and every programmer should study it. It is not only elegant, it is subtle and this often means it is incorrectly implemented and incorrectly explained. [ ... ]



The Knapsack Problem

I like problems that look simple and turn out to be really difficult. It's the way that something simple can hide a complexity that you never guessed at. Fortunately for me the universe seems to be bu [ ... ]


Other Projects

<ASIN:0735627134>

<ASIN:B001VNC6A2@ALL>

<ASIN:1430218177>

<ASIN:0735626588>

<ASIN:B003BGUOKQ@ALL>

<ASIN:0470912332>

<ASIN:1435454278>



Last Updated ( Friday, 30 July 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.