Exemplo 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);
        }
Exemplo n.º 2
0
        static public SphereShapeDesc CreateSphereShapeFromMesh(StaticModel mesh, float scale)
        {
            List <Vector3> verts = mesh.GetModelVertices(scale);

            float largestSquareLength = 0.0f;

            foreach (Vector3 vertex in verts)
            {
                float vertSqrLng = vertex.LengthSquared();
                if (vertSqrLng > largestSquareLength)
                {
                    largestSquareLength = vertSqrLng;
                }
            }

            SphereShapeDesc shape = new SphereShapeDesc();

            shape.Radius = (float)Math.Sqrt(largestSquareLength);

            return(shape);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Handles a message sent to this component.
        /// </summary>
        /// <param name="message">Message to be handled</param>
        /// <returns>True, if handled, otherwise false</returns>
        public override bool ExecuteMessage(IMessage message)
        {
            if (message.UniqueTarget != this.parentEntity.UniqueID)
            {
                return(false);
            }

            switch (message.Type)
            {
            case MessageType.GetModelVertices:
            {
                MsgGetModelVertices msgGetVerts = message as MsgGetModelVertices;
                message.TypeCheck(msgGetVerts);

                if (model == null)
                {
                    return(false);
                }

                msgGetVerts.Vertices = model.GetModelVertices(this.parentEntity.Scale);
            }
                return(true);

            case MessageType.GetModelIndices:
            {
                MsgGetModelIndices msgGetInds = message as MsgGetModelIndices;
                message.TypeCheck(msgGetInds);

                if (model == null)
                {
                    return(false);
                }

                msgGetInds.Indices = model.GetModelIndices();
            }
                return(true);

            case MessageType.SetModelColor:
            {
                MsgSetModelColor msgSetColor = message as MsgSetModelColor;
                message.TypeCheck(msgSetColor);

                this.ModelColor = msgSetColor.Color;
            }
                return(true);

            case MessageType.GetModelColor:
            {
                MsgGetModelColor msgGetColor = message as MsgGetModelColor;
                message.TypeCheck(msgGetColor);

                msgGetColor.Color = this.modelColor;
            }
                return(true);

            case MessageType.SetModelOpacity:
            {
                MsgSetModelOpacity msgSetOpacity = message as MsgSetModelOpacity;
                message.TypeCheck(msgSetOpacity);

                msgSetOpacity.Opacity = this.opacity;
            }
                return(true);

            case MessageType.GetModelOpacity:
            {
                MsgGetModelOpacity msgGetOpacity = message as MsgGetModelOpacity;
                message.TypeCheck(msgGetOpacity);

                this.Opacity = msgGetOpacity.Opacity;
            }
                return(true);

            case MessageType.PositionChanged:
            {
                MsgPositionChanged msgPosChanged = message as MsgPositionChanged;
                message.TypeCheck(msgPosChanged);

                // We update the render mesh's transform (position, rotation),
                // and its bounding sphere's position as well.
                this.worldTransform        = this.scaleMatrix * this.parentEntity.Rotation * Matrix.CreateTranslation(msgPosChanged.Position);
                this.boundingSphere.Center = this.parentEntity.Position;
            }
                return(true);

            case MessageType.RotationChanged:
            {
                MsgRotationChanged msgRotChanged = message as MsgRotationChanged;
                message.TypeCheck(msgRotChanged);

                // We update the render mesh's transform (position, rotation).
                this.worldTransform = this.scaleMatrix * msgRotChanged.Rotation * Matrix.CreateTranslation(this.parentEntity.Position);
            }
                return(true);

            case MessageType.BodyActivated:
            {
                MsgPhysicsBodyActivated msgBodyActive = message as MsgPhysicsBodyActivated;
                message.TypeCheck(msgBodyActive);

                ActivateComponent();
            }
                return(true);

            case MessageType.BodyDeactivated:
            {
                MsgPhysicsBodyDeactivated msgBodyDeactivated = message as MsgPhysicsBodyDeactivated;
                message.TypeCheck(msgBodyDeactivated);

                DeactivateComponent();
            }
                return(true);

            default:
                return(false);
            }
        }
Exemplo 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);
            }
            ;
        }