示例#1
0
        public void TestRaycastComplex()
        {
            // lines
            Vector2[][] lines =
            {
                new[] { new Vector2(5.01675f, 21.14647f), new Vector2(9.35574f,  23.23984f) },
                new[] { new Vector2(9.35574f, 23.23984f), new Vector2(5.01675f,  21.14647f) },
                new[] { new Vector2(4.6742f,  12.27819f), new Vector2(18.37627f, 18.32993f) }
            };
            Vector2[] intrDot =
            {
                new Vector2(7.66f, 22.42f),
                new Vector2(7.66f, 22.42f),
                new Vector2(7.16f, 13.38f)
            };

            // Polygon points
            Vector2[] rect =
            {
                new Vector2(6.0f, 8.0f), new Vector2(4.0f, 12.0f)
            };
            Vector2[] polPoints =
            {
                new Vector2(8.0f,  6.0f), new Vector2(14.0f, 6.0f),
                new Vector2(14.0f, 2.0f), new Vector2(8.0f, 2.0f)
            };
            //Create Rectangles
            RectangleShape rectShape = new RectangleShape(rect[0], rect[1].X, rect[1].Y);
            //Create Polygons
            PolygonShape polyShape = new PolygonShape(polPoints);

            //Create Complex obj
            IShape[]     shapes    = { rectShape, polyShape };
            ComplexShape compShape = new ComplexShape(shapes);

            Collision.Transform compTranform =
                new Collision.Transform(new Vector2(5.0456f, 8.1234543f), 23.023f * Mathf.Deg2Rad);
            Console.WriteLine(23.023f * Mathf.Deg2Rad);

            // True fraction data
            float[] trueFraction = { 0.6090305f, 0.3909697f, 0.1815236f };

            for (var obj = 0; obj < lines.Length; obj++)
            {
                float   fraction;
                Vector2 normal;
                bool    rayCast = compShape.RayCast(compTranform, lines[obj][0], lines[obj][1], out fraction,
                                                    out normal);
                Assert.That(rayCast, "RayCast to Complex test({0}) failed.", obj);
                Vector2 point = lines[obj][0] + (lines[obj][1] - lines[obj][0]) * fraction;
                string  err   = String.Format(
                    "Intersect RayCast to Complex test({0}) failed; Intersect: trueFraction {1} (testFraction: {2}), intrPoint({3}), testPoint {4}, normal {5}",
                    obj, trueFraction[obj], fraction, intrDot[obj], point, normal);
                Assert.That(Mathf.FloatEquals(fraction, trueFraction[obj], 0.005f), err);
            }
        }