Exemplo n.º 1
0
        public Bridge()
        {            
	        Body ground;
            {
                PolygonDef sd = new PolygonDef();
                sd.ShapeType = ShapeType.e_polygonShape;
                sd.SetAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.Position = new Vector(0, -10);
                
                ground = world.CreateBody(bd);
                ground.CreateShape(sd);
            }

            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.5f, 0.125f);
                sd.Density = 20.0f;
                sd.Friction = 0.2f;

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

                RevoluteJointDef jd = new RevoluteJointDef();
			    const float numPlanks = 30;

                Body prevBody = ground;

                for (float i = 0; i < numPlanks; ++i)
                {
                    bd.Position = new Vector(-14.5f + i, 5);
                    Body body = world.CreateBody(bd);
				    body.CreateShape(sd);
                    body.SetMassFromShapes();

				    Vector anchor = new Vector(-15 + i, 5);
                    jd.Initialize(prevBody, body, anchor);
                    world.CreateJoint(jd);

				    prevBody = body;
                }

                Vector anchor2 = new Vector(-15 + numPlanks, 5);
			    jd.Initialize(prevBody, ground, anchor2);
			    world.CreateJoint(jd);
            }
        }
Exemplo n.º 2
0
        private void Start()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

                var fd = new FixtureDef
                {
                    Shape    = shape,
                    Density  = 20.0f,
                    Friction = 0.2f
                };

                var jd = new RevoluteJointDef {
                    CollideConnected = false
                };

                const float y        = 25.0f;
                var         prevBody = ground;
                for (var i = 0; i < 30; ++i)
                {
                    var bd = new BodyDef {
                        BodyType = BodyType.DynamicBody
                    };
                    bd.Position.Set(0.5f + i, y);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

                    var anchor = new Vector2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    World.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 3
0
        public Tumbler()
        {
            World = new World();
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);
            }

            {
                var bd = new BodyDef
                {
                    BodyType   = BodyType.DynamicBody,
                    AllowSleep = false,
                    Position   = new Vector2(0.0f, 10.0f)
                };
                var body = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.5f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(0.5f, 10.0f, new Vector2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape, 5.0f);

                var jd = new RevoluteJointDef
                {
                    BodyA          = ground,
                    BodyB          = body,
                    LocalAnchorA   = new Vector2(0.0f, 10.0f),
                    LocalAnchorB   = new Vector2(0.0f, 0.0f),
                    ReferenceAngle = 0.0f,
                    MotorSpeed     = 0.05f * Settings.Pi,
                    MaxMotorTorque = 1e8f,
                    EnableMotor    = true
                };
                _joint = (RevoluteJoint)World.CreateJoint(jd);
            }

            _count = 0;
        }
Exemplo n.º 4
0
        public RevoluteJoint CreateRevoluteJointJoint(Body bodyA, Body bodyB, bool reverse = false)
        {
            var jd         = new RevoluteJointDef();
            var motorSpeed = 3.501000000000000e+00f;

            jd.bodyA        = bodyA;
            jd.bodyB        = bodyB;
            jd.localAnchorB = new Vector2(-0.3f, 0);

            jd.enableLimit = true;
            jd.enableMotor = true;
            jd.upperAngle  = reverse ? DegToRad(90)  : DegToRad(180);
            jd.lowerAngle  = reverse ? DegToRad(0)  : DegToRad(90);
            //jd.referenceAngle = DegToRad(90);
            jd.motorSpeed     = reverse ? -motorSpeed : motorSpeed;
            jd.maxMotorTorque = 30.120000000000000e+00f;
            return(b2DWorld.CreateJoint(jd) as RevoluteJoint);
        }
Exemplo n.º 5
0
        public Joint AddJoint(Body b1, Body b2, float x, float y, bool collideConnected, float upperAngle, float lowerAngle, float referenceAngle = 0)
        {
            RevoluteJointDef jd = new RevoluteJointDef();

            jd.Initialize(b1, b2, new Vec2(x / metric, y / metric));
            jd.CollideConnected = collideConnected;
            jd.EnableMotor      = true;
            jd.EnableLimit      = true;

            jd.LowerAngle = lowerAngle;
            jd.UpperAngle = upperAngle;

            jd.ReferenceAngle = referenceAngle;

            Joint joint = world.CreateJoint(jd);

            return(joint);
        }
Exemplo n.º 6
0
        public Bridge()
        {
            Body ground = null;
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -10.0f);
                ground = _world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.5f, 0.125f);
                sd.Density  = 20.0f;
                sd.Friction = 0.2f;

                RevoluteJointDef jd        = new RevoluteJointDef();
                const int        numPlanks = 30;

                Body prevBody = ground;
                for (int i = 0; i < numPlanks; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-14.5f + 1.0f * i, 5.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(sd);
                    body.SetMassFromShapes();

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

                    prevBody = body;
                }

                Vec2 anchor_ = new Vec2(-15.0f + 1.0f * numPlanks, 5.0f);
                jd.Initialize(prevBody, ground, anchor_);
                _world.CreateJoint(jd);
            }
        }
Exemplo n.º 7
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                EdgeShape shape = new EdgeShape();
                shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                shape.Density = 0;
                ground.CreateFixture(shape);
            }

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                const float y        = 25.0f;
                Body        prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.5f + i, y);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vec2 anchor = new Vec2((float)(i), y);
                    jd.Initialize(prevBody, body, anchor);
                    m_world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 8
0
        public Body AddNode(Body parent, Vec2 localAnchor, int depth, float offset, float a)
        {
            Vec2 h = new Vec2(0.0f, a);

            Vec2 p = parent.GetPosition() + localAnchor - h;

            BodyDef bodyDef = new BodyDef();

            bodyDef.type     = BodyType._dynamicBody;
            bodyDef.Position = p;
            Body body = m_world.CreateBody(bodyDef);

            PolygonShape shape = new PolygonShape();

            shape.SetAsBox(0.25f * a, a);
            shape.Density = 20;
            body.CreateFixture(shape);

            if (depth == e_depth)
            {
                return(body);
            }

            Vec2 a1    = new Vec2(offset, -a);
            Vec2 a2    = new Vec2(-offset, -a);
            Body body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            Body body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.bodyA        = body;
            jointDef.localAnchorB = h;

            jointDef.localAnchorA = a1;
            jointDef.bodyB        = body1;
            m_world.CreateJoint(jointDef);

            jointDef.localAnchorA = a2;
            jointDef.bodyB        = body2;
            m_world.CreateJoint(jointDef);

            return(body);
        }
        private Body AddNode(Body parent, Vector2 localAnchor, int depth, float offset, float a)
        {
            float   density = 20.0f;
            Vector2 h       = new Vector2(0.0f, a);

            Vector2 p = parent.Position + localAnchor - h;

            BodyDef bodyDef = new BodyDef();

            bodyDef.Type     = BodyType.Dynamic;
            bodyDef.Position = p;
            Body body = BodyFactory.CreateFromDef(World, bodyDef);

            PolygonShape shape = new PolygonShape(density);

            shape.SetAsBox(0.25f * a, a);
            body.AddFixture(shape);

            if (depth == _depth)
            {
                return(body);
            }

            Vector2 a1    = new Vector2(offset, -a);
            Vector2 a2    = new Vector2(-offset, -a);
            Body    body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            Body    body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.BodyA        = body;
            jointDef.LocalAnchorB = h;

            jointDef.LocalAnchorA = a1;
            jointDef.BodyB        = body1;
            JointFactory.CreateFromDef(World, jointDef);

            jointDef.LocalAnchorA = a2;
            jointDef.BodyB        = body2;
            JointFactory.CreateFromDef(World, jointDef);

            return(body);
        }
Exemplo n.º 10
0
        private Body AddNode(Body parent, Vector2 localAnchor, int depth, float offset, float a)
        {
            var density = 20.0f;
            var h       = new Vector2(0.0f, a);

            var p = parent.GetPosition() + localAnchor - h;

            var bodyDef = new BodyDef();

            bodyDef.BodyType = BodyType.DynamicBody;
            bodyDef.Position = p;
            var body = World.CreateBody(bodyDef);

            var shape = new PolygonShape();

            shape.SetAsBox(0.25f * a, a);
            body.CreateFixture(shape, density);

            if (depth == Depth)
            {
                return(body);
            }

            var a1    = new Vector2(offset, -a);
            var a2    = new Vector2(-offset, -a);
            var body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a);
            var body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a);

            var jointDef = new RevoluteJointDef();

            jointDef.BodyA        = body;
            jointDef.LocalAnchorB = h;

            jointDef.LocalAnchorA = a1;
            jointDef.BodyB        = body1;
            World.CreateJoint(jointDef);

            jointDef.LocalAnchorA = a2;
            jointDef.BodyB        = body2;
            World.CreateJoint(jointDef);

            return(body);
        }
Exemplo n.º 11
0
        public Revolute()
        {
            Body ground = null;
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -10.0f);
                ground = _world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            {
                CircleDef sd = new CircleDef();
                sd.Radius  = 0.5f;
                sd.Density = 5.0f;

                BodyDef bd = new BodyDef();

                RevoluteJointDef rjd = new RevoluteJointDef();

                bd.Position.Set(0.0f, 20.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                body.SetMassFromShapes();

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

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

                _joint = (RevoluteJoint)_world.CreateJoint(rjd);
            }
        }
Exemplo n.º 12
0
    // Use this for initialization
    protected override void Init()
    {
        var ground = API.CreateBody(world, new Vector2(0, 0), 0, BodyType.STATIC_BODY);

        API.AddEdgeShape(ground, new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f), new ShapeDef(0));

        ShapeDef sp = new ShapeDef(20.0f);

        sp.friction = 0.2f;

        RevoluteJointDef def      = new RevoluteJointDef();
        IntPtr           prevBody = ground;

        for (int i = 0; i < e_count; ++i)
        {
            var b = API.CreateBody(world, new Vector2(-14.5f + i, 5.0f), 0, BodyType.DYNAMIC_BODY);
            API.AddBoxShape(b, 0.5f, 0.125f, Vector2.zero, 0, sp);

            def.Initialize(prevBody, b, new Vector2(-15.0f + i, 5.0f));
            API.CreateRevoluteJoint(world, def);
            prevBody = b;
        }

        def.Initialize(prevBody, ground, new Vector2(-15.0f + e_count, 5.0f));
        API.CreateRevoluteJoint(world, def);

        for (int i = 0; i < 2; ++i)
        {
            var       b        = API.CreateBody(world, new Vector2(-8.0f + 8 * i, 12), 0, BodyType.DYNAMIC_BODY);
            Vector2[] vertices = new Vector2[3];
            vertices[0].Set(-0.5f, 0.0f);
            vertices[1].Set(0.5f, 0.0f);
            vertices[2].Set(0.0f, 1.5f);
            API.AddPolygonShape(b, vertices, vertices.Length, new ShapeDef(1.0f));
        }

        for (int i = 0; i < 3; ++i)
        {
            var b = API.CreateBody(world, new Vector2(-6.0f + 6 * i, 10), 0, BodyType.DYNAMIC_BODY);
            API.AddCircleShape(b, 0.5f, new ShapeDef(1.0f));
        }
    }
Exemplo n.º 13
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                fd.friction = 0.2f;

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                float y = 25.0f;
                Body prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vector2 anchor = new Vector2((float)i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 14
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                FixtureDef fd = new FixtureDef();
                fd.shape    = shape;
                fd.density  = 20.0;
                fd.friction = 0.2;

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.collideConnected = false;

                double y        = 25.0;
                Body   prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.type     = BodyType.Dynamic;
                    bd.position = new Vector2(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);

                    Vector2 anchor = new Vector2((double)i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 15
0
    void Start()
    {
        if (connectedBody == null)
        {
            return;
        }
        var jointDef = new RevoluteJointDef();

#if USING_BOX2DX
        jointDef.Initialize(connectedBody, GetComponent <Box2DBody>(), anchor.ToVec2());
#else
        jointDef.Initialize(connectedBody, GetComponent <Box2DBody>(), anchor);
#endif
        jointDef.MotorSpeed       = motorSpeed;
        jointDef.MaxMotorTorque   = maxMotorTorque;
        jointDef.EnableMotor      = enableMotor;
        jointDef.CollideConnected = collideConnected;

        joint = (RevoluteJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
Exemplo n.º 16
0
        /// <summary>
        /// Creates a distance joint between two bodies
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <param name="anchor"></param>
        /// <param name="limits"></param>
        /// <param name="motor"></param>
        public RevoluteJoint joinBodies_Rotate(Body b1, Body b2, Vector2 anchor, Vector2 limits, float motor)
        {
            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.enableLimit = false;
            if (motor == 0)
            {
                rjd.enableMotor = false;
            }
            else
            {
                rjd.enableMotor = true;
            }
            rjd.motorSpeed       = motor;
            rjd.collideConnected = false;
            rjd.Initialize(b1, b2, anchor);
            RevoluteJoint rj = physicsWorld.CreateJoint(rjd) as RevoluteJoint;

            return(rj);
        }
Exemplo n.º 17
0
        private ChainTest()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                ground = BodyFactory.CreateFromDef(World, bd);

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

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

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

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.CollideConnected = false;

                const float y        = 25.0f;
                Body        prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(0.5f + i, y);
                    Body body = BodyFactory.CreateFromDef(World, bd);

                    body.AddFixture(fd);

                    Vector2 anchor = new Vector2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    JointFactory.CreateFromDef(World, jd);

                    prevBody = body;
                }
            }
        }
Exemplo n.º 18
0
        public Tumbler()
        {
            Body ground = null;

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

            {
                BodyDef bd = new BodyDef();
                bd.type       = BodyType._dynamicBody;
                bd.allowSleep = false;
                bd.Position.Set(0.0f, 10.0f);
                Body body = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.Density = 5;
                shape.SetAsBox(0.5f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape);
                shape.SetAsBox(0.5f, 10.0f, new Vec2(-10.0f, 0.0f), 0.0f);
                body.CreateFixture(shape);
                shape.SetAsBox(10.0f, 0.5f, new Vec2(0.0f, 10.0f), 0.0f);
                body.CreateFixture(shape);
                shape.SetAsBox(10.0f, 0.5f, new Vec2(0.0f, -10.0f), 0.0f);
                body.CreateFixture(shape);

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.bodyA = ground;
                jd.bodyB = body;
                jd.localAnchorA.Set(0.0f, 10.0f);
                jd.localAnchorB.Set(0.0f, 0.0f);
                jd.referenceAngle = 0.0f;
                jd.motorSpeed     = 0.05f * (float)Math.PI;
                jd.maxMotorTorque = 1e8f;
                jd.enableMotor    = true;
                m_joint           = (RevoluteJoint)m_world.CreateJoint(jd);
            }

            m_count = 0;
        }
Exemplo n.º 19
0
        public Revolute()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

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

                RevoluteJointDef rjd = new RevoluteJointDef();

                bd.position = new Vector2(0.0f, 20.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                float w = 100.0f;
                body.SetAngularVelocity(w);
                body.SetLinearVelocity(new Vector2(-8.0f * w, 0.0f));

                rjd.Initialize(ground, body, new Vector2(0.0f, 12.0f));
                rjd.motorSpeed = 1.0f * (float)Settings.b2_pi;
                rjd.maxMotorTorque = 10000.0f;
                rjd.enableMotor = false;
                rjd.lowerAngle = -0.25f * (float)Settings.b2_pi;
                rjd.upperAngle = 0.5f * (float)Settings.b2_pi;
                rjd.enableLimit = true;
                rjd.collideConnected = true;

                _joint = (RevoluteJoint)_world.CreateJoint(rjd);
            }
        }
Exemplo n.º 20
0
        public Revolute()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

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

                RevoluteJointDef rjd = new RevoluteJointDef();

                bd.position = new Vector2(0.0f, 20.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                float w = 100.0f;
                body.SetAngularVelocity(w);
                body.SetLinearVelocity(new Vector2(-8.0f * w, 0.0f));

                rjd.Initialize(ground, body, new Vector2(0.0f, 12.0f));
                rjd.motorSpeed       = 1.0f * (float)Settings.b2_pi;
                rjd.maxMotorTorque   = 10000.0f;
                rjd.enableMotor      = false;
                rjd.lowerAngle       = -0.25f * (float)Settings.b2_pi;
                rjd.upperAngle       = 0.5f * (float)Settings.b2_pi;
                rjd.enableLimit      = true;
                rjd.collideConnected = true;

                _joint = (RevoluteJoint)_world.CreateJoint(rjd);
            }
        }
Exemplo n.º 21
0
        public Joint JointRevolute(RigidBody body, float x, float y, bool isCollide, float lowerAngle, float upperAngle,
                                   float referenceAngle)
        {
            var p = GetBody().GetPosition() + new Vec2(x / PhysicsController.metric, y / PhysicsController.metric);

            RevoluteJointDef jd = new RevoluteJointDef();

            jd.Initialize(GetBody(), body.GetBody(), p);
            jd.CollideConnected = isCollide;
            jd.EnableMotor      = true;
            jd.EnableLimit      = true;

            jd.LowerAngle = lowerAngle / 180 * (float)Math.PI;
            jd.UpperAngle = upperAngle / 180 * (float)Math.PI;

            jd.ReferenceAngle = referenceAngle;

            Joint joint = Physics.GetWorld().CreateJoint(jd);

            return(joint);
        }
Exemplo n.º 22
0
        private TumblerTest()
        {
            Body ground;

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

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

                PolygonShape shape = new PolygonShape(5.0f);
                shape.SetAsBox(0.5f, 10.0f, new Vector2(10.0f, 0.0f), 0.0f);
                body.AddFixture(shape);
                shape.SetAsBox(0.5f, 10.0f, new Vector2(-10.0f, 0.0f), 0.0f);
                body.AddFixture(shape);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, 10.0f), 0.0f);
                body.AddFixture(shape);
                shape.SetAsBox(10.0f, 0.5f, new Vector2(0.0f, -10.0f), 0.0f);
                body.AddFixture(shape);

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.BodyA          = ground;
                jd.BodyB          = body;
                jd.LocalAnchorA   = new Vector2(0.0f, 10.0f);
                jd.LocalAnchorB   = new Vector2(0.0f, 0.0f);
                jd.ReferenceAngle = 0.0f;
                jd.MotorSpeed     = 0.05f * MathConstants.Pi;
                jd.MaxMotorTorque = 1e8f;
                jd.EnableMotor    = true;
                _joint            = (RevoluteJoint)JointFactory.CreateFromDef(World, jd);
            }

            _count2 = 0;
        }
Exemplo n.º 23
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -10.0f);
                ground = _world.CreateBody(bd);

                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 10.0f);
                ground.CreateShape(sd);
            }

            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.6f, 0.125f);
                sd.Density  = 20.0f;
                sd.Friction = 0.2f;

                RevoluteJointDef jd = new RevoluteJointDef();
                jd.CollideConnected = false;

                const float y        = 25.0f;
                Body        prevBody = ground;
                for (int i = 0; i < 30; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(0.5f + i, y);
                    Body body = _world.CreateBody(bd);
                    body.CreateShape(sd);
                    body.SetMassFromShapes();

                    Vec2 anchor = new Vec2(i, y);
                    jd.Initialize(prevBody, body, anchor);
                    _world.CreateJoint(jd);

                    prevBody = body;
                }
            }
        }
        private MobileBalancedTest()
        {
            Body ground;

            // Create ground body.
            {
                BodyDef bodyDef = new BodyDef();
                bodyDef.Position = new Vector2(0.0f, 20.0f);
                ground           = BodyFactory.CreateFromDef(World, bodyDef);
            }

            float   a = 0.5f;
            Vector2 h = new Vector2(0.0f, a);

            Body root = AddNode(ground, Vector2.Zero, 0, 3.0f, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.BodyA        = ground;
            jointDef.BodyB        = root;
            jointDef.LocalAnchorA = Vector2.Zero;
            jointDef.LocalAnchorB = h;
            JointFactory.CreateFromDef(World, jointDef);
        }
Exemplo n.º 25
0
        public Mobile()
        {
            Body ground;

            // Create ground body.
            {
                var bodyDef = new BodyDef();
                bodyDef.Position.Set(0.0f, 20.0f);
                ground = World.CreateBody(bodyDef);
            }

            var a = 0.5f;
            var h = new Vector2(0.0f, a);

            var root = AddNode(ground, Vector2.Zero, 0, 3.0f, a);

            var jointDef = new RevoluteJointDef();

            jointDef.BodyA = ground;
            jointDef.BodyB = root;
            jointDef.LocalAnchorA.SetZero();
            jointDef.LocalAnchorB = h;
            World.CreateJoint(jointDef);
        }
Exemplo n.º 26
0
        public MobileBalanced()
        {
            Body ground;

            // Create ground body.
            {
                BodyDef bodyDef = new BodyDef();
                bodyDef.Position.Set(0.0f, 20.0f);
                ground = m_world.CreateBody(bodyDef);
            }

            float a = 0.5f;
            Vec2  h = new Vec2(0.0f, a);

            Body root = AddNode(ground, new Vec2(0, 0), 0, 3.0f, a);

            RevoluteJointDef jointDef = new RevoluteJointDef();

            jointDef.bodyA = ground;
            jointDef.bodyB = root;
            jointDef.localAnchorA.SetZero();
            jointDef.localAnchorB = h;
            m_world.CreateJoint(jointDef);
        }
Exemplo n.º 27
0
        public BodyTypes()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 3.0f);
                _attachment = _world.CreateBody(bd);

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

            // Define platform
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-4.0f, 5.0f);
                _platform = _world.CreateBody(bd);

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

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.friction = 0.6f;
                fd.density = 2.0f;
                _platform.CreateFixture(fd);

                RevoluteJointDef rjd = new RevoluteJointDef();
                rjd.Initialize(_attachment, _platform, new Vector2(0.0f, 5.0f));
                rjd.maxMotorTorque = 50.0f;
                rjd.enableMotor = true;
                _world.CreateJoint(rjd);

                PrismaticJointDef pjd = new PrismaticJointDef();
                pjd.Initialize(ground, _platform, new Vector2(0.0f, 5.0f), new Vector2(1.0f, 0.0f));

                pjd.maxMotorForce = 1000.0f;
                pjd.enableMotor = true;
                pjd.lowerTranslation = -10.0f;
                pjd.upperTranslation = 10.0f;
                pjd.enableLimit = true;

                _world.CreateJoint(pjd);

                _speed = 3.0f;
            }

            // Create a payload
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(0.0f, 8.0f);
                Body body = _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;

                body.CreateFixture(fd);
            }
        }
Exemplo n.º 28
0
        public Revolute()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

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

                //fd.filter.categoryBits = 2;

                ground.CreateFixture(fd);
            }

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                var rjd = new RevoluteJointDef();

                bd.Position.Set(-10.0f, 20.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                var w = 100.0f;
                body.SetAngularVelocity(w);
                body.SetLinearVelocity(new Vector2(-8.0f * w, 0.0f));

                rjd.Initialize(ground, body, new Vector2(-10.0f, 12.0f));
                rjd.MotorSpeed       = 1.0f * Settings.Pi;
                rjd.MaxMotorTorque   = 10000.0f;
                rjd.EnableMotor      = false;
                rjd.LowerAngle       = -0.25f * Settings.Pi;
                rjd.UpperAngle       = 0.5f * Settings.Pi;
                rjd.EnableLimit      = true;
                rjd.CollideConnected = true;

                m_joint = (RevoluteJoint)World.CreateJoint(rjd);
            }

            {
                var circle_shape = new CircleShape();
                circle_shape.Radius = 3.0f;

                var circle_bd = new BodyDef();
                circle_bd.BodyType = BodyType.DynamicBody;
                circle_bd.Position.Set(5.0f, 30.0f);

                var fd = new FixtureDef();
                fd.Density = 5.0f;
                var filter = fd.Filter;
                filter.MaskBits = 1;
                fd.Filter       = filter;
                fd.Shape        = circle_shape;

                m_ball = World.CreateBody(circle_bd);
                m_ball.CreateFixture(fd);

                var polygon_shape = new PolygonShape();
                polygon_shape.SetAsBox(10.0f, 0.2f, new Vector2(-10.0f, 0.0f), 0.0f);

                var polygon_bd = new BodyDef();
                polygon_bd.Position.Set(20.0f, 10.0f);
                polygon_bd.BodyType = BodyType.DynamicBody;
                polygon_bd.Bullet   = true;
                var polygon_body = World.CreateBody(polygon_bd);
                polygon_body.CreateFixture(polygon_shape, 2.0f);

                var rjd = new RevoluteJointDef();
                rjd.Initialize(ground, polygon_body, new Vector2(20.0f, 10.0f));
                rjd.LowerAngle  = -0.25f * Settings.Pi;
                rjd.UpperAngle  = 0.0f * Settings.Pi;
                rjd.EnableLimit = true;
                World.CreateJoint(rjd);
            }

            // Tests mass computation of a small object far from the origin
            {
                var bodyDef = new BodyDef();
                bodyDef.BodyType = BodyType.DynamicBody;
                var body = World.CreateBody(bodyDef);

                var polyShape = new PolygonShape();
                var verts     = new Vector2[3];
                verts[0].Set(17.63f, 36.31f);
                verts[1].Set(17.52f, 36.69f);
                verts[2].Set(17.19f, 36.36f);
                polyShape.Set(verts);

                var polyFixtureDef = new FixtureDef();
                polyFixtureDef.Shape   = polyShape;
                polyFixtureDef.Density = 1;

                body.CreateFixture(polyFixtureDef); //assertion hits inside here
            }
        }
Exemplo n.º 29
0
        public SliderCrank()
        {
            Body ground = null;
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 10.0f);

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, -10.0f);
                ground = _world.CreateBody(bd);
                ground.CreateFixture(sd);
            }

            {
                // Define crank.
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.5f, 2.0f);
                sd.Density = 1.0f;

                RevoluteJointDef rjd = new RevoluteJointDef();

                Body prevBody = ground;

                BodyDef bd = new BodyDef();
                bd.Position.Set(0.0f, 7.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                body.SetMassFromShapes();

                rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f));
                rjd.MotorSpeed     = 1.0f * Box2DNet.Common.Settings.Pi;
                rjd.MaxMotorTorque = 10000.0f;
                rjd.EnableMotor    = true;
                _joint1            = (RevoluteJoint)_world.CreateJoint(rjd);

                prevBody = body;

                // Define follower.
                sd.SetAsBox(0.5f, 4.0f);
                bd.Position.Set(0.0f, 13.0f);
                body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                body.SetMassFromShapes();

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

                prevBody = body;

                // Define piston
                sd.SetAsBox(1.5f, 1.5f);
                bd.Position.Set(0.0f, 17.0f);
                body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                body.SetMassFromShapes();

                rjd.Initialize(prevBody, body, new Vec2(0.0f, 17.0f));
                _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;

                _joint2 = (PrismaticJoint)_world.CreateJoint(pjd);

                // Create a payload
                sd.Density = 2.0f;
                bd.Position.Set(0.0f, 23.0f);
                body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                body.SetMassFromShapes();
            }
        }
Exemplo n.º 30
0
        public Bridge()
        {
            Body ground;

            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

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

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

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

                var jd = new RevoluteJointDef();

                var prevBody = ground;
                for (var i = 0; i < Count; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(-14.5f + 1.0f * i, 5.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

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

                    if (i == Count >> 1)
                    {
                        _middle = body;
                    }

                    prevBody = body;
                }

                {
                    var anchor = new Vector2(-15.0f + 1.0f * Count, 5.0f);
                    jd.Initialize(prevBody, ground, anchor);
                    World.CreateJoint(jd);
                }
            }

            for (var i = 0; i < 2; ++i)
            {
                var vertices = new Vector2[3];
                vertices[0].Set(-0.5f, 0.0f);
                vertices[1].Set(0.5f, 0.0f);
                vertices[2].Set(0.0f, 1.5f);

                var shape = new PolygonShape();
                shape.Set(vertices);

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

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

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
                var body = World.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
Exemplo n.º 31
0
        public Bridge()
        {
            Body ground = null;
		    {
			    BodyDef bd = new BodyDef();
			    ground = _world.CreateBody(bd);

			    PolygonShape shape = new PolygonShape();
			    shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(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.type = BodyType.Dynamic;
				    bd.position = new Vector2(-14.5f + 1.0f * i, 5.0f);
				    Body body = _world.CreateBody(bd);
				    body.CreateFixture(fd);

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

                    if (i == (e_count >> 1))
                    {
                        _middle = body;
                    }

				    prevBody = body;
			    }

                Vector2 anchor2 = new Vector2(-15.0f + 1.0f * e_count, 5.0f);
			    jd.Initialize(prevBody, ground, anchor2);
			    _world.CreateJoint(jd);
		    }

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

                PolygonShape shape = new PolygonShape();
			    shape.Set(vertices, 3);

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 1.0f;

                BodyDef bd = new BodyDef();
		        bd.type = BodyType.Dynamic;
			    bd.position = new Vector2(-8.0f + 8.0f * i, 12.0f);
			    Body body = _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.type = BodyType.Dynamic;
			    bd.position = new Vector2(-6.0f + 6.0f * i, 10.0f);
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(fd);
		    }
        }
Exemplo n.º 32
0
        Gears()
	    {
		    Body ground = null;
		    {
			    BodyDef bd = new BodyDef();
			    ground = _world.CreateBody(bd);

			    PolygonShape shape = new PolygonShape();
			    shape.SetAsEdge(new Vector2(50.0f, 0.0f), new Vector2(-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.type = BodyType.Dynamic;
			    bd1.position = new Vector2(-3.0f, 12.0f);
			    Body body1 = _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.GetAngle() - ground.GetAngle();
			    _joint1 = (RevoluteJoint)_world.CreateJoint(jd1);

                BodyDef bd2 = new BodyDef();
                bd2.type = BodyType.Dynamic;
			    bd2.position = new Vector2(0.0f, 12.0f);
			    Body body2 = _world.CreateBody(bd2);
			    body2.CreateFixture(circle2, 5.0f);

                RevoluteJointDef jd2 = new RevoluteJointDef();
			    jd2.Initialize(ground, body2, bd2.position);
			    _joint2 = (RevoluteJoint)_world.CreateJoint(jd2);

                BodyDef bd3 = new BodyDef();
                bd3.type = BodyType.Dynamic;
			    bd3.position = new Vector2(2.5f, 12.0f);
			    Body body3 = _world.CreateBody(bd3);
			    body3.CreateFixture(box, 5.0f);

                PrismaticJointDef jd3 = new PrismaticJointDef();
			    jd3.Initialize(ground, body3, bd3.position, new Vector2(0.0f, 1.0f));
			    jd3.lowerTranslation = -5.0f;
			    jd3.upperTranslation = 5.0f;
			    jd3.enableLimit = true;

			    _joint3 = (PrismaticJoint)_world.CreateJoint(jd3);

                GearJointDef jd4 = new GearJointDef();
			    jd4.bodyA = body1;
			    jd4.bodyB = body2;
			    jd4.joint1 = _joint1;
			    jd4.joint2 = _joint2;
			    jd4.ratio = circle2._radius / circle1._radius;
			    _joint4 = (GearJoint)_world.CreateJoint(jd4);

                GearJointDef jd5 = new GearJointDef();
			    jd5.bodyA = body2;
			    jd5.bodyB = body3;
			    jd5.joint1 = _joint2;
			    jd5.joint2 = _joint3;
			    jd5.ratio = -1.0f / circle2._radius;
			    _joint5 = (GearJoint)_world.CreateJoint(jd5);
		    }
	    }
Exemplo n.º 33
0
        /// <summary>
        /// Resets the bike and the driver to their initial positions
        /// Resets all angular and linear speeds
        /// Recreates the joints if they were destroyed
        /// </summary>
        public void Reset()
        {
            foreach (var part in parts)
            {
                part.SetAngularVelocity(0);
                part.SetLinearVelocity(Vector2.Zero);
            }

            frontWheel.SetTransform(frontWheelInitPos / Level.FACTOR, 0);
            frontFork.SetTransform(frontForkInitPos / Level.FACTOR, 0);
            rearWheel.SetTransform(rearWheelInitPos / Level.FACTOR, 0);
            rearFork.SetTransform(rearForkInitPos / Level.FACTOR,
                                  rearForkInitRot * Level.DEG_TO_RAD);
            bikeBody.SetTransform(bikeBodyInitPos / Level.FACTOR,
                                  bikeBodyInitRot * Level.DEG_TO_RAD);

            head.SetTransform(headInitPos / Level.FACTOR, headInitRot * Level.DEG_TO_RAD);
            humanBody.SetTransform(humanBodyInitPos / Level.FACTOR, 0);
            hand.SetTransform(handInitPos / Level.FACTOR, handInitRot * Level.DEG_TO_RAD);
            arm.SetTransform(armInitPos / Level.FACTOR, armInitRot * Level.DEG_TO_RAD);

            if (OffTheBike)
            {
                RevoluteJointDef handToBikeDef = new RevoluteJointDef();
                Vector2          anchor        = hand.GetWorldPoint(new Vector2(17.06f / Level.FACTOR,
                                                                                4.26f / Level.FACTOR));
                handToBikeDef.Initialize(hand, bikeBody, anchor);
                handToBikeDef.maxMotorTorque = 300.0f;
                handToBike = world.CreateJoint(handToBikeDef);

                RevoluteJointDef humanToBikeDef = new RevoluteJointDef();
                anchor    = humanBody.GetWorldCenter();
                anchor.Y += (30.0f / Level.FACTOR);
                humanToBikeDef.Initialize(humanBody, bikeBody, anchor);
                humanToBikeDef.maxMotorTorque = 300.0f;
                humanToBike = world.CreateJoint(humanToBikeDef);

                DistanceJointDef armToBikeDef = new DistanceJointDef();
                armToBikeDef.Initialize(hand, bikeBody,
                                        hand.GetWorldPoint(new Vector2(-17.00f / Level.FACTOR,
                                                                       4.26f / Level.FACTOR)),
                                        bikeBody.GetWorldCenter());
                armToBikeDef.length           = 40.0f / Level.FACTOR;
                armToBikeDef.frequencyHz      = 10.0f;
                armToBikeDef.dampingRatio     = 1.0f;
                armToBikeDef.collideConnected = true;
                armToBike = world.CreateJoint(armToBikeDef);

                OffTheBike = false;
            }

            if (!bikeBody.IsAwake())
            {
                bikeBody.SetAwake(true);
            }

            if (RotationData.device)
            {
                motor._enableMotor = true;
            }
        }
Exemplo n.º 34
0
        TheoJansen()
        {
            _offset = new Vector2(0.0f, 8.0f);
            _motorSpeed = 2.0f;
            _motorOn = true;
            Vector2 pivot = new Vector2(0.0f, 0.8f);

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

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

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

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

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-40.0f + 2.0f * i, 0.5f);

                Body body = _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.type = BodyType.Dynamic;
                bd.position = pivot + _offset;
                _chassis = _world.CreateBody(bd);
                _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.type = BodyType.Dynamic;
                bd.position = pivot + _offset;
                _wheel = _world.CreateBody(bd);
                _wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(_wheel, _chassis, pivot + _offset);
                jd.collideConnected = false;
                jd.motorSpeed = _motorSpeed;
                jd.maxMotorTorque = 400.0f;
                jd.enableMotor = _motorOn;
                _motorJoint = (RevoluteJoint)_world.CreateJoint(jd);
            }

            Vector2 wheelAnchor;

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

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

            _wheel.SetTransform(_wheel.GetPosition(), 120.0f * (float)Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.GetPosition(), -120.0f * (float)Settings.b2_pi / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
Exemplo n.º 35
0
        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();
            FixtureDef fd2 = new FixtureDef();

            fd1.filter.groupIndex = -1;
            fd2.filter.groupIndex = -1;
            fd1.density = 1.0f;
            fd2.density = 1.0f;

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

            Vector2[] vertices = new Vector2[3];

            if (s > 0.0f)
            {
                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Set(vertices, 3);

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

                vertices[0] = Vector2.Zero;
                vertices[1] = p6 - p4;
                vertices[2] = p5 - p4;
                poly2.Set(vertices, 3);
            }

            fd1.shape = poly1;
            fd2.shape = poly2;

            BodyDef bd1 = new BodyDef();
            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 = _world.CreateBody(bd1);
            Body body2 = _world.CreateBody(bd2);

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

            DistanceJointDef djd = new DistanceJointDef();

            // Using a soft distanceraint 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 + _offset, p5 + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body1, body2, p3 + _offset, p4 + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset);
            _world.CreateJoint(djd);

            djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset);
            _world.CreateJoint(djd);

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.Initialize(body2, _chassis, p4 + _offset);
            _world.CreateJoint(rjd);
        }
Exemplo n.º 36
0
        public Dominos()
        {
            Body b1 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                b1 = _world.CreateBody(bd);
                b1.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(-1.5f, 10.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                FixtureDef fd = new FixtureDef();
                fd.shape = shape;
                fd.density = 20.0f;
                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 = _world.CreateBody(bd);
                    body.CreateFixture(fd);
                }
            }

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

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(1.0f, 6.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

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

                BodyDef bd = new BodyDef();
                bd.position = new Vector2(-7.0f, 4.0f);
                b2 = _world.CreateBody(bd);
                b2.CreateFixture(shape, 0.0f);
            }

            Body b3 = null;
            {
                PolygonShape shape = new PolygonShape();
                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 = _world.CreateBody(bd);
                b3.CreateFixture(shape, 10.0f);
            }

            RevoluteJointDef jd = new RevoluteJointDef();
            Vector2 anchor;

            anchor = new Vector2(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.collideConnected = true;
            _world.CreateJoint(jd);

            Body b4 = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.25f, 0.25f);

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(-10.0f, 15.0f);
                b4 = _world.CreateBody(bd);
                b4.CreateFixture(shape, 10.0f);
            }

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

            Body b5 = null;
            {
                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(6.5f, 3.0f);
                b5 = _world.CreateBody(bd);

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

                fd.shape = shape;
                fd.density = 10.0f;
                fd.friction = 0.1f;

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

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

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

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

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(6.5f, 4.1f);
                b6 = _world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
            }

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

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

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
                bd.position = new Vector2(7.4f, 1.0f);

                b7 = _world.CreateBody(bd);
                b7.CreateFixture(shape, 10.0f);
            }

            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();
            _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.type = BodyType.Dynamic;
                    bd.position = new Vector2(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                }
            }
        }
Exemplo n.º 37
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(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.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 7.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 5.0f));
                    rjd.motorSpeed = 1.0f * (float)Settings.b2_pi;
                    rjd.maxMotorTorque = 10000.0f;
                    rjd.enableMotor = true;
                    _joint1 = (RevoluteJoint)_world.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 13.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd3 = new RevoluteJointDef();
                    rjd3.Initialize(prevBody, body, new Vector2(0.0f, 9.0f));
                    rjd3.enableMotor = false;
                    _world.CreateJoint(rjd3);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 17.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    RevoluteJointDef rjd2 = new RevoluteJointDef();
                    rjd2.Initialize(prevBody, body, new Vector2(0.0f, 17.0f));
                    _world.CreateJoint(rjd2);

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

                    pjd.maxMotorForce = 1000.0f;
                    pjd.enableMotor = true;

                    _joint2 = (PrismaticJoint)_world.CreateJoint(pjd);
                }

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType.Dynamic;
                    bd.position = new Vector2(0.0f, 23.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }
Exemplo n.º 38
0
        public GearJoint()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.SetTwoSided(new Vector2(50.0f, 0.0f), new Vector2(-50.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

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

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

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

                var bd1 = new BodyDef();
                bd1.BodyType = BodyType.StaticBody;
                bd1.Position.Set(10.0f, 9.0f);
                var body1 = World.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                var bd2 = new BodyDef();
                bd2.BodyType = BodyType.DynamicBody;
                bd2.Position.Set(10.0f, 8.0f);
                var body2 = World.CreateBody(bd2);
                body2.CreateFixture(box, 5.0f);

                var bd3 = new BodyDef();
                bd3.BodyType = BodyType.DynamicBody;
                bd3.Position.Set(10.0f, 6.0f);
                var body3 = World.CreateBody(bd3);
                body3.CreateFixture(circle2, 5.0f);

                var jd1 = new RevoluteJointDef();
                jd1.Initialize(body1, body2, bd1.Position);
                var joint1 = World.CreateJoint(jd1);

                var jd2 = new RevoluteJointDef();
                jd2.Initialize(body2, body3, bd3.Position);
                var joint2 = World.CreateJoint(jd2);

                var jd4 = new GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body3;
                jd4.Joint1 = joint1;
                jd4.Joint2 = joint2;
                jd4.Ratio  = circle2.Radius / circle1.Radius;
                World.CreateJoint(jd4);
            }

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

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

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

                var bd1 = new BodyDef();
                bd1.BodyType = BodyType.DynamicBody;
                bd1.Position.Set(-3.0f, 12.0f);
                var body1 = World.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                var jd1 = new RevoluteJointDef();
                jd1.BodyA          = ground;
                jd1.BodyB          = body1;
                jd1.LocalAnchorA   = ground.GetLocalPoint(bd1.Position);
                jd1.LocalAnchorB   = body1.GetLocalPoint(bd1.Position);
                jd1.ReferenceAngle = body1.GetAngle() - ground.GetAngle();
                _joint1            = (RevoluteJoint)World.CreateJoint(jd1);

                var bd2 = new BodyDef();
                bd2.BodyType = BodyType.DynamicBody;
                bd2.Position.Set(0.0f, 12.0f);
                var body2 = World.CreateBody(bd2);
                body2.CreateFixture(circle2, 5.0f);

                var jd2 = new RevoluteJointDef();
                jd2.Initialize(ground, body2, bd2.Position);
                _joint2 = (RevoluteJoint)World.CreateJoint(jd2);

                var bd3 = new BodyDef();
                bd3.BodyType = BodyType.DynamicBody;
                bd3.Position.Set(2.5f, 12.0f);
                var body3 = World.CreateBody(bd3);
                body3.CreateFixture(box, 5.0f);

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

                _joint3 = (PrismaticJoint)World.CreateJoint(jd3);

                var jd4 = new GearJointDef();
                jd4.BodyA  = body1;
                jd4.BodyB  = body2;
                jd4.Joint1 = _joint1;
                jd4.Joint2 = _joint2;
                jd4.Ratio  = circle2.Radius / circle1.Radius;
                _joint4    = (Box2DSharp.Dynamics.Joints.GearJoint)World.CreateJoint(jd4);

                var jd5 = new GearJointDef();
                jd5.BodyA  = body2;
                jd5.BodyB  = body3;
                jd5.Joint1 = _joint2;
                jd5.Joint2 = _joint3;
                jd5.Ratio  = -1.0f / circle2.Radius;
                _joint5    = (Box2DSharp.Dynamics.Joints.GearJoint)World.CreateJoint(jd5);
            }
        }
Exemplo n.º 39
0
        protected override void Create()
        {
            _hz    = 4.0f;
            _zeta  = 0.7f;
            _speed = 50.0f;

            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();

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

                shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));
                ground.CreateFixture(fd);

                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, dx = 5.0f;

                for (var i = 0; i < 10; ++i)
                {
                    var y2 = hs[i];
                    shape.Set(new Vector2(x, y1), new Vector2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                for (var i = 0; i < 10; ++i)
                {
                    var y2 = hs[i];
                    shape.Set(new Vector2(x, y1), new Vector2(x + dx, y2));
                    ground.CreateFixture(fd);
                    y1 = y2;
                    x += dx;
                }

                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 80.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 10.0f, 5.0f));
                ground.CreateFixture(fd);

                x += 20.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x + 40.0f, 0.0f));
                ground.CreateFixture(fd);

                x += 40.0f;
                shape.Set(new Vector2(x, 0.0f), new Vector2(x, 20.0f));
                ground.CreateFixture(fd);
            }

            // Teeter
            {
                var bd = new BodyDef();
                bd.Position.Set(140.0f, 1.0f);
                bd.BodyType = BodyType.DynamicBody;
                var body = World.CreateBody(bd);

                var box = new PolygonShape();
                box.SetAsBox(10.0f, 0.25f);
                body.CreateFixture(box, 1.0f);

                var jd = new RevoluteJointDef();
                jd.Initialize(ground, body, body.GetPosition());
                jd.LowerAngle  = -8.0f * Settings.Pi / 180.0f;
                jd.UpperAngle  = 8.0f * Settings.Pi / 180.0f;
                jd.EnableLimit = true;
                World.CreateJoint(jd);

                body.ApplyAngularImpulse(100.0f, true);
            }

            // Bridge
            {
                var N     = 20;
                var shape = new PolygonShape();
                shape.SetAsBox(1.0f, 0.125f);

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

                var jd = new RevoluteJointDef();

                var prevBody = ground;
                for (var i = 0; i < N; ++i)
                {
                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position.Set(161.0f + 2.0f * i, -0.125f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(fd);

                    var anchor = new Vector2(160.0f + 2.0f * i, -0.125f);
                    jd.Initialize(prevBody, body, anchor);
                    World.CreateJoint(jd);

                    prevBody = body;
                }

                {
                    var anchor = new Vector2(160.0f + 2.0f * N, -0.125f);
                    jd.Initialize(prevBody, ground, anchor);
                    World.CreateJoint(jd);
                }
            }

            // Boxes
            {
                var box = new PolygonShape();
                box.SetAsBox(0.5f, 0.5f);

                Body body = null;
                var  bd   = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;

                bd.Position.Set(230.0f, 0.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 1.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 2.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 3.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);

                bd.Position.Set(230.0f, 4.5f);
                body = World.CreateBody(bd);
                body.CreateFixture(box, 0.5f);
            }

            // Car
            {
                var chassis  = new PolygonShape();
                var vertices = new Vector2[8];
                vertices[0].Set(-1.5f, -0.5f);
                vertices[1].Set(1.5f, -0.5f);
                vertices[2].Set(1.5f, 0.0f);
                vertices[3].Set(0.0f, 0.9f);
                vertices[4].Set(-1.15f, 0.9f);
                vertices[5].Set(-1.5f, 0.2f);
                chassis.Set(vertices);

                var circle = new CircleShape();
                circle.Radius = 0.4f;

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 1.0f);
                _car = World.CreateBody(bd);
                _car.CreateFixture(chassis, 1.0f);

                var fd = new FixtureDef();
                fd.Shape    = circle;
                fd.Density  = 1.0f;
                fd.Friction = 0.9f;

                bd.Position.Set(-1.0f, 0.35f);
                _wheel1 = World.CreateBody(bd);
                _wheel1.CreateFixture(fd);

                bd.Position.Set(1.0f, 0.4f);
                _wheel2 = World.CreateBody(bd);
                _wheel2.CreateFixture(fd);

                var jd   = new WheelJointDef();
                var axis = new Vector2(0.0f, 1.0f);

                jd.Initialize(_car, _wheel1, _wheel1.GetPosition(), axis);
                jd.MotorSpeed     = 0.0f;
                jd.MaxMotorTorque = 20.0f;
                jd.EnableMotor    = true;
                jd.FrequencyHz    = _hz;
                jd.DampingRatio   = _zeta;
                _spring1          = (WheelJoint)World.CreateJoint(jd);

                jd.Initialize(_car, _wheel2, _wheel2.GetPosition(), axis);
                jd.MotorSpeed     = 0.0f;
                jd.MaxMotorTorque = 10.0f;
                jd.EnableMotor    = false;
                jd.FrequencyHz    = _hz;
                jd.DampingRatio   = _zeta;
                _spring2          = (WheelJoint)World.CreateJoint(jd);
            }
        }
Exemplo n.º 40
0
        /// <summary>
        /// Creates a new motorcycle and a driver into the given Box2D world.
        /// Creates all the parts of the motorcycle and driver and joints them together.
        /// </summary>
        /// <param name="pBikeSpeed">A pointer to the variable that describes the speed of the
        ///                          motorcycle</param>
        /// <param name="pRotationData">RotationData to provide the information of the rotation
        ///                             of the device</param>
        /// <param name="pWorld">The Box2D world where the bike is created into</param>
        /// <param name="pCamPos">A pointer to the variable that describes the position of the
        ///                       camera</param>
        /// <param name="pContent">The used ContentManager instance</param>
        /// <param name="pSpriteBatch">The used SpriteBatch instance</param>
        public Bike(float [] pBikeSpeed, RotationData pRotationData, World pWorld, float[] pCamPos,
                    ContentManager pContent)
        {
            OffTheBike   = false;
            camPos       = pCamPos;
            world        = pWorld;
            content      = pContent;
            RotationData = pRotationData;

            bikeSpeed = pBikeSpeed;

            frontWheel = CreateCirclePart("wheel", frontWheelInitPos, 35.0f, 0, 0.1f, 0.9f, 0.2f);
            frontFork  = CreateBoxPart("susp_lower_long", frontForkInitPos, 20.53f, 21.33f, 0, 0.8f,
                                       1.0f, 0.2f);
            rearWheel = CreateCirclePart("rearWheel", rearWheelInitPos, 32.0f, 0, 0.4f, 1.0f,
                                         0.2f);
            rearFork = CreateBoxPart("rearFork", rearForkInitPos, 64.0f, 17.0f, rearForkInitRot,
                                     0.5f, 1.0f, 0.2f);
            bikeBody = CreateBikeBody(bikeBodyInitPos, bikeBodyInitRot, 0.5f, 1.0f, 0.2f);

            RevoluteJointDef motorDef = new RevoluteJointDef();

            motorDef.Initialize(rearWheel, rearFork, rearWheel.GetWorldCenter());
            motorDef.maxMotorTorque = 2.0f;
            motorDef.enableMotor    = true;
            motor = (RevoluteJoint)world.CreateJoint(motorDef);

            RevoluteJointDef rearForkBodyDef = new RevoluteJointDef();
            Vector2          anchor          = rearFork.GetWorldCenter();

            anchor.X += (32.0f / Level.FACTOR);
            anchor.Y += (13.5f / Level.FACTOR);
            rearForkBodyDef.Initialize(rearFork, bikeBody, anchor);
            rearForkBodyDef.bodyA          = rearFork;
            rearForkBodyDef.bodyB          = bikeBody;
            rearForkBodyDef.maxMotorTorque = 300.0f;
            world.CreateJoint(rearForkBodyDef);

            RevoluteJointDef frontWheelJointDef = new RevoluteJointDef();

            frontWheelJointDef.Initialize(frontWheel, frontFork, frontWheel.GetWorldCenter());
            frontWheelJointDef.maxMotorTorque = 300.0f;
            world.CreateJoint(frontWheelJointDef);

            DistanceJointDef frontSuspToBikeDef = new DistanceJointDef();

            frontSuspToBikeDef.Initialize(bikeBody, frontFork,
                                          frontFork.GetWorldCenter() + new Vector2(0, 0.4f),
                                          frontFork.GetWorldCenter());
            frontSuspToBikeDef.frequencyHz      = 4.0f;
            frontSuspToBikeDef.dampingRatio     = 0.1f;
            frontSuspToBikeDef.collideConnected = true;
            world.CreateJoint(frontSuspToBikeDef);

            DistanceJointDef rearForkDistanceDef = new DistanceJointDef();

            rearForkDistanceDef.Initialize(bikeBody, rearFork,
                                           rearFork.GetWorldCenter() + new Vector2(0, 0.4f),
                                           rearFork.GetWorldCenter());
            rearForkDistanceDef.frequencyHz      = 7.0f;
            rearForkDistanceDef.dampingRatio     = 0.1f;
            rearForkDistanceDef.collideConnected = true;
            world.CreateJoint(rearForkDistanceDef);

            PrismaticJointDef fSuspBikePrismaticDef = new PrismaticJointDef();

            fSuspBikePrismaticDef.Initialize(bikeBody, frontFork, bikeBody.GetWorldCenter(),
                                             new Vector2(0, 1));
            fSuspBikePrismaticDef.enableLimit      = true;
            fSuspBikePrismaticDef.lowerTranslation = -0.2f;
            fSuspBikePrismaticDef.upperTranslation = 0.2f;
            fSuspBikePrismaticDef.collideConnected = true;
            world.CreateJoint(fSuspBikePrismaticDef);

            humanBody = CreateBoxPart("human", humanBodyInitPos, 17.0f, 64.0f, 0, 0.1f, 1.0f,
                                      0.2f);
            head = CreateBoxPart("head", headInitPos, 38.4f, 29.9f, headInitRot, 0.1f, 1.0f,
                                 0.2f);
            hand = CreateBoxPart("hand", handInitPos, 34.13f, 8.53f, handInitRot, 0.1f, 1.0f,
                                 0.2f);
            arm = CreateBoxPart("arm", armInitPos, 42.67f, 8.53f, armInitRot, 0.1f, 1.0f, 0.2f);

            WeldJointDef headToHumanDef = new WeldJointDef();

            headToHumanDef.Initialize(head, humanBody, head.GetWorldCenter());
            world.CreateJoint(headToHumanDef);

            RevoluteJointDef humanToBikeDef = new RevoluteJointDef();

            anchor    = humanBody.GetWorldCenter();
            anchor.Y += (30.0f / Level.FACTOR);
            humanToBikeDef.Initialize(humanBody, bikeBody, anchor);
            humanToBikeDef.maxMotorTorque = 300.0f;
            humanToBike = world.CreateJoint(humanToBikeDef);

            RevoluteJointDef humanToArmDef = new RevoluteJointDef();

            anchor = arm.GetWorldPoint(new Vector2(-21.33f / Level.FACTOR, 4.26f / Level.FACTOR));
            humanToArmDef.Initialize(humanBody, arm, anchor);
            humanToArmDef.maxMotorTorque = 300.0f;
            world.CreateJoint(humanToArmDef);

            RevoluteJointDef armToHandDef = new RevoluteJointDef();

            anchor = arm.GetWorldPoint(new Vector2(21.33f / Level.FACTOR, 4.26f / Level.FACTOR));
            armToHandDef.Initialize(arm, hand, anchor);
            armToHandDef.maxMotorTorque = 300.0f;
            world.CreateJoint(armToHandDef);

            RevoluteJointDef handToBikeDef = new RevoluteJointDef();

            anchor = hand.GetWorldPoint(new Vector2(17.06f / Level.FACTOR, 4.26f / Level.FACTOR));
            handToBikeDef.Initialize(hand, bikeBody, anchor);
            handToBikeDef.maxMotorTorque = 300.0f;
            handToBike = world.CreateJoint(handToBikeDef);

            DistanceJointDef armToBikeDef = new DistanceJointDef();

            armToBikeDef.Initialize(hand, bikeBody,
                                    hand.GetWorldPoint(new Vector2(-17.00f / Level.FACTOR,
                                                                   4.26f / Level.FACTOR)),
                                    bikeBody.GetWorldCenter());
            armToBikeDef.length           = 40.0f / Level.FACTOR;
            armToBikeDef.frequencyHz      = 10.0f;
            armToBikeDef.dampingRatio     = 1.0f;
            armToBikeDef.collideConnected = true;
            armToBike = world.CreateJoint(armToBikeDef);
        }
Exemplo n.º 41
0
        public WorldData Deserialize(Stream stream)
        {
            XMLFragmentElement root = XMLFragmentParser.LoadFromStream(stream);

            if (root.Name.ToLower() != "world")
                throw new Exception();

            WorldData data = new WorldData();

            if (root.Attributes.Count == 0)
                throw new Exception("No version");
            else if (int.Parse(root.Attributes[0].Value) != WorldXmlSerializer.XmlVersion)
                throw new Exception("Wrong version XML file");

            data.Version = int.Parse(root.Attributes[0].Value);

            foreach (var main in root.Elements)
            {
                switch (main.Name.ToLower())
                {
                case "gravity":
                    {
                        data.Gravity = ReadVector(main);
                    }
                    break;

                case "shapes":
                    {
                        foreach (var n in main.Elements)
                        {
                            if (n.Name.ToLower() != "shape")
                                throw new Exception();

                            ShapeType type = (ShapeType)Enum.Parse(typeof(ShapeType), n.Attributes[0].Value, true);
                            string name = "";

                            switch (type)
                            {
                            case ShapeType.Circle:
                                {
                                    CircleShape shape = new CircleShape();

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "radius":
                                            shape.Radius = float.Parse(sn.Value);
                                            break;
                                        case "position":
                                            shape.Position = ReadVector(sn);
                                            break;
                                        default:
                                            throw new Exception();
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            case ShapeType.Polygon:
                                {
                                    PolygonShape shape = new PolygonShape();

                                    foreach (var sn in n.Elements)
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "name":
                                            name = sn.Value;
                                            break;
                                        case "vertices":
                                            {
                                                List<Vec2> verts = new List<Vec2>();

                                                foreach (var vert in sn.Elements)
                                                    verts.Add(ReadVector(vert));

                                                shape.Vertices = verts.ToArray();
                                            }
                                            break;
                                        case "centroid":
                                            shape.Centroid = ReadVector(sn);
                                            break;
                                        }
                                    }

                                    _shapes.Add(new ShapeSerialized(shape, name));
                                }
                                break;
                            }
                        }
                    }
                    break;
                case "fixtures":
                    {
                        foreach (var n in main.Elements)
                        {
                            FixtureDef fixture = new FixtureDef();

                            if (n.Name.ToLower() != "fixture")
                                throw new Exception();

                            string name = "";
                            int id = 0;

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "shape":
                                    id = int.Parse(sn.Value);
                                    break;
                                case "density":
                                    fixture.Density = float.Parse(sn.Value);
                                    break;
                                case "filterdata":
                                    fixture.Filter = (FilterData)ReadSimpleType(sn, typeof(FilterData), true);
                                    break;
                                case "friction":
                                    fixture.Friction = float.Parse(sn.Value);
                                    break;
                                case "issensor":
                                    fixture.IsSensor = bool.Parse(sn.Value);
                                    break;
                                case "restitution":
                                    fixture.Restitution = float.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    fixture.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                }
                            }

                            fixture.Shape = _shapes[id].Shape;

                            _fixtures.Add(new FixtureDefSerialized(fixture, id, name));
                        }
                    }
                    break;
                case "bodies":
                    {
                        foreach (var n in main.Elements)
                        {
                            BodyDef body = new BodyDef();

                            if (n.Name.ToLower() != "body")
                                throw new Exception();

                            body.BodyType = (BodyType)Enum.Parse(typeof(BodyType), n.Attributes[0].Value, true);
                            List<int> fixtures = new List<int>();
                            string name = "";

                            foreach (var sn in n.Elements)
                            {
                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "active":
                                    body.Active = bool.Parse(sn.Value);
                                    break;
                                case "allowsleep":
                                    body.AllowSleep = bool.Parse(sn.Value);
                                    break;
                                case "angle":
                                    body.Angle = float.Parse(sn.Value);
                                    break;
                                case "angulardamping":
                                    body.AngularDamping = float.Parse(sn.Value);
                                    break;
                                case "angularvelocity":
                                    body.AngularVelocity = float.Parse(sn.Value);
                                    break;
                                case "awake":
                                    body.Awake = bool.Parse(sn.Value);
                                    break;
                                case "bullet":
                                    body.Bullet = bool.Parse(sn.Value);
                                    break;
                                case "fixedrotation":
                                    body.FixedRotation = bool.Parse(sn.Value);
                                    break;
                                case "inertiascale":
                                    body.InertiaScale = float.Parse(sn.Value);
                                    break;
                                case "lineardamping":
                                    body.LinearDamping = float.Parse(sn.Value);
                                    break;
                                case "linearvelocity":
                                    body.LinearVelocity = ReadVector(sn);
                                    break;
                                case "position":
                                    body.Position = ReadVector(sn);
                                    break;
                                case "userdata":
                                    body.UserData = ReadSimpleType(sn, null, false);
                                    break;
                                case "fixtures":
                                    {
                                        foreach (var v in sn.Elements)
                                            fixtures.Add(int.Parse(v.Value));
                                        break;
                                    }
                                }
                            }

                            _bodies.Add(new BodyDefSerialized(null, body, fixtures, name));
                        }
                    }
                    break;
                case "joints":
                    {
                        foreach (var n in main.Elements)
                        {
                            JointDef mainDef = null;

                            if (n.Name.ToLower() != "joint")
                                throw new Exception();

                            JointType type = (JointType)Enum.Parse(typeof(JointType), n.Attributes[0].Value, true);

                            int bodyA = -1, bodyB = -1;
                            bool collideConnected = false;
                            object userData = null;
                            string name = "";

                            switch (type)
                            {
                            case JointType.Distance:
                                mainDef = new DistanceJointDef();
                                break;
                            case JointType.Friction:
                                mainDef = new FrictionJointDef();
                                break;
                            case JointType.Line:
                                mainDef = new LineJointDef();
                                break;
                            case JointType.Prismatic:
                                mainDef = new PrismaticJointDef();
                                break;
                            case JointType.Pulley:
                                mainDef = new PulleyJointDef();
                                break;
                            case JointType.Revolute:
                                mainDef = new RevoluteJointDef();
                                break;
                            case JointType.Weld:
                                mainDef = new WeldJointDef();
                                break;
                            default:
                                throw new Exception("Invalid or unsupported joint");
                            }

                            foreach (var sn in n.Elements)
                            {
                                // check for specific nodes
                                switch (type)
                                {
                                case JointType.Distance:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "dampingratio":
                                            ((DistanceJointDef)mainDef).DampingRatio = float.Parse(sn.Value);
                                            break;
                                        case "frequencyhz":
                                            ((DistanceJointDef)mainDef).FrequencyHz = float.Parse(sn.Value);
                                            break;
                                        case "length":
                                            ((DistanceJointDef)mainDef).Length = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((DistanceJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((DistanceJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Friction:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((FrictionJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((FrictionJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxforce":
                                            ((FrictionJointDef)mainDef).MaxForce = float.Parse(sn.Value);
                                            break;
                                        case "maxtorque":
                                            ((FrictionJointDef)mainDef).MaxTorque = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Line:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((LineJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((LineJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((LineJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((LineJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((LineJointDef)mainDef).LocalAxisA = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((LineJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((LineJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((LineJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((LineJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Prismatic:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((PrismaticJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((PrismaticJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PrismaticJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PrismaticJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "localaxisa":
                                            ((PrismaticJointDef)mainDef).LocalAxis = ReadVector(sn);
                                            break;
                                        case "maxmotorforce":
                                            ((PrismaticJointDef)mainDef).MaxMotorForce = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((PrismaticJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowertranslation":
                                            ((PrismaticJointDef)mainDef).LowerTranslation = float.Parse(sn.Value);
                                            break;
                                        case "uppertranslation":
                                            ((PrismaticJointDef)mainDef).UpperTranslation = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((PrismaticJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Pulley:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "groundanchora":
                                            ((PulleyJointDef)mainDef).GroundAnchorA = ReadVector(sn);
                                            break;
                                        case "groundanchorb":
                                            ((PulleyJointDef)mainDef).GroundAnchorB = ReadVector(sn);
                                            break;
                                        case "lengtha":
                                            ((PulleyJointDef)mainDef).LengthA = float.Parse(sn.Value);
                                            break;
                                        case "lengthb":
                                            ((PulleyJointDef)mainDef).LengthB = float.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((PulleyJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((PulleyJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxlengtha":
                                            ((PulleyJointDef)mainDef).MaxLengthA = float.Parse(sn.Value);
                                            break;
                                        case "maxlengthb":
                                            ((PulleyJointDef)mainDef).MaxLengthB = float.Parse(sn.Value);
                                            break;
                                        case "ratio":
                                            ((PulleyJointDef)mainDef).Ratio = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Revolute:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "enablelimit":
                                            ((RevoluteJointDef)mainDef).EnableLimit = bool.Parse(sn.Value);
                                            break;
                                        case "enablemotor":
                                            ((RevoluteJointDef)mainDef).EnableMotor = bool.Parse(sn.Value);
                                            break;
                                        case "localanchora":
                                            ((RevoluteJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((RevoluteJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        case "maxmotortorque":
                                            ((RevoluteJointDef)mainDef).MaxMotorTorque = float.Parse(sn.Value);
                                            break;
                                        case "motorspeed":
                                            ((RevoluteJointDef)mainDef).MotorSpeed = float.Parse(sn.Value);
                                            break;
                                        case "lowerangle":
                                            ((RevoluteJointDef)mainDef).LowerAngle = float.Parse(sn.Value);
                                            break;
                                        case "upperangle":
                                            ((RevoluteJointDef)mainDef).UpperAngle = float.Parse(sn.Value);
                                            break;
                                        case "referenceangle":
                                            ((RevoluteJointDef)mainDef).ReferenceAngle = float.Parse(sn.Value);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Weld:
                                    {
                                        switch (sn.Name.ToLower())
                                        {
                                        case "localanchora":
                                            ((WeldJointDef)mainDef).LocalAnchorA = ReadVector(sn);
                                            break;
                                        case "localanchorb":
                                            ((WeldJointDef)mainDef).LocalAnchorB = ReadVector(sn);
                                            break;
                                        }
                                    }
                                    break;
                                case JointType.Gear:
                                    throw new Exception("Gear joint is unsupported");
                                }

                                switch (sn.Name.ToLower())
                                {
                                case "name":
                                    name = sn.Value;
                                    break;
                                case "bodya":
                                    bodyA = int.Parse(sn.Value);
                                    break;
                                case "bodyb":
                                    bodyB = int.Parse(sn.Value);
                                    break;
                                case "collideconnected":
                                    collideConnected = bool.Parse(sn.Value);
                                    break;
                                case "userdata":
                                    userData = ReadSimpleType(sn, null, false);
                                    break;
                                }
                            }

                            mainDef.CollideConnected = collideConnected;
                            mainDef.UserData = userData;
                            _joints.Add(new JointDefSerialized(mainDef, bodyA, bodyB, name));
                        }
                    }
                    break;
                }
            }

            return data;
        }