示例#1
0
        /// <inheritdoc />
        protected override void PostStep()
        {
            if (Input.GetKeyDown(KeyCode.A))
            {
                _positionB.X -= 0.1f;
            }

            if (Input.GetKeyDown(KeyCode.D))
            {
                _positionB.X += 0.1f;
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                _positionB.Y -= 0.1f;
            }

            if (Input.GetKeyDown(KeyCode.W))
            {
                _positionB.Y += 0.1f;
            }

            if (Input.GetKeyDown(KeyCode.Q))
            {
                _angleB += 0.1f * Settings.Pi;
            }

            if (Input.GetKeyDown(KeyCode.E))
            {
                _angleB -= 0.1f * Settings.Pi;
            }

            _transformB.Set(_positionB, _angleB);

            var input = new DistanceInput();

            input.ProxyA.Set(_polygonA, 0);
            input.ProxyB.Set(_polygonB, 0);
            input.TransformA = _transformA;
            input.TransformB = _transformB;
            input.UseRadii   = true;
            var cache = SimplexCache.Create();

            cache.Count = 0;
            DistanceAlgorithm.Distance(out var output, ref cache, input);

            DrawString($"distance = {output.Distance}");

            DrawString($"iterations = {output.Iterations}");

            {
                var color = Color.FromArgb(230, 230, 230);
                var v     = new Vector2[Settings.MaxPolygonVertices];
                for (var i = 0; i < _polygonA.Count; ++i)
                {
                    v[i] = MathUtils.Mul(_transformA, _polygonA.Vertices[i]);
                }

                Drawer.DrawPolygon(v, _polygonA.Count, color);

                for (var i = 0; i < _polygonB.Count; ++i)
                {
                    v[i] = MathUtils.Mul(_transformB, _polygonB.Vertices[i]);
                }

                Drawer.DrawPolygon(v, _polygonB.Count, color);
            }

            var x1 = output.PointA;
            var x2 = output.PointB;

            var c1 = Color.FromArgb(255, 0, 0);

            Drawer.DrawPoint(x1, 4.0f, c1);

            var c2 = Color.FromArgb(255, 255, 0);

            Drawer.DrawPoint(x2, 4.0f, c2);
        }
示例#2
0
        /// <inheritdoc />
        protected override void PostStep()
        {
            var transformA = new Transform {
                Position = new Vector2(0.0f, 0.25f)
            };

            transformA.Rotation.SetIdentity();

            var transformB = new Transform();

            transformB.SetIdentity();

            var input = new ShapeCastInput();

            input.ProxyA.Set(_vAs, _countA, _radiusA);
            input.ProxyB.Set(_vBs, _countB, _radiusB);
            input.TransformA = transformA;
            input.TransformB = transformB;
            input.TranslationB.Set(8.0f, 0.0f);

            var hit = DistanceAlgorithm.ShapeCast(out var output, input);

            var transformB2 = new Transform
            {
                Rotation = transformB.Rotation, Position = transformB.Position + output.Lambda * input.TranslationB
            };

            var distanceInput = new DistanceInput
            {
                TransformA = transformA,
                TransformB = transformB2,
                UseRadii   = false
            };

            distanceInput.ProxyA.Set(_vAs, _countA, _radiusA);
            distanceInput.ProxyB.Set(_vBs, _countB, _radiusB);
            var simplexCache = SimplexCache.Create();

            DistanceAlgorithm.Distance(out var distanceOutput, ref simplexCache, distanceInput);

            DrawString(
                $"hit = {hit}, iters = {output.Iterations}, lambda = {output.Lambda}, distance = {distanceOutput.Distance}");

            var vertices = new Vector2[Settings.MaxPolygonVertices];

            for (var i = 0; i < _countA; ++i)
            {
                vertices[i] = MathUtils.Mul(transformA, _vAs[i]);
            }

            //g_debugDraw.DrawCircle(vertices[0], _radiusA, b2Color(0.9f, 0.9f, 0.9f));
            Drawer.DrawPolygon(vertices, _countA, Color.FromArgb(230, 230, 230));

            for (var i = 0; i < _countB; ++i)
            {
                vertices[i] = MathUtils.Mul(transformB, _vBs[i]);
            }

            //g_debugDraw.DrawCircle(vertices[0], _radiusB, b2Color(0.5f, 0.9f, 0.5f));
            Drawer.DrawPolygon(vertices, _countB, Color.FromArgb(127, 230, 127));

            for (var i = 0; i < _countB; ++i)
            {
                vertices[i] = MathUtils.Mul(transformB2, _vBs[i]);
            }

            //g_debugDraw.DrawCircle(vertices[0], _radiusB, b2Color(0.5f, 0.7f, 0.9f));
            Drawer.DrawPolygon(vertices, _countB, Color.FromArgb(127, 129, 230));

            if (hit)
            {
                var p1 = output.Point;
                Drawer.DrawPoint(p1, 10.0f, Color.FromArgb(230, 77, 77));
                var p2 = p1 + output.Normal;
                Drawer.DrawSegment(p1, p2, Color.FromArgb(230, 77, 77));
            }
        }