Example #1
0
        public override void Keyboard(KeyboardManager keyboardManager)
        {
            if (keyboardManager.IsNewKeyPress(Keys.A))
            {
                _spring1.MotorSpeed = _speed;
            }
            else if (keyboardManager.IsNewKeyPress(Keys.S))
            {
                _spring1.MotorSpeed = 0.0f;
            }
            else if (keyboardManager.IsNewKeyPress(Keys.D))
            {
                _spring1.MotorSpeed = -_speed;
            }
            else if (keyboardManager.IsNewKeyPress(Keys.Q))
            {
                _hz = Math.Max(0.0f, _hz - 1.0f);

                JointHelper.LinearStiffness(_hz, 0, _spring1.BodyA, _spring1.BodyB, out float stiffness, out _);
                _spring1.Stiffness = stiffness;
                _spring2.Stiffness = stiffness;
            }
            else if (keyboardManager.IsNewKeyPress(Keys.E))
            {
                _hz += 1.0f;
                JointHelper.LinearStiffness(_hz, 0, _spring1.BodyA, _spring1.BodyB, out float stiffness, out _);
                _spring1.Stiffness = stiffness;
                _spring2.Stiffness = stiffness;
            }

            base.Keyboard(keyboardManager);
        }
 void Start()
 {
     lever = hinge.GetComponent <Lever>();
     lever.LeverPercentage = 0;
     audio         = GetComponent <AudioSource>();
     jointHelper   = GetComponent <JointHelper>();
     wrenchEnabled = true;
 }
Example #3
0
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0f, 20f);

            _obstacles = BodyFactory.CreateBody(World);
            FixtureFactory.AttachEdge(new Vector2(-16f, -1f), new Vector2(-14f, 1f), _obstacles);
            FixtureFactory.AttachEdge(new Vector2(-14f, 1f), new Vector2(-12f, -1f), _obstacles);

            FixtureFactory.AttachEdge(new Vector2(14f, -1f), new Vector2(12f, 5f), _obstacles);
            FixtureFactory.AttachEdge(new Vector2(14f, -1f), new Vector2(16f, 5f), _obstacles);

            _angleBody[0]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[0].BodyType = BodyType.Dynamic;
            _angleBody[0].Friction = 0.7f;
            _angleBody[0].Position = new Vector2(-15f, -5f);
            _angleBody[1]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[1].BodyType = BodyType.Dynamic;
            _angleBody[1].Friction = 0.7f;
            _angleBody[1].Position = new Vector2(-18f, 5f);
            _angleBody[2]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _angleBody[2].BodyType = BodyType.Dynamic;
            _angleBody[2].Friction = 0.7f;
            _angleBody[2].Position = new Vector2(-10f, 5f);

            World.AddJoint(new AngleJoint(_angleBody[0], _angleBody[1]));
            World.AddJoint(new AngleJoint(_angleBody[0], _angleBody[2]));

            _distanceBody[0]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[0].BodyType = BodyType.Dynamic;
            _distanceBody[0].Friction = 0.7f;
            _distanceBody[0].Position = new Vector2(11.5f, -4f);
            _distanceBody[1]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[1].BodyType = BodyType.Dynamic;
            _distanceBody[1].Friction = 0.7f;
            _distanceBody[1].Position = new Vector2(16.5f, -4f);
            _distanceBody[2]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[2].BodyType = BodyType.Dynamic;
            _distanceBody[2].Friction = 0.7f;
            _distanceBody[2].Position = new Vector2(11.5f, -6f);
            _distanceBody[3]          = BodyFactory.CreateRectangle(World, 1.5f, 1.5f, 1f);
            _distanceBody[3].BodyType = BodyType.Dynamic;
            _distanceBody[3].Friction = 0.7f;
            _distanceBody[3].Position = new Vector2(16.5f, -6f);

            DistanceJoint softDistance = new DistanceJoint(_distanceBody[0], _distanceBody[1], Vector2.Zero, Vector2.Zero);

            JointHelper.LinearStiffness(5f, 0.3f, softDistance.BodyA, softDistance.BodyB, out var stiffness, out var damping);
            softDistance.Damping   = damping;
            softDistance.Stiffness = stiffness;
            World.AddJoint(softDistance);
            World.AddJoint(new DistanceJoint(_distanceBody[2], _distanceBody[3], Vector2.Zero, Vector2.Zero));

            // create sprites based on bodies
            _angleCube    = new Sprite(Managers.TextureManager.TextureFromShape(_angleBody[0].FixtureList[0].Shape, "Square", Colors.Gold, Colors.Orange, Colors.Grey, 1f));
            _distanceCube = new Sprite(Managers.TextureManager.TextureFromShape(_distanceBody[0].FixtureList[0].Shape, "Stripe", Colors.Red, Colors.Blue, Colors.Grey, 4f));
        }
Example #4
0
 private void FixedUpdate()
 {
     if (fixedJoint && !fixedJoint.connectedBody)
     {
         JointHelper.Disconnect(fixedJoint, true);
         fixedJoint = null;
         Debug.Log("disconnect, connected body lost");
     }
 }
 void Start()
 {
     grabbable       = hinge.GetComponent <Grabbable>();
     wrenchCollider  = GetComponent <Collider>();
     lever           = hinge.GetComponent <Lever>();
     wrenchRigidbody = GetComponent <Rigidbody>();
     hingeRigidbody  = hinge.GetComponent <Rigidbody>();
     jointHelper     = hinge.GetComponent <JointHelper>();
     audio           = GetComponent <AudioSource>();
 }
Example #6
0
        private void OnCollisionEnter(Collision collisionInfo)
        {
            SeaMoth componentInParent = collisionInfo.gameObject.GetComponentInParent <SeaMoth>();

            GrapplingHook component = collisionInfo.gameObject.GetComponent <GrapplingHook>();

            if (staticAttached || (fixedJoint && fixedJoint.connectedBody) || componentInParent != null || component != null)
            {
                return;
            }

            Rigidbody targetRigidbody = GetTargetRigidbody(collisionInfo.gameObject);

            rb.velocity = Vector3.zero;

            if (targetRigidbody != null && JointHelper.ConnectFixed(gameObject, targetRigidbody))
            {
                staticAttached = false;
            }
            else
            {
                staticAttached = true;
                rb.isKinematic = true;
            }

            Utils.PlayFMODAsset(hitSound, transform, 5f);

            Vector3 vector = default(Vector3);

            int num = 0;

            for (int i = 0; i < collisionInfo.contacts.Length; i++)
            {
                ContactPoint contactPoint = collisionInfo.contacts[i];
                if (num == 0)
                {
                    vector = contactPoint.normal;
                }
                else
                {
                    vector += contactPoint.normal;
                }
                num++;
            }
            if (num > 0)
            {
                vector /= num;
                Vector3 eulerAngles = Quaternion.LookRotation(transform.forward, vector).eulerAngles;
                eulerAngles.z        -= 90f;
                transform.eulerAngles = eulerAngles;
            }
            VFXSurface component2 = collisionInfo.gameObject.GetComponent <VFXSurface>();

            VFXSurfaceTypeManager.main.Play(component2, VFXEventTypes.impact, transform.position, transform.rotation, null);
        }
Example #7
0
        public void Release()
        {
            if (fixedJoint)
            {
                JointHelper.Disconnect(fixedJoint, true);
                fixedJoint = null;
            }

            fxControl.StopAndDestroy(0, 1.5f);

            staticAttached = false;
        }
Example #8
0
        private WheelJointTest()
        {
            Body ground;

            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

            _enableLimit = true;
            _enableMotor = false;
            _motorSpeed  = 10.0f;

            {
                CircleShape shape = new CircleShape(5.0f);
                shape.Radius = 2.0f;

                BodyDef bd = new BodyDef();
                bd.Type       = BodyType.Dynamic;
                bd.Position   = new Vector2(0.0f, 10.0f);
                bd.AllowSleep = false;
                Body body = BodyFactory.CreateFromDef(World, bd);
                body.AddFixture(shape);

                WheelJointDef jd = new WheelJointDef();

                // Horizontal
                jd.Initialize(ground, body, bd.Position, new Vector2(0.0f, 1.0f));

                jd.MotorSpeed       = _motorSpeed;
                jd.MaxMotorTorque   = 10000.0f;
                jd.EnableMotor      = _enableMotor;
                jd.LowerTranslation = -3.0f;
                jd.UpperTranslation = 3.0f;
                jd.EnableLimit      = _enableLimit;

                float hertz        = 1.0f;
                float dampingRatio = 0.7f;
                JointHelper.LinearStiffness(hertz, dampingRatio, ground, body, out float stiffness, out float damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;

                _joint = (WheelJoint)JointFactory.CreateFromDef(World, jd);
            }
        }
Example #9
0
        private DistanceJointTest()
        {
            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            Body body = BodyFactory.CreateRectangle(World, 1f, 1f, 5.0f, new Vector2(0, 5), bodyType: BodyType.Dynamic);

            body.SleepingAllowed = false;
            body.AngularDamping  = 0.1f;

            float hertz        = 1.0f;
            float dampingRatio = 0.7f;

            _joint = JointFactory.CreateDistanceJoint(World, ground, body, new Vector2(0, 15), body.Position, true);
            _joint.CollideConnected = true;

            JointHelper.LinearStiffness(hertz, dampingRatio, _joint.BodyA, _joint.BodyB, out float stiffness, out float damping);
            _joint.Stiffness = stiffness;
            _joint.Damping   = damping;
            _joint.MaxLength = _joint.Length;
            _joint.MinLength = _joint.Length;
        }
Example #10
0
        private DistanceJointTest()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

            {
                BodyDef bd = new BodyDef();
                bd.Type           = BodyType.Dynamic;
                bd.AngularDamping = 0.1f;

                bd.Position = new Vector2(0.0f, 5.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);

                PolygonShape shape = new PolygonShape(5.0f);
                shape.SetAsBox(0.5f, 0.5f);
                body.AddFixture(shape);

                _hertz        = 1.0f;
                _dampingRatio = 0.7f;

                DistanceJointDef jd = new DistanceJointDef();
                jd.Initialize(ground, body, new Vector2(0.0f, 15.0f), bd.Position);
                jd.CollideConnected = true;
                _length             = jd.Length;
                _minLength          = _length;
                _maxLength          = _length;
                JointHelper.LinearStiffness(_hertz, _dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;

                _joint = (DistanceJoint)JointFactory.CreateFromDef(World, jd);
            }
        }
Example #11
0
        private void MouseDown(Vector2 p)
        {
            if (_fixedMouseJoint != null)
            {
                return;
            }

            Fixture fixture = World.TestPoint(p);

            if (fixture != null)
            {
                Body body = fixture.Body;
                _fixedMouseJoint          = new FixedMouseJoint(body, p);
                _fixedMouseJoint.MaxForce = 1000.0f * body.Mass;

                JointHelper.LinearStiffness(5.0f, 0.7f, body, null, out float stiffness, out float damping);
                _fixedMouseJoint.Stiffness = stiffness;
                _fixedMouseJoint.Damping   = damping;

                World.AddJoint(_fixedMouseJoint);
                body.Awake = true;
            }
        }
Example #12
0
        private DominosTest()
        {
            Body b1;
            {
                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                b1 = BodyFactory.CreateFromDef(World, bd);
                b1.AddFixture(shape);
            }

            {
                PolygonShape shape = new PolygonShape(0.0f);
                shape.SetAsBox(6.0f, 0.25f);

                BodyDef bd = new BodyDef();
                bd.Position = new Vector2(-1.5f, 10.0f);
                Body ground = BodyFactory.CreateFromDef(World, bd);
                ground.AddFixture(shape);
            }

            {
                PolygonShape shape = new PolygonShape(20.0f);
                shape.SetAsBox(0.1f, 1.0f);

                FixtureDef fd = new FixtureDef();
                fd.Shape    = shape;
                fd.Friction = 0.1f;

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(-6.0f + 1.0f * i, 11.25f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(fd);
                }
            }

            {
                PolygonShape shape = new PolygonShape(0.0f);
                shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f);

                BodyDef bd = new BodyDef();
                bd.Position = new Vector2(1.0f, 6.0f);
                Body ground = BodyFactory.CreateFromDef(World, bd);
                ground.AddFixture(shape);
            }

            Body b2;
            {
                PolygonShape shape = new PolygonShape(0.02f);
                shape.SetAsBox(0.25f, 1.5f);

                BodyDef bd = new BodyDef();
                bd.Position = new Vector2(-7.0f, 4.0f);
                b2          = BodyFactory.CreateFromDef(World, bd);
                b2.AddFixture(shape);
            }

            Body b3;
            {
                PolygonShape shape = new PolygonShape(10.0f);
                shape.SetAsBox(6.0f, 0.125f);

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(-0.9f, 1.0f);
                bd.Angle    = -0.15f;

                b3 = BodyFactory.CreateFromDef(World, bd);
                b3.AddFixture(shape);
            }

            RevoluteJointDef jd = new RevoluteJointDef();
            Vector2          anchor;

            anchor = new Vector2(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.CollideConnected = true;
            JointFactory.CreateFromDef(World, jd);

            Body b4;

            {
                PolygonShape shape = new PolygonShape(10.0f);
                shape.SetAsBox(0.25f, 0.25f);

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(-10.0f, 15.0f);
                b4          = BodyFactory.CreateFromDef(World, bd);
                b4.AddFixture(shape);
            }

            anchor = new Vector2(-7.0f, 15.0f);
            jd.Initialize(b2, b4, anchor);
            JointFactory.CreateFromDef(World, jd);

            Body b5;

            {
                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(6.5f, 3.0f);
                b5          = BodyFactory.CreateFromDef(World, bd);

                PolygonShape shape = new PolygonShape(10.0f);
                FixtureDef   fd    = new FixtureDef();

                fd.Shape    = shape;
                fd.Friction = 0.1f;

                shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f);
                b5.AddFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f);
                b5.AddFixture(fd);

                shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f);
                b5.AddFixture(fd);
            }

            anchor = new Vector2(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            JointFactory.CreateFromDef(World, jd);

            Body b6;

            {
                PolygonShape shape = new PolygonShape(30.0f);
                shape.SetAsBox(1.0f, 0.1f);

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(6.5f, 4.1f);
                b6          = BodyFactory.CreateFromDef(World, bd);
                b6.AddFixture(shape);
            }

            anchor = new Vector2(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            JointFactory.CreateFromDef(World, jd);

            Body b7;
            {
                PolygonShape shape = new PolygonShape(10.0f);
                shape.SetAsBox(0.1f, 1.0f);

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(7.4f, 1.0f);

                b7 = BodyFactory.CreateFromDef(World, bd);
                b7.AddFixture(shape);
            }

            DistanceJointDef djd = new DistanceJointDef();

            djd.BodyA        = b3;
            djd.BodyB        = b7;
            djd.LocalAnchorA = new Vector2(6.0f, 0.0f);
            djd.LocalAnchorB = new Vector2(0.0f, -1.0f);
            Vector2 d = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA);

            djd.Length = d.Length();

            JointHelper.LinearStiffness(1.0f, 1.0f, djd.BodyA, djd.BodyB, out float stiffness, out float damping);
            djd.Stiffness = stiffness;
            djd.Damping   = damping;

            JointFactory.CreateFromDef(World, djd);

            {
                float radius = 0.2f;

                CircleShape shape = new CircleShape(10.0f);
                shape.Radius = radius;

                for (int i = 0; i < 4; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(shape);
                }
            }
        }
Example #13
0
        private WebTest()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

                EdgeShape shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.AddFixture(shape);
            }

            {
                PolygonShape shape = new PolygonShape(5.0f);
                shape.SetAsBox(0.5f, 0.5f);

                BodyDef bd = new BodyDef();
                bd.Type = BodyType.Dynamic;

                bd.Position = new Vector2(-5.0f, 5.0f);
                _bodies[0]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[0].AddFixture(shape);

                bd.Position = new Vector2(5.0f, 5.0f);
                _bodies[1]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[1].AddFixture(shape);

                bd.Position = new Vector2(5.0f, 15.0f);
                _bodies[2]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[2].AddFixture(shape);

                bd.Position = new Vector2(-5.0f, 15.0f);
                _bodies[3]  = BodyFactory.CreateFromDef(World, bd);
                _bodies[3].AddFixture(shape);

                DistanceJointDef jd = new DistanceJointDef();
                Vector2          p1, p2, d;

                float frequencyHz  = 2.0f;
                float dampingRatio = 0.0f;

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[0];
                jd.LocalAnchorA = new Vector2(-10.0f, 0.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[0]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[1];
                jd.LocalAnchorA = new Vector2(10.0f, 0.0f);
                jd.LocalAnchorB = new Vector2(0.5f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[1]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[2];
                jd.LocalAnchorA = new Vector2(10.0f, 20.0f);
                jd.LocalAnchorB = new Vector2(0.5f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[2]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = ground;
                jd.BodyB        = _bodies[3];
                jd.LocalAnchorA = new Vector2(-10.0f, 20.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[3]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[0];
                jd.BodyB        = _bodies[1];
                jd.LocalAnchorA = new Vector2(0.5f, 0.0f);
                jd.LocalAnchorB = new Vector2(-0.5f, 0.0f);

                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[4]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[1];
                jd.BodyB        = _bodies[2];
                jd.LocalAnchorA = new Vector2(0.0f, 0.5f);
                jd.LocalAnchorB = new Vector2(0.0f, -0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[5]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[2];
                jd.BodyB        = _bodies[3];
                jd.LocalAnchorA = new Vector2(-0.5f, 0.0f);
                jd.LocalAnchorB = new Vector2(0.5f, 0.0f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[6]   = JointFactory.CreateFromDef(World, jd);

                jd.BodyA        = _bodies[3];
                jd.BodyB        = _bodies[0];
                jd.LocalAnchorA = new Vector2(0.0f, -0.5f);
                jd.LocalAnchorB = new Vector2(0.0f, 0.5f);
                p1        = jd.BodyA.GetWorldPoint(jd.LocalAnchorA);
                p2        = jd.BodyB.GetWorldPoint(jd.LocalAnchorB);
                d         = p2 - p1;
                jd.Length = d.Length();
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                _joints[7]   = JointFactory.CreateFromDef(World, jd);
            }
        }
Example #14
0
        public override void LoadContent()
        {
            base.LoadContent();

            World.Gravity = new Vector2(0f, 10f);

            HasCursor           = false;
            EnableCameraControl = true;

            _hzFront  = 8.5f;
            _hzBack   = 5.0f;
            _zeta     = 0.85f;
            _maxSpeed = 50.0f;

            // terrain
            _ground = BodyFactory.CreateBody(World);
            {
                Vertices terrain = new Vertices();
                terrain.Add(new Vector2(-20f, -5f));
                terrain.Add(new Vector2(-20f, 0f));
                terrain.Add(new Vector2(20f, 0f));
                terrain.Add(new Vector2(25f, -0.25f));
                terrain.Add(new Vector2(30f, -1f));
                terrain.Add(new Vector2(35f, -4f));
                terrain.Add(new Vector2(40f, 0f));
                terrain.Add(new Vector2(45f, 0f));
                terrain.Add(new Vector2(50f, 1f));
                terrain.Add(new Vector2(55f, 2f));
                terrain.Add(new Vector2(60f, 2f));
                terrain.Add(new Vector2(65f, 1.25f));
                terrain.Add(new Vector2(70f, 0f));
                terrain.Add(new Vector2(75f, -0.3f));
                terrain.Add(new Vector2(80f, -1.5f));
                terrain.Add(new Vector2(85f, -3.5f));
                terrain.Add(new Vector2(90f, 0f));
                terrain.Add(new Vector2(95f, 0.5f));
                terrain.Add(new Vector2(100f, 1f));
                terrain.Add(new Vector2(105f, 2f));
                terrain.Add(new Vector2(110f, 2.5f));
                terrain.Add(new Vector2(115f, 1.3f));
                terrain.Add(new Vector2(120f, 0f));
                terrain.Add(new Vector2(160f, 0f));
                terrain.Add(new Vector2(159f, 10f));
                terrain.Add(new Vector2(201f, 10f));
                terrain.Add(new Vector2(200f, 0f));
                terrain.Add(new Vector2(240f, 0f));
                terrain.Add(new Vector2(250f, -5f));
                terrain.Add(new Vector2(250f, 10f));
                terrain.Add(new Vector2(270f, 10f));
                terrain.Add(new Vector2(270f, 0));
                terrain.Add(new Vector2(310f, 0));
                terrain.Add(new Vector2(310f, -5));

                for (int i = 0; i < terrain.Count - 1; ++i)
                {
                    FixtureFactory.AttachEdge(terrain[i], terrain[i + 1], _ground);
                }

                _ground.Friction = 0.6f;
            }

            // teeter board
            {
                _board          = BodyFactory.CreateBody(World);
                _board.BodyType = BodyType.Dynamic;
                _board.Position = new Vector2(140.0f, -1.0f);

                PolygonShape box = new PolygonShape(1f);
                box.Vertices = PolygonUtils.CreateRectangle(10.0f, 0.25f);
                _teeter      = new Sprite(Managers.TextureManager.TextureFromShape(box, "Stripe", Colors.Gold, Colors.Black, Colors.Black, 1f));

                _board.AddFixture(box);

                RevoluteJoint teeterAxis = JointFactory.CreateRevoluteJoint(World, _ground, _board, Vector2.Zero);
                teeterAxis.LowerLimit   = -8.0f * MathConstants.Pi / 180.0f;
                teeterAxis.UpperLimit   = 8.0f * MathConstants.Pi / 180.0f;
                teeterAxis.LimitEnabled = true;

                _board.ApplyAngularImpulse(-100.0f);
            }

            // bridge
            {
                _bridgeSegments = new List <Body>();

                const int    segmentCount = 20;
                PolygonShape shape        = new PolygonShape(1f);
                shape.Vertices = PolygonUtils.CreateRectangle(1.0f, 0.125f);

                _bridge = new Sprite(Managers.TextureManager.TextureFromShape(shape, Colors.Gold, Colors.Black));

                Body prevBody = _ground;
                for (int i = 0; i < segmentCount; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(161f + 2f * i, 0.125f);
                    Fixture fix = body.AddFixture(shape);
                    fix.Friction = 0.6f;
                    JointFactory.CreateRevoluteJoint(World, prevBody, body, -Vector2.UnitX);

                    prevBody = body;
                    _bridgeSegments.Add(body);
                }

                JointFactory.CreateRevoluteJoint(World, _ground, prevBody, Vector2.UnitX);
            }

            // boxes
            {
                _boxes = new List <Body>();
                PolygonShape box = new PolygonShape(1f);
                box.Vertices = PolygonUtils.CreateRectangle(0.5f, 0.5f);
                _box         = new Sprite(Managers.TextureManager.TextureFromShape(box, "Square", Colors.Sky, Colors.Sunset, Colors.Black, 1f));

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(220f, -0.5f);
                body.AddFixture(box);
                _boxes.Add(body);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(220f, -1.5f);
                body.AddFixture(box);
                _boxes.Add(body);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(220f, -2.5f);
                body.AddFixture(box);
                _boxes.Add(body);
            }

            // car
            {
                Vertices vertices = new Vertices(8);
                vertices.Add(new Vector2(-2.5f, 0.08f));
                vertices.Add(new Vector2(-2.375f, -0.46f));
                vertices.Add(new Vector2(-0.58f, -0.92f));
                vertices.Add(new Vector2(0.46f, -0.92f));
                vertices.Add(new Vector2(2.5f, -0.17f));
                vertices.Add(new Vector2(2.5f, 0.205f));
                vertices.Add(new Vector2(2.3f, 0.33f));
                vertices.Add(new Vector2(-2.25f, 0.35f));

                PolygonShape chassis = new PolygonShape(vertices, 2f);

                _car          = BodyFactory.CreateBody(World);
                _car.BodyType = BodyType.Dynamic;
                _car.Position = new Vector2(0.0f, -1.0f);
                _car.AddFixture(chassis);

                _wheelBack          = BodyFactory.CreateBody(World);
                _wheelBack.BodyType = BodyType.Dynamic;
                _wheelBack.Position = new Vector2(-1.709f, -0.78f);
                Fixture fix = _wheelBack.AddFixture(new CircleShape(0.5f, 0.8f));
                fix.Friction = 0.9f;

                _wheelFront          = BodyFactory.CreateBody(World);
                _wheelFront.BodyType = BodyType.Dynamic;
                _wheelFront.Position = new Vector2(1.54f, -0.8f);
                _wheelFront.AddFixture(new CircleShape(0.5f, 1f));

                Vector2 axis = new Vector2(0.0f, -1.2f);
                _springBack                = new WheelJoint(_car, _wheelBack, _wheelBack.Position, axis, true);
                _springBack.MotorSpeed     = 0.0f;
                _springBack.MaxMotorTorque = 20.0f;
                _springBack.MotorEnabled   = true;

                JointHelper.LinearStiffness(_hzBack, _zeta, _springBack.BodyA, _springBack.BodyB, out float stiffness, out float damping);
                _springBack.Stiffness = stiffness;
                _springBack.Damping   = damping;
                World.AddJoint(_springBack);

                _springFront                = new WheelJoint(_car, _wheelFront, _wheelFront.Position, axis, true);
                _springFront.MotorSpeed     = 0.0f;
                _springFront.MaxMotorTorque = 10.0f;
                _springFront.MotorEnabled   = false;

                JointHelper.LinearStiffness(_hzFront, _zeta, _springFront.BodyA, _springFront.BodyB, out stiffness, out damping);
                _springFront.Stiffness = stiffness;
                _springFront.Damping   = damping;
                World.AddJoint(_springFront);

                // GFX
                _carBody = new Sprite(Managers.TextureManager.GetTexture("Car"), Managers.TextureManager.CalculateOrigin(_car));
                _wheel   = new Sprite(Managers.TextureManager.GetTexture("Wheel"));
            }

            Camera.MinRotation = -0.05f;
            Camera.MaxRotation = 0.05f;

            Camera.TrackingBody   = _car;
            Camera.EnableTracking = true;
        }
Example #15
0
        private WebTest()
        {
            World.JointRemoved += JointRemovedFired;
            World.BodyRemoved  += BodyRemovedFired;

            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            {
                _bodies[0] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 5.0f), bodyType: BodyType.Dynamic);
                _bodies[1] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 5.0f), bodyType: BodyType.Dynamic);
                _bodies[2] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(5.0f, 15.0f), bodyType: BodyType.Dynamic);
                _bodies[3] = BodyFactory.CreateRectangle(World, 1f, 1f, 5, new Vector2(-5.0f, 15.0f), bodyType: BodyType.Dynamic);

                Vector2 p1, p2, d;

                float frequencyHz  = 2.0f;
                float dampingRatio = 0.0f;

                DistanceJoint dj = new DistanceJoint(ground, _bodies[0], new Vector2(-10.0f, 0.0f), new Vector2(-0.5f, -0.5f));

                p1        = dj.BodyA.GetWorldPoint(dj.LocalAnchorA);
                p2        = dj.BodyB.GetWorldPoint(dj.LocalAnchorB);
                d         = p2 - p1;
                dj.Length = d.Length();

                //Velcro: We only calculate this once as the mass is identical for all bodies
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, dj.BodyA, dj.BodyB, out float stiffness, out float damping);
                dj.Stiffness = stiffness;
                dj.Damping   = damping;
                _joints[0]   = dj;
                World.AddJoint(_joints[0]);

                DistanceJoint dj1 = new DistanceJoint(ground, _bodies[1], new Vector2(10.0f, 0.0f), new Vector2(0.5f, -0.5f));
                dj1.Stiffness = stiffness;
                dj1.Damping   = damping;
                _joints[1]    = dj1;
                World.AddJoint(_joints[1]);

                DistanceJoint dj2 = new DistanceJoint(ground, _bodies[2], new Vector2(10.0f, 20.0f), new Vector2(0.5f, 0.5f));
                dj2.Stiffness = stiffness;
                dj2.Damping   = damping;
                _joints[2]    = dj2;
                World.AddJoint(_joints[2]);

                DistanceJoint dj3 = new DistanceJoint(ground, _bodies[3], new Vector2(-10.0f, 20.0f), new Vector2(-0.5f, 0.5f));
                dj3.Stiffness = stiffness;
                dj3.Damping   = damping;
                _joints[3]    = dj3;
                World.AddJoint(_joints[3]);

                DistanceJoint dj4 = new DistanceJoint(_bodies[0], _bodies[1], new Vector2(0.5f, 0.0f), new Vector2(-0.5f, 0.0f));
                dj4.Stiffness = stiffness;
                dj4.Damping   = damping;
                _joints[4]    = dj4;
                World.AddJoint(_joints[4]);

                DistanceJoint dj5 = new DistanceJoint(_bodies[1], _bodies[2], new Vector2(0.0f, 0.5f), new Vector2(0.0f, -0.5f));
                dj5.Stiffness = stiffness;
                dj5.Damping   = damping;
                _joints[5]    = dj5;
                World.AddJoint(_joints[5]);

                DistanceJoint dj6 = new DistanceJoint(_bodies[2], _bodies[3], new Vector2(-0.5f, 0.0f), new Vector2(0.5f, 0.0f));
                dj6.Stiffness = stiffness;
                dj6.Damping   = damping;
                _joints[6]    = dj6;
                World.AddJoint(_joints[6]);

                DistanceJoint dj7 = new DistanceJoint(_bodies[3], _bodies[0], new Vector2(0.0f, -0.5f), new Vector2(0.0f, 0.5f));
                dj7.Stiffness = stiffness;
                dj7.Damping   = damping;
                _joints[7]    = dj7;
                World.AddJoint(_joints[7]);
            }
        }
Example #16
0
        private void CreateLeg(float s, Vector2 wheelAnchor)
        {
            Vector2 p1 = new Vector2(5.4f * s, -6.1f);
            Vector2 p2 = new Vector2(7.2f * s, -1.2f);
            Vector2 p3 = new Vector2(4.3f * s, -1.9f);
            Vector2 p4 = new Vector2(3.1f * s, 0.8f);
            Vector2 p5 = new Vector2(6.0f * s, 1.5f);
            Vector2 p6 = new Vector2(2.5f * s, 3.7f);

            PolygonShape poly1 = new PolygonShape(1);
            PolygonShape poly2 = new PolygonShape(1);

            Vertices vertices = new Vertices(3);

            if (s > 0.0f)
            {
                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);
                poly1.Vertices = vertices;

                vertices[0]    = Vector2.Zero;
                vertices[1]    = p5 - p4;
                vertices[2]    = p6 - p4;
                poly2.Vertices = vertices;
            }
            else
            {
                vertices.Add(p1);
                vertices.Add(p3);
                vertices.Add(p2);
                poly1.Vertices = vertices;

                vertices[0]    = Vector2.Zero;
                vertices[1]    = p6 - p4;
                vertices[2]    = p5 - p4;
                poly2.Vertices = vertices;
            }

            Body body1 = BodyFactory.CreateBody(World);

            body1.BodyType       = BodyType.Dynamic;
            body1.Position       = _offset;
            body1.AngularDamping = 10.0f;

            Body body2 = BodyFactory.CreateBody(World);

            body2.BodyType       = BodyType.Dynamic;
            body2.Position       = p4 + _offset;
            body2.AngularDamping = 10.0f;

            Fixture f1 = body1.CreateFixture(poly1);

            f1.CollisionGroup = -1;

            Fixture f2 = body2.CreateFixture(poly2);

            f2.CollisionGroup = -1;

            // Using a soft distance constraint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            float dampingRatio = 0.5f;
            float frequencyHz  = 10.0f;

            DistanceJoint djd = new DistanceJoint(body1, body2, p2 + _offset, p5 + _offset, true);

            JointHelper.LinearStiffness(frequencyHz, dampingRatio, djd.BodyA, djd.BodyB, out var stiffness, out var damping);
            djd.Damping   = damping;
            djd.Stiffness = stiffness;
            World.AddJoint(djd);

            DistanceJoint djd2 = new DistanceJoint(body1, body2, p3 + _offset, p4 + _offset, true);

            djd2.Damping   = damping;
            djd2.Stiffness = stiffness;
            World.AddJoint(djd2);

            DistanceJoint djd3 = new DistanceJoint(body1, _wheel, p3 + _offset, wheelAnchor + _offset, true);

            djd3.Damping   = damping;
            djd3.Stiffness = stiffness;
            World.AddJoint(djd3);

            DistanceJoint djd4 = new DistanceJoint(body2, _wheel, p6 + _offset, wheelAnchor + _offset, true);

            djd4.Damping   = damping;
            djd4.Stiffness = stiffness;
            World.AddJoint(djd4);

            RevoluteJoint rjd = new RevoluteJoint(body2, _chassis, p4 + _offset, true);

            World.AddJoint(rjd);
        }
Example #17
0
 private void Start()
 {
     _lidJointHelper = mainLid.GetComponent <JointHelper>();
     _taskLogic      = GetComponent <TaskLogic>();
     _particle       = GetComponentInChildren <ParticleSystem>();
 }
        private void CreateLeg(float s, Vector2 wheelAnchor)
        {
            Vector2 p1 = new Vector2(5.4f * s, -6.1f);
            Vector2 p2 = new Vector2(7.2f * s, -1.2f);
            Vector2 p3 = new Vector2(4.3f * s, -1.9f);
            Vector2 p4 = new Vector2(3.1f * s, 0.8f);
            Vector2 p5 = new Vector2(6.0f * s, 1.5f);
            Vector2 p6 = new Vector2(2.5f * s, 3.7f);

            FixtureDef fd1 = new FixtureDef(), fd2 = new FixtureDef();

            fd1.Filter.Group = -1;
            fd2.Filter.Group = -1;

            PolygonShape poly1 = new PolygonShape(1.0f), poly2 = new PolygonShape(1.0f);

            if (s > 0.0f)
            {
                Vertices vertices = new Vertices(3);

                vertices.Add(p1);
                vertices.Add(p2);
                vertices.Add(p3);
                poly1.Vertices = vertices;

                vertices[0]    = Vector2.Zero;
                vertices[1]    = p5 - p4;
                vertices[2]    = p6 - p4;
                poly2.Vertices = vertices;
            }
            else
            {
                Vertices vertices = new Vertices(3);

                vertices.Add(p1);
                vertices.Add(p3);
                vertices.Add(p2);
                poly1.Vertices = vertices;

                vertices[0]    = Vector2.Zero;
                vertices[1]    = p6 - p4;
                vertices[2]    = p5 - p4;
                poly2.Vertices = vertices;
            }

            fd1.Shape = poly1;
            fd2.Shape = poly2;

            BodyDef bd1 = new BodyDef(), bd2 = new BodyDef();

            bd1.Type     = BodyType.Dynamic;
            bd2.Type     = BodyType.Dynamic;
            bd1.Position = _offset;
            bd2.Position = p4 + _offset;

            bd1.AngularDamping = 10.0f;
            bd2.AngularDamping = 10.0f;

            Body body1 = BodyFactory.CreateFromDef(World, bd1);
            Body body2 = BodyFactory.CreateFromDef(World, bd2);

            body1.AddFixture(fd1);
            body2.AddFixture(fd2);

            {
                DistanceJointDef jd = new DistanceJointDef();

                // Using a soft distance constraint can reduce some jitter.
                // It also makes the structure seem a bit more fluid by
                // acting like a suspension system.
                float dampingRatio = 0.5f;
                float frequencyHz  = 10.0f;

                jd.Initialize(body1, body2, p2 + _offset, p5 + _offset);
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                JointFactory.CreateFromDef(World, jd);

                jd.Initialize(body1, body2, p3 + _offset, p4 + _offset);
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                JointFactory.CreateFromDef(World, jd);

                jd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset);
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                JointFactory.CreateFromDef(World, jd);

                jd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset);
                JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping);
                jd.Stiffness = stiffness;
                jd.Damping   = damping;
                JointFactory.CreateFromDef(World, jd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(body2, _chassis, p4 + _offset);
                JointFactory.CreateFromDef(World, jd);
            }
        }
Example #19
0
        public Ragdoll(World world, Vector2 position)
        {
            // Physics
            // Head
            _head                = BodyFactory.CreateCircle(world, 0.75f, 10f);
            _head.BodyType       = BodyType.Dynamic;
            _head.AngularDamping = LimbAngularDamping;
            _head.Mass           = 2f;
            _head.Position       = position;

            // Torso
            Body          = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            Body.BodyType = BodyType.Dynamic;
            Body.Mass     = 1f;
            Body.SetTransform(position + new Vector2(0f, 1.75f), MathHelper.Pi / 2f);

            _middleBody          = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            _middleBody.BodyType = BodyType.Dynamic;
            _middleBody.Mass     = 1f;
            _middleBody.SetTransform(position + new Vector2(0f, 3f), MathHelper.Pi / 2f);

            _lowerBody          = BodyFactory.CreateCapsule(world, 0.5f, 0.75f, LegDensity);
            _lowerBody.BodyType = BodyType.Dynamic;
            _lowerBody.Mass     = 1f;
            _lowerBody.SetTransform(position + new Vector2(0f, 4.25f), MathHelper.Pi / 2f);

            // Left Arm
            _lowerLeftArm                = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _lowerLeftArm.BodyType       = BodyType.Dynamic;
            _lowerLeftArm.AngularDamping = LimbAngularDamping;
            _lowerLeftArm.Mass           = 2f;
            _lowerLeftArm.Rotation       = 1.4f;
            _lowerLeftArm.Position       = position + new Vector2(-4f, 2.2f);

            _upperLeftArm                = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _upperLeftArm.BodyType       = BodyType.Dynamic;
            _upperLeftArm.AngularDamping = LimbAngularDamping;
            _upperLeftArm.Mass           = 2f;
            _upperLeftArm.Rotation       = 1.4f;
            _upperLeftArm.Position       = position + new Vector2(-2f, 1.8f);

            // Right Arm
            _lowerRightArm                = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _lowerRightArm.BodyType       = BodyType.Dynamic;
            _lowerRightArm.AngularDamping = LimbAngularDamping;
            _lowerRightArm.Mass           = 2f;
            _lowerRightArm.Rotation       = -1.4f;
            _lowerRightArm.Position       = position + new Vector2(4f, 2.2f);

            _upperRightArm                = BodyFactory.CreateCapsule(world, 1f, 0.45f, ArmDensity);
            _upperRightArm.BodyType       = BodyType.Dynamic;
            _upperRightArm.AngularDamping = LimbAngularDamping;
            _upperRightArm.Mass           = 2f;
            _upperRightArm.Rotation       = -1.4f;
            _upperRightArm.Position       = position + new Vector2(2f, 1.8f);

            // Left Leg
            _lowerLeftLeg                = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _lowerLeftLeg.BodyType       = BodyType.Dynamic;
            _lowerLeftLeg.AngularDamping = LimbAngularDamping;
            _lowerLeftLeg.Mass           = 2f;
            _lowerLeftLeg.Position       = position + new Vector2(-0.6f, 8f);

            _upperLeftLeg                = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _upperLeftLeg.BodyType       = BodyType.Dynamic;
            _upperLeftLeg.AngularDamping = LimbAngularDamping;
            _upperLeftLeg.Mass           = 2f;
            _upperLeftLeg.Position       = position + new Vector2(-0.6f, 6f);

            // Right Leg
            _lowerRightLeg                = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _lowerRightLeg.BodyType       = BodyType.Dynamic;
            _lowerRightLeg.AngularDamping = LimbAngularDamping;
            _lowerRightLeg.Mass           = 2f;
            _lowerRightLeg.Position       = position + new Vector2(0.6f, 8f);

            _upperRightLeg                = BodyFactory.CreateCapsule(world, 1f, 0.5f, LegDensity);
            _upperRightLeg.BodyType       = BodyType.Dynamic;
            _upperRightLeg.AngularDamping = LimbAngularDamping;
            _upperRightLeg.Mass           = 2f;
            _upperRightLeg.Position       = position + new Vector2(0.6f, 6f);


            // head -> upper body
            DistanceJoint jointHeadBody = new DistanceJoint(_head, Body, new Vector2(0f, 1f), new Vector2(-0.75f, 0f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointHeadBody.BodyA, jointHeadBody.BodyB, out var stiffness, out var damping);
            jointHeadBody.CollideConnected = true;
            jointHeadBody.Damping          = damping;
            jointHeadBody.Stiffness        = stiffness;
            jointHeadBody.Length           = 0.025f;
            world.AddJoint(jointHeadBody);

            // lowerLeftArm -> upperLeftArm
            DistanceJoint jointLeftArm = new DistanceJoint(_lowerLeftArm, _upperLeftArm, new Vector2(0f, -1f), new Vector2(0f, 1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointLeftArm.BodyA, jointLeftArm.BodyB, out stiffness, out damping);
            jointLeftArm.CollideConnected = true;
            jointLeftArm.Damping          = damping;
            jointLeftArm.Stiffness        = stiffness;
            jointLeftArm.Length           = 0.02f;
            world.AddJoint(jointLeftArm);

            // upperLeftArm -> upper body
            DistanceJoint jointLeftArmBody = new DistanceJoint(_upperLeftArm, Body, new Vector2(0f, -1f), new Vector2(-0.15f, 1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointLeftArmBody.BodyA, jointLeftArmBody.BodyB, out stiffness, out damping);
            jointLeftArmBody.Damping   = damping;
            jointLeftArmBody.Stiffness = stiffness;
            jointLeftArmBody.Length    = 0.02f;
            world.AddJoint(jointLeftArmBody);

            // lowerRightArm -> upperRightArm
            DistanceJoint jointRightArm = new DistanceJoint(_lowerRightArm, _upperRightArm, new Vector2(0f, -1f), new Vector2(0f, 1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointRightArm.BodyA, jointRightArm.BodyB, out stiffness, out damping);
            jointRightArm.CollideConnected = true;
            jointRightArm.Damping          = damping;
            jointRightArm.Stiffness        = stiffness;
            jointRightArm.Length           = 0.02f;
            world.AddJoint(jointRightArm);

            // upperRightArm -> upper body
            DistanceJoint jointRightArmBody = new DistanceJoint(_upperRightArm, Body, new Vector2(0f, -1f), new Vector2(-0.15f, -1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointRightArmBody.BodyA, jointRightArmBody.BodyB, out stiffness, out damping);
            jointRightArmBody.Damping   = damping;
            jointRightArmBody.Stiffness = stiffness;
            jointRightArmBody.Length    = 0.02f;
            world.AddJoint(jointRightArmBody);

            // lowerLeftLeg -> upperLeftLeg
            DistanceJoint jointLeftLeg = new DistanceJoint(_lowerLeftLeg, _upperLeftLeg, new Vector2(0f, -1.1f), new Vector2(0f, 1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointLeftLeg.BodyA, jointLeftLeg.BodyB, out stiffness, out damping);
            jointLeftLeg.CollideConnected = true;
            jointLeftLeg.Damping          = damping;
            jointLeftLeg.Stiffness        = stiffness;
            jointLeftLeg.Length           = 0.05f;
            world.AddJoint(jointLeftLeg);

            // upperLeftLeg -> lower body
            DistanceJoint jointLeftLegBody = new DistanceJoint(_upperLeftLeg, _lowerBody, new Vector2(0f, -1.1f), new Vector2(0.7f, 0.8f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointLeftLegBody.BodyA, jointLeftLegBody.BodyB, out stiffness, out damping);
            jointLeftLegBody.CollideConnected = true;
            jointLeftLegBody.Damping          = damping;
            jointLeftLegBody.Stiffness        = stiffness;
            jointLeftLegBody.Length           = 0.02f;
            world.AddJoint(jointLeftLegBody);

            // lowerRightleg -> upperRightleg
            DistanceJoint jointRightLeg = new DistanceJoint(_lowerRightLeg, _upperRightLeg, new Vector2(0f, -1.1f), new Vector2(0f, 1f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointRightLeg.BodyA, jointRightLeg.BodyB, out stiffness, out damping);
            jointRightLeg.CollideConnected = true;
            jointRightLeg.Damping          = damping;
            jointRightLeg.Stiffness        = stiffness;
            jointRightLeg.Length           = 0.05f;
            world.AddJoint(jointRightLeg);

            // upperRightleg -> lower body
            DistanceJoint jointRightLegBody = new DistanceJoint(_upperRightLeg, _lowerBody, new Vector2(0f, -1.1f), new Vector2(0.7f, -0.8f));

            JointHelper.LinearStiffness(Frequency, DampingRatio, jointRightLegBody.BodyA, jointRightLegBody.BodyB, out stiffness, out damping);
            jointRightLegBody.CollideConnected = true;
            jointRightLegBody.Damping          = damping;
            jointRightLegBody.Stiffness        = stiffness;
            jointRightLegBody.Length           = 0.02f;
            world.AddJoint(jointRightLegBody);

            // upper body -> middle body
            RevoluteJoint jointUpperTorso = new RevoluteJoint(Body, _middleBody, Body.Position + new Vector2(0f, 0.625f), true);

            jointUpperTorso.LimitEnabled = true;
            jointUpperTorso.SetLimits(-MathHelper.Pi / 16f, MathHelper.Pi / 16f);
            world.AddJoint(jointUpperTorso);

            // middle body -> lower body
            RevoluteJoint jointLowerTorso = new RevoluteJoint(_middleBody, _lowerBody, _middleBody.Position + new Vector2(0f, 0.625f), true);

            jointLowerTorso.LimitEnabled = true;
            jointLowerTorso.SetLimits(-MathHelper.Pi / 8f, MathHelper.Pi / 8f);
            world.AddJoint(jointLowerTorso);

            // GFX
            _face      = new Sprite(ContentWrapper.CircleTexture(0.75f, "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Grey, 1f));
            _torso     = new Sprite(ContentWrapper.PolygonTexture(PolygonUtils.CreateRoundedRectangle(1.5f, 2f, 0.75f, 0.75f, 2), "Stripe", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 2.0f));
            _upperLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonUtils.CreateCapsule(1.9f, 0.45f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f));
            _lowerLimb = new Sprite(ContentWrapper.PolygonTexture(PolygonUtils.CreateCapsule(2f, 0.5f, 16), "Square", ContentWrapper.Gold, ContentWrapper.Orange, ContentWrapper.Black, 1f));
        }
Example #20
0
        private void CreateLeg(World world, float direction, Vector2 wheelAnchor, int index)
        {
            Vector2[] points =
            {
                new Vector2(5.4f * direction,  6.1f),
                new Vector2(7.2f * direction,  1.2f),
                new Vector2(4.3f * direction,  1.9f),
                new Vector2(3.1f * direction, -0.8f),
                new Vector2(6.0f * direction, -1.5f),
                new Vector2(2.5f * direction, -3.7f)
            };

            PolygonShape legPolygon      = new PolygonShape(1f);
            PolygonShape shoulderPolygon = new PolygonShape(1f);

            if (direction < 0f)
            {
                legPolygon.Vertices      = new Vertices(new[] { points[0], points[1], points[2] });
                shoulderPolygon.Vertices = new Vertices(new[] { Vector2.Zero, points[4] - points[3], points[5] - points[3] });
            }

            if (direction > 0f)
            {
                legPolygon.Vertices      = new Vertices(new[] { points[0], points[2], points[1] });
                shoulderPolygon.Vertices = new Vertices(new[] { Vector2.Zero, points[5] - points[3], points[4] - points[3] });
            }

            Body leg = BodyFactory.CreateBody(world);

            leg.BodyType       = BodyType.Dynamic;
            leg.Position       = _position;
            leg.AngularDamping = 10f;

            if (direction < 0f)
            {
                _leftLegs[index] = leg;
            }

            if (direction > 0f)
            {
                _rightLegs[index] = leg;
            }

            Body shoulder = BodyFactory.CreateBody(world);

            shoulder.BodyType       = BodyType.Dynamic;
            shoulder.Position       = points[3] + _position;
            shoulder.AngularDamping = 10f;

            if (direction < 0f)
            {
                _leftShoulders[index] = shoulder;
            }

            if (direction > 0f)
            {
                _rightShoulders[index] = shoulder;
            }

            Fixture legFixture = leg.CreateFixture(legPolygon);

            legFixture.CollisionGroup = -1;

            Fixture shoulderFixture = shoulder.CreateFixture(shoulderPolygon);

            shoulderFixture.CollisionGroup = -1;

            float dampingRatio = 0.5f;
            float frequencyHz  = 10.0f;

            // Using a soft distancejoint can reduce some jitter.
            // It also makes the structure seem a bit more fluid by
            // acting like a suspension system.
            DistanceJoint djd = new DistanceJoint(leg, shoulder, points[1] + _position, points[4] + _position, true);

            JointHelper.LinearStiffness(frequencyHz, dampingRatio, djd.BodyA, djd.BodyB, out float stiffness, out float damping);
            djd.Damping   = damping;
            djd.Stiffness = stiffness;

            world.AddJoint(djd);
            _walkerJoints.Add(djd);

            DistanceJoint djd2 = new DistanceJoint(leg, shoulder, points[2] + _position, points[3] + _position, true);

            djd2.Damping   = damping;
            djd2.Stiffness = stiffness;

            world.AddJoint(djd2);
            _walkerJoints.Add(djd2);

            DistanceJoint djd3 = new DistanceJoint(leg, _wheel, points[2] + _position, wheelAnchor + _position, true);

            djd3.Damping   = damping;
            djd3.Stiffness = stiffness;

            world.AddJoint(djd3);
            _walkerJoints.Add(djd3);

            DistanceJoint djd4 = new DistanceJoint(shoulder, _wheel, points[5] + _position, wheelAnchor + _position, true);

            djd4.Damping   = damping;
            djd4.Stiffness = stiffness;

            world.AddJoint(djd4);
            _walkerJoints.Add(djd4);

            RevoluteJoint rjd = new RevoluteJoint(shoulder, _chassis, points[3] + _position, true);

            world.AddJoint(rjd);
        }
Example #21
0
        private CantileverTest()
        {
            Body ground = BodyFactory.CreateEdge(World, new Vector2(-40, 0), new Vector2(40, 0));

            {
                PolygonShape shape = new PolygonShape(20);
                shape.SetAsBox(0.5f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(-14.5f + 1.0f * i, 5.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(fd);

                    Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    JointFactory.CreateFromDef(World, jd);

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape(20.0f);
                shape.SetAsBox(1.0f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                WeldJointDef jd = new WeldJointDef();

                float frequencyHz  = 5.0f;
                float dampingRatio = 0.7f;

                Body prevBody = ground;
                for (int i = 0; i < 3; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(-14.0f + 2.0f * i, 15.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(fd);

                    Vector2 anchor = new Vector2(-15.0f + 2.0f * i, 15.0f);
                    jd.Initialize(prevBody, body, anchor);
                    JointHelper.AngularStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                    jd.Stiffness = stiffness;
                    jd.Damping   = damping;

                    JointFactory.CreateFromDef(World, jd);

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape(20.0f);
                shape.SetAsBox(0.5f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                WeldJointDef jd = new WeldJointDef();

                Body prevBody = ground;
                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(-4.5f + 1.0f * i, 5.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(fd);

                    if (i > 0)
                    {
                        Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f);
                        jd.Initialize(prevBody, body, anchor);
                        JointFactory.CreateFromDef(World, jd);
                    }

                    prevBody = body;
                }
            }

            {
                PolygonShape shape = new PolygonShape(20.0f);
                shape.SetAsBox(0.5f, 0.125f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                WeldJointDef jd           = new WeldJointDef();
                float        frequencyHz  = 8.0f;
                float        dampingRatio = 0.7f;

                Body prevBody = ground;
                for (int i = 0; i < _count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(5.5f + 1.0f * i, 10.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(fd);

                    if (i > 0)
                    {
                        Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f);
                        jd.Initialize(prevBody, body, anchor);

                        JointHelper.AngularStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping);
                        jd.Stiffness = stiffness;
                        jd.Damping   = damping;

                        JointFactory.CreateFromDef(World, jd);
                    }

                    prevBody = body;
                }
            }

            for (int i = 0; i < 2; ++i)
            {
                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));

                PolygonShape shape = new PolygonShape(vertices, 1.0f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(-8.0f + 8.0f * i, 12.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);
                body.AddFixture(fd);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;

                BodyDef bd = new BodyDef();
                bd.Type     = BodyType.Dynamic;
                bd.Position = new Vector2(-6.0f + 6.0f * i, 10.0f);
                Body body = BodyFactory.CreateFromDef(World, bd);
                body.AddFixture(fd);
            }
        }
Example #22
0
        private CarTest()
        {
            _hz    = 4.0f;
            _zeta  = 0.7f;
            _speed = 50.0f;

            Body ground = BodyFactory.CreateEdge(World, new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
            {
                float[] hs = { 0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f };

                float       x = 20.0f, y1 = 0.0f;
                const float dx = 5.0f;

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    FixtureFactory.AttachEdge(new Vector2(x, y1), new Vector2(x + dx, y2), ground);
                    y1 = y2;
                    x += dx;
                }

                for (int i = 0; i < 10; ++i)
                {
                    float y2 = hs[i];
                    FixtureFactory.AttachEdge(new Vector2(x, y1), new Vector2(x + dx, y2), ground);
                    y1 = y2;
                    x += dx;
                }

                FixtureFactory.AttachEdge(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f), ground);
                x += 80.0f;
                FixtureFactory.AttachEdge(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f), ground);
                x += 40.0f;
                FixtureFactory.AttachEdge(new Vector2(x, 0.0f), new Vector2(x + 10.0f, 5.0f), ground);
                x += 20.0f;
                FixtureFactory.AttachEdge(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f), ground);
                x += 40.0f;
                FixtureFactory.AttachEdge(new Vector2(x, 0.0f), new Vector2(x, 20.0f), ground);

                ground.Friction = 0.6f;
            }

            // Teeter
            {
                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(140.0f, 1.0f);

                PolygonShape box = new PolygonShape(1);
                box.Vertices = PolygonUtils.CreateRectangle(10.0f, 0.25f);
                body.CreateFixture(box);

                RevoluteJoint jd = JointFactory.CreateRevoluteJoint(World, ground, body, Vector2.Zero);
                jd.LowerLimit   = -8.0f * MathConstants.Pi / 180.0f;
                jd.UpperLimit   = 8.0f * MathConstants.Pi / 180.0f;
                jd.LimitEnabled = true;

                body.ApplyAngularImpulse(100.0f);
            }

            //Bridge
            {
                const int    N     = 20;
                PolygonShape shape = new PolygonShape(1);
                shape.Vertices = PolygonUtils.CreateRectangle(1.0f, 0.125f);

                Body prevBody = ground;
                for (int i = 0; i < N; ++i)
                {
                    Body body = BodyFactory.CreateBody(World);
                    body.BodyType = BodyType.Dynamic;
                    body.Position = new Vector2(161.0f + 2.0f * i, -0.125f);
                    Fixture fix = body.CreateFixture(shape);
                    fix.Friction = 0.6f;

                    Vector2 anchor = new Vector2(-1, 0);
                    JointFactory.CreateRevoluteJoint(World, prevBody, body, anchor);

                    prevBody = body;
                }

                Vector2 anchor2 = new Vector2(1.0f, 0);
                JointFactory.CreateRevoluteJoint(World, ground, prevBody, anchor2);
            }

            // Boxes
            {
                PolygonShape box = new PolygonShape(0.5f);
                box.Vertices = PolygonUtils.CreateRectangle(0.5f, 0.5f);

                Body body = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(230.0f, 0.5f);
                body.CreateFixture(box);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(230.0f, 1.5f);
                body.CreateFixture(box);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(230.0f, 2.5f);
                body.CreateFixture(box);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(230.0f, 3.5f);
                body.CreateFixture(box);

                body          = BodyFactory.CreateBody(World);
                body.BodyType = BodyType.Dynamic;
                body.Position = new Vector2(230.0f, 4.5f);
                body.CreateFixture(box);
            }

            // Car
            {
                Vertices vertices = new Vertices(8);
                vertices.Add(new Vector2(-1.5f, -0.5f));
                vertices.Add(new Vector2(1.5f, -0.5f));
                vertices.Add(new Vector2(1.5f, 0.0f));
                vertices.Add(new Vector2(0.0f, 0.9f));
                vertices.Add(new Vector2(-1.15f, 0.9f));
                vertices.Add(new Vector2(-1.5f, 0.2f));

                PolygonShape chassis = new PolygonShape(vertices, 1);

                CircleShape circle = new CircleShape(0.4f, 1);

                _car          = BodyFactory.CreateBody(World);
                _car.BodyType = BodyType.Dynamic;
                _car.Position = new Vector2(0.0f, 1.0f);
                _car.CreateFixture(chassis);

                _wheel1          = BodyFactory.CreateBody(World);
                _wheel1.BodyType = BodyType.Dynamic;
                _wheel1.Position = new Vector2(-1.0f, 0.35f);
                _wheel1.CreateFixture(circle);
                _wheel1.Friction = 0.9f;

                _wheel2          = BodyFactory.CreateBody(World);
                _wheel2.BodyType = BodyType.Dynamic;
                _wheel2.Position = new Vector2(1.0f, 0.4f);
                _wheel2.CreateFixture(circle);
                _wheel2.Friction = 0.9f;

                Vector2 axis = new Vector2(0.0f, 1.0f);
                _spring1                = new WheelJoint(_car, _wheel1, _wheel1.Position, axis, true);
                _spring1.MotorSpeed     = 0.0f;
                _spring1.MaxMotorTorque = 20.0f;
                _spring1.MotorEnabled   = true;

                JointHelper.LinearStiffness(_hz, _zeta, _spring1.BodyA, _spring1.BodyB, out float stiffness, out float damping);
                _spring1.Stiffness = stiffness;
                _spring1.Damping   = damping;
                World.AddJoint(_spring1);

                _spring2                = new WheelJoint(_car, _wheel2, _wheel2.Position, axis, true);
                _spring2.MotorSpeed     = 0.0f;
                _spring2.MaxMotorTorque = 10.0f;
                _spring2.MotorEnabled   = false;

                JointHelper.LinearStiffness(_hz, _zeta, _spring2.BodyA, _spring1.BodyB, out stiffness, out damping);
                _spring2.Stiffness = stiffness;
                _spring2.Damping   = damping;
                World.AddJoint(_spring2);
            }
        }
Example #23
0
        public WebOfGoo(World world, Vector2 position, float radius, int rings, int sides)
        {
            _ringBodys  = new List <List <Body> >(rings);
            _ringJoints = new List <DistanceJoint>();

            for (int i = 1; i < rings; i++)
            {
                Vertices vertices = PolygonUtils.CreateCircle(i * 2.9f, sides);
                vertices.Translate(ref position);
                List <Body> bodies = new List <Body>(sides);

                //Create the first goo
                Body previous = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[0]);
                previous.BodyType = BodyType.Dynamic;

                bodies.Add(previous);

                //Connect the first goo to the next
                for (int j = 1; j < vertices.Count; j++)
                {
                    Body current = BodyFactory.CreateCircle(world, radius, 0.2f, vertices[j]);
                    current.BodyType = BodyType.Dynamic;

                    DistanceJoint joint = new DistanceJoint(previous, current, Vector2.Zero, Vector2.Zero);
                    joint.Stiffness  = 4.0f;
                    joint.Damping    = 0.5f;
                    joint.Breakpoint = Breakpoint;
                    world.AddJoint(joint);
                    _ringJoints.Add(joint);

                    previous = current;
                    bodies.Add(current);
                }

                //Connect the first and the last goo
                DistanceJoint jointClose = new DistanceJoint(bodies[0], bodies[bodies.Count - 1], Vector2.Zero, Vector2.Zero);
                JointHelper.LinearStiffness(4.0f, 0.5f, jointClose.BodyA, jointClose.BodyB, out var stiffness, out var damping);
                jointClose.Stiffness  = stiffness;
                jointClose.Damping    = damping;
                jointClose.Breakpoint = Breakpoint;
                world.AddJoint(jointClose);
                _ringJoints.Add(jointClose);

                _ringBodys.Add(bodies);
            }

            //Create an outer ring
            Vertices frame = PolygonUtils.CreateCircle(rings * 2.9f - 0.9f, sides);

            frame.Translate(ref position);

            Body anchor = BodyFactory.CreateBody(world, position);

            anchor.BodyType = BodyType.Static;

            //Attach the outer ring to the anchor
            for (int i = 0; i < _ringBodys[rings - 2].Count; i++)
            {
                DistanceJoint joint = new DistanceJoint(anchor, _ringBodys[rings - 2][i], frame[i], _ringBodys[rings - 2][i].Position, true);
                JointHelper.LinearStiffness(8.0f, 0.5f, joint.BodyA, joint.BodyB, out var stiffness, out var damping);
                joint.Stiffness  = stiffness;
                joint.Damping    = damping;
                joint.Breakpoint = Breakpoint;
                world.AddJoint(joint);
                _ringJoints.Add(joint);
            }

            //Interconnect the rings
            for (int i = 1; i < _ringBodys.Count; i++)
            {
                for (int j = 0; j < sides; j++)
                {
                    DistanceJoint joint = new DistanceJoint(_ringBodys[i - 1][j], _ringBodys[i][j], Vector2.Zero, Vector2.Zero);
                    JointHelper.LinearStiffness(4.0f, 0.5f, joint.BodyA, joint.BodyB, out var stiffness, out var damping);
                    joint.Stiffness  = stiffness;
                    joint.Damping    = damping;
                    joint.Breakpoint = Breakpoint;
                    world.AddJoint(joint);
                    _ringJoints.Add(joint);
                }
            }

            _link = new Sprite(Managers.TextureManager.GetTexture("Link"));
            _goo  = new Sprite(Managers.TextureManager.GetTexture("Goo"));
        }
        private SerializationTest()
        {
            Body ground = BodyFactory.CreateEdge(World, new Vector2(-20, 0), new Vector2(20, 0));

            //Friction and distance joint
            {
                Body bodyA = BodyFactory.CreateCircle(World, 1, 1.5f, new Vector2(10, 25));
                bodyA.BodyType = BodyType.Dynamic;

                Body bodyB = BodyFactory.CreateRectangle(World, 1, 1, 1, new Vector2(-1, 25));
                bodyB.BodyType = BodyType.Dynamic;

                FrictionJoint frictionJoint = JointFactory.CreateFrictionJoint(World, bodyB, ground, Vector2.Zero);
                frictionJoint.CollideConnected = true;
                frictionJoint.MaxForce         = 100;

                JointFactory.CreateDistanceJoint(World, bodyA, bodyB);
            }

            //Wheel joint
            {
                Vertices vertices = new Vertices(6);
                vertices.Add(new Vector2(-1.5f, -0.5f));
                vertices.Add(new Vector2(1.5f, -0.5f));
                vertices.Add(new Vector2(1.5f, 0.0f));
                vertices.Add(new Vector2(0.0f, 0.9f));
                vertices.Add(new Vector2(-1.15f, 0.9f));
                vertices.Add(new Vector2(-1.5f, 0.2f));

                Body carBody = BodyFactory.CreatePolygon(World, vertices, 1, new Vector2(0, 1));
                carBody.BodyType = BodyType.Dynamic;

                Body wheel1 = BodyFactory.CreateCircle(World, 0.4f, 1, new Vector2(-1.0f, 0.35f));
                wheel1.BodyType = BodyType.Dynamic;
                wheel1.Friction = 0.9f;

                Body wheel2 = BodyFactory.CreateCircle(World, 0.4f, 1, new Vector2(1.0f, 0.4f));
                wheel2.BodyType = BodyType.Dynamic;
                wheel2.Friction = 0.9f;

                Vector2 axis = new Vector2(0.0f, 1.0f);

                WheelJoint spring1 = JointFactory.CreateWheelJoint(World, carBody, wheel1, axis);
                spring1.MotorSpeed     = 0.0f;
                spring1.MaxMotorTorque = 20.0f;
                spring1.MotorEnabled   = true;

                JointHelper.LinearStiffness(4, 0.8f, spring1.BodyA, spring1.BodyB, out float stiffness, out float damping);

                spring1.Stiffness = stiffness;
                spring1.Damping   = damping;

                WheelJoint spring2 = JointFactory.CreateWheelJoint(World, carBody, wheel2, axis);
                spring2.MotorSpeed     = 0.0f;
                spring2.MaxMotorTorque = 10.0f;
                spring2.MotorEnabled   = false;
                spring2.Stiffness      = stiffness;
                spring2.Damping        = damping;
            }

            //Prismatic joint
            {
                Body body = BodyFactory.CreateRectangle(World, 2, 2, 5, new Vector2(-10.0f, 10.0f));
                body.BodyType = BodyType.Dynamic;
                body.Rotation = 0.5f * MathConstants.Pi;

                Vector2 axis = new Vector2(2.0f, 1.0f);
                axis.Normalize();

                PrismaticJoint joint = JointFactory.CreatePrismaticJoint(World, ground, body, Vector2.Zero, axis);
                joint.MotorSpeed    = 5.0f;
                joint.MaxMotorForce = 1000.0f;
                joint.MotorEnabled  = true;
                joint.LowerLimit    = -10.0f;
                joint.UpperLimit    = 20.0f;
                joint.LimitEnabled  = true;
            }

            // Pulley joint
            {
                Body body1 = BodyFactory.CreateRectangle(World, 2, 4, 5, new Vector2(-10.0f, 16.0f));
                body1.BodyType = BodyType.Dynamic;

                Body body2 = BodyFactory.CreateRectangle(World, 2, 4, 5, new Vector2(10.0f, 16.0f));
                body2.BodyType = BodyType.Dynamic;

                Vector2 anchor1      = new Vector2(-10.0f, 16.0f + 2.0f);
                Vector2 anchor2      = new Vector2(10.0f, 16.0f + 2.0f);
                Vector2 worldAnchor1 = new Vector2(-10.0f, 16.0f + 2.0f + 12.0f);
                Vector2 worldAnchor2 = new Vector2(10.0f, 16.0f + 2.0f + 12.0f);

                JointFactory.CreatePulleyJoint(World, body1, body2, anchor1, anchor2, worldAnchor1, worldAnchor2, 1.5f, true);
            }

            //Revolute joint
            {
                Body ball = BodyFactory.CreateCircle(World, 3.0f, 5.0f, new Vector2(5.0f, 30.0f));
                ball.BodyType = BodyType.Dynamic;

                Body polygonBody = BodyFactory.CreateRectangle(World, 20, 0.4f, 2, new Vector2(10, 10));
                polygonBody.BodyType = BodyType.Dynamic;
                polygonBody.IsBullet = true;

                RevoluteJoint joint = JointFactory.CreateRevoluteJoint(World, ground, polygonBody, new Vector2(10, 0));
                joint.LowerLimit   = -0.25f * MathConstants.Pi;
                joint.UpperLimit   = 0.0f * MathConstants.Pi;
                joint.LimitEnabled = true;
            }

            //Weld joint
            {
                PolygonShape shape = new PolygonShape(PolygonUtils.CreateRectangle(0.5f, 0.125f), 20);

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

                    Vector2 anchor = new Vector2(0.5f, 0);

                    if (i == 0)
                    {
                        anchor = new Vector2(-15f, 5);
                    }

                    JointFactory.CreateWeldJoint(World, prevBody, body, anchor, new Vector2(-0.5f, 0));
                    prevBody = body;
                }
            }

            //Rope joint
            {
                LinkFactory.CreateChain(World, new Vector2(-10, 10), new Vector2(-20, 10), 0.1f, 0.5f, 10, 0.1f, true);
            }

            //Angle joint
            {
                Body fA = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(-5, 4));
                fA.BodyType = BodyType.Dynamic;
                fA.Rotation = MathConstants.Pi / 3;

                Body fB = BodyFactory.CreateRectangle(World, 4, 4, 1, new Vector2(5, 4));
                fB.BodyType = BodyType.Dynamic;

                AngleJoint joint = new AngleJoint(fA, fB);
                joint.TargetAngle = MathConstants.Pi / 2;
                World.AddJoint(joint);
            }

            //Motor joint
            {
                Body body = BodyFactory.CreateRectangle(World, 4, 1, 2, new Vector2(0, 35));
                body.BodyType = BodyType.Dynamic;
                body.Friction = 0.6f;

                MotorJoint motorJoint = JointFactory.CreateMotorJoint(World, ground, body);
                motorJoint.MaxForce      = 1000.0f;
                motorJoint.MaxTorque     = 1000.0f;
                motorJoint.LinearOffset  = new Vector2(0, 35);
                motorJoint.AngularOffset = MathConstants.Pi / 3f;
            }
        }