예제 #1
0
    public static HullCollision2D detectCollisionResponse(OBBHull2D a, OBBHull2D b)
    {
        Dictionary <Vector2, float> axisValues = new Dictionary <Vector2, float>(); //norm to the overlap value

        axisValues.Add(a.getXNormal(), checkAxisPenetration(a, b, a.getXNormal()));
        axisValues.Add(a.getYNormal(), checkAxisPenetration(a, b, a.getYNormal()));
        if (a.rotation != b.rotation)
        {
            axisValues.Add(b.getXNormal(), checkAxisPenetration(a, b, b.getXNormal()));
            axisValues.Add(b.getYNormal(), checkAxisPenetration(a, b, b.getYNormal()));
        }
        Dictionary <Vector2, float> .Enumerator enumerator = axisValues.GetEnumerator();
        enumerator.MoveNext();
        Vector2 pos = a.position;
        KeyValuePair <Vector2, float> bestPenetration = enumerator.Current; //need to set one to compare too, no null value to just allow all checking in the for loop

        if (bestPenetration.Value == 0.0f)
        {
            return(null);
        }
        while (enumerator.MoveNext())
        {
            if (Mathf.Abs(enumerator.Current.Value) < Mathf.Abs(bestPenetration.Value))
            {
                bestPenetration = enumerator.Current;
            }
            if (bestPenetration.Value == 0.0f)
            {
                return(null);
            }
        }
        HullCollision2D collision;
        Vector2         norm = bestPenetration.Key;

        if (bestPenetration.Value <= 0.0f) //ooh this was an easy thing to miss
        {
            norm *= -1.0f;
        }

        collision = new HullCollision2D(a, b, norm, Mathf.Abs(bestPenetration.Value));
        //collision.contactPoint = pos;
        return(collision);
    }
예제 #2
0
    public static HullCollision2D detectCollisionResponse(OBBHull2D a, AABBHull2D b)
    {
        Dictionary <Vector2, float> axisValues = new Dictionary <Vector2, float>(); //norm to the overlap value

        axisValues.Add(Vector2.up, checkAxisPenetration(a, b, Vector2.up));
        axisValues.Add(Vector2.right, checkAxisPenetration(a, b, Vector2.right));
        if (a.rotation != 0.0f)
        {
            axisValues.Add(a.getXNormal(), checkAxisPenetration(a, b, a.getXNormal()));
            axisValues.Add(a.getYNormal(), checkAxisPenetration(a, b, a.getYNormal()));
        }
        Dictionary <Vector2, float> .Enumerator enumerator = axisValues.GetEnumerator();
        enumerator.MoveNext();
        KeyValuePair <Vector2, float> bestPenetration = enumerator.Current; //need to set one to compare too, no null value to just allow all checking in the for loop

        if (bestPenetration.Value == 0.0f)
        {
            return(null);
        }
        while (enumerator.MoveNext())
        {
            if (Mathf.Abs(enumerator.Current.Value) < Mathf.Abs(bestPenetration.Value))
            {
                bestPenetration = enumerator.Current;
            }
            if (bestPenetration.Value == 0.0f)
            {
                return(null);
            }
        }
        HullCollision2D collision;
        Vector2         norm = bestPenetration.Key;


        if (Vector2.Dot(a.velocity, norm) >= 0.0f)
        {
            norm *= -1.0f;
        }


        collision = new HullCollision2D(a, b, norm, Mathf.Abs(bestPenetration.Value));
        return(collision);
    }
예제 #3
0
 /*
  * VERIFIED WORKING, based off OBB v OBB Collision
  */
 public static bool detectCollision(OBBHull2D a, AABBHull2D b)
 {
     if (checkAxis(a, b, a.getXNormal()) &&
         checkAxis(a, b, a.getYNormal()) &&
         checkAxis(a, b, Vector2.left) &&
         checkAxis(a, b, Vector2.up))
     {
         return(true);
     }
     return(false);
 }
예제 #4
0
 /*
  * Verified Working, possible error in using 4 checkaxis, i think i missed a check somewhere deeper in the code
  */
 public static bool detectCollision(OBBHull2D lft, OBBHull2D rgt)
 {
     //check each axis on each side, need to make better
     if (checkAxis(lft, rgt, lft.getXNormal()) &&
         checkAxis(lft, rgt, lft.getYNormal()) &&
         checkAxis(lft, rgt, rgt.getXNormal()) &&
         checkAxis(lft, rgt, rgt.getYNormal()))
     {
         return(true);
     }
     return(false);
 }