Exemple #1
0
        public void TestPolygonCircleCollision()
        {
            PolygonShape polyShape = new PolygonShape(new Vector2[]
            {
                new Vector2(2.291772f, 0.6106892f),
                new Vector2(2.80672f, 1.035656f),
                new Vector2(3.09072f, 1.088906f),
                new Vector2(3.436845f, 0.9380314f),
                new Vector2(3.556658f, 0.649594f),
                new Vector2(3.525595f, 0.383344f),
                new Vector2(3.290365f, 0.03150498f),
                new Vector2(2.9109f, -0.288045f)
            });

            Collision.Transform polyTransform = new Collision.Transform(new Vector2(10.47f, -5.44f), -1.349715f);

            CircleShape circleShape = new CircleShape(new Vector2(0, 0), 0.01f);

            Collision.Transform circleTransform = new Collision.Transform(new Vector2(11.51633f, -8.27487f), 5.145f);

            // circle and poly should collide
            CollisionResult result;

            Assert.That(polyShape.IntersectsWith(polyTransform, circleShape, circleTransform, out result), "PolygonCircle collision failed");

            // should not collide in this case
            Collision.Transform anotherCircleTransform = new Collision.Transform(new Vector2(15.51633f, -8.27487f), 5.145f);
            Assert.That(!polyShape.IntersectsWith(polyTransform, circleShape, anotherCircleTransform, out result), "PolygonCircle collision failed");
        }
Exemple #2
0
        public void TestPolygonPolygonCollision()
        {
            // Import data
            var poly    = Polygons.Poly;
            var results = PolyPolyTest.PolyPolyResults;

            for (int obj = 0; obj < results.Length; obj++)
            {
                CollisionResult result;
                // Create polygon objects
                PolygonShape firstPol  = new PolygonShape(poly[results[obj].FirstShape].Points);
                PolygonShape secondPol = new PolygonShape(poly[results[obj].SecondShape].Points);

                // Transform polygons
                Collision.Transform firstPolTransform =
                    new Collision.Transform(poly[results[obj].FirstShape].Trans,
                                            poly[results[obj].FirstShape].Rotate);
                Collision.Transform secondPolTransform =
                    new Collision.Transform(poly[results[obj].SecondShape].Trans,
                                            poly[results[obj].SecondShape].Rotate);
                var testResult = firstPol.IntersectsWith(firstPolTransform, secondPol,
                                                         secondPolTransform, out result);
                string err = String.Format("Polygon to Polygon test({0}) failed; Polygon({1}), Polygon({2})",
                                           obj, results[obj].FirstShape, results[obj].SecondShape);
                Assert.That(results[obj].Result == testResult, err);
            }
        }
        public void SingleTestRectRectCollision()
        {
            Vector2[] firstPol =
            {
                new Vector2(-58.99589f,  -180.19639f), new Vector2(-173.56437f, 156.44204f),
                new Vector2(-116.36934f,  143.46347f),
                new Vector2(85.68483f,     64.82733f), new Vector2(175.23662f, -90.592f)
            };
            Vector2[] secondPol =
            {
                new Vector2(-196.38893f, -132.43044f), new Vector2(43.23116f, 78.08958f),
                new Vector2(-65.40522f, -98.55061f)
            };

            CollisionResult result;
            //Create rectangle objects
            PolygonShape firstPolShape  = new PolygonShape(firstPol);
            PolygonShape secondPolShape = new PolygonShape(secondPol);

            // Transform Shapes
            Collision.Transform firstPolTransform =
                new Collision.Transform(new Vector2(152.17828f, -197.50353f), 3.57942f);
            Collision.Transform secondPolTransform =
                new Collision.Transform(new Vector2(-55.65048f, -119.74799f), 4.94887f);
            // Make test
            var testResult = firstPolShape.IntersectsWith(firstPolTransform, secondPolShape,
                                                          secondPolTransform, out result);

            Assert.That(testResult, "Error Kobzar");
            Console.Write("Point: {0} \n", result.Points[0]);
            Console.Write("Normal: {0}", result.Normals[0]);
        }
        public void TestPointPolygonCollision()
        {
            // 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 };
            // Polygon points
            Vector2[] polPoints =
            {
                new Vector2(-15.30f,    -4.4560f), new Vector2(-9.9910f,  -5.25101f),
                new Vector2(-4.99f,      -10.05f), new Vector2(-5.001f,    -15.081f),
                new Vector2(-10.0333f, -19.8301f), new Vector2(-15.234f,  -22.4598f),
                new Vector2(-20.90f,      -15.0f), new Vector2(-20.0123f, -10.3398f)
            };
            //Create circle obj
            PolygonShape polShape = new PolygonShape(polPoints);

            Collision.Transform polTransform =
                new Collision.Transform(new Vector2(-0.0005f, 15.045f), 45.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 = polShape.IntersectsWith(polTransform, pointShape, pointTranform, out result);
                string err        = String.Format("Point to Polygon test({0}) failed. Polygon:()2.34f°, Point({1})", obj,
                                                  points[obj]);
                Assert.That(trueResults[obj] == testResult, err);
            }
        }
        public void TestPolyPolyCollision()
        {
            // true results: not touching, intersection, touching
            bool[] trueResults = { false, true, true };
            // First Polygon values (triangle)
            Vector2[][] firstPol =
            {
                new[]
                {
                    new Vector2(-934.934f, 7222.009f), new Vector2(-1022.2f, 6909.892f),
                    new Vector2(-1200.29f, 1005.92f)
                },
                new[]
                {
                    new Vector2(99.29f, 302.924f), new Vector2(66.002f, 180.892f), new Vector2(210.09f, 103.92f),
                    new Vector2(333.09f, 111.2f), new Vector2(399.009f, 193.0f), new Vector2(410.0901f, 303.192f),
                    new Vector2(300.0f, 400.0f), new Vector2(190.099f, 347.9201f)
                },
                new[]
                {
                    new Vector2(1.0f, 1.0f), new Vector2(1.0f, 3.0f), new Vector2(3.0f, 3.0f),
                    new Vector2(3.0f, 1.0f)
                }
            };

            Vector2[][] secondPol =
            {
                new[]
                {
                    new Vector2(934.934f, -7222.009f), new Vector2(1022.2f, -6909.892f),
                    new Vector2(1200.29f, -1005.92f)
                },
                new[]
                {
                    new Vector2(45.001f, 450.92f), new Vector2(301.002f, 197.8924f), new Vector2(390.29f, 345.92f),
                    new Vector2(410.0123f, 499.9999f)
                },
                new[]
                {
                    new Vector2(3.0f, 3.0f), new Vector2(3.0f, 5.0f),
                    new Vector2(5.0f, 5.0f), new Vector2(5.0f, 3.0f)
                }
            };

            //Turns for first and second Shapes
            Vector2[] turnShape =
            {
                new Vector2(281.2345f, 101.765f),
                new Vector2(22.098f,      4.76f),
                new Vector2(360.0f, 360.0f) //wery hard make touching by hand, so use 360°
            };

            //Displacement for first and second Shapes
            Vector2[][] dispShape =
            {
                new[] { new Vector2(-1500.234f, -2345.76f), new Vector2(2111.2345f, 1111.765f) },
                new[] { new Vector2(220.0f,        330.0f), new Vector2(220.0f,        330.0f) },
                new[] { new Vector2(2.0f,            2.0f), new Vector2(2.0f,            2.0f) }
            };

            for (var obj = 0; obj < trueResults.Length; obj++)
            {
                CollisionResult result;
                // Create polygon objects
                PolygonShape firstPolShape  = new PolygonShape(firstPol[obj]);
                PolygonShape secondPolShape = new PolygonShape(secondPol[obj]);
                // Transform Shapes
                Collision.Transform firstPolTransform =
                    new Collision.Transform(dispShape[obj][0], turnShape[obj].X * Mathf.Deg2Rad);
                Collision.Transform secondPolTransform =
                    new Collision.Transform(dispShape[obj][1], turnShape[obj].Y * Mathf.Deg2Rad);
                // Make test
                var testResult = firstPolShape.IntersectsWith(firstPolTransform, secondPolShape,
                                                              secondPolTransform, out result);
                string p1 = "";
                foreach (var points in firstPol[obj])
                {
                    p1 = p1 + points + ";";
                }
                string p2 = "";
                foreach (var points in secondPol[obj])
                {
                    p2 = p2 + points + ";";
                }

                string err =
                    String.Format("Polygon to Polygon test({0}) failed; Shape1:({1}){2}° Shape2:({3}) {4}°", obj,
                                  p1, turnShape[obj].X, p2, turnShape[obj].Y);
                Assert.That(trueResults[obj] == testResult, err);
            }
        }