Example #1
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            PolyShapesCallback callback = new PolyShapesCallback();

            callback._circle._radius = 2.0f;
            callback._circle._p      = new Vector2(0.0f, 2.1f);
            callback._transform.SetIdentity();
            callback._debugDraw = _debugDraw;

            AABB aabb;

            callback._circle.ComputeAABB(out aabb, ref callback._transform, 0);

            _world.QueryAABB(callback.ReportFixture, ref aabb);

            Color color = new ColorR(0.4, 0.7, 0.8);

            _debugDraw.DrawCircle(callback._circle._p, callback._circle._radius, color);

            _debugDraw.DrawString(50, _textLine, "Press 1-5 to drop stuff");
            _textLine += 15;
            _debugDraw.DrawString(50, _textLine, "Press a to (de)activate some bodies");
            _textLine += 15;
            _debugDraw.DrawString(50, _textLine, "Press d to destroy a body");
            _textLine += 15;
        }
Example #2
0
        public override void Step(Framework.Settings settings)
        {
            Manifold manifold = new Manifold();

            Collision.CollidePolygons(ref manifold, _polygonA, ref _transformA, _polygonB, ref _transformB);

            WorldManifold worldManifold = new WorldManifold(ref manifold, ref _transformA, _polygonA._radius, ref _transformB, _polygonB._radius);

            _debugDraw.DrawString(50, _textLine, "point count = {0:n}", manifold._pointCount);
            _textLine += 15;

            {
                Color color             = new Color(0.9f, 0.9f, 0.9f);
                FixedArray8 <Vector2> v = new FixedArray8 <Vector2>();
                for (int i = 0; i < _polygonA._vertexCount; ++i)
                {
                    v[i] = MathUtils.Multiply(ref _transformA, _polygonA._vertices[i]);
                }
                _debugDraw.DrawPolygon(ref v, _polygonA._vertexCount, color);

                for (int i = 0; i < _polygonB._vertexCount; ++i)
                {
                    v[i] = MathUtils.Multiply(ref _transformB, _polygonB._vertices[i]);
                }
                _debugDraw.DrawPolygon(ref v, _polygonB._vertexCount, color);
            }

            for (int i = 0; i < manifold._pointCount; ++i)
            {
                _debugDraw.DrawPoint(worldManifold._points[i], 0.5f, new Color(0.9f, 0.3f, 0.3f));
            }
        }
Example #3
0
        public override void Step(Framework.Settings settings)
        {
            _debugDraw.DrawString(50, _textLine, "Keys: left = a, brake = s, right = d, toggle motor = m");
            _textLine += 15;

            base.Step(settings);
        }
Example #4
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            // Traverse the contact results. Apply a force on shapes
            // that overlap the sensor.
            for (int i = 0; i < e_count; ++i)
            {
                if (_touching[i] == false)
                {
                    continue;
                }

                Body body   = _bodies[i];
                Body ground = _sensor.GetBody();

                CircleShape circle = (CircleShape)_sensor.GetShape();
                Vector2     center = ground.GetWorldPoint(circle._p);

                Vector2 position = body.GetPosition();

                Vector2 d = center - position;
                if (d.LengthSquared < Alt.Box2D.Settings.b2_epsilon * Alt.Box2D.Settings.b2_epsilon)
                {
                    continue;
                }

                d.Normalize();
                Vector2 F = 100.0f * d;
                body.ApplyForce(F, position);
            }
        }
Example #5
0
 public override void Step(Framework.Settings settings)
 {
     base.Step(settings);
     _debugDraw.DrawString(50, _textLine, "This demonstrates a soft distance joint.");
     _textLine += 15;
     _debugDraw.DrawString(50, _textLine, "Press: (b) to delete a body, (j) to delete a joint");
     _textLine += 15;
 }
Example #6
0
 public override void Step(Framework.Settings settings)
 {
     base.Step(settings);
     _debugDraw.DrawString(50, _textLine, "Keys: (l) limits, (a) left, (s) off, (d) right");
     _textLine += 15;
     //float torque1 = _joint1.GetMotorTorque();
     //_debugDraw.DrawString(50, _textLine, "Motor Torque = %4.0f, %4.0f : Motor Force = %4.0f", (float) torque1, (float) torque2, (float) force3);
     //_textLine += 15;
 }
Example #7
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            float ratio = _joint1.GetRatio();
            float L     = _joint1.GetLength1() + ratio * _joint1.GetLength2();

            _debugDraw.DrawString(50, _textLine, "L1 + {0:n} * L2 = {1:n}", (float)ratio, (float)L);
            _textLine += 15;
        }
Example #8
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);
            _debugDraw.DrawString(50, _textLine, "Keys: (l) limits, (m) motors, (p) speed");
            _textLine += 15;
            double force = _joint.GetMotorForce();

            _debugDraw.DrawString(50, _textLine, "Motor Force = {0:n}", (double)force);
            _textLine += 15;
        }
Example #9
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);
            _debugDraw.DrawString(50, _textLine, "Keys: (f) toggle friction, (m) toggle motor");
            _textLine += 15;
            double torque = _joint1.GetMotorTorque();

            _debugDraw.DrawString(50, _textLine, "Motor Torque = {0:n}", (double)torque);
            _textLine += 15;
        }
Example #10
0
        public override void Step(Framework.Settings settings)
        {
            uint oldFlag = settings.enableContinuous;

            settings.enableContinuous = 0;
            base.Step(settings);
            _debugDraw.DrawString(5, _textLine, "Press 'c' to create a circle.");
            _textLine += 15;

            settings.enableContinuous = oldFlag;
        }
Example #11
0
        public override void Step(Framework.Settings settings)
        {
            ContactManager cm               = _world.GetContactManager();
            int            height           = cm.BroadPhase.ComputeHeight();
            int            leafCount        = cm.BroadPhase.ProxyCount;
            int            minimumNodeCount = 2 * leafCount - 1;
            double         minimumHeight    = (double)(Math.Ceiling(Math.Log((double)minimumNodeCount) / Math.Log(2.0)));

            _debugDraw.DrawString(5, _textLine, string.Format("dynamic tree Height = {0}, min = {1}", height, minimumHeight));
            _textLine += 15;

            base.Step(settings);
        }
Example #12
0
        public override void Step(Framework.Settings settings)
        {
            if (_stepCount == 12)
            {
                _stepCount += 0;
            }

            base.Step(settings);

            if (_stepCount % 60 == 0)
            {
                Launch();
            }
        }
Example #13
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            DistanceInput input = new DistanceInput();

            input.proxyA.Set(_polygonA, 0);
            input.proxyB.Set(_polygonB, 0);
            input.transformA = _transformA;
            input.transformB = _transformB;
            input.useRadii   = true;
            SimplexCache cache = new SimplexCache();

            cache.count = 0;
            DistanceOutput output = new DistanceOutput();

            Distance.ComputeDistance(out output, out cache, ref input);

            _debugDraw.DrawString(50, _textLine, "distance = {0:n}", output.distance);
            _textLine += 15;

            _debugDraw.DrawString(50, _textLine, "iterations = {0:n}", output.iterations);
            _textLine += 15;

            {
                Color color             = new Color(0.9f, 0.9f, 0.9f);
                FixedArray8 <Vector2> v = new FixedArray8 <Vector2>();
                for (int i = 0; i < _polygonA._vertexCount; ++i)
                {
                    v[i] = MathUtils.Multiply(ref _transformA, _polygonA._vertices[i]);
                }
                _debugDraw.DrawPolygon(ref v, _polygonA._vertexCount, color);

                for (int i = 0; i < _polygonB._vertexCount; ++i)
                {
                    v[i] = MathUtils.Multiply(ref _transformB, _polygonB._vertices[i]);
                }
                _debugDraw.DrawPolygon(ref v, _polygonB._vertexCount, color);
            }

            Vector2 x1 = output.pointA;
            Vector2 x2 = output.pointB;


            _debugDraw.DrawPoint(x1, 0.5f, new Color(1.0f, 0.0f, 0.0f));
            _debugDraw.DrawPoint(x2, 0.5f, new Color(1.0f, 0.0f, 0.0f));

            _debugDraw.DrawSegment(x1, x2, new Color(1.0f, 1.0f, 0.0f));
        }
Example #14
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            float ratio = 0.0f;
            float value = 0.0f;

            ratio = _joint4.GetRatio();
            value = _joint1.GetJointAngle() + ratio * _joint2.GetJointAngle();
            _debugDraw.DrawString(50, _textLine, "theta1 + {0:n} * theta2 = {1:n}", (float)ratio, (float)value);
            _textLine += 15;

            ratio = _joint5.GetRatio();
            value = _joint2.GetJointAngle() + ratio * _joint3.GetJointTranslation();
            _debugDraw.DrawString(50, _textLine, "theta2 + {0:n} * delta = {1:n}", (float)ratio, (float)value);
            _textLine += 15;
        }
Example #15
0
        public override void Step(Framework.Settings settings)
        {
            bool sleeping = true;

            for (Body b = _world.GetBodyList(); b != null; b = b.GetNext())
            {
                if (b.GetType() != BodyType.Dynamic)
                {
                    continue;
                }

                if (b.IsAwake())
                {
                    sleeping = false;
                }
            }

            if (_stepCount == 180)
            {
                _stepCount += 0;
            }

            //if (sleeping)
            //{
            //	CreateCircle();
            //}

            base.Step(settings);

            for (Body b = _world.GetBodyList(); b != null; b = b.GetNext())
            {
                if (b.GetType() != BodyType.Dynamic)
                {
                    continue;
                }

                Vector2 p = b.GetPosition();
                if (p.X <= -10.0f || 10.0f <= p.X || p.Y <= 0.0f || 20.0f <= p.Y)
                {
                    p.X += 0.0f;
                }
            }

            _debugDraw.DrawString(5, _textLine, "Press 'c' to create a circle.");
            _textLine += 15;
        }
Example #16
0
        public override void Step(Framework.Settings settings)
        {
            if (_break)
            {
                Break();
                _broke = true;
                _break = false;
            }

            // Cache velocities to improve movement on breakage.
            if (_broke == false)
            {
                _velocity        = _body1.GetLinearVelocity();
                _angularVelocity = _body1.GetAngularVelocity();
            }

            base.Step(settings);
        }
Example #17
0
        public override void Step(Framework.Settings settings)
        {
            bool advanceRay = settings.pause == 0 || settings.singleStep != 0;

            base.Step(settings);
            _debugDraw.DrawString(5, _textLine, "Press 1-5 to drop stuff");
            _textLine += 15;

            double  L      = 25.0;
            Vector2 point1 = new Vector2(0.0f, 10.0f);
            Vector2 d      = new Vector2(L * (double)Math.Cos(_angle), -L * Math.Abs((double)Math.Sin(_angle)));
            Vector2 point2 = point1 + d;

            _world.RayCast((fixture, point, normal, fraction) =>
            {
                _fixture = fixture;
                _point   = point;
                _normal  = normal;

                return(fraction);
            }, point1, point2);

            if (_fixture != null)
            {
                _debugDraw.DrawPoint(_point, .5f, new ColorR(0.4f, 0.9f, 0.4f));

                _debugDraw.DrawSegment(point1, _point, new ColorR(0.8f, 0.8f, 0.8f));

                Vector2 head = _point + 0.5f * _normal;
                _debugDraw.DrawSegment(_point, head, new ColorR(0.9f, 0.9f, 0.4f));
            }
            else
            {
                _debugDraw.DrawSegment(point1, point2, new ColorR(0.8f, 0.8f, 0.8f));
            }

            if (advanceRay)
            {
                _angle += 0.25 * Math.PI / 180.0;
            }
        }
Example #18
0
        public override void Step(Framework.Settings settings)
        {
            // Drive the kinematic body.
            if (_platform.GetType() == BodyType.Kinematic)
            {
                Alt.Box2D.Transform tf;
                _platform.GetTransform(out tf);
                Vector2 p = tf.Position;
                Vector2 v = _platform.GetLinearVelocity();

                if ((p.X < -10.0 && v.X < 0.0) ||
                    (p.X > 10.0 && v.X > 0.0))
                {
                    v.X = -v.X;
                    _platform.SetLinearVelocity(v);
                }
            }

            base.Step(settings);
            _debugDraw.DrawString(5, _textLine, "Keys: (d) dynamic, (s) static, (k) kinematic");
            _textLine += 15;
        }
Example #19
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            _debugDraw.DrawString(50, _textLine, "Press: (,) to launch a bullet.");

            if (_stepCount == 300)
            {
                if (_bullet != null)
                {
                    _world.DestroyBody(_bullet);
                    _bullet = null;
                }

                {
                    CircleShape shape = new CircleShape();
                    shape._radius = 0.25f;

                    FixtureDef fd = new FixtureDef();
                    fd.shape       = shape;
                    fd.density     = 20.0f;
                    fd.restitution = 0.05f;

                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.bullet   = true;
                    bd.position = new Vector2(-31.0, 5.0);

                    _bullet = _world.CreateBody(bd);
                    _bullet.CreateFixture(fd);

                    _bullet.SetLinearVelocity(new Vector2(400.0, 0.0));
                }
            }

            _textLine += 15;
        }
Example #20
0
 public override void Step(Framework.Settings settings)
 {
     base.Step(settings);
     _debugDraw.DrawString(5, _textLine, "Press: (c) create a shape, (d) destroy a shape.");
     _textLine += 15;
 }
Example #21
0
 public override void Step(Framework.Settings settings)
 {
     base.Step(settings);
     _debugDraw.DrawString(5, _textLine, "This tests various character collision shapes");
     _textLine += 15;
 }
Example #22
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            Sweep sweepA = new Sweep();

            sweepA.c0          = new Vector2(24.0f, -60.0f);
            sweepA.a0          = 2.95f;
            sweepA.c           = sweepA.c0;
            sweepA.a           = sweepA.a0;
            sweepA.localCenter = Vector2.Zero;

            Sweep sweepB = new Sweep();

            sweepB.c0          = new Vector2(53.474274f, -50.252514f);
            sweepB.a0          = 513.36676f;
            sweepB.c           = new Vector2(54.595478f, -51.083473f);
            sweepB.a           = 513.62781f;
            sweepB.localCenter = Vector2.Zero;

            TOIInput input = new TOIInput();

            input.proxyA.Set(_shapeA, 0);
            input.proxyB.Set(_shapeB, 0);
            input.sweepA = sweepA;
            input.sweepB = sweepB;
            input.tMax   = 1.0f;

            TOIOutput output;

            Alt.Box2D.TimeOfImpact.CalculateTimeOfImpact(out output, ref input);

            _debugDraw.DrawString(50, _textLine, "toi = {0:n}", output.t);
            _textLine += 15;

            _debugDraw.DrawString(50, _textLine, "max toi iters = {0:n}, max root iters = {1:n}", Alt.Box2D.TimeOfImpact.b2_toiMaxIters, Alt.Box2D.TimeOfImpact.b2_toiMaxRootIters);
            _textLine += 15;

            FixedArray8 <Vector2> vertices = new FixedArray8 <Vector2>();

            Alt.Box2D.Transform transformA;
            sweepA.GetTransform(out transformA, 0.0f);
            for (int i = 0; i < _shapeA._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformA, _shapeA._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeA._vertexCount, new ColorR(0.9, 0.9, 0.9));

            Alt.Box2D.Transform transformB;
            sweepB.GetTransform(out transformB, 0.0f);

            //NoNeed	Vector2 localPoint = new Vector2(2.0f, -0.1f);
            //NoNeed	Vector2 rB = MathUtils.Multiply(ref transformB, localPoint) - sweepB.c0;
            //NoNeed	double wB = sweepB.a - sweepB.a0;
            //NoNeed	Vector2 vB = sweepB.c - sweepB.c0;
            //Vector2 v = vB + MathUtils.Cross(wB, rB);

            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new ColorR(0.5, 0.9, 0.5));

            sweepB.GetTransform(out transformB, output.t);
            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new ColorR(0.5f, 0.7f, 0.9f));

            sweepB.GetTransform(out transformB, 1.0f);
            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new ColorR(0.9, 0.5, 0.5));
        }
Example #23
0
        public override void Step(Framework.Settings settings)
        {
            base.Step(settings);

            Sweep sweepA = new Sweep();

            sweepA.c0          = Vector2.Zero;
            sweepA.a0          = 0.0f;
            sweepA.c           = sweepA.c0;
            sweepA.a           = sweepA.a0;
            sweepA.localCenter = Vector2.Zero;

            Sweep sweepB = new Sweep();

            sweepB.c0          = new Vector2(-0.20382018f, 2.1368704f);
            sweepB.a0          = -3.1664171f;
            sweepB.c           = new Vector2(-0.26699525f, 2.3552670f);
            sweepB.a           = -3.3926492f;
            sweepB.localCenter = Vector2.Zero;

            TOIInput input = new TOIInput();

            input.proxyA.Set(_shapeA);
            input.proxyB.Set(_shapeB);
            input.sweepA = sweepA;
            input.sweepB = sweepB;
            input.tMax   = 1.0f;

            TOIOutput output;

            XNA.TimeOfImpact.CalculateTimeOfImpact(out output, ref input);

            _debugDraw.DrawString(50, _textLine, "toi = {0:n}", output.t);
            _textLine += 15;

            _debugDraw.DrawString(50, _textLine, "max toi iters = {0:n}, max root iters = {1:n}", XNA.TimeOfImpact.b2_toiMaxIters, XNA.TimeOfImpact.b2_toiMaxRootIters);
            _textLine += 15;

            FixedArray8 <Vector2> vertices = new FixedArray8 <Vector2>();

            Transform transformA;

            sweepA.GetTransform(out transformA, 0.0f);
            for (int i = 0; i < _shapeA._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformA, _shapeA._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeA._vertexCount, new Color(0.9f, 0.9f, 0.9f));

            Transform transformB;

            sweepB.GetTransform(out transformB, 0.0f);

            Vector2 localPoint = new Vector2(2.0f, -0.1f);
            Vector2 rB         = MathUtils.Multiply(ref transformB, localPoint) - sweepB.c0;
            float   wB         = sweepB.a - sweepB.a0;
            Vector2 vB         = sweepB.c - sweepB.c0;
            Vector2 v          = vB + MathUtils.Cross(wB, rB);

            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new Color(0.5f, 0.9f, 0.5f));

            sweepB.GetTransform(out transformB, output.t);
            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new Color(0.5f, 0.7f, 0.9f));

            sweepB.GetTransform(out transformB, 1.0f);
            for (int i = 0; i < _shapeB._vertexCount; ++i)
            {
                vertices[i] = MathUtils.Multiply(ref transformB, _shapeB._vertices[i]);
            }
            _debugDraw.DrawPolygon(ref vertices, _shapeB._vertexCount, new Color(0.9f, 0.5f, 0.5f));
        }
Example #24
0
        public override void Step(Framework.Settings settings)
        {
            _rayActor = null;
            for (int i = 0; i < e_actorCount; ++i)
            {
                _actors[i].fraction = 1.0f;
                _actors[i].overlap  = false;
            }

            if (_automated == true)
            {
                int actionCount = Math.Max(1, e_actorCount >> 2);

                for (int i = 0; i < actionCount; ++i)
                {
                    Action();
                }
            }

            Query();
            RayCast();

            for (int i = 0; i < e_actorCount; ++i)
            {
                Actor actor = _actors[i];
                if (actor.proxyId == -1)
                {
                    continue;
                }

                Color ca = new ColorR(0.9, 0.9, 0.9);
                if (actor == _rayActor && actor.overlap)
                {
                    ca = new ColorR(0.9, 0.6, 0.6);
                }
                else if (actor == _rayActor)
                {
                    ca = new ColorR(0.6, 0.9, 0.6);
                }
                else if (actor.overlap)
                {
                    ca = new ColorR(0.6, 0.6, 0.9);
                }

                _debugDraw.DrawAABB(ref actor.aabb, ca);
            }

            Color c = new ColorR(0.7, 0.7, 0.7);

            _debugDraw.DrawAABB(ref _queryAABB, c);

            _debugDraw.DrawSegment(_rayCastInput.p1, _rayCastInput.p2, c);

            Color c1 = new ColorR(0.2, 0.9, 0.2);
            Color c2 = new ColorR(0.9, 0.2, 0.2);

            _debugDraw.DrawPoint(_rayCastInput.p1, 6.0, c1);
            _debugDraw.DrawPoint(_rayCastInput.p2, 6.0, c2);

            if (_rayActor != null)
            {
                Color   cr = new ColorR(0.2, 0.2, 0.9);
                Vector2 p  = _rayCastInput.p1 + _rayActor.fraction * (_rayCastInput.p2 - _rayCastInput.p1);
                _debugDraw.DrawPoint(p, 6.0, cr);
            }

            {
                int height = _tree.ComputeHeight();
                _debugDraw.DrawString(5, _textLine, "dynamic tree Height = %d", height);
                _textLine += 15;
            }

            ++_stepCount;
        }
Example #25
0
        public override void Step(Framework.Settings settings)
        {
            bool advanceRay = settings.pause == 0 || settings.singleStep != 0;

            base.Step(settings);
            _debugDraw.DrawString(5, _textLine, "Press 1-5 to drop stuff, m to change the mode");
            _textLine += 15;
            _debugDraw.DrawString(5, _textLine, string.Format("Mode = {0}", _mode));
            _textLine += 15;

            double  L      = 11.0f;
            Vector2 point1 = new Vector2(0.0f, 10.0f);
            Vector2 d      = new Vector2(L * (double)Math.Cos(_angle), L * (double)Math.Sin(_angle));
            Vector2 point2 = point1 + d;

            Vector2 point = Vector2.Zero, normal = Vector2.Zero;

            switch (_mode)
            {
            case RayCastMode.Closest:
                bool hitClosest = false;
                _world.RayCast((f, p, n, fr) =>
                {
                    Body body = f.GetBody();
                    if (body.GetUserData() != null)
                    {
                        int index = (int)body.GetUserData();
                        if (index == 0)
                        {
                            // filter
                            return(-1.0f);
                        }
                    }

                    hitClosest = true;
                    point      = p;
                    normal     = n;
                    return(fr);
                }, point1, point2);

                if (hitClosest)
                {
                    _debugDraw.DrawPoint(point, .5f, new ColorR(0.4f, 0.9f, 0.4f));

                    _debugDraw.DrawSegment(point1, point, new ColorR(0.8f, 0.8f, 0.8f));

                    Vector2 head = point + 0.5f * normal;
                    _debugDraw.DrawSegment(point, head, new ColorR(0.9f, 0.9f, 0.4f));
                }
                else
                {
                    _debugDraw.DrawSegment(point1, point2, new ColorR(0.8f, 0.8f, 0.8f));
                }

                break;

            case RayCastMode.Any:
                bool hitAny = false;
                _world.RayCast((f, p, n, fr) =>
                {
                    Body body = f.GetBody();
                    if (body.GetUserData() != null)
                    {
                        int index = (int)body.GetUserData();
                        if (index == 0)
                        {
                            // filter
                            return(-1.0f);
                        }
                    }

                    hitAny = true;
                    point  = p;
                    normal = n;
                    return(0);
                }, point1, point2);

                if (hitAny)
                {
                    _debugDraw.DrawPoint(point, .5f, new ColorR(0.4f, 0.9f, 0.4f));

                    _debugDraw.DrawSegment(point1, point, new ColorR(0.8f, 0.8f, 0.8f));

                    Vector2 head = point + 0.5f * normal;
                    _debugDraw.DrawSegment(point, head, new ColorR(0.9f, 0.9f, 0.4f));
                }
                else
                {
                    _debugDraw.DrawSegment(point1, point2, new ColorR(0.8f, 0.8f, 0.8f));
                }
                break;

            case RayCastMode.Multiple:
                var points  = new List <Vector2>();
                var normals = new List <Vector2>();
                _world.RayCast((f, p, n, fr) =>
                {
                    Body body = f.GetBody();
                    if (body.GetUserData() != null)
                    {
                        int index = (int)body.GetUserData();
                        if (index == 0)
                        {
                            // filter
                            return(-1.0f);
                        }
                    }

                    points.Add(p);
                    normals.Add(n);
                    return(1.0f);
                }, point1, point2);

                _debugDraw.DrawSegment(point1, point2, new ColorR(0.8f, 0.8f, 0.8f));

                for (int i = 0; i < points.Count; i++)
                {
                    _debugDraw.DrawPoint(points[i], .5f, new ColorR(0.4f, 0.9f, 0.4f));

                    _debugDraw.DrawSegment(point1, points[i], new ColorR(0.8f, 0.8f, 0.8f));

                    Vector2 head = points[i] + 0.5f * normals[i];
                    _debugDraw.DrawSegment(points[i], head, new ColorR(0.9f, 0.9f, 0.4f));
                }
                break;

            default:
                break;
            }

            if (advanceRay)
            {
                _angle += 0.25f * (double)Math.PI / 180.0f;
            }
        }
Example #26
0
        public override void Step(Framework.Settings settings)
        {
            _rayActor = null;
            for (int i = 0; i < e_actorCount; ++i)
            {
                _actors[i].fraction = 1.0f;
                _actors[i].overlap  = false;
            }

            if (_automated == true)
            {
                int actionCount = Math.Max(1, e_actorCount >> 2);

                for (int i = 0; i < actionCount; ++i)
                {
                    Action();
                }
            }

            Query();
            RayCast();

            for (int i = 0; i < e_actorCount; ++i)
            {
                Actor actor = _actors[i];
                if (actor.proxyId == -1)
                {
                    continue;
                }

                Color ca = new Color(0.9f, 0.9f, 0.9f);
                if (actor == _rayActor && actor.overlap)
                {
                    ca = new Color(0.9f, 0.6f, 0.6f);
                }
                else if (actor == _rayActor)
                {
                    ca = new Color(0.6f, 0.9f, 0.6f);
                }
                else if (actor.overlap)
                {
                    ca = new Color(0.6f, 0.6f, 0.9f);
                }

                _debugDraw.DrawAABB(ref actor.aabb, ca);
            }

            Color c = new Color(0.7f, 0.7f, 0.7f);

            _debugDraw.DrawAABB(ref _queryAABB, c);

            _debugDraw.DrawSegment(_rayCastInput.p1, _rayCastInput.p2, c);

            Color c1 = new Color(0.2f, 0.9f, 0.2f);
            Color c2 = new Color(0.9f, 0.2f, 0.2f);

            _debugDraw.DrawPoint(_rayCastInput.p1, 6.0f, c1);
            _debugDraw.DrawPoint(_rayCastInput.p2, 6.0f, c2);

            if (_rayActor != null)
            {
                Color   cr = new Color(0.2f, 0.2f, 0.9f);
                Vector2 p  = _rayCastInput.p1 + _rayActor.fraction * (_rayCastInput.p2 - _rayCastInput.p1);
                _debugDraw.DrawPoint(p, 6.0f, cr);
            }

            ++_stepCount;
        }
Example #27
0
        public override void Step(Framework.Settings settings)
        {
            stepCount++;
            base.Step(settings);

            // We are going to destroy some bodies according to contact
            // points. We must buffer the bodies that should be destroyed
            // because they may belong to multiple contact points.
            int k_maxNuke = 6;

            Body[] nuke      = new Body[k_maxNuke];
            int    nukeCount = 0;

            // Traverse the contact results. Destroy bodies that
            // are touching heavier bodies.
            for (int i = 0; i < _pointCount; ++i)
            {
                ContactPoint point = _points[i];

                Body  body1 = point.fixtureA.GetBody();
                Body  body2 = point.fixtureB.GetBody();
                float mass1 = body1.GetMass();
                float mass2 = body2.GetMass();

                if (mass1 > 0.0f && mass2 > 0.0f)
                {
                    if (mass2 > mass1)
                    {
                        nuke[nukeCount++] = body1;
                    }
                    else
                    {
                        nuke[nukeCount++] = body2;
                    }

                    if (nukeCount == k_maxNuke)
                    {
                        break;
                    }
                }
            }

            List <Body> dupes = new List <Body>();

            // Destroy the bodies, skipping duplicates.
            int j = 0;

            while (j < nukeCount)
            {
                Body b = nuke[j++];
                while (j < nukeCount && nuke[j] == b)
                {
                    ++j;
                }

                if (b != null && !dupes.Contains(b))
                {
                    _world.DestroyBody(b);
                    dupes.Add(b);
                }
            }
        }