public override object Process(Scene scene, Frame node)
        {
            if (scene == null || scene.Physics == null)
            {
                return(null);
            }

            string tag = node.Tag;

            if (tag == null)
            {
                return(null);
            }

            var match = _regex.Match(tag);

            if (!match.Success)
            {
                return(null);
            }

            var obj = node.Component as IFrameMesh;

            if (obj == null)
            {
                return(null);
            }

            CharacterControllerDesc controllerDesc;
            var shapeDesc = obj.Mesh.CreateShapeDescriptor();

            if (shapeDesc.Type == Physics.ShapeType.BOX)
            {
                BoxShapeDesc box = (BoxShapeDesc)shapeDesc;
                controllerDesc = new BoxControllerDesc
                {
                    Extents     = box.Dimensions,
                    Position    = box.LocalPose.Translation + node.GlobalPose.Translation,
                    UpDirection = HeightFieldAxis.Y,
                    SlopeLimit  = (float)Math.Cos(Numerics.ToRadians(45.0f)),
                    Name        = node.Name
                };
            }
            else if (shapeDesc.Type == Physics.ShapeType.CAPSULE)
            {
                CapsuleShapeDesc capsuleShape = (CapsuleShapeDesc)shapeDesc;
                controllerDesc = new CapsuleControllerDesc
                {
                    Height      = capsuleShape.Height,
                    Position    = capsuleShape.LocalPose.Translation + node.GlobalPose.Translation,
                    Radius      = capsuleShape.Radius,
                    UpDirection = HeightFieldAxis.Y,
                    SlopeLimit  = (float)Math.Cos(Numerics.ToRadians(45.0f)),
                    Name        = node.Name
                };
            }
            else
            {
                return(null);
            }

            Frame bindedNode = null;

            if (match.Groups["BINDING"].Success)
            {
                bindedNode = scene.FindNode(match.Groups["BINDING"].Value);
            }
            if (ControllerCreated != null)
            {
                ControllerCreated(controllerDesc, bindedNode);
            }


            var controller = CharacterControllerManager.Instance.CreateController(scene.Physics, controllerDesc);

            if (bindedNode != null)
            {
                bindedNode.BindTo(controller);
            }

            obj.Mesh.Dispose();
            node.Remove();
            node.Dispose();

            return(controller);
        }
Beispiel #2
0
        /// <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);
            }
            ;
        }