Code: Select all
Vector3[] vertices = voxelMesh.Vertices;
int[] indices = voxelMesh.Indices;
int numIndices = indices.Length;
int numTriangles = vertices.Length;
var scaleTransform = Matrix.CreateScale(ScaleFactor);
List<CompoundShapeEntry> triangles = new List<CompoundShapeEntry>(numIndices / 3);
for (int i = 0; i < numIndices; i += 3)
{
// [Recentered Triangle]
Vector3 centre;
TriangleShape tri = new TriangleShape(
Vector3.Transform(vertices[indices[i]], scaleTransform),
Vector3.Transform(vertices[indices[i + 1]], scaleTransform),
Vector3.Transform(vertices[indices[i + 2]], scaleTransform),
out centre
);
tri.Sidedness = TriangleSidedness.DoubleSided;
triangles.Add(new CompoundShapeEntry(tri, centre, 1f));
}
entity = noMass ? new CompoundBody(triangles) : new CompoundBody(triangles, settings.Mass);
// Set the centre/origin offset created by CompoundBody
entityCentreOffset = Matrix.CreateTranslation(-entity.Position); // Works correctly for scaling
For example using an offset of -entity.Position will result in the physics mesh (white wireframe) and model aligning but with a pivot point (yellow box) that is not central
Whereas if I use an offset that is central to the model then I get a central pivot point but with a physics mesh that is not central (more pronounced when scaling)
I am scaling the object using
Code: Select all
voxelMesh.WorldTransform = Matrix.CreateScale(ScaleFactor) * entityCentreOffset * entity.WorldTransform;