示例#1
0
        public void TestRaycastCircle()
        {
            // lines
            Vector2[][] lines =
            {
                new[] { new Vector2(5.023f,   -5.024f), new Vector2(12.04f,        -9.72f) },
                new[] { new Vector2(12.04f,    -9.72f), new Vector2(5.023f,       -5.024f) },
                new[] { new Vector2(20.0001f, -4.999f), new Vector2(15.100001f, -20.0202f) }
            };
            Vector2[] intrDot =
            {
                new Vector2(10.23f, -8.51f),
                new Vector2(10.23f, -8.51f),
                new Vector2(19.22f, -7.33f)
            };
            // True fraction data
            float[] trueFraction = { 0.7421412f, 0.2578588f, 0.1555718f };

            //Create circle obj
            CircleShape circleShape = new CircleShape(Vector2.Zero, 5.02302f);

            Collision.Transform circleTransform =
                new Collision.Transform(new Vector2(15.05f, -10.06f), 33.34f * Mathf.Deg2Rad);

            for (var obj = 0; obj < lines.Length; obj++)
            {
                float   fraction;
                Vector2 normal;
                bool    rayCast = circleShape.RayCast(circleTransform, lines[obj][0], lines[obj][1], out fraction,
                                                      out normal);
                Vector2 point = lines[obj][0] + (lines[obj][1] - lines[obj][0]) * fraction;

                Assert.That(rayCast, "RayCast to Circle test failed. Result {0} is incorrect", obj);
                string err =
                    String.Format(
                        "RayCast to Circle 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);
            }
        }
示例#2
0
        public void TestCircleRayCast()
        {
            CircleShape circleShape = new CircleShape(new Vector2(-3.0f, 6.0f), 4.15f);

            Collision.Transform circleTransform = new Collision.Transform(new Vector2(-1.05f, 0), 0.0f);

            Vector2 from = new Vector2(0, -20);
            Vector2 to   = new Vector2(0, 6);


            float   fraction;
            Vector2 normal;
            bool    rayCast = circleShape.RayCast(circleTransform, from, to, out fraction, out normal);

            Assert.That(rayCast, "CircleShape.RayCast() result is incorrect");

            Assert.That(Mathf.FloatEquals(fraction, 0.9635197f, 0.005f), "CircleShape.RayCast() fraction is incorrect: " + fraction);

            Vector2 point = from + (to - from) * fraction;

            Console.WriteLine("Connection point is at fraction {0}, point {1}, normal {2}", fraction, point, normal);
        }