Esempio n. 1
0
        private Collision2D(PhysicsBody2D A, PhysicsBody2D B)
        {
            // For ease of computing around static dynamic collisions I have decided the static body should always be BodyA
            if (A.flagBodyType.HasFlag(PhysicsEngine.BodyType.STATIC) || A.flagBodyType.HasFlag(PhysicsEngine.BodyType.KINEMATIC) || B.flagBodyType.HasFlag(PhysicsEngine.BodyType.STATIC) || B.flagBodyType.HasFlag(PhysicsEngine.BodyType.KINEMATIC))
            {
                if (A.flagBodyType.HasFlag(PhysicsEngine.BodyType.STATIC) || A.flagBodyType.HasFlag(PhysicsEngine.BodyType.KINEMATIC))
                {
                    BodyA = A;
                    BodyB = B;
                }
                else
                {
                    BodyA = B;
                    BodyB = A;
                }
            }
            else
            {
                BodyA = A;
                BodyB = B;
            }

            type = PhysicsEngine.CollisionType.NONE;
        }
Esempio n. 2
0
        /// <summary>
        /// Evaluates whether the collision is occuring or not
        /// </summary>
        /// <returns>Whether a collision is occuring or not</returns>
        public bool Evaluate()
        {
            bool test = BodyA.shape.OverlapTest(BodyB.shape, BodyA.transform, BodyB.transform);

            // If the test returns true, collision is occuring

            // If this type is start and test is true, collision is continuing
            if (type == PhysicsEngine.CollisionType.START && test)
            {
                type = PhysicsEngine.CollisionType.STAY;
                //BodyA.Broadcast(this, "OnCollision2DStay");
                //BodyB.Broadcast(this, "OnCollision2DStay");
                BodyA.PhysicsBody2DCallStay(this);
                BodyB.PhysicsBody2DCallStay(this);
            }
            // If this type is none and test is true, collision is starting
            if (type == PhysicsEngine.CollisionType.NONE && test)
            {
                type = PhysicsEngine.CollisionType.START;
                //BodyA.Broadcast(this, "OnCollision2DStart");
                //BodyB.Broadcast(this, "OnCollision2DStart");
                BodyA.PhysicsBody2DCallStart(this);
                BodyB.PhysicsBody2DCallStart(this);
            }
            // If this type is stop and test is false, collision is none
            if (type == PhysicsEngine.CollisionType.STOP && !test)
            {
                type = PhysicsEngine.CollisionType.NONE;
            }
            // If this type is start or stay and test is false, collision is ending
            if ((type == PhysicsEngine.CollisionType.START || type == PhysicsEngine.CollisionType.STAY) && !test)
            {
                type = PhysicsEngine.CollisionType.STOP;
                //BodyA.Broadcast(this, "OnCollision2DStop");
                //BodyB.Broadcast(this, "OnCollision2DStop");
                BodyA.PhysicsBody2DCallStop(this);
                BodyB.PhysicsBody2DCallStop(this);
            }
            // If this type is stop and test is true, collision is starting, again
            if (type == PhysicsEngine.CollisionType.STOP && test)
            {
                type = PhysicsEngine.CollisionType.START;
                //BodyA.Broadcast(this, "OnCollision2DStart");
                //BodyB.Broadcast(this, "OnCollision2DStart");
                BodyA.PhysicsBody2DCallStart(this);
                BodyB.PhysicsBody2DCallStart(this);
            }

            if (test)
            {
                foreach (Collision2D.OnCollision callback in BodyA.collisionCallbacks)
                {
                    callback.Invoke(this);
                }

                foreach (Collision2D.OnCollision callback in BodyB.collisionCallbacks)
                {
                    callback.Invoke(this);
                }
            }

            return(test);
        }