Example #1
0
        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();
            }
        }
Example #2
0
        public ColliderShape CreateShape(IInlineColliderShapeDesc desc)
        {
            ColliderShape shape = null;

            var shapeType = desc.GetType();

            if (shapeType == 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 (shapeType == 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 (shapeType == typeof(CylinderColliderShapeDesc))
            {
                var cylinderDesc = (CylinderColliderShapeDesc)desc;
                shape = new CylinderColliderShape(cylinderDesc.Height, cylinderDesc.Radius, cylinderDesc.Orientation)
                {
                    LocalOffset = cylinderDesc.LocalOffset, LocalRotation = cylinderDesc.LocalRotation
                };
            }
            else if (shapeType == typeof(SphereColliderShapeDesc))
            {
                var sphereDesc = (SphereColliderShapeDesc)desc;
                shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius)
                {
                    LocalOffset = sphereDesc.LocalOffset
                };
            }
            else if (shapeType == typeof(StaticPlaneColliderShapeDesc))
            {
                var planeDesc = (StaticPlaneColliderShapeDesc)desc;
                shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset);
            }
            else if (shapeType == typeof(ColliderShapeAssetDesc))
            {
                var assetDesc = (ColliderShapeAssetDesc)desc;

                if (assetDesc.Shape == null)
                {
                    return(null);
                }

                if (assetDesc.Shape.Shape == null)
                {
                    assetDesc.Shape.Shape = PhysicsColliderShape.Compose(assetDesc.Shape.Descriptions);
                }

                shape = assetDesc.Shape.Shape;
            }

            if (shape != null)
            {
                shape.Parent = null; //from now parent might change
                shape.UpdateLocalTransformations();
            }

            return(shape);
        }