Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// 4 body orientovaného obdélníku
- /// - top-left, top-right, bottom-right, bottom-left !!! (clockwise order)
- /// </summary>
- /// <summary>
- /// Test kolize - jestli dva orientované rectangles (ve světových souřadnicích) kolidují
- /// </summary>
- public static bool IsCollidingRectangles(Vector2[] a, Vector2[] b)
- {
- if (!CollidesWith(a, b)) // Všechny 4 body b jsou za nějakou stranou a (tj. určitě není kolize)
- return false;
- if (!CollidesWith(b, a))
- return false;
- return true;
- }
- /// <summary>
- /// Jestli koliduje a -> b (pak se testuje i obráceně...)
- /// </summary>
- public static bool CollidesWith(Vector2[] a, Vector2[] b)
- {
- for (int i = 0; i < 4; i++)
- {
- int j = (i + 1) % 4;
- Vector2 vectorStart = a[j];
- Vector2 vectorDirection = a[i] - a[j];
- bool collisionFound = false;
- for (int y = 0; y < 4; y++)
- if (IsPointOnRightSide(vectorStart, vectorDirection, b[y]))
- collisionFound = true;
- if (!collisionFound)
- return false;
- }
- return true;
- }
- /// <summary>
- /// Tests line (vector + point) vs. other point
- /// - vectorDirection = LinePoint1 - VectorStart (VectorStart = LinePoint2)
- /// - returns true, if the point is on the right side of the line
- /// </summary>
- private static bool IsPointOnRightSide(Vector2 vectorStart, Vector2 vectorDirection, Vector2 point)
- {
- Vector2 secondVector = point - vectorStart;
- float dotProduct = Vector2.Dot(vectorDirection, secondVector); // = skalární součin
- return dotProduct > 0f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement