/// <summary> /// Check if a polygon fully cointains another polygon /// </summary> /// <param name="p"></param> /// <returns></returns> private bool CheckContains(Polygon p) { Vector2 mine = GetCorners()[0]; Vector2[] otherCorners = p.GetCorners(); Vector2 limit = new Vector2(Math.Min(this.Bounds.X, p.Bounds.X) - 2, Math.Min(this.Bounds.Y, p.Bounds.Y) - 2); int count = 0; for (int i = 0; i < otherCorners.Length; i++) { if (LocationManager.linesIntersect(mine, limit, otherCorners[i], otherCorners[(i + 1) % otherCorners.Length])) count++; } return count % 2 == 0; }
/// <summary> /// Gets an array of colision data including both polygon lines and the actual intersect point /// </summary> /// <param name="p"></param> /// <returns>[TgtLineStart, TgtLineEnd, MyLineStart, MyLineEnd, IntersectionPoint]</returns> public Vector2[] CheckColisions_Accurate_GetLine(Polygon p) { Vector2[] myCorners = GetCorners(); Vector2[] otherCorners = p.GetCorners(); for (int i = 0; i < myCorners.Length; i++) { for (int i2 = 0; i2 < otherCorners.Length; i2++) { if (LocationManager.linesIntersect(myCorners[i], myCorners[(i + 1) % myCorners.Length], otherCorners[i2], otherCorners[(i2 + 1) % otherCorners.Length])) return new Vector2[] { otherCorners[i2], otherCorners[(i2 + 1) % otherCorners.Length],myCorners[i], myCorners[(i + 1) % myCorners.Length], LocationManager.getIntersectionPoint(myCorners[i], myCorners[(i + 1) % myCorners.Length], otherCorners[i2], otherCorners[(i2 + 1) % otherCorners.Length]).Value}; } } return null; }
/// <summary> /// Checks if two polygons are coliding (including encapsulation) /// </summary> /// <param name="p"></param> /// <returns></returns> public bool CheckColisions_Accurate(Polygon p) { Vector2[] myCorners = GetCorners(); Vector2[] otherCorners = p.GetCorners(); for (int i = 0; i < myCorners.Length; i++) { for (int i2 = 0; i2 < otherCorners.Length; i2++) { if (LocationManager.linesIntersect(myCorners[i], myCorners[(i + 1) % myCorners.Length], otherCorners[i2], otherCorners[(i2 + 1) % otherCorners.Length])) return true; } } return !CheckContains(p) || !p.CheckContains(this); }