The problem:
the following code creates a correctly shaped convexhull but the collisions were off. ConvexHull and Mobilemesh (others?) find their own center of mass not the same as the center 3DSmax used for export even after aligning the model.
Using the ModelDrawer this was verified.
Model model = Content.Load<Model>("myModel");
Vector3[] vertices;
int []indices;
BEPUphysics.DataStructures.TriangleMesh.GetVerticesAndIndicesFromModel(model, out vertices, out indices);
Entity entity = physEntity = new ConvexHull(vertices);
To Fix:
Add the following line after after creating the entity.
physEntity.CollisionInformation.LocalPosition = physEntity.Position;
There were quite a few post with people having similar issues but I never found a direct way to fix it...
Hopefully this will help someone who needs it.
Rendered XNA model not matching ConvexHull position [Solved]
-
- Posts: 1
- Joined: Thu Dec 08, 2011 11:13 pm
Re: Rendered XNA model not matching ConvexHull position [Sol
I'd like to add a note for others looking for shape-graphics matching, but who want to keep the 'physical' center of mass:
While using the LocalPosition to offset the collidable can make the shape match graphics, the collision shape will no longer rotate around its 'true' center of mass. Sometimes this is desired, though usually the true center of mass should be kept as the pivot.
To keep the true center of mass, consider using the post-construction position to offset the graphics with a local transform.
Some shapes, like the ConvexHullShape and MobileMeshShape, output the center computed during construction directly. This is what the Entity uses to recenter itself; you can use it directly if convenient.
While using the LocalPosition to offset the collidable can make the shape match graphics, the collision shape will no longer rotate around its 'true' center of mass. Sometimes this is desired, though usually the true center of mass should be kept as the pivot.
To keep the true center of mass, consider using the post-construction position to offset the graphics with a local transform.
Some shapes, like the ConvexHullShape and MobileMeshShape, output the center computed during construction directly. This is what the Entity uses to recenter itself; you can use it directly if convenient.