Wednesday, January 9, 2008

3D model collision

For a long time, 3D collision has been the biggest problem I have faced in the field of game development. XNA comes with the BoundingBox and BoundingSphere classes that provide a very fast collision solution. However, these classes do not give you very accurate collision detection when applied to 3D models, because they do not check each triangle in the model.

The solution to this problem is writing a CollisionMesh class. This class will be will be used to check Model vs. Model collision, using the model’s triangles.

However, the kind of collision checking is very costly, so it is important to minimize the amount of times we use this method. We can do this by using the BoundingBox class, to check if two models are even close to each other. If model1 and model2’s bounding boxes don’t collide, there is no reason to make the very costly triangle collision test.

Also, it is a good idea to use a low-poly version of you 3D model for collision testing, depending on how accurate your collision needs to be.

For a concrete example of this collision theory, download this sample. Here is a short walkthrough, on how to use the CollisionMesh class:

Creating a CollisionMesh is very simple:

Model MyModel = Content.Load<Model>("MyModel");
CollisionMesh collisionMesh;
collisionMesh = CollisionMesh.FromModel(MyModel, Vector3.Zero, Vector3.Zero);
CollisionMesh.RefreshMatrix();
After this, use the following code to check if a movment with the movmentVector, will result in a collision:

Vector3 movmentVector = new Vector3(1,0,0);
bool result = collisionMesh.Move(movmentVector, otherCollisionMesh);
collisionMesh.RefreshMatrix();


10 comments:

R5 said...

Hello, can you use any model, I tryed to use a model.x, wich i made in google sketchup and exported to .x format, I even used two diffrente exporters, but I have allways the same error compiling.

on CollisionMesh.cs, on line:

"Vector3[] vertices = tagData["Vertices"] as Vector3[];"

The error: "Object reference not set to an instance of an object."

TagData is null, why?

DX Jas said...

Hi,
Another way to optimize the process is to create a collision hull for your model so when you NEED to do it you're not doing it with your 5000 poly model but with a 50 poly shell.

Happy Coding!
Jason
http://www.codemonkeyjas.blogspot.com

Eoin Carroll said...

@Ricardo. I had the same problem and the solution is that before you compile, go to the properties in model.x and set content processor field to collisionprocessor. This should clear up the error.

It took me about 2 months to figure that out :)

Unknown said...

How could you use this class to handle checking collision between one model vs. many models (such as checking buildings)? Since the Move function actually moves the bounding box, I can't just look through and call move each time. I also tried just moving them then moving them back and that causes insane lag with only about 1 vs. 5 models.

marco said...

To all data analyst, data managers, software engineers and data miners.


Take the online survey for Molecular Algorithm: A Better Approach in Data Integration

Anonymous said...

Steffan I'm hoping you can clear something up for me.

Each model can be moving around in 3D space, right. Therefore, at what point do you update the models rotation? Is it in the call to CollisionMesh.FromModel, indicating you'd need to recreate the mesh each time? Is it in RefreshMatrix, and if so where is it getting the orientation for each object?

Do you call CollisionMesh.FromModel just once to create, then call RefreshMatrix every frame?

Thanks

In your example I just don't see the details?

QUADgnim said...

Steffan,

Is there a way to use your class with Quaternion rotation? I don't know how to convert a Quaternion rotation to Vector3?

You can ignore my previous post. Once I looked at the source code and not just the sample in the post I realized what you were doing and how it work.s

Thanks

EroPkA said...

File is unavailable for download...

Unknown said...

can you provide a new link for the file.

somethingreallyinconsiderate said...

hello can you post a link so we may download the class - the existing link is broken - Thanks in advance!