XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
My XNA Book
       Go to section on this site

Additional info

Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


XNA 3.0 Game Programming Recipes

Riemer Grootjans

Chapter 4: Working with Models (113 pages)

What good is a game without any objects? It would be quite impossible, however, to manually specify the shapes of all objects you use in your game. Luckily, XNA can load Models that have been created in specific modeling applications and have been saved to disk. Once you致e loaded them into your XNA project, you can move and rotate your Models around in your scene and even animate them!

The first recipes of this chapter explain how to load a Model from a file and how to position it correctly in your 3D world. Next, you値l find a detailed discussion on the structure of a Model object, allowing you to make your Models come to life by animating them.

Furthermore, this chapter presents some different collision detection mechanisms, because most games rely heavily on this. These checks range from some very fast, volume-based checks to the most detailed per-triangle check.

Because some of the advanced recipes require a custom content processor for the Models, this chapter demonstrates how to extend the default Model processor and how to pass objects of custom classes using a custom-built TypeWriter and a custom-built TypeReader.

You値l definitely need some models to make your 3D world come alive. However, if you use an orthogonal Projection matrix (see recipe 2-13), a 2D game can also use Models.

4-1. Load and Render a Model Using the BasicEffect Class
This recipe will show you how to load 3D Models from disk into your 3D World. As an extra, the whole structure of a 3D Model is explained using some examples.

4-2. Set Different World Matrices for Different Objects, Combining World Matrices
The World Matrix is needed when you want to reposition, rotate and/or scale a 3D Model before rendering it to the screen. This recipe shows you how to do this, and goes one step further by show you the correct way to combine multiple operations together into one matrix.

4-3. Find the Rotation Angle Corresponding to a Direction
When you are moving a Model based only on its previous and current 3D location, you should make sure the Model is correctly rotated.

4-4. Use Acceleration to Control Velocity
This recipe shows you how you can make your Model move smoothly. Using acceleration, your Model will slowly speed up until it reaches its maximum speed.

4-5. Construct the Global BoundingSphere Around a Model
The Global BoundingSphere of a Model is the smallest sphere surrounding the Model. The global BoundingSphere is required in many cases, and especially for fast collision detection. This recipe shows you how to create this Global BoundingSphere.

4-6. Scale the Model to a Predefined Size
When loaded from a file, most Models will have to be scaled up/down before you can use them in your application. This Recipe shows you how you can scale a Model to a size of your choice immediately when it is loaded into your XNA project.

4-7. Render a Model Using Custom Effects and Custom Textures (Easy Approach)
This recipe explains how you can use your own HLSL effects to render a Model.

4-8. Visualize the Bone Structure of a Model
Before you can move over to animation, you need to know how to visualize the Bone structure of a 3D Model. In this recipe, you値l learn how you can automatically write down this Bone structure to a .txt file.

4-9. Make the Bones Move Individually: Model Animation
This recipe shows how you can move/rotate the different parts of Model, for example rotate the turret and canon of a tank.

4-10. Use BoundingSpheres for Basic Model Collision Detection
This recipe uses the global BoundingSphere of a 3D Model to quickly perform a coarse check for possible collisions between other Models.

4-11. Use Ray-Traced Collision Detection for Small/Fast Objects
You might run into trouble detecting collisions between small, very fast objects (such as bullets). This recipe shows you how you can use the Ray class to detect collisions at any speed.

4-12. Extend the Model Content Processor to Load Custom Effects (Clean Approach)
This recipe shows you how you can extend the default Model Processor. In this example, the Model will come into your XNA project with your HLSL effects loaded.

4-13. Gain Direct Access to Vertex Position Data by Extending the Model Processor
The Custom Model Processor created in this recipe give you XNA code full access to all vertices of your Model.

4-14. Gain Direct Access to Vertex Position Data of Each ModelMesh by Extending the Model Processor
This recipe explores the structure of the Model, as it stores the vertices by ModelMesh, instead of putting them all together as in the previous recipe.

4-15. Gain Direct Access to Vertex Position Data by Defining a Custom TypeWriter and TypeReader
This recipe shows how you can pass objects of a custom class from your Content Processor to your XNA code by coding your own TypeReader and TypeWriter.

4-16. Store Multiple Objects in the Tag Property by Defining a Custom TypeWriter and TypeReader
The Tag property of the Model is extremely useful for passing data from the Content Processor to your XNA code. However, you can only store a single object in this property. This recipe shows you how you can solve this, by writing another custom TypeWriter and TypeReader.

4-17. Correctly Tilt a Model Corresponding to the Terrain Underneath
What good is a terrain if you cannot move your objects over it? In this recipe, you値l learn how to correctly position a four-wheel vehicle on your terrain, so its four wheels touch the ground.

4-18. Detect Ray-Model Collisions Using Per-Triangle Checks
For the finest of collision detections, you need to be able to check whether a Ray collides with a triangle. In this mathematically advanced recipe, you値l learn how to detect collisions between a Ray and any triangle of a Model. This recipe is written so it remains valid even for animated Models.

4-19. Detect Whether the Pointer Is Over a Model
Although it seems trivial to detect whether the mouse pointer is over a 3D Model, this is quite difficult as it comes down to checking whether a 3D ray collides with the Model. The result of the previous recipe is used to solve this, while you値l get more insight on how the 3D -> Screen transformation works.

Recipe 4-17: A tank is being tilted around its X and Z axes so its four wheels touch the ground.


If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !

- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans

This site in English
This site in Korean
This site in Czech

Microsoft MVP Award

2007 - 2011 MVP Award
DirectX - XNA


XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
-- Expand all --

Thank you!

Support this site --
any amount is welcome !

Stay up-to-date

I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!