public void TestRectanglePolygonCollision() { // Import data var rect = Rects.Rect; var poly = Polygons.Poly; var results = RectPolyTest.RectPolyResults; for (int obj = 0; obj < results.Length; obj++) { CollisionResult result; //Create objects RectangleShape rectShape = new RectangleShape(rect[results[obj].FirstShape].Center, rect[results[obj].FirstShape].Width, rect[results[obj].FirstShape].Height); Collision.Transform rectTransform = new Collision.Transform(rect[results[obj].FirstShape].Trans, rect[results[obj].FirstShape].Rotate); PolygonShape polShape = new PolygonShape(poly[results[obj].SecondShape].Points); Collision.Transform polTransform = new Collision.Transform(poly[results[obj].SecondShape].Trans, poly[results[obj].SecondShape].Rotate); var testResult = rectShape.IntersectsWith(rectTransform, polShape, polTransform, out result); string err = String.Format( "Rectangle to Polygon test({0}) failed; Rectangle_ID: {1}, Polygon_ID: {2}", obj, results[obj].FirstShape, results[obj].SecondShape); Assert.That(results[obj].Result == testResult, err); } }
public void TestRectangleRectangleCollision() { // Import data var rect = Rects.Rect; var results = RectRectTest.RectRectResults; for (int obj = 0; obj < results.Length; obj++) { CollisionResult result; //Create rectangle objects RectangleShape firstRect = new RectangleShape(rect[results[obj].FirstShape].Center, rect[results[obj].FirstShape].Width, rect[results[obj].FirstShape].Height); RectangleShape secondRect = new RectangleShape(rect[results[obj].SecondShape].Center, rect[results[obj].SecondShape].Width, rect[results[obj].SecondShape].Height); // Transform Rectangles Collision.Transform firstRectTransform = new Collision.Transform(rect[results[obj].FirstShape].Trans, rect[results[obj].FirstShape].Rotate); Collision.Transform secondRectTransform = new Collision.Transform(rect[results[obj].SecondShape].Trans, rect[results[obj].SecondShape].Rotate); var testResult = firstRect.IntersectsWith(firstRectTransform, secondRect, secondRectTransform, out result); string err = String.Format( "Rectangle to Rectangle test({0}) failed; Rectangle({1}), Rectangle({2})", obj, results[obj].FirstShape, results[obj].SecondShape); Assert.That(results[obj].Result == testResult, err); } }
public void TestRectRectCollision() { // true results: not touching, intersection, touching bool[] trueResults = { false, true, true }; // First rectangle values Vector2[][] firstRec = { new[] { new Vector2(-590.333f, -23456.0f), new Vector2(99.99f, 1111.01f) }, new[] { new Vector2(-73.999f, 60.001f), new Vector2(10.012f, 109.0002f) }, new[] { new Vector2(0, 8.0f), new Vector2(4.0f, 2.0f) } }; // Second rectangle values Vector2[][] secondRec = { new[] { new Vector2(1050.009f, 907.039f), new Vector2(2.00123f, 222.33f) }, new[] { new Vector2(3.23f, 53.0101f), new Vector2(97.0301f, 3.99991f) }, new[] { new Vector2(3.0f, 8.0f), new Vector2(2.0f, 2.0f) } }; //Turns for first and second Shapes Vector2[] turnShape = { new Vector2(355.0f, 1.98765f), new Vector2(72.909f, 22.0001f), new Vector2(360.0f, 360.0f) //wery hard make touching by hand, so use 360° }; for (var obj = 0; obj < trueResults.Length; obj++) { CollisionResult result; //Create rectangle objects RectangleShape firstRectShape = new RectangleShape(Vector2.Zero, firstRec[obj][1].X, firstRec[obj][1].Y); RectangleShape secondRectShape = new RectangleShape(Vector2.Zero, secondRec[obj][1].X, secondRec[obj][1].Y); // Transform Shapes Collision.Transform firstRectTransform = new Collision.Transform(firstRec[obj][0], turnShape[obj].X * Mathf.Deg2Rad); Collision.Transform secondRectTransform = new Collision.Transform(secondRec[obj][0], turnShape[obj].Y * Mathf.Deg2Rad); // Make test var testResult = firstRectShape.IntersectsWith(firstRectTransform, secondRectShape, secondRectTransform, out result); string err = String.Format( "Rectangle to Rectangle test({0}) failed; Rect1: center({1}), size({2}x{3}){4}° - Rect2: center ({5}), size({6}x{7}){8}°", obj, firstRec[obj][0], firstRec[obj][1].X, firstRec[obj][1].Y, turnShape[obj].X, secondRec[obj][0], secondRec[obj][1].X, secondRec[obj][1].Y, turnShape[obj].Y); Assert.That(trueResults[obj] == testResult, err); } }
public void TestPointRectangleCollision() { // true results bool[] trueResults = { true, true, true, true, false }; // Points coordinates Vector2[] points = { new Vector2(-1.23f, 0.9834f), new Vector2(1.234f, 1.199f), new Vector2(1.00009f, -0.999f), new Vector2(-0.96001f, -0.86023f), new Vector2(234.432f, 99.66f) }; float[] pointTurn = { 234.12f, 12.234f, 11.11f, 66.66f, 111.111f }; //Create circle obj RectangleShape rectShape = new RectangleShape(Vector2.Zero, 6.340010f, 4.020202f); Collision.Transform rectTransform = new Collision.Transform(new Vector2(-0.0005f, -0.345f), 1.34f * Mathf.Deg2Rad); //Create points and check collisions for (var obj = 0; obj < trueResults.Length; obj++) { CollisionResult result; PointShape pointShape = new PointShape(Vector2.Zero); Collision.Transform pointTranform = new Collision.Transform(points[obj], pointTurn[obj] * Mathf.Deg2Rad); var testResult = rectShape.IntersectsWith(rectTransform, pointShape, pointTranform, out result); string err = String.Format( "Point to Rectangle test({0}) failed. Rect:(center=6.340010f, 4.020202f : WH= -0.0005f, " + "-0.345f)1.34f°, Point({1})", obj, points[obj]); Assert.That(trueResults[obj] == testResult, err); } }