Note that this is not a full, multi-iteration physics system! This can be used for simple, arcade style physics. Based on http://elancev.name/oliver/2D%20polygon.htm#tut5
Inheritance: Component, IUpdatable
コード例 #1
0
        /// <summary>
        /// handles the collision of two non-overlapping rigidbodies. New velocities will be assigned to each rigidbody as appropriate.
        /// </summary>
        /// <param name="other">Other.</param>
        /// <param name="inverseMTV">Inverse MT.</param>
        void processCollision(ArcadeRigidbody other, ref Vector2 minimumTranslationVector)
        {
            // we compute a response for the two colliding objects. The calculations are based on the relative velocity of the objects
            // which gets reflected along the collided surface normal. Then a part of the response gets added to each object based on mass.
            var relativeVelocity = velocity - other.velocity;

            calculateResponseVelocity(ref relativeVelocity, ref minimumTranslationVector, out relativeVelocity);

            // now we use the masses to linearly scale the response on both rigidbodies
            var totalInverseMass      = _inverseMass + other._inverseMass;
            var ourResponseFraction   = _inverseMass / totalInverseMass;
            var otherResponseFraction = other._inverseMass / totalInverseMass;

            velocity       += relativeVelocity * ourResponseFraction;
            other.velocity -= relativeVelocity * otherResponseFraction;
        }
コード例 #2
0
 /// <summary>
 /// separates two overlapping rigidbodies. Handles the case of either being immovable as well.
 /// </summary>
 /// <param name="other">Other.</param>
 /// <param name="minimumTranslationVector"></param>
 void processOverlap(ArcadeRigidbody other, ref Vector2 minimumTranslationVector)
 {
     if (isImmovable)
     {
         other.Entity.transform.position += minimumTranslationVector;
     }
     else if (other.isImmovable)
     {
         Entity.transform.position -= minimumTranslationVector;
     }
     else
     {
         Entity.transform.position       -= minimumTranslationVector * 0.5f;
         other.Entity.transform.position += minimumTranslationVector * 0.5f;
     }
 }
コード例 #3
0
ファイル: ArcadeRigidbody.cs プロジェクト: fusspawn/Nez
        /// <summary>
        /// separates two overlapping rigidbodies. Handles the case of either being immovable as well.
        /// </summary>
        /// <param name="other">Other.</param>
        /// <param name="minimumTranslationVector"></param>
        void processOverlap(ArcadeRigidbody other, ref Vector2 minimumTranslationVector)
        {
            if (isImmovable)
            {
                other.entity.colliders.unregisterAllCollidersWithPhysicsSystem();
                other.entity.transform.position += minimumTranslationVector;
                other.entity.colliders.registerAllCollidersWithPhysicsSystem();
            }
            else if (other.isImmovable)
            {
                entity.transform.position -= minimumTranslationVector;
            }
            else
            {
                entity.transform.position -= minimumTranslationVector * 0.5f;

                other.entity.colliders.unregisterAllCollidersWithPhysicsSystem();
                other.entity.transform.position += minimumTranslationVector * 0.5f;
                other.entity.colliders.registerAllCollidersWithPhysicsSystem();
            }
        }
コード例 #4
0
ファイル: ArcadeRigidbody.cs プロジェクト: RastaCow/Nez
        /// <summary>
        /// separates two overlapping rigidbodies. Handles the case of either being immovable as well.
        /// </summary>
        /// <param name="other">Other.</param>
        /// <param name="minimumTranslationVector"></param>
        void processOverlap( ArcadeRigidbody other, ref Vector2 minimumTranslationVector )
        {
            if( isImmovable )
            {
                other.entity.colliders.unregisterAllCollidersWithPhysicsSystem();
                other.entity.transform.position += minimumTranslationVector;
                other.entity.colliders.registerAllCollidersWithPhysicsSystem();
            }
            else if( other.isImmovable )
            {
                entity.transform.position -= minimumTranslationVector;
            }
            else
            {
                entity.transform.position -= minimumTranslationVector * 0.5f;

                other.entity.colliders.unregisterAllCollidersWithPhysicsSystem();
                other.entity.transform.position += minimumTranslationVector * 0.5f;
                other.entity.colliders.registerAllCollidersWithPhysicsSystem();
            }
        }
コード例 #5
0
ファイル: ArcadeRigidbody.cs プロジェクト: RastaCow/Nez
        /// <summary>
        /// handles the collision of two non-overlapping rigidbodies. New velocities will be assigned to each rigidbody as appropriate.
        /// </summary>
        /// <param name="other">Other.</param>
        /// <param name="inverseMTV">Inverse MT.</param>
        void processCollision( ArcadeRigidbody other, ref Vector2 minimumTranslationVector )
        {
            // we compute a response for the two colliding objects. The calculations are based on the relative velocity of the objects
            // which gets reflected along the collided surface normal. Then a part of the response gets added to each object based on mass.
            var relativeVelocity = velocity - other.velocity;

            calculateResponseVelocity( ref relativeVelocity, ref minimumTranslationVector, out relativeVelocity );

            // now we use the masses to linearly scale the response on both rigidbodies
            var totalInverseMass = _inverseMass + other._inverseMass;
            var ourResponseFraction = _inverseMass / totalInverseMass;
            var otherResponseFraction = other._inverseMass / totalInverseMass;

            velocity += relativeVelocity * ourResponseFraction;
            other.velocity -= relativeVelocity * otherResponseFraction;
        }
コード例 #6
0
ファイル: ArcadeRigidbody.cs プロジェクト: prime31/Nez
		/// <summary>
		/// separates two overlapping rigidbodies. Handles the case of either being immovable as well.
		/// </summary>
		/// <param name="other">Other.</param>
		/// <param name="minimumTranslationVector"></param>
		void processOverlap( ArcadeRigidbody other, ref Vector2 minimumTranslationVector )
		{
			if( isImmovable )
			{
				other.entity.transform.position += minimumTranslationVector;
			}
			else if( other.isImmovable )
			{
				entity.transform.position -= minimumTranslationVector;
			}
			else
			{
				entity.transform.position -= minimumTranslationVector * 0.5f;
				other.entity.transform.position += minimumTranslationVector * 0.5f;
			}
		}