HkShape CreateShape(MyModel model, HkShapeType shapeType) { if (model.HavokCollisionShapes != null && model.HavokCollisionShapes.Length > 0) { HkShape sh = model.HavokCollisionShapes[0]; sh.AddReference(); return(sh); } switch (shapeType) { case HkShapeType.Box: Vector3 halfExtents = (model.BoundingBox.Max - model.BoundingBox.Min) / 2; return(new HkBoxShape(Vector3.Max(halfExtents - 0.1f, new Vector3(0.05f)), 0.02f)); break; case HkShapeType.Sphere: return(new HkSphereShape(model.BoundingSphere.Radius)); break; case HkShapeType.ConvexVertices: m_tmpVerts.Clear(); for (int i = 0; i < model.GetVerticesCount(); i++) { m_tmpVerts.Add(model.GetVertex(i)); } return(new HkConvexVerticesShape(m_tmpVerts.GetInternalArray(), m_tmpVerts.Count, true, 0.1f)); break; } throw new InvalidOperationException("This shape is not supported"); }
private void RequestShapeBlockingInternal(int x, int y, int z, out HkShape shape, out HkReferencePolicy refPolicy, bool lod1physics) { ProfilerShort.Begin("MyVoxelPhysicsBody.RequestShapeBlocking"); if (!m_bodiesInitialized) { CreateRigidBodies(); } int lod = lod1physics ? 1 : 0; var cellCoord = new MyCellCoord(lod, new Vector3I(x, y, z)); shape = HkShape.Empty; // shape must take ownership, otherwise shapes created here will leak, since I can't remove reference refPolicy = HkReferencePolicy.TakeOwnership; //MyPrecalcComponent.QueueJobCancel(m_workTracker, cellCoord); if (m_voxelMap.MarkedForClose) { ProfilerShort.End(); return; } if (MyDebugDrawSettings.DEBUG_DRAW_REQUEST_SHAPE_BLOCKING) { BoundingBoxD aabb; MyVoxelCoordSystems.GeometryCellCoordToWorldAABB(m_voxelMap.PositionLeftBottomCorner, ref cellCoord, out aabb); MyRenderProxy.DebugDrawAABB(aabb, lod1physics ? Color.Yellow : Color.Red, 1, 1, true); } ProfilerShort.Begin("Generating geometry"); MyIsoMesh geometryData = CreateMesh(m_voxelMap.Storage, cellCoord); ProfilerShort.End(); if (!MyIsoMesh.IsEmpty(geometryData)) { ProfilerShort.Begin("Shape from geometry"); shape = CreateShape(geometryData, true); shape.AddReference(); var args = new MyPrecalcJobPhysicsPrefetch.Args() { GeometryCell = cellCoord, TargetPhysics = this, Tracker = m_workTracker, SimpleShape = shape }; MyPrecalcJobPhysicsPrefetch.Start(args); m_needsShapeUpdate = true; ProfilerShort.End(); } ProfilerShort.End(); }
public override void CreatePhysicsShape(out HkShape shape, out HkMassProperties massProperties, float mass) { float num; float num2; shape = HkShape.Empty; MyModel model = base.Entity.Render.GetModel(); BoundingBox boundingBox = model.BoundingBox; ComputeShapeDimensions(boundingBox, out num, out num2); Vector3 vector = Vector3.Up * num; Vector3 vertexA = ((boundingBox.Min + boundingBox.Max) * 0.5f) + (vector * 0.2f); Vector3 vertexB = ((boundingBox.Min + boundingBox.Max) * 0.5f) - (vector * 0.45f); bool flag = true; if (MyFakes.TREE_MESH_FROM_MODEL) { HkShape[] havokCollisionShapes = model.HavokCollisionShapes; if ((havokCollisionShapes != null) && (havokCollisionShapes.Length != 0)) { if (havokCollisionShapes.Length != 1) { shape = (HkShape) new HkListShape(havokCollisionShapes, HkReferencePolicy.None); } else { shape = havokCollisionShapes[0]; shape.AddReference(); } flag = false; } } if (flag) { shape = (HkShape) new HkCapsuleShape(vertexA, vertexB, num2); } massProperties = HkInertiaTensorComputer.ComputeCapsuleVolumeMassProperties(vertexA, vertexB, num2, mass); }
private void RequestShapeBlockingInternal(int x, int y, int z, out HkShape shape, out HkReferencePolicy refPolicy, bool lod1physics) { ProfilerShort.Begin("MyVoxelPhysicsBody.RequestShapeBlocking"); if (!m_bodiesInitialized) CreateRigidBodies(); int lod = lod1physics ? 1 : 0; var cellCoord = new MyCellCoord(lod, new Vector3I(x, y, z)); shape = HkShape.Empty; // shape must take ownership, otherwise shapes created here will leak, since I can't remove reference refPolicy = HkReferencePolicy.TakeOwnership; //MyPrecalcComponent.QueueJobCancel(m_workTracker, cellCoord); if (m_voxelMap.MarkedForClose) { ProfilerShort.End(); return; } if (MyDebugDrawSettings.DEBUG_DRAW_REQUEST_SHAPE_BLOCKING) { BoundingBoxD aabb; MyVoxelCoordSystems.GeometryCellCoordToWorldAABB(m_voxelMap.PositionLeftBottomCorner, ref cellCoord, out aabb); MyRenderProxy.DebugDrawAABB(aabb, lod1physics ? Color.Yellow : Color.Red, 1, 1, true); } ProfilerShort.Begin("Generating geometry"); MyIsoMesh geometryData = CreateMesh(m_voxelMap.Storage, cellCoord); ProfilerShort.End(); if (!MyIsoMesh.IsEmpty(geometryData)) { ProfilerShort.Begin("Shape from geometry"); shape = CreateShape(geometryData, true); shape.AddReference(); var args = new MyPrecalcJobPhysicsPrefetch.Args() {GeometryCell = cellCoord, TargetPhysics = this, Tracker = m_workTracker, SimpleShape = shape}; MyPrecalcJobPhysicsPrefetch.Start(args); m_needsShapeUpdate = true; ProfilerShort.End(); } ProfilerShort.End(); }