Esempio n. 1
0
        public MovingJoint()
        {
            var capeFixture = new FixtureDef(new PolygonShape(0.08f, 0.4f, (float)(Math.PI)), 0.2f);
            var capeBody = new BodyDef(BodyType.Dynamic, new Vec2(0, 10));

            Body testbody = m_world.CreateBody(capeBody);
            testbody.CreateFixture(capeFixture);

            staticBody = m_world.CreateBody(new BodyDef(BodyType.Static, testbody.WorldCenter + new Vec2(-0.08f / 25, 0.4f)));
            staticBody.CreateFixture(new CircleShape(0.15f), 0);

            {
                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(testbody, staticBody, testbody.WorldCenter + new Vec2(0.0f, 0.4f));
                joint = (RevoluteJoint)m_world.CreateJoint(rjd);
            }

            // build cape
            Body lastBody = testbody;
            for (int i = 0; i < 8; ++i)
            {
                capeBody.Position = new Vec2(capeBody.Position.X, capeBody.Position.Y - 0.8f);

                var nextBody = m_world.CreateBody(capeBody);
                nextBody.CreateFixture(capeFixture);

                var joint = new RevoluteJointDef();
                joint.Initialize(lastBody, nextBody, nextBody.WorldCenter + new Vec2(0.0f, 0.4f));
                m_world.CreateJoint(joint);

                lastBody = nextBody;
            }
        }
Esempio n. 2
0
        public Revolute()
        {
            System.Collections.Generic.List<JointDef> joints = new System.Collections.Generic.List<JointDef>();

            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

            {
                CircleShape shape = new CircleShape();
                shape.Radius = 0.5f;

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

                RevoluteJointDef rjd = new RevoluteJointDef();

                bd.Position = new Vec2(0.0f, 20.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                float w = 100.0f;
                body.AngularVelocity = w;
                body.LinearVelocity = new Vec2(-8.0f * w, 0.0f);

                rjd.Initialize(ground, body, new Vec2(0.0f, 12.0f));
                rjd.MotorSpeed = 1.0f * (float)Math.PI;
                rjd.MaxMotorTorque = 10000.0f;
                rjd.EnableMotor = false;
                rjd.LowerAngle = -0.25f * (float)Math.PI;
                rjd.UpperAngle = 0.5f * (float)Math.PI;
                rjd.EnableLimit = true;
                rjd.CollideConnected = true;

                m_joint = (RevoluteJoint)m_world.CreateJoint(rjd);
                joints.Add(rjd);
            }

            using (System.IO.FileStream fs = new System.IO.FileStream("out.xml", System.IO.FileMode.Create))
            {
                var serializer = Box2CS.Serialize.WorldSerializer.SerializeWorld(m_world, new Box2CS.Serialize.WorldXmlSerializer());

                foreach (var j in joints)
                    serializer.AddJoint(j);

                serializer.Serialize(fs);
            }
        }
Esempio n. 3
0
        public Car()
        {
            {	// car body
                PolygonShape poly1 = new PolygonShape(), poly2 = new PolygonShape();

                // bottom half
                poly1.Vertices = new Vec2[]
                {
                    new Vec2(2.2f,-0.74f),
                    new Vec2(2.2f,-0.2f),
                    new Vec2(1.0f,0),
                    new Vec2(-2.2f,0),
                    new Vec2(-2.2f,-0.74f)
                };

                FixtureDef fixture1 = new FixtureDef();

                fixture1.Filter.GroupIndex = -1;
                fixture1.Shape = poly1;
                fixture1.Density		= 20.0f;
                fixture1.Friction		= 0.68f;

                // top half
                poly2.Vertices = new Vec2[]
                {
                    new Vec2(1.0f,0),
                    new Vec2(0.5f,0.74f),
                    new Vec2(-1.3f,0.7f),
                    new Vec2(-1.7f,0),
                };

                FixtureDef fixture2 = new FixtureDef();

                fixture2.Filter.GroupIndex = -1;
                fixture2.Shape = poly2;
                fixture2.Density		= 5.0f;
                fixture2.Friction		= 0.68f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0, 0);

                m_vehicle = m_world.CreateBody(bd);
                m_vehicle.CreateFixture(fixture1);
                m_vehicle.CreateFixture(fixture2);
            }

            {	// vehicle wheels
                CircleShape circ = new CircleShape();
                circ.Radius = 0.58608f;

                FixtureDef wheelFix = new FixtureDef();
                wheelFix.Shape = circ;
                wheelFix.Density = 40.0f;
                wheelFix.Friction = 0.8f;
                wheelFix.Filter.GroupIndex = -1;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.AllowSleep = false;
                bd.Position = new Vec2(1.2f, -0.8f);

                m_rightWheel = m_world.CreateBody(bd);
                m_rightWheel.CreateFixture(wheelFix);

                bd.Position = new Vec2(-1.2f, -0.8f);
                m_leftWheel = m_world.CreateBody(bd);
                m_leftWheel.CreateFixture(wheelFix);
            }

            {	// join wheels to chassis
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(m_vehicle, m_leftWheel, m_leftWheel.WorldCenter);
                jd.CollideConnected = false;
                jd.EnableMotor = true;
                jd.MaxMotorTorque = 10.0f;
                jd.MotorSpeed = 0.0f;
                m_leftJoint = (RevoluteJoint)m_world.CreateJoint(jd);

                jd.Initialize(m_vehicle, m_rightWheel, m_rightWheel.WorldCenter);
                jd.CollideConnected = false;
                m_rightJoint = (RevoluteJoint)m_world.CreateJoint(jd);
            }
        }
Esempio n. 4
0
        public BodyTypes()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f));

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

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 3.0f);
                m_attachment = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 2.0f);
                m_attachment.CreateFixture(shape, 2.0f);
            }

            // Define platform
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-4.0f, 5.0f);
                m_platform = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 4.0f, new Vec2(4.0f, 0.0f), 0.5f * (float)Math.PI);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;
                m_platform.CreateFixture(fd);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(m_attachment, m_platform, new Vec2(0.0f, 5.0f));
                rjd.MaxMotorTorque = 50.0f;
                rjd.EnableMotor = true;
                m_world.CreateJoint(rjd);

                PrismaticJointDef pjd = new PrismaticJointDef();
                pjd.Initialize(ground, m_platform, new Vec2(0.0f, 5.0f), new Vec2(1.0f, 0.0f));

                pjd.MaxMotorForce = 1000.0f;
                pjd.EnableMotor = true;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit = true;

                m_world.CreateJoint(pjd);

                m_speed = 3.0f;
            }

            // Create a payload
            {
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 8.0f);
                m_payload = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.75f, 0.75f);

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Friction = 0.6f;
                fd.Density = 2.0f;

                m_payload.CreateFixture(fd);
            }
        }
Esempio n. 5
0
        public Dominos()
        {
            Body b1 = m_world.CreateBody(new BodyDef());
            b1.CreateFixture(new PolygonShape(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)), 0.0f);
            b1.UserData = 1;

            {
                Body ground = m_world.CreateBody(new BodyDef(BodyType.Static, new Vec2(-1.5f, 10.0f)));
                ground.CreateFixture(new PolygonShape(6.0f, 0.25f), 0.0f);
                ground.UserData = "ground";
            }

            for (int i = 0; i < 10; ++i)
            {
                Body body = m_world.CreateBody(new BodyDef(BodyType.Dynamic, new Vec2(-6.0f + 1.0f * i, 11.25f), 0));
                body.CreateFixture(new FixtureDef(new PolygonShape(0.1f, 1.0f), 20.0f, 0.0f, 0.1f));
                body.UserData = "domino " + i.ToString();
            }

            {
                Body ground = m_world.CreateBody(new BodyDef(BodyType.Static, new Vec2(1.0f, 6.0f)));
                ground.CreateFixture(new PolygonShape(7.0f, 0.25f, Vec2.Empty, 0.3f), 0.0f);
                ground.UserData = "ground";
            }

            Body b2;
            {
                b2 = m_world.CreateBody(new BodyDef(BodyType.Static, new Vec2(-7.0f, 4.0f)));
                b2.CreateFixture(new PolygonShape(0.25f, 1.5f), 0.0f);
                b2.UserData = b2;
            }

            Body b3;
            {
                b3 = m_world.CreateBody(new BodyDef(BodyType.Dynamic, new Vec2(-0.9f, 1.0f), -0.15f));
                b3.CreateFixture(new PolygonShape(6.0f, 0.125f), 10.0f);
                b3.UserData = 3;
            }

            RevoluteJointDef jd = new RevoluteJointDef();
            Vec2 anchor = new Vec2(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.CollideConnected = true;
            m_world.CreateJoint(jd);

            Body b4;
            {
                b4 = m_world.CreateBody(new BodyDef(BodyType.Dynamic, new Vec2(-10.0f, 15.0f)));
                b4.CreateFixture(new PolygonShape(0.25f, 0.25f), 10.0f);
                b4.UserData = 4;
            }

            anchor = new Vec2(-7.0f, 15.0f);
            jd.Initialize(b2, b4, anchor);
            m_world.CreateJoint(jd);

            Body b5;
            {
                b5 = m_world.CreateBody(new BodyDef(BodyType.Dynamic, new Vec2(6.5f, 3.0f)));

                b5.CreateFixture(new FixtureDef(new PolygonShape(1.0f, 0.1f, new Vec2(0.0f, -0.9f), 0.0f), 10.0f, 0.0f, 0.1f));
                b5.CreateFixture(new FixtureDef(new PolygonShape(0.1f, 1.0f, new Vec2(-0.9f, 0.0f), 0.0f), 10.0f, 0.0f, 0.1f));
                b5.CreateFixture(new FixtureDef(new PolygonShape(0.1f, 1.0f, new Vec2(0.9f, 0.0f), 0.0f), 10.0f, 0.0f, 0.1f));

                b5.UserData = 5;
            }

            anchor = new Vec2(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            m_world.CreateJoint(jd);

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

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(6.5f, 4.1f);
                b6 = m_world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
                b6.UserData = 6;
            }

            anchor = new Vec2(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            m_world.CreateJoint(jd);

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

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

                b7 = m_world.CreateBody(bd);
                b7.CreateFixture(shape, 10.0f);
                b7.UserData = 7;
            }

            DistanceJointDef djd = new DistanceJointDef();
            djd.BodyA = b3;
            djd.BodyB = b7;
            djd.LocalAnchorA = new Vec2(6.0f, 0.0f);
            djd.LocalAnchorB = new Vec2(0.0f, -1.0f);
            Vec2 d = djd.BodyB.GetWorldPoint(djd.LocalAnchorB) - djd.BodyA.GetWorldPoint(djd.LocalAnchorA);
            djd.Length = d.Length();
            m_world.CreateJoint(djd);

            {
                float radius = 0.2f;

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

                for (int i = 0; i < 4; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                    body.UserData = "circle";
                }
            }
        }
Esempio n. 6
0
        public Gears()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

            {
                CircleShape circle1 = new CircleShape();
                circle1.Radius = 1.0f;

                CircleShape circle2 = new CircleShape();
                circle2.Radius = 2.0f;

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

                BodyDef bd1 = new BodyDef();
                bd1.BodyType = BodyType.Dynamic;
                bd1.Position = new Vec2(-3.0f, 12.0f);
                Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                RevoluteJointDef jd1 = new RevoluteJointDef();
                jd1.BodyA = ground;
                jd1.BodyB = body1;
                jd1.LocalAnchorA = ground.GetLocalPoint(bd1.Position);
                jd1.LocalAnchorB = body1.GetLocalPoint(bd1.Position);
                jd1.ReferenceAngle = body1.Angle - ground.Angle;
                m_joint1 = (RevoluteJoint)m_world.CreateJoint(jd1);

                BodyDef bd2 = new BodyDef();
                bd2.BodyType = BodyType.Dynamic;
                bd2.Position = new Vec2(0.0f, 12.0f);
                Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                RevoluteJointDef jd2 = new RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.Position);
                m_joint2 = (RevoluteJoint)m_world.CreateJoint(jd2);

                BodyDef bd3 = new BodyDef();
                bd3.BodyType = BodyType.Dynamic;
                bd3.Position = new Vec2(2.5f, 12.0f);
                Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

                PrismaticJointDef jd3 = new PrismaticJointDef();
                jd3.Initialize(ground, body3, bd3.Position, new Vec2(0.0f, 1.0f));
                jd3.LowerTranslation = -5.0f;
                jd3.UpperTranslation = 5.0f;
                jd3.EnableLimit = true;

                m_joint3 = (PrismaticJoint)m_world.CreateJoint(jd3);

                GearJointDef jd4 = new GearJointDef();
                jd4.BodyA = body1;
                jd4.BodyB = body2;
                jd4.JointA = m_joint1;
                jd4.JointB = m_joint2;
                jd4.Ratio = circle2.Radius / circle1.Radius;
                m_joint4 = (GearJoint)m_world.CreateJoint(jd4);

                GearJointDef jd5 = new GearJointDef();
                jd5.BodyA = body2;
                jd5.BodyB = body3;
                jd5.JointA = m_joint2;
                jd5.JointB = m_joint3;
                jd5.Ratio = -1.0f / circle2.Radius;
                m_joint5 = (GearJoint)m_world.CreateJoint(jd5);
            }
        }
Esempio n. 7
0
        public TheoJansen()
        {
            m_offset = new Vec2(0.0f, 8.0f);
            m_motorSpeed = 2.0f;
            m_motorOn = true;
            Vec2 pivot = new Vec2(0.0f, 0.8f);

            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

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

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

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

                const float groundHeight = 0.19f;
                const float groundHeight2 = groundHeight * 2;

                shape.SetAsBox(10, groundHeight, new Vec2(-14, (groundHeight / 2)), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(8, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(6, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(4, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);

                shape.SetAsBox(2, groundHeight, new Vec2(-14, (groundHeight / 2) + groundHeight2 + groundHeight2 + groundHeight2 + groundHeight2), 0);
                ground.CreateFixture(shape, 0.0f);
            }

            // Balls
            for (int i = 0; i < 25; ++i)
            {
                CircleShape shape = new CircleShape();
                shape.Radius = 0.25f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-40.0f + 2.0f * i, 3f);

                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);
            }

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

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = pivot + m_offset;
                m_chassis = m_world.CreateBody(bd);
                m_chassis.CreateFixture(sd);
            }

            {
                CircleShape shape = new CircleShape();
                shape.Radius = 1.6f;

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = pivot + m_offset;
                m_wheel = m_world.CreateBody(bd);
                m_wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
                jd.CollideConnected = false;
                jd.MotorSpeed = m_motorSpeed;
                jd.MaxMotorTorque = 400.0f;
                jd.EnableMotor = m_motorOn;
                m_motorJoint = (RevoluteJoint)m_world.CreateJoint(jd);
            }

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

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

            m_wheel.SetTransform(m_wheel.Position, 120.0f * (float)Math.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            m_wheel.SetTransform(m_wheel.Position, -120.0f * (float)Math.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
Esempio n. 8
0
        void CreateLeg(float s, Vec2 wheelAnchor)
        {
            Vec2 p1 = new Vec2(5.4f * s, -6.1f);
            Vec2 p2 = new Vec2(7.2f * s, -1.2f);
            Vec2 p3 = new Vec2(4.3f * s, -1.9f);
            Vec2 p4 = new Vec2(3.1f * s, 0.8f);
            Vec2 p5 = new Vec2(6.0f * s, 1.5f);
            Vec2 p6 = new Vec2(2.5f * s, 3.7f);

            FixtureDef fd1 = new FixtureDef(), fd2 = new FixtureDef();
            fd1.Filter.GroupIndex = fd2.Filter.GroupIndex = -1;
            fd1.Density = 1.0f;
            fd2.Density = 1.0f;

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

            if (s > 0.0f)
            {
                Vec2[] vertices = new Vec2[3];

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Vertices = vertices;

                vertices[0] = Vec2.Empty;
                vertices[1] = p5 - p4;
                vertices[2] = p6 - p4;
                poly2.Vertices = vertices;
            }
            else
            {
                Vec2[] vertices = new Vec2[3];

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Vertices = vertices;

                vertices[0] = Vec2.Empty;
                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.BodyType = BodyType.Dynamic;
            bd2.BodyType = BodyType.Dynamic;
            bd1.Position = m_offset;
            bd2.Position = p4 + m_offset;

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

            Body body1 = m_world.CreateBody(bd1);
            Body body2 = m_world.CreateBody(bd2);

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

            DistanceJointDef djd = 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.
            djd.DampingRatio = 0.5f;
            djd.FrequencyHz = 10.0f;

            djd.Initialize(body1, body2, p2 + m_offset, p5 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, body2, p3 + m_offset, p4 + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body1, m_wheel, p3 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            djd.Initialize(body2, m_wheel, p6 + m_offset, wheelAnchor + m_offset);
            m_world.CreateJoint(djd);

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.Initialize(body2, m_chassis, p4 + m_offset);
            m_world.CreateJoint(rjd);
        }
Esempio n. 9
0
        public Bridge()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 20.0f;
                fd.Friction = 0.2f;

                RevoluteJointDef jd = new RevoluteJointDef();

                Body prevBody = ground;
                for (int i = 0; i < e_count; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(-14.5f + 1.0f * i, 5.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    if (i == (e_count >> 1))
                        m_middle = body;

                    prevBody = body;
                }

                jd.Initialize(prevBody, ground, new Vec2(-15.0f + 1.0f * e_count, 5.0f));
                m_world.CreateJoint(jd);
            }

            for (int i = 0; i < 2; ++i)
            {
                Vec2[] vertices = new Vec2[3];
                vertices[0] = new Vec2(-0.5f, 0.0f);
                vertices[1] = new Vec2(0.5f, 0.0f);
                vertices[2] = new Vec2(0.0f, 1.5f);

                PolygonShape shape = new PolygonShape();
                shape.Vertices = vertices;

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-8.0f + 8.0f * i, 12.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.Shape = shape;
                fd.Density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-6.0f + 6.0f * i, 10.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
Esempio n. 10
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

            {
                Body prevBody = ground;

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 7.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f));
                    rjd.MotorSpeed = 1.0f * (float)Math.PI;
                    rjd.MaxMotorTorque = 10000.0f;
                    rjd.EnableMotor = true;
                    m_joint1 = (RevoluteJoint)m_world.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 13.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 9.0f));
                    rjd.EnableMotor = false;
                    m_world.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 17.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 17.0f));
                    m_world.CreateJoint(rjd);

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.Initialize(ground, body, new Vec2(0.0f, 17.0f), new Vec2(0.0f, 1.0f));

                    pjd.MaxMotorForce = 1000.0f;
                    pjd.EnableMotor = true;

                    m_joint2 = (PrismaticJoint)m_world.CreateJoint(pjd);
                }

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

                    BodyDef bd = new BodyDef();
                    bd.BodyType = BodyType.Dynamic;
                    bd.Position = new Vec2(0.0f, 23.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }