private bool GetColision_Accurate(Vector2[] myCoords, Vector2[] otherCoords) { Vector2 limit = new Vector2(-10000000, -10000000); for (int i = 0; i < 4; i++) { int ccount1 = 0; int ccount2 = 0; for (int i2 = 0; i2 < 4; i2++) { if (LocationManager.linesIntersect(new Vector2[] { myCoords[i], limit }, new Vector2[] { otherCoords[i2], otherCoords[(i2 + 1) % 4] })) { ccount1++; } if (LocationManager.linesIntersect(new Vector2[] { otherCoords[i], limit }, new Vector2[] { myCoords[i2], myCoords[(i2 + 1) % 4] })) { ccount2++; } if (LocationManager.linesIntersect(new Vector2[] { myCoords[i], myCoords[(i + 1) % 4] }, new Vector2[] { otherCoords[i2], otherCoords[(i2 + 1) % 4] })) { return(true); } } if (ccount1 == 1 || ccount2 == 1) { return(true); } } return(false); }
/// <summary> /// Check if a polygon encapulates a point /// </summary> /// <param name="v"></param> /// <returns></returns> public bool Contains(Vector2 v) { Vector2 limit = new Vector2(this.bounds.X - 2, this.bounds.Y - 2); int count = 0; for (int i = 0; i < GetCorners().Length; i++) { if (LocationManager.linesIntersect(v, limit, GetCorners()[i], GetCorners()[(i + 1) % GetCorners().Length])) count++; } return count % 2 == 1; }
/// <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); }