/* * bool Inisde( x, y, l, r, b, t )//x,y are the point, l,r,b,t are the extents of the rectangle{ return x > l && x < r && y > b && y < t;} * */ public bool Intersects(ExtendedRectangle rectangle) { return Check (this.CollisionRectangle, this.Origin,this.Rotation, rectangle.CollisionRectangle,rectangle.Origin,rectangle.Rotation); }
private bool CheckAxisCollision(ExtendedRectangle rectA, ExtendedRectangle rectB, Vector2 aAxis) { int[] aRectangleAScalars = { GenerateScalar(rectB.UpperLeft, aAxis), GenerateScalar(rectB.UpperRight, aAxis), GenerateScalar(rectB.LowerLeft, aAxis), GenerateScalar(rectB.LowerRight, aAxis) }; int[] aRectangleBScalars = { GenerateScalar(rectA.UpperLeft, aAxis), GenerateScalar(rectA.UpperRight, aAxis), GenerateScalar(rectA.LowerLeft, aAxis), GenerateScalar(rectA.LowerRight, aAxis) }; int aRectangleAMinimum = Min(aRectangleAScalars); int aRectangleAMaximum = Max(aRectangleAScalars); int aRectangleBMinimum = Min(aRectangleBScalars); int aRectangleBMaximum = Max(aRectangleBScalars); //If we have overlaps between the Rectangles (i.e. Min of B is less than Max of A) //then we are detecting a collision between the rectangles on this Axis if ((aRectangleBMinimum <= aRectangleAMaximum && aRectangleBMaximum >= aRectangleAMaximum) || (aRectangleAMinimum <= aRectangleBMaximum && aRectangleAMaximum >= aRectangleBMaximum)) return true; return false; }
public bool Check(Rectangle theRectangleA, Vector2 theOriginA, float theRotationA, Rectangle theRectangleB, Vector2 theOriginB, float theRotationB) { ExtendedRectangle rectA = new ExtendedRectangle (theRectangleA, theRotationA, theOriginA); ExtendedRectangle rectB = new ExtendedRectangle (theRectangleB, theRotationB, theOriginB); theOriginA += rectA.UpperLeft; theOriginB += rectB.UpperLeft; rectB.Rotate (theOriginB, theRotationB); rectB.RotateInverse (theOriginA, theRotationA); rectA.AddVector (-theOriginA); rectB.AddVector (-theOriginA); if ((rectB.MinX () > rectA.MaxX ()) || (rectB.MaxX () < rectA.MinX ()) // x-axis of A || (rectB.MinY () > rectA.MaxY ()) || (rectB.MaxY () < rectA.MinY ()) // y-axis of A || (!CheckAxisCollision (rectA, rectB, rectB.UpperLeft - rectB.UpperRight)) // x-axis of B || (!CheckAxisCollision (rectA, rectB, rectB.UpperLeft - rectB.LowerLeft))) // y-axis of B return false; return true; }