public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] == null) { return; } if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = PhysicsColliderShape.CreateShape(ColliderShapes[0]); ColliderShape?.UpdateLocalTransformations(); } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc == null) { continue; } if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = PhysicsColliderShape.CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } }
private void DrawDebugCompound(ref Matrix viewProj, CompoundColliderShape compound, PhysicsElement element) { for (var i = 0; i < compound.Count; i++) { var subShape = compound[i]; switch (subShape.Type) { case ColliderShapeTypes.StaticPlane: continue; case ColliderShapeTypes.Compound: DrawDebugCompound(ref viewProj, (CompoundColliderShape)compound[i], element); break; default: { var physTrans = element.BoneIndex == -1 ? element.Collider.PhysicsWorldTransform : element.BoneWorldMatrix; physTrans = Matrix.Multiply(subShape.PositiveCenterMatrix, physTrans); //must account collider shape scaling Matrix worldTrans; Matrix.Multiply(ref subShape.DebugPrimitiveScaling, ref physTrans, out worldTrans); physicsSystem.PhysicsEngine.DebugEffect.WorldViewProj = worldTrans * viewProj; physicsSystem.PhysicsEngine.DebugEffect.Color = element.Collider.IsActive ? Color.Green : Color.Red; physicsSystem.PhysicsEngine.DebugEffect.UseUv = subShape.Type != ColliderShapeTypes.ConvexHull; physicsSystem.PhysicsEngine.DebugEffect.Apply(); subShape.DebugPrimitive.Draw(); } break; } } }
private static ColliderShape Compose(IReadOnlyList <IColliderShapeDesc> descs) { ColliderShape res = null; try { if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } return(res); }
private static ColliderShape Compose(IReadOnlyList<IColliderShapeDesc> descs) { ColliderShape res = null; try { if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } return res; }
internal static ColliderShape Compose(IReadOnlyList <IAssetColliderShapeDesc> descs) { ColliderShape res = null; if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); res.IsPartOfAsset = true; } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; res.IsPartOfAsset = true; } return(res); }
internal static ColliderShape Compose(IReadOnlyList<IAssetColliderShapeDesc> descs) { ColliderShape res = null; if (descs.Count == 1) //single shape case { res = CreateShape(descs[0]); res.IsPartOfAsset = true; } else if (descs.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in descs) { compound.AddChildShape(CreateShape(desc)); } res = compound; res.IsPartOfAsset = true; } return res; }
private static ColliderShape CreateShape(IColliderShapeDesc desc) { ColliderShape shape = null; var type = desc.GetType(); if (type == typeof(Box2DColliderShapeDesc)) { var boxDesc = (Box2DColliderShapeDesc)desc; shape = new Box2DColliderShape(boxDesc.HalfExtent) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else if (type == typeof(BoxColliderShapeDesc)) { var boxDesc = (BoxColliderShapeDesc)desc; shape = new BoxColliderShape(boxDesc.HalfExtents) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else if (type == typeof(CapsuleColliderShapeDesc)) { var capsuleDesc = (CapsuleColliderShapeDesc)desc; shape = new CapsuleColliderShape(capsuleDesc.Is2D, capsuleDesc.Radius, capsuleDesc.Height, capsuleDesc.UpAxis) { LocalOffset = capsuleDesc.LocalOffset, LocalRotation = capsuleDesc.LocalRotation }; } else if (type == typeof(CylinderColliderShapeDesc)) { var cylinderDesc = (CylinderColliderShapeDesc)desc; shape = new CylinderColliderShape(cylinderDesc.HalfExtents, cylinderDesc.UpAxis) { LocalOffset = cylinderDesc.LocalOffset, LocalRotation = cylinderDesc.LocalRotation }; } else if (type == typeof(SphereColliderShapeDesc)) { var sphereDesc = (SphereColliderShapeDesc)desc; shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius) { LocalOffset = sphereDesc.LocalOffset }; } else if (type == typeof(StaticPlaneColliderShapeDesc)) { var planeDesc = (StaticPlaneColliderShapeDesc)desc; shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset); } else if (type == typeof(ConvexHullColliderShapeDesc)) { var convexDesc = (ConvexHullColliderShapeDesc)desc; //Optimize performance and focus on less shapes creation since this shape could be nested if (convexDesc.ConvexHulls.Count == 1) { if (convexDesc.ConvexHulls[0].Count == 1) { shape = new ConvexHullColliderShape(convexDesc.ConvexHulls[0][0], convexDesc.ConvexHullsIndices[0][0], convexDesc.Scaling) { NeedsCustomCollisionCallback = true }; shape.UpdateLocalTransformations(); shape.Description = desc; return(shape); } if (convexDesc.ConvexHulls[0].Count <= 1) { return(null); } var subCompound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls[0].Count; i++) { var verts = convexDesc.ConvexHulls[0][i]; var indices = convexDesc.ConvexHullsIndices[0][i]; var subHull = new ConvexHullColliderShape(verts, indices, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); subCompound.Description = desc; return(subCompound); } if (convexDesc.ConvexHulls.Count <= 1) { return(null); } var compound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls.Count; i++) { var verts = convexDesc.ConvexHulls[i]; var indices = convexDesc.ConvexHullsIndices[i]; if (verts.Count == 1) { var subHull = new ConvexHullColliderShape(verts[0], indices[0], convexDesc.Scaling); subHull.UpdateLocalTransformations(); compound.AddChildShape(subHull); } else if (verts.Count > 1) { var subCompound = new CompoundColliderShape(); for (var b = 0; b < verts.Count; b++) { var subVerts = verts[b]; var subIndex = indices[b]; var subHull = new ConvexHullColliderShape(subVerts, subIndex, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); compound.AddChildShape(subCompound); } } compound.UpdateLocalTransformations(); compound.Description = desc; return(compound); } if (shape != null) { shape.UpdateLocalTransformations(); shape.Description = desc; } return(shape); }
internal static ColliderShape CreateShape(IColliderShapeDesc desc) { ColliderShape shape = null; var type = desc.GetType(); if (type == typeof(BoxColliderShapeDesc)) { var boxDesc = (BoxColliderShapeDesc)desc; if (boxDesc.Is2D) { shape = new Box2DColliderShape(new Vector2(boxDesc.Size.X, boxDesc.Size.Y)) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } else { shape = new BoxColliderShape(boxDesc.Size) { LocalOffset = boxDesc.LocalOffset, LocalRotation = boxDesc.LocalRotation }; } } else if (type == typeof(CapsuleColliderShapeDesc)) { var capsuleDesc = (CapsuleColliderShapeDesc)desc; shape = new CapsuleColliderShape(capsuleDesc.Is2D, capsuleDesc.Radius, capsuleDesc.Length, capsuleDesc.Orientation) { LocalOffset = capsuleDesc.LocalOffset, LocalRotation = capsuleDesc.LocalRotation }; } else if (type == typeof(CylinderColliderShapeDesc)) { var cylinderDesc = (CylinderColliderShapeDesc)desc; shape = new CylinderColliderShape(cylinderDesc.Height, cylinderDesc.Radius, cylinderDesc.Orientation) { LocalOffset = cylinderDesc.LocalOffset, LocalRotation = cylinderDesc.LocalRotation }; } else if (type == typeof(SphereColliderShapeDesc)) { var sphereDesc = (SphereColliderShapeDesc)desc; shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius) { LocalOffset = sphereDesc.LocalOffset }; } else if (type == typeof(StaticPlaneColliderShapeDesc)) { var planeDesc = (StaticPlaneColliderShapeDesc)desc; shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset); } else if (type == typeof(ConvexHullColliderShapeDesc)) { var convexDesc = (ConvexHullColliderShapeDesc)desc; //Optimize performance and focus on less shapes creation since this shape could be nested if (convexDesc.ConvexHulls.Count == 1) { if (convexDesc.ConvexHulls[0].Count == 1 && convexDesc.ConvexHullsIndices[0][0].Count > 0) { shape = new ConvexHullColliderShape(convexDesc.ConvexHulls[0][0], convexDesc.ConvexHullsIndices[0][0], convexDesc.Scaling) { NeedsCustomCollisionCallback = true }; shape.UpdateLocalTransformations(); shape.Description = desc; return shape; } if (convexDesc.ConvexHulls[0].Count <= 1) return null; var subCompound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls[0].Count; i++) { var verts = convexDesc.ConvexHulls[0][i]; var indices = convexDesc.ConvexHullsIndices[0][i]; if(indices.Count == 0) continue; var subHull = new ConvexHullColliderShape(verts, indices, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); subCompound.Description = desc; return subCompound; } if (convexDesc.ConvexHulls.Count <= 1) return null; var compound = new CompoundColliderShape { NeedsCustomCollisionCallback = true }; for (var i = 0; i < convexDesc.ConvexHulls.Count; i++) { var verts = convexDesc.ConvexHulls[i]; var indices = convexDesc.ConvexHullsIndices[i]; if (verts.Count == 1) { if(indices[0].Count == 0) continue; var subHull = new ConvexHullColliderShape(verts[0], indices[0], convexDesc.Scaling); subHull.UpdateLocalTransformations(); compound.AddChildShape(subHull); } else if (verts.Count > 1) { var subCompound = new CompoundColliderShape(); for (var b = 0; b < verts.Count; b++) { var subVerts = verts[b]; var subIndex = indices[b]; if (subIndex.Count == 0) continue; var subHull = new ConvexHullColliderShape(subVerts, subIndex, convexDesc.Scaling); subHull.UpdateLocalTransformations(); subCompound.AddChildShape(subHull); } subCompound.UpdateLocalTransformations(); compound.AddChildShape(subCompound); } } compound.UpdateLocalTransformations(); compound.Description = desc; return compound; } else if (type == typeof(ColliderShapeAssetDesc)) { var assetDesc = (ColliderShapeAssetDesc)desc; if (assetDesc.Shape == null) { return null; } if (assetDesc.Shape.Shape == null) { assetDesc.Shape.Shape = Compose(assetDesc.Shape.Descriptions); } shape = assetDesc.Shape.Shape; } if (shape == null) return shape; shape.UpdateLocalTransformations(); shape.Description = desc; return shape; }
public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] == null) return; if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = PhysicsColliderShape.CreateShape(ColliderShapes[0]); ColliderShape?.UpdateLocalTransformations(); } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc == null) continue; if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = PhysicsColliderShape.CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } }
public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } try { CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] != null) { if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = CreateShape(ColliderShapes[0]); if (ColliderShape == null) return; ColliderShape.UpdateLocalTransformations(); } } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc != null) { if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } }
public void ComposeShape() { ColliderShapeChanged = false; if (ColliderShape != null) { if (!ColliderShape.IsPartOfAsset) { ColliderShape.Dispose(); ColliderShape = null; } else { ColliderShape = null; } } try { CanScaleShape = true; if (ColliderShapes.Count == 1) //single shape case { if (ColliderShapes[0] != null) { if (ColliderShapes[0].GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } ColliderShape = CreateShape(ColliderShapes[0]); if (ColliderShape == null) { return; } ColliderShape.UpdateLocalTransformations(); } } else if (ColliderShapes.Count > 1) //need a compound shape in this case { var compound = new CompoundColliderShape(); foreach (var desc in ColliderShapes) { if (desc != null) { if (desc.GetType() == typeof(ColliderShapeAssetDesc)) { CanScaleShape = false; } var subShape = CreateShape(desc); if (subShape != null) { compound.AddChildShape(subShape); } } } ColliderShape = compound; ColliderShape.UpdateLocalTransformations(); } } catch (DllNotFoundException) { //during pre process and build process we often don't have the physics native engine running. } }