XNA 2.0 Game Programming Recipes
Chapter 4: Working with Models (108 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.
- Website design & XNA + DirectX code : Riemer Grootjans -
©2003 - 2011 Riemer Grootjans