Ejemplo n.º 1
0
        protected override void DoAddCollider(FarseerPhysics.Dynamics.Body body, float mass)
        {
            switch (plane2d)
            {
            case Plane2d.XY:
                to2dMode = Physics.To2dMode.DropZ;
                break;

            case Plane2d.XZ:
                to2dMode = Physics.To2dMode.DropY;
                break;

            case Plane2d.YZ:
                to2dMode = Physics.To2dMode.DropX;
                break;
            }
            connectedBody = body;
            Vector2  scale = VectorConverter.Convert(transform.lossyScale, to2dMode);
            Vertices v     = new Vertices();

            for (int i = 0; i < relativePoints.Length; i++)
            {
                v.Add(relativePoints[i] * scale);
            }
            Physics.AddMesh(body, isTrigger, EarclipDecomposer.ConvexPartition(v), mass);
        }
Ejemplo n.º 2
0
 internal CollisionData(FarseerPhysics.Dynamics.Body localBody, FarseerPhysics.Dynamics.Contacts.ContactConstraint impulse, int pointIndex)
 {
     if (localBody == impulse.BodyA)
     {
         this.pos            = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].rA + impulse.BodyA.Position);
         this.normal         = impulse.Normal;
         this.normalImpulse  = PhysicsConvert.ToDualityUnit(PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].NormalImpulse * Time.SPFMult));
         this.tangentImpulse = PhysicsConvert.ToDualityUnit(PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].TangentImpulse * Time.SPFMult));
         this.normalMass     = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].NormalMass);
         this.tangentMass    = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].TangentMass);
     }
     else if (localBody == impulse.BodyB)
     {
         this.pos            = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].rB + impulse.BodyB.Position);
         this.normal         = -impulse.Normal;
         this.normalImpulse  = PhysicsConvert.ToDualityUnit(PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].NormalImpulse * Time.SPFMult));
         this.tangentImpulse = PhysicsConvert.ToDualityUnit(PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].TangentImpulse * Time.SPFMult));
         this.normalMass     = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].NormalMass);
         this.tangentMass    = PhysicsConvert.ToDualityUnit(impulse.Points[pointIndex].TangentMass);
     }
     else
     {
         throw new ArgumentException("Local body is not part of the collision", "localBody");
     }
 }
Ejemplo n.º 3
0
        private void SetPhysicalBody(Physics.Dynamics.Body physicalBody)
        {
            if (this.physicalBody != null)
            {
                this.physicalBody.OnCollision -= PhysicalBody_OnCollision;
            }

            this.physicalBody              = physicalBody;
            this.physicalBody.OnCollision += PhysicalBody_OnCollision;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Destroy the collision body attached to this body.
        /// </summary>
        public void DestroyCollisionBody()
        {
            if (CollisionBody == null)
            {
                return;
            }

            m_position = CollisionBody.Position * GameEnvironment.k_invPhysicsScale;
            if (!VisualRotationOnly)
            {
                m_rotation = CollisionBody.Rotation;
            }

            CollisionWorld.RemoveBody(CollisionBody);
            CollisionWorld = null;
            CollisionBody  = null;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create and attach a new collision body to this Entity.
        /// </summary>
        /// <param name="world">CollisionWorld instance.</param>
        /// <param name="type">Type of collision body.  Dynamic bodies receive responses, kinematic bodies do not.  Static bodies cannot move.</param>
        /// <param name="flags">Flags.</param>
        public void CreateCollisionBody(Physics.Dynamics.World world, Physics.Dynamics.BodyType type, CollisionFlags flags = CollisionFlags.Default)
        {
            if (CollisionBody != null)
            {
                throw new ArgumentException("CreateCollisionBody called on Entity where collision body already exists.");
            }
            CollisionWorld = world;

            Physics.Dynamics.Body body = world.CreateBody();

            body.BodyType = type;
            body.Position = m_position * GameEnvironment.k_physicsScale;
            if (!VisualRotationOnly)
            {
                body.Rotation = m_rotation;
            }
            body.LinearVelocity  = m_velocity * GameEnvironment.k_physicsScale;
            body.FixedRotation   = flags.HasFlag(CollisionFlags.FixedRotation);
            body.SleepingAllowed = !flags.HasFlag(CollisionFlags.DisableSleep);
            body.IsBullet        = flags.HasFlag(CollisionFlags.IsBullet);
            body.UserData        = this;

            CollisionBody = body;
        }
Ejemplo n.º 6
0
        public Entity GetEntityAt(Vector2 position, out Physics.Dynamics.Body body, out Physics.Dynamics.Fixture fixture)
        {
            fixture = PhysicalWorld.TestPoint(position.ToFPVector2());
            body    = null;

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

            body = fixture.Body;

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

            if (body.UserData is IPhysicsUserData)
            {
                return(((IPhysicsUserData)body.UserData).CustomData as Entity);
            }

            return(null);
        }
Ejemplo n.º 7
0
 public Physics(Entity entity, FarseerPhysics.Dynamics.Body body)
 {
     this.body = body;
     body.UserData = entity;
 }
Ejemplo n.º 8
0
        ///// <summary>
        ///// Запишет коорднаты тела в фрейм, проведя конвертирование координат из мира фарсира.
        ///// </summary>
        //static public Frame3D GetFrame(FarseeBody fb, double z = 0)
        //{
        //    Frame3D frame = Vector2ToFrame3D(ConvertUnits.ToDisplayUnits(fb.Position), Angle.FromRad(fb.Rotation), z);
        //    return frame;
        //}

        /// <summary>
        /// Запишет коорднаты тела в фрейм, проведя конвертирование координат из мира фарсира.
        /// Возьмёт Z, pitch и roll из frame.
        /// </summary>
        static public Frame3D GetFrame(FBody fb, Frame3D frame)
        {
            Frame3D f = Vector2ToFrame3D(ToDisplayUnits(fb.Position), Angle.FromRad(fb.Rotation), frame);

            return(f);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Поставит тело из движка место в нужную позицию и повернёт, проведя конвертирование координат в мир фарсира.
 /// </summary>
 static public void PutInFrame(FBody fb, Frame3D frame)
 {
     fb.Position = ToSimUnits(Frame3DToVector2(frame));
     fb.Rotation = (float)frame.Yaw.Radian;
 }
Ejemplo n.º 10
0
		///// <summary>
		///// Запишет коорднаты тела в фрейм, проведя конвертирование координат из мира фарсира.
		///// </summary>		
		//static public Frame3D GetFrame(FarseeBody fb, double z = 0)
		//{
		//    Frame3D frame = Vector2ToFrame3D(ConvertUnits.ToDisplayUnits(fb.Position), Angle.FromRad(fb.Rotation), z);
		//    return frame;
		//}

		/// <summary>
		/// Запишет коорднаты тела в фрейм, проведя конвертирование координат из мира фарсира.
		/// Возьмёт Z, pitch и roll из frame.
		/// </summary>		
		static public Frame3D GetFrame(FBody fb, Frame3D frame)
		{
			Frame3D f = Vector2ToFrame3D(ToDisplayUnits(fb.Position), Angle.FromRad(fb.Rotation), frame);
			return f;
		}
Ejemplo n.º 11
0
		/// <summary>
		/// Поставит тело из движка место в нужную позицию и повернёт, проведя конвертирование координат в мир фарсира.
		/// </summary>		
		static public void PutInFrame(FBody fb, Frame3D frame)
		{
			fb.Position = ToSimUnits(Frame3DToVector2(frame));
			fb.Rotation = (float)frame.Yaw.Radian;
		}
Ejemplo n.º 12
0
        /// <summary>
        /// Destroy the collision body attached to this body.
        /// </summary>
        public void DestroyCollisionBody()
        {
            if (CollisionBody == null) return;

            m_position = CollisionBody.Position * GameEnvironment.k_invPhysicsScale;
            if (!VisualRotationOnly) m_rotation = CollisionBody.Rotation;

            CollisionWorld.RemoveBody(CollisionBody);
            CollisionWorld = null;
            CollisionBody = null;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Create and attach a new collision body to this Entity.
        /// </summary>
        /// <param name="world">CollisionWorld instance.</param>
        /// <param name="type">Type of collision body.  Dynamic bodies receive responses, kinematic bodies do not.  Static bodies cannot move.</param>
        /// <param name="flags">Flags.</param>
        public void CreateCollisionBody(Physics.Dynamics.World world, Physics.Dynamics.BodyType type, CollisionFlags flags = CollisionFlags.Default)
        {
            if (CollisionBody != null) throw new ArgumentException("CreateCollisionBody called on Entity where collision body already exists.");
            CollisionWorld = world;

            Physics.Dynamics.Body body = world.CreateBody();

            body.BodyType = type;
            body.Position = m_position * GameEnvironment.k_physicsScale;
            if (!VisualRotationOnly) body.Rotation = m_rotation;
            body.LinearVelocity = m_velocity * GameEnvironment.k_physicsScale;
            body.FixedRotation = flags.HasFlag(CollisionFlags.FixedRotation);
            body.SleepingAllowed = !flags.HasFlag(CollisionFlags.DisableSleep);
            body.IsBullet = flags.HasFlag(CollisionFlags.IsBullet);
            body.UserData = this;

            CollisionBody = body;
        }
Ejemplo n.º 14
0
        public Entity GetEntityAt(Vector2 position, out Physics.Dynamics.Body body)
        {
            Physics.Dynamics.Fixture buffer;

            return(GetEntityAt(position, out body, out buffer));
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Destroy the collision body attached to this body.
        /// </summary>
        public void DestroyCollisionBody()
        {
            if (CollisionBody == null) return;

            m_collisionWorld.RemoveBody(CollisionBody);
            m_collisionWorld = null;
            CollisionBody = null;
        }