Click or drag to resize
DigitalRuneHow To: Load a 3D Collision Model Using the XNA Content Pipeline

This article discusses how you can define collision shapes for a 3D model. This sample can be found in the Samples.

In almost all 3D games collisions between complex shaped objects must be detected. For collision detection the triangle meshes can be used directly but this is a lot slower than using boxes, spheres or convex shapes. Therefore, for each model a collision model should be created that consists only of simple primitives. This collection of shapes approximates the shape of the detailed 3D graphics model. This article discusses how we can use the XNA content pipeline to load shapes from a collision model created in a 3D modeling tool.

This topic contains the following sections:

The graphics model

Here is a space ship model from the opened in Blender:

Collision-Model-01

The model consists of several thousand faces – not ideal for collision detection.

The collision model

For practical collision detection we have created a collision model that consists only of a few boxes, spheres and convex shapes. It is a crude approximation of the detailed graphics model – but sufficiently detailed for most cases.

Collision-Model-02

Using a 3D modeling tool for placing the approximated collision shapes is a lot more practical than defining the collision shapes in source code.

Next, we need a way to load this data in our XNA game. With the standard XNA model processor we can load the model but at runtime this leaves us only with the triangle mesh data. What we really need are boxes, spheres, etc. Therefore, we need a custom content processor for the XNA content pipeline.

Naming scheme

When a collision model is imported in the content pipeline, triangle mesh data is imported. That means, instead of a box the content processor sees a triangle mesh with 8 vertices. To make the work easier for our content processor we apply a naming scheme to the collision model that is created in Blender. The name of each box collision shape ends with "Box"; for example "LeftFin_Box". Similarly, each sphere is named "XxxSphere" and each convex shape is named "XxxConvex". This naming scheme allows the content processor to know which shape it must create for each mesh in the model.

Sample

For source code examples, please have a look a the ContentPipelineSample of the Samples.

Here is screenshot of the example. The model that is used for the fancy graphics.

Collision-Model-03

The collision model that is used for collision detection:

Collision-Model-04