/// <summary> /// Returns true if the given Rectangle is colliding with the given axis. /// </summary> /// <param name="other"></param> /// <param name="axis"></param> /// <returns></returns> private bool IsAxisCollision(RotatableRectangle other, Vector2 axis) { int thisMinScaler, otherMinScaler, thisMaxScaler, otherMaxScaler; // this scalers int scaler = GenerateScalar(this.topLeft, axis); thisMinScaler = scaler; thisMaxScaler = scaler; scaler = GenerateScalar(this.topRight, axis); if (scaler < thisMinScaler) thisMinScaler = scaler; if (scaler > thisMaxScaler) thisMaxScaler = scaler; scaler = GenerateScalar(this.bottomLeft, axis); if (scaler < thisMinScaler) thisMinScaler = scaler; if (scaler > thisMaxScaler) thisMaxScaler = scaler; scaler = GenerateScalar(this.bottomRight, axis); if (scaler < thisMinScaler) thisMinScaler = scaler; if (scaler > thisMaxScaler) thisMaxScaler = scaler; // other scalers scaler = GenerateScalar(other.topLeft, axis); otherMinScaler = scaler; otherMaxScaler = scaler; scaler = GenerateScalar(other.topRight, axis); if (scaler < otherMinScaler) otherMinScaler = scaler; if (scaler > otherMaxScaler) otherMaxScaler = scaler; scaler = GenerateScalar(other.bottomLeft, axis); if (scaler < otherMinScaler) otherMinScaler = scaler; if (scaler > otherMaxScaler) otherMaxScaler = scaler; scaler = GenerateScalar(other.bottomRight, axis); if (scaler < otherMinScaler) otherMinScaler = scaler; if (scaler > otherMaxScaler) otherMaxScaler = scaler; if (thisMinScaler <= otherMaxScaler && thisMaxScaler >= otherMaxScaler) return true; else if (otherMinScaler <= thisMaxScaler && otherMaxScaler >= thisMaxScaler) return true; return false; }
/// <summary> /// Returns true if the Rectangle intersects with the other Rectangle. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool Intersects(RotatableRectangle other) { if (this.recalculateCorners) CalculateCorners(); if (other.recalculateCorners) other.CalculateCorners(); Vector2 axis1 = this.topRight - this.topLeft; Vector2 axis2 = this.topRight - this.bottomRight; Vector2 axis3 = other.topLeft - other.bottomLeft; Vector2 axis4 = other.topLeft - other.topRight; if (!IsAxisCollision(other, axis1)) return false; if (!IsAxisCollision(other, axis2)) return false; if (!IsAxisCollision(other, axis3)) return false; if (!IsAxisCollision(other, axis4)) return false; return true; }