Page 1 of 1

Triangle mesh stability?

Posted: Tue Apr 05, 2011 4:03 am
by b0b
First off, thanks for releasing your library for free. I looked into BulletX and JigLibX, but they rely on unsafe code, which causes compilation issues against Win Phone. Your library is compatible from the get-go, so Kudos!

I'm in the process of testing some scenarios out with this library. So far basic primitives (boxes and spheres) seem fine. When I add a compound body that consists of about 100 triangles (it's the shape of a bottle), the model either swims around when it collides with the ground (a flat box) or in some cases it falls through the ground entirely. When the model settles, it appears to be floating 45 degrees off the ground.

I just wanted to drop in and ask if triangular meshes are supported with stable results and I'm just not doing something right or if the functionality is beta. The following is a outline of how I setup the physics sim.

Code: Select all

// Populate physical world
mSpace = new Space();

// Static ground (length of 1 or higher produces the same results, but 0 is what I'm after (flat ground))
mSpace.Add(new Box(new Vector3(0, 0, 0), 20, 20, 0));

// Dynamic sphere (bounces and slides along the ground fine)
Sphere sphere = new Sphere(new Vector3(0, 0, 5), 0.5f, 1);
sphere.Material.Bounciness = 0.5f;
mSpace.Add(sphere);

// Construct triangle mesh
List<CompoundShapeEntry> list = new List<CompoundShapeEntry>();
for (int i = 0; i < numPoints; i += 3)
{
	CompoundShapeEntry entry = new CompoundShapeEntry(new TriangleShape(new Vector3(x1, y1, z1),
															new Vector3(x2, y2, z2),
															new Vector3(x3, y3, z3)));
	listAdd(entry);
}

// Finally create a dynamic body
CompoundBody body = new CompoundBody(list, 1);
body.Position = new Vector3(4, 0, 5);
body.Material.Bounciness = 0.1f;
mSpace.Add(body);

mSpace.ForceUpdater.Gravity = new Vector3(0, 0, -9.81f);


// Game loop starts here
// ... In the update game function. Manually setting the dt doesn't make any diff.
mSpace.Update();

// Loop through entities and update the model matrices
Model[i].WorldTransform = mSpace.Entities[i].WorldTransform;

Thanks for any assistance on this matter.

Re: Triangle mesh stability?

Posted: Tue Apr 05, 2011 4:29 am
by Norbo
I just wanted to drop in and ask if triangular meshes are supported with stable results and I'm just not doing something right or if the functionality is beta.
CompoundBodies composed of a bunch of triangle shapes should be fine; it's not really different than using any other shape.

Collision shapes will recenter themselves, though. When the triangle is constructed, it will put the vertices into its local space. There's another constructor that has an out parameter which tells you what the center of the shape was. You can use that center as the translation of the shape in the CompoundShapeEntry.

There may be another related issue; the CompoundShape (created internally by the CompoundBody prefab entity type) will also recenter the shapes into its local space. The CompoundShape constructor outputs that position. The CompoundBody prefab entity type uses that center as its Position. Your graphical model may not match the collision shapes due to the offset. To handle this, offset the graphical model by the center found by the CompoundShape (or equivalently the CompoundBody's post-construction Position).

The recentering process is a bit annoying, and I'd like to address it directly in the future, though v0.16.0 at least includes a proper mobile mesh.

Edit:
The mobile mesh is actually available in the latest development fork (http://bepuphysics.codeplex.com/SourceC ... evelopment). It's not quite ready for use, but it can be fiddled with.

Re: Triangle mesh stability?

Posted: Tue Apr 05, 2011 12:01 pm
by b0b
Thanks. Your explanation helped fixed the translational offset I was experiencing. The main issue I had was actually due to improperly uploading the vertex data to your library (whoops ;). Everything's peachy now.

Cheers!