Ejemplo n.º 1
0
 public bool TestCollisionCompatibility(ICollisionVolume other)
 {
     if (other is SATCollisionVolume) return true;
     else return false;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Tests if two collision volumes are overlapping
        /// </summary>
        /// <param name="otherCV">The collision volume to test against</param>
        /// <returns>True if there is an overlap.</returns>
        public bool TestCollision(ICollisionVolume other)
        {
            if (other == this) return false;
            if (TestCollisionCompatibility(other) == false) return false;
            SATCollisionVolume otherCV = (SATCollisionVolume)other;

            float thisMin, thisMax, otherMin, otherMax;
            float location; //projected location of the vertex along the axis

            List<Vector2> allAxes = new List<Vector2>();

            //initialize collection of all axes to be tested
            foreach (Vector2 axis in TestAxes)
                allAxes.Add(axis);
            foreach (Vector2 axis in otherCV.TestAxes)
                allAxes.Add(axis);

            foreach (Vector2 axis in allAxes)
            {
                //reset the min and max for this axis test
                thisMin = otherMin = float.PositiveInfinity;
                thisMax = otherMax = float.NegativeInfinity;

                //find this object's min and max when projected against this axis
                foreach (Vector2 vertex in cachedWorldCoordinateVertices)
                {
                    location = Vector2.Dot(vertex, axis);
                    if (location < thisMin) thisMin = location;
                    if (location > thisMax) thisMax = location;
                }

                //find the other object's min and max when projected against this axis
                foreach (Vector2 vertex in otherCV.cachedWorldCoordinateVertices)
                {
                    location = Vector2.Dot(vertex, axis);
                    if (location < otherMin) otherMin = location;
                    if (location > otherMax) otherMax = location;
                }

                //determine if the segments overlap
                if (thisMax < otherMin || thisMin > otherMax)
                    return false;
            }

            //if the loop has completed and the projected segments overlap on all axes then the objects are touching.
            return true;
        }