Graphics Reference
In-Depth Information
shape = new BulletSharp.SphereShape(extent.Radius);
}
else // use mesh vertices directly
{
// for each SubMesh, merge the vertex and index
// buffers to create a TriangleMeshShape for collisions
List<Vector3> vertices = new List<Vector3>();
List<int> indices = new List<int>();
int vertexOffset = 0;
foreach (var sm in m.Mesh.SubMeshes)
{
vertexOffset += vertices.Count;
indices.AddRange(
(from indx in m.Mesh.IndexBuffers
[(int)sm.IndexBufferIndex]
select vertexOffset + (int)indx));
vertices.AddRange(
(from v in m.Mesh
.VertexBuffers[(int)sm.VertexBufferIndex]
select v.Position - extent.Center));
}
// Create the collision shape
shape = new BvhTriangleMeshShape(
new TriangleIndexVertexArray(indices.ToArray(),
vertices.ToArray()), true);
}
#endregion
7.
At the end of the intiailizePhysics action, we will create the rigid body from the
shape, determine if it is a dynamic or static shape (that is, whether it can be affected
by collisions and gravity), and add it to the physics world object for simulation:
m.World = Matrix.Identity; // Reset mesh location
float mass; Vector3 vec; // use radius as mass
shape.GetBoundingSphere(out vec, out mass);
// Create the rigid body, if static/kinematic set mass to 0
var body = new BulletSharp.RigidBody(
new RigidBodyConstructionInfo(
name.Contains("static") ? 0 : mass,
new MeshMotionState(m),
shape, shape.CalculateLocalInertia(mass)));
if (body.IsStaticObject)
{
 
Search WWH ::




Custom Search