private PrismaticTest()
        {
            Body ground;
            {
                ground = BodyFactory.CreateBody(World);

                PolygonShape shape3 = new PolygonShape(0);
                shape3.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape3);
            }

            PolygonShape shape = new PolygonShape(5);
            shape.SetAsBox(2.0f, 0.5f);

            Body body = BodyFactory.CreateBody(World);
            body.BodyType = BodyType.Dynamic;
            body.Position = new Vector2(0.0f, 10.0f);

            body.CreateFixture(shape);

            _fixedJoint = new FixedPrismaticJoint(body, body.Position, new Vector2(0.5f, 1.0f));
            _fixedJoint.MotorSpeed = 5.0f;
            _fixedJoint.MaxMotorForce = 1000.0f;
            _fixedJoint.MotorEnabled = true;
            _fixedJoint.LowerLimit = -10.0f;
            _fixedJoint.UpperLimit = 20.0f;
            _fixedJoint.LimitEnabled = true;

            World.AddJoint(_fixedJoint);

            PolygonShape shape2 = new PolygonShape(5);
            shape2.SetAsBox(2.0f, 0.5f);

            Body body2 = BodyFactory.CreateBody(World);
            body2.BodyType = BodyType.Dynamic;
            body2.Position = new Vector2(10.0f, 10.0f);

            body2.CreateFixture(shape2);

            _joint = new PrismaticJoint(ground, body2, ground.GetLocalPoint(body2.Position), Vector2.Zero,
                                        new Vector2(0.5f, 1.0f));
            _joint.MotorSpeed = 5.0f;
            _joint.MaxMotorForce = 1000.0f;
            _joint.MotorEnabled = true;
            _joint.LowerLimit = -10.0f;
            _joint.UpperLimit = 20.0f;
            _joint.LimitEnabled = true;

            World.AddJoint(_joint);
        }
        private EdgeShapes()
        {
            // Ground body
            {
                Body ground = BodyFactory.CreateBody(World);

                float x1 = -20.0f;
                float y1 = 2.0f*(float) Math.Cos(x1/10.0f*(float) Math.PI);
                for (int i = 0; i < 80; ++i)
                {
                    float x2 = x1 + 0.5f;
                    float y2 = 2.0f*(float) Math.Cos(x2/10.0f*(float) Math.PI);

                    PolygonShape shape = new PolygonShape(0);
                    shape.SetAsEdge(new Vector2(x1, y1), new Vector2(x2, y2));
                    ground.CreateFixture(shape);

                    x1 = x2;
                    y1 = y2;
                }
            }

            {
                Vertices vertices = new Vertices(3);
                vertices.Add(new Vector2(-0.5f, 0.0f));
                vertices.Add(new Vector2(0.5f, 0.0f));
                vertices.Add(new Vector2(0.0f, 1.5f));
                _polygons[0] = new PolygonShape(20);
                _polygons[0].Set(vertices);
            }

            {
                Vertices vertices = new Vertices(3);
                vertices.Add(new Vector2(-0.1f, 0.0f));
                vertices.Add(new Vector2(0.1f, 0.0f));
                vertices.Add(new Vector2(0.0f, 1.5f));
                _polygons[1] = new PolygonShape(20);
                _polygons[1].Set(vertices);
            }

            {
                const float w = 1.0f;
                float b = w/(2.0f + (float) Math.Sqrt(2.0f));
                float s = (float) Math.Sqrt(2.0f)*b;

                Vertices vertices = new Vertices(8);
                vertices.Add(new Vector2(0.5f*s, 0.0f));
                vertices.Add(new Vector2(0.5f*w, b));
                vertices.Add(new Vector2(0.5f*w, b + s));
                vertices.Add(new Vector2(0.5f*s, w));
                vertices.Add(new Vector2(-0.5f*s, w));
                vertices.Add(new Vector2(-0.5f*w, b + s));
                vertices.Add(new Vector2(-0.5f*w, b));
                vertices.Add(new Vector2(-0.5f*s, 0.0f));
                _polygons[2] = new PolygonShape(20);
                _polygons[2].Set(vertices);
            }

            {
                _polygons[3] = new PolygonShape(20);
                _polygons[3].SetAsBox(0.5f, 0.5f);
            }

            {
                _circle = new CircleShape(0.5f, 1);
            }

            _bodyIndex = 0;
            _angle = 0.0f;
        }
        private BridgeTest()
        {
            Body ground;
            {
                ground = new Body(World);

                PolygonShape shape = new PolygonShape(0);
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape);
            }
            {
                Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f);
                PolygonShape shape = new PolygonShape(box, 20);

                Body prevBody = ground;
                for (int i = 0; i < Count; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(-14.5f + 1.0f*i, 5.0f);

                    Fixture fixture = body.CreateFixture(shape);
                    fixture.Friction = 0.2f;

                    Vector2 anchor = new Vector2(-0.5f, 0.0f);
                    RevoluteJoint jd = new RevoluteJoint(prevBody, body,
                                                         prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor);
                    World.AddJoint(jd);

                    prevBody = body;
                }

                Vector2 anchor2 = new Vector2(0.5f, 0.0f);
                RevoluteJoint jd2 = new RevoluteJoint(ground, prevBody,
                                                      ground.GetLocalPoint(prevBody.GetWorldPoint(anchor2)), anchor2);
                World.AddJoint(jd2);
            }

            Vertices vertices = new Vertices(3);
            vertices.Add(new Vector2(-0.5f, 0.0f));
            vertices.Add(new Vector2(0.5f, 0.0f));
            vertices.Add(new Vector2(0.0f, 1.5f));

            for (int i = 0; i < 2; ++i)
            {
                PolygonShape shape = new PolygonShape(vertices, 1);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(-8.0f + 8.0f*i, 12.0f);

                body.CreateFixture(shape);
            }

            for (int i = 0; i < 3; ++i)
            {
                CircleShape shape = new CircleShape(0.5f, 1);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(-6.0f + 6.0f*i, 10.0f);

                body.CreateFixture(shape);
            }
        }
        private CharacterCollisionTest()
        {
            //Ground body
            Fixture ground = FixtureFactory.CreateEdge(World, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

            // Collinear edges
            PolygonShape shape = new PolygonShape(1);
            shape.SetAsEdge(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f));
            ground.Body.CreateFixture(shape);
            shape.SetAsEdge(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f));
            ground.Body.CreateFixture(shape);
            shape.SetAsEdge(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f));
            ground.Body.CreateFixture(shape);

            // Square tiles
            PolygonShape tile = new PolygonShape(1);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f);
            ground.Body.CreateFixture(tile);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f);
            ground.Body.CreateFixture(tile);
            tile.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f);
            ground.Body.CreateFixture(tile);

            // Square made from an edge loop.
            Vertices vertices = new Vertices(4);
            vertices.Add(new Vector2(-1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 3.0f));
            vertices.Add(new Vector2(1.0f, 5.0f));
            vertices.Add(new Vector2(-1.0f, 5.0f));
            LoopShape loopShape = new LoopShape(vertices, 0);
            ground.Body.CreateFixture(loopShape);

            // Edge loop.
            vertices = new Vertices(10);
            vertices.Add(new Vector2(0.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 0.0f));
            vertices.Add(new Vector2(6.0f, 2.0f));
            vertices.Add(new Vector2(4.0f, 1.0f));
            vertices.Add(new Vector2(2.0f, 2.0f));
            vertices.Add(new Vector2(-2.0f, 2.0f));
            vertices.Add(new Vector2(-4.0f, 3.0f));
            vertices.Add(new Vector2(-6.0f, 2.0f));
            vertices.Add(new Vector2(-6.0f, 0.0f));

            FixtureFactory.CreateLoopShape(World, vertices, 0, new Vector2(-10, 4));

            // Square character
            Fixture squareCharacter = FixtureFactory.CreateRectangle(World, 1, 1, 20);
            squareCharacter.Body.Position = new Vector2(-3.0f, 5.0f);
            squareCharacter.Body.BodyType = BodyType.Dynamic;
            squareCharacter.Body.FixedRotation = true;
            squareCharacter.Body.SleepingAllowed = false;

            squareCharacter.OnCollision += CharacterOnCollision;
            squareCharacter.OnSeparation += CharacterOnSeparation;

            // Square character 2
            Fixture squareCharacter2 = FixtureFactory.CreateRectangle(World, 0.5f, 0.5f, 20);
            squareCharacter2.Body.Position = new Vector2(-5.0f, 5.0f);
            squareCharacter2.Body.BodyType = BodyType.Dynamic;
            squareCharacter2.Body.FixedRotation = true;
            squareCharacter2.Body.SleepingAllowed = false;

            // Hexagon character
            float angle = 0.0f;
            const float delta = Settings.Pi/3.0f;
            vertices = new Vertices(6);

            for (int i = 0; i < 6; ++i)
            {
                vertices.Add(new Vector2(0.5f*(float) Math.Cos(angle), 0.5f*(float) Math.Sin(angle)));
                angle += delta;
            }

            Fixture hexCharacter = FixtureFactory.CreatePolygon(World, vertices, 20);
            hexCharacter.Body.Position = new Vector2(-5.0f, 8.0f);
            hexCharacter.Body.BodyType = BodyType.Dynamic;
            hexCharacter.Body.FixedRotation = true;
            hexCharacter.Body.SleepingAllowed = false;

            // Circle character
            Fixture circleCharacter = FixtureFactory.CreateCircle(World, 0.5f, 20);
            circleCharacter.Body.Position = new Vector2(3.0f, 5.0f);
            circleCharacter.Body.BodyType = BodyType.Dynamic;
            circleCharacter.Body.FixedRotation = true;
            circleCharacter.Body.SleepingAllowed = false;
        }
        private RopeTest()
        {
            Body ground;
            {
                ground = new Body(World);

                PolygonShape shape = new PolygonShape(0);
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape);
            }

            {
                const float y = 15;

                Body prevBody = ground;

                for (int i = 0; i < Count; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(0.5f + 1.0f*i, y);

                    if (i == Count - 1)
                    {
                        Vertices box = PolygonTools.CreateRectangle(1.5f, 1.5f);
                        PolygonShape shape = new PolygonShape(box, 1);
                        shape.Density = 100;
                        Fixture fixture = body.CreateFixture(shape);
                        fixture.Friction = 0.2f;
                        fixture.CollisionFilter.CollisionCategories = Category.Cat2;
                        fixture.CollisionFilter.CollidesWith = Category.All & ~Category.Cat2;
                        body.Position = new Vector2(1.0f*i, y);
                        body.AngularDamping = 0.4f;
                    }
                    else
                    {
                        Vertices box = PolygonTools.CreateRectangle(0.5f, 0.125f);
                        PolygonShape shape = new PolygonShape(box, 1);
                        shape.Density = 20;
                        Fixture fixture = body.CreateFixture(shape);
                        fixture.Friction = 0.2f;
                        fixture.CollisionFilter.CollisionCategories = Category.Cat1;
                        fixture.CollisionFilter.CollidesWith = Category.All & ~Category.Cat2;
                    }

                    Vector2 anchor = new Vector2(i, y);
                    RevoluteJoint jd = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(ref anchor),
                                                         body.GetLocalPoint(ref anchor));
                    jd.CollideConnected = false;

                    World.AddJoint(jd);

                    prevBody = body;
                }

                _rj = new RopeJoint(ground, prevBody, new Vector2(0, y), Vector2.Zero);

                const float extraLength = 0.01f;
                _rj.MaxLength = Count - 1.0f + extraLength;

                World.AddJoint(_rj);
            }
        }
        private TheoJansenTest()
        {
            _offset = new Vector2(0.0f, 8.0f);
            _motorSpeed = 2.0f;
            _motorOn = true;
            Vector2 pivot = new Vector2(0.0f, 0.8f);

            // Ground
            {
                Body ground = BodyFactory.CreateBody(World);

                PolygonShape shape = new PolygonShape(0);
                shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f));
                ground.CreateFixture(shape);

                shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(-50.0f, 10.0f));
                ground.CreateFixture(shape);

                shape.SetAsEdge(new Vector2(50.0f, 0.0f), new Vector2(50.0f, 10.0f));
                ground.CreateFixture(shape);
            }

            // Balls
            for (int i = 0; i < 40; ++i)
            {
                CircleShape shape = new CircleShape(0.25f, 1);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(-40.0f + 2.0f*i, 0.5f);

                body.CreateFixture(shape);
            }

            // Chassis
            {
                PolygonShape shape = new PolygonShape(1);
                shape.SetAsBox(2.5f, 1.0f);

                _chassis = BodyFactory.CreateBody(World);
                _chassis.BodyType = BodyType.Dynamic;
                _chassis.Position = pivot + _offset;

                Fixture fixture = _chassis.CreateFixture(shape);
                fixture.CollisionFilter.CollisionGroup = -1;
            }

            {
                CircleShape shape = new CircleShape(1.6f, 1);

                _wheel = BodyFactory.CreateBody(World);
                _wheel.BodyType = BodyType.Dynamic;
                _wheel.Position = pivot + _offset;

                Fixture fixture = _wheel.CreateFixture(shape);
                fixture.CollisionFilter.CollisionGroup = -1;
            }

            {
                //_motorJoint = new RevoluteJoint(_wheel, _chassis, pivot + _offset);
                _motorJoint = new RevoluteJoint(_wheel, _chassis, _wheel.GetLocalPoint(_chassis.Position), Vector2.Zero);
                _motorJoint.CollideConnected = false;
                _motorJoint.MotorSpeed = _motorSpeed;
                _motorJoint.MaxMotorTorque = 400.0f;
                _motorJoint.MotorEnabled = _motorOn;
                World.AddJoint(_motorJoint);
            }

            Vector2 wheelAnchor = pivot + new Vector2(0.0f, -0.8f);

            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.Position, 120.0f*Settings.Pi/180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.Position, -120.0f*Settings.Pi/180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
        private SliderCrankTest()
        {
            Body ground;
            {
                ground = BodyFactory.CreateBody(World);

                PolygonShape shape = new PolygonShape(0);
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape);
            }

            {
                Body prevBody = ground;

                // Define crank.
                {
                    PolygonShape shape = new PolygonShape(2);
                    shape.SetAsBox(0.5f, 2.0f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(0.0f, 7.0f);

                    body.CreateFixture(shape);

                    Vector2 anchor = new Vector2(0.0f, -2.0f);
                    _joint1 = new RevoluteJoint(prevBody, body, prevBody.GetLocalPoint(body.GetWorldPoint(anchor)),
                                                anchor);
                    _joint1.MotorSpeed = 1.0f*Settings.Pi;
                    _joint1.MaxMotorTorque = 10000.0f;
                    _joint1.MotorEnabled = true;
                    World.AddJoint(_joint1);

                    prevBody = body;
                }

                // Define follower.
                {
                    PolygonShape shape = new PolygonShape(2);
                    shape.SetAsBox(0.5f, 4.0f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(0.0f, 13.0f);

                    body.CreateFixture(shape);

                    Vector2 anchor = new Vector2(0.0f, -4.0f);
                    RevoluteJoint rjd3 = new RevoluteJoint(prevBody, body,
                                                           prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor);
                    rjd3.MotorEnabled = false;
                    World.AddJoint(rjd3);

                    prevBody = body;
                }

                // Define piston
                {
                    PolygonShape shape = new PolygonShape(2);
                    shape.SetAsBox(1.5f, 1.5f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(0.0f, 17.0f);

                    body.CreateFixture(shape);

                    Vector2 anchor = Vector2.Zero;
                    RevoluteJoint rjd2 = new RevoluteJoint(prevBody, body,
                                                           prevBody.GetLocalPoint(body.GetWorldPoint(anchor)), anchor);
                    World.AddJoint(rjd2);

                    _joint2 = new FixedPrismaticJoint(body, new Vector2(0.0f, 17.0f), new Vector2(0.0f, 1.0f));
                    _joint2.MaxMotorForce = 1000.0f;
                    _joint2.MotorEnabled = true;

                    World.AddJoint(_joint2);
                }

                // Create a payload
                {
                    PolygonShape shape = new PolygonShape(2);
                    shape.SetAsBox(1.5f, 1.5f);

                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(0.0f, 23.0f);

                    body.CreateFixture(shape);
                }
            }
        }