/// <summary>
/// Add the collision data for an object. This involves looking
/// for a physics file that matches the mesh file's name, and
/// loading the information from that file to build collision
/// volumes.
/// </summary>
/// <param name="objNode">the object for which we are adding the collision data</param>
private void AddCollisionObject(Entity modelEntity, SceneNode modelSceneNode,
long oid, string physicsName)
{
// Create a set of collision shapes for the object
List<CollisionShape> shapes = new List<CollisionShape>();
PhysicsData pd = new PhysicsData();
PhysicsSerializer ps = new PhysicsSerializer();
try {
Stream stream = ResourceManager.FindCommonResourceData(physicsName);
ps.ImportPhysics(pd, stream);
for (int i=0; i<modelEntity.SubEntityCount; i++) {
SubEntity subEntity = modelEntity.GetSubEntity(i);
if (subEntity.IsVisible) {
string submeshName = subEntity.SubMesh.Name;
List<CollisionShape> subEntityShapes = pd.GetCollisionShapes(submeshName);
foreach (CollisionShape subShape in subEntityShapes) {
// static objects will be transformed here, but movable objects
// are transformed on the fly
subShape.Transform(modelSceneNode.DerivedScale,
modelSceneNode.DerivedOrientation,
modelSceneNode.DerivedPosition);
shapes.Add(subShape);
log.InfoFormat("Added collision shape: {0} for {1}",
subShape, submeshName);
}
}
}
} catch (Exception e) {
// Unable to load physics data -- use a sphere or no collision data?
log.WarnFormat("Unable to load physics data: {0}", e);
}
foreach (CollisionShape shape in shapes)
collisionManager.AddCollisionShape(shape, oid);
}