static public SphereShapeDesc CreateSphereShapeFromMesh(StaticModel mesh, float scale) { List <Vector3> verts = mesh.GetModelVertices(scale); float largestSquareLength = 0.0f; foreach (Vector3 vertex in verts) { float vertSqrLng = vertex.LengthSquared(); if (vertSqrLng > largestSquareLength) { largestSquareLength = vertSqrLng; } } SphereShapeDesc shape = new SphereShapeDesc(); shape.Radius = (float)Math.Sqrt(largestSquareLength); return(shape); }
/// <summary> /// Create a shape from a <seealso cref="ShapeType"/> and the BaseEntity information. /// </summary> /// <param name="type"></param> /// <returns></returns> protected ShapeDesc CreateShapeFromType(ShapeType type) { float scale = this.parentEntity.Scale; this.height *= scale; this.width *= scale; this.depth *= scale; this.diameter *= scale; switch (type) { case ShapeType.Box: { if (null == this.physMesh) { var shape = new BoxShapeDesc(); shape.Extents = new Vector3(this.width, this.height, this.depth); return(shape); } else { return(CreateBoxShapeFromMesh(this.physMesh, scale)); } } case ShapeType.Sphere: { if (null == this.physMesh) { var shape = new SphereShapeDesc(); shape.Radius = this.diameter / 2.0f; return(shape); } else { return(CreateSphereShapeFromMesh(this.physMesh, scale)); } } case ShapeType.Heightfield: { // Unsupported by this method, use CreateHeightfieldShape() return(null); } case ShapeType.Capsule: { var shape = new CapsuleShapeDesc(); shape.Radius = this.diameter / 2.0f; shape.Length = this.height; return(shape); } case ShapeType.Cylinder: { var shape = new CylinderShapeDesc(); shape.Height = this.height; shape.Radius = this.diameter / 2.0f; return(shape); } case ShapeType.Cone: { var shape = new ConeShapeDesc(); shape.Height = this.height; shape.Radius = this.diameter / 2.0f; return(shape); } case ShapeType.TriangleMesh: { if (isDynamic) { throw new Exception("Triangle Mesh shapes do not support dynamic physics"); } TriangleMeshShapeDesc shape = new TriangleMeshShapeDesc(); if (this.physMesh == null) { MsgGetModelVertices msgGetVerts = ObjectPool.Aquire <MsgGetModelVertices>(); msgGetVerts.UniqueTarget = this.parentEntity.UniqueID; this.parentEntity.Game.SendMessage(msgGetVerts); shape.Vertices = msgGetVerts.Vertices; MsgGetModelIndices msgGetInds = ObjectPool.Aquire <MsgGetModelIndices>(); msgGetInds.UniqueTarget = this.parentEntity.UniqueID; this.parentEntity.Game.SendMessage(msgGetInds); shape.Indices = msgGetInds.Indices; } else { shape.Vertices = physMesh.GetModelVertices(this.parentEntity.Scale); shape.Indices = physMesh.GetModelIndices(); } if ((shape.Vertices.Count == 0) || (shape.Indices.Count == 0)) { return(null); } return(shape); } default: // Throw exception return(null); } ; }