Exemple #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;
            }
        }
Exemple #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);
            }
        }
Exemple #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);
            }
        }
Exemple #4
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);
            }
        }
Exemple #5
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);
        }
Exemple #6
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);
                }
            }
        }