Esempio n. 1
0
        static public BoxShapeDesc CreateBoxShapeFromMesh(StaticModel mesh, float scale)
        {
            List <Vector3> verts = mesh.GetModelVertices(scale);

            Vector3 negExtent = Vector3.Zero;
            Vector3 posExtent = Vector3.Zero;

            foreach (Vector3 vertex in verts)
            {
                if (vertex.X < negExtent.X)
                {
                    negExtent.X = vertex.X;
                }

                if (vertex.X > posExtent.X)
                {
                    posExtent.X = vertex.X;
                }

                if (vertex.Y < negExtent.Y)
                {
                    negExtent.Y = vertex.Y;
                }

                if (vertex.Y > posExtent.Y)
                {
                    posExtent.Y = vertex.Y;
                }

                if (vertex.Z < negExtent.Z)
                {
                    negExtent.Z = vertex.Z;
                }

                if (vertex.Z > posExtent.Z)
                {
                    posExtent.Z = vertex.Z;
                }
            }

            BoxShapeDesc shape = new BoxShapeDesc();

            shape.Extents = (posExtent - negExtent);

            return(shape);
        }
Esempio n. 2
0
        public void BoxStack(int totalRows, Vector3 offset)
        {
            int currentColumnMax = totalRows;

            float        boxScale = 9.0f;
            StaticModel  mesh     = this.game.ModelLoader.LoadStaticModel("Models/WoodCrate/Crate1");
            BoxShapeDesc boxShape = PhysicsComponent.CreateBoxShapeFromMesh(mesh, boxScale);

            for (int rows = 0; rows < totalRows; ++rows, --currentColumnMax)
            {
                for (int columns = 0; columns < currentColumnMax; ++columns)
                {
                    Vector3 boxPos = offset + new Vector3(columns * boxShape.Extents.X,
                                                          rows * boxShape.Extents.Y + (boxShape.Extents.Y * 0.5f),
                                                          0.0f);

                    boxPos.X -= currentColumnMax * boxShape.Extents.X * 0.5f;

                    int boxTemplate = 2;
                    if (rows == (totalRows - 1))
                    {
                        // Use a smoking box if we're on the top row
                        boxTemplate = 3;
                    }

                    var boxEntity = new BaseEntity(this.game, boxPos, Matrix.Identity, 9.0f);
                    this.game.SceneManager.AddEntityByTemplateID(boxEntity, boxTemplate);

                    if (boxTemplate == 3)
                    {
                        ParticleEmitterComponent emitter = boxEntity.GetComponentByType(ComponentType.ParticleEmitterComponent) as ParticleEmitterComponent;
                        // These give offsets for the emitter and how many particles per second to emit.
                        emitter.InitializeEmitterSettings(Vector3.Zero, 7, Vector3.Zero);
                    }
                }
            }
        }
        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);
        }
Esempio n. 4
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);
            }
            ;
        }