Problems with triangle mesh collision.

Dec 26, 2007 at 12:31 PM

Great initiative! The XNA community really needs a managed 3D physics engine.

I have tried to make a test application that where two boxes fall on a triangle mesh. I have used the code from the TriangleMeshObject class and the model StaticMesh.x. However, the boxes will not seem to collide with the triangle mesh. They collide fine with each other and the floor. Here is the initialization code:

protected override void LoadContent()
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height;

boxModel = Content.Load<Model>("unitcube");
teapotModel = Content.Load<Model>("StaticMesh");

physicSystem = new PhysicsSystem();
physicSystem.CollisionSystem = new CollisionSystemGrid(32, 32, 4, 13, 13, 13);

physicSystem.EnableFreezing = true;
physicSystem.SolverType = PhysicsSystem.Solver.Normal;
physicSystem.CollisionSystem.UseSweepTests = true;

Vector3 pos1 = new Vector3(1.4f, 3, 0);
Vector3 pos2 = new Vector3(-1.4f, 8, 0);
floorScale = new Vector3(8, 1, 8);

box1Body = new Body();
box2Body = new Body();
floorBody = new Body();
teapotBody = new Body();

box1ColSkin = new CollisionSkin(box1Body);
box2ColSkin = new CollisionSkin(box2Body);
floorColSkin = new CollisionSkin(floorBody);
teapotColSkin = new CollisionSkin(teapotBody);

triangleMesh = new TriangleMesh();

Dictionary<string, object> tagData = teapotModel.Tag as Dictionary<string, object>;
Vector3[] vertices = tagData"Vertices" as Vector3[];
int[] indices = tagData"Indices" as int[];

TriangleVertexIndices[] triangleIndices = new TriangleVertexIndicesindices.Length / 3;
for (int i = 0; i < indices.Length / 3; i++)
// JigLibX expects the triangles to be counterclockwise. XNA processor gives them clockwise.
triangleIndicesi = new TriangleVertexIndices(indicesi * 3 + 2, indicesi * 3 + 1, indicesi * 3 + 0);

triangleMesh.CreateMesh(vertices, vertices.Length, triangleIndices, triangleIndices.Length, 4, 1.0f);

box1ColSkin.AddPrimitive(new Box(Vector3.One * -0.5f, Matrix.Identity, Vector3.One), (int)MaterialTable.MaterialID.UserDefined, new MaterialProperties(0.9f, 0.5f, 0.5f));
box2ColSkin.AddPrimitive(new Box(Vector3.One * -0.5f, Matrix.Identity, Vector3.One), (int)MaterialTable.MaterialID.UserDefined, new MaterialProperties(0.9f, 0.5f, 0.5f));
floorColSkin.AddPrimitive(new Box(floorScale * -0.5f, Matrix.Identity, floorScale), (int)MaterialTable.MaterialID.UserDefined, new MaterialProperties(1.1f, 0.8f, 0.8f));
teapotColSkin.AddPrimitive(triangleMesh, 1, new MaterialProperties(0.5f, 0.5f, 0.5f));

box1Body.CollisionSkin = box1ColSkin;
box2Body.CollisionSkin = box2ColSkin;
floorBody.CollisionSkin = floorColSkin;
teapotBody.CollisionSkin = teapotColSkin;

box1Body.Mass = 10;
box2Body.Mass = 10;
floorBody.Mass = 10;
teapotBody.Mass = 10;
floorBody.Immovable = true;
teapotBody.Immovable = true;

box1Body.MoveTo(pos1, Matrix.Identity);
box2Body.MoveTo(pos2, Matrix.Identity);


box1Transform = Matrix.CreateTranslation(pos1);
box2Transform = Matrix.CreateTranslation(pos2);
floorTransform = Matrix.CreateScale(floorScale);

Can anybody see what I am doing wrong? Am I missing something?

Thanks in advance,

Per Rasmussen.
Dec 27, 2007 at 12:19 AM
mhhh, seems correct for me. If you want you can send me your project and i try to fix it.

Dec 27, 2007 at 10:33 PM
I took the sample (which worked fine) and transformed it into my code step by step to see when it stopped working. Funny thing was that it did not stop working. Bottomline is that it works now even though I am still not sure why it didn't earlier. One thing I did learn, though, was that two immovable objects should not touch each other. This wasn't my main problem but it did make the framerate drop extremely.

Anyway, I apologize for the inconvenience.
Dec 28, 2007 at 11:31 AM
Yeah, but there was also a bug in the CollisionSystemGrid (its now fixed - check the latest source) which under special circumstances makes the collisionSystem unaware of the triangleMesh. The bug was hard to find (debugging physic engines is ****) but i had your code - So, thank you very much!