Ejemplo n.º 1
0
Archivo: Gun.cs Proyecto: prepare/box2c
        public Gun()
        {
            {
                Body ground = m_world.CreateBody(new BodyDef());
                ground.CreateFixture(new PolygonShape(new Vec2(-80, 0), new Vec2(80, 0)), 0);

                ground.CreateFixture(new PolygonShape(new Vec2(0, 15), new Vec2(-1.5f, 4)), 0);
                ground.CreateFixture(new PolygonShape(new Vec2(4.5f, 15), new Vec2(3.0f, 4)), 0);
                ground.CreateFixture(new PolygonShape(new Vec2(-1.5f, 4), new Vec2(3.0f, 4)), 0);

                {
                    Body feeder = m_world.CreateBody(new BodyDef(BodyType.Dynamic, new Vec2(-0.05f, 3)));
                    feeder.CreateFixture(new PolygonShape(new Vec2(-1.39f + 0.04f, 1.2f), new Vec2(-1.39f, 1), new Vec2(2.99f, 1), new Vec2(2.99f + 0.04f, 1.2f)), 0.5f);

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.Initialize(feeder, ground, feeder.WorldCenter, (feeder.WorldCenter - new Vec2(2.25f, 15)).Normalized());
                    pjd.CollideConnected = false;
                    pjd.EnableLimit      = true;
                    pjd.LowerTranslation = 0;
                    pjd.UpperTranslation = 15.0f - (.1f);
                    m_world.CreateJoint(pjd);
                }
            }

            {
                const float addX = 0.16f;
                const float addY = 1.42f;
                Vec2        pos  = new Vec2(6.90f, 8.45f);
                for (int i = 0; i < 9; ++i)
                {
                    AddBullet(pos, (float)((Math.PI) + (Math.PI / 2)));
                    pos += new Vec2(addX, addY);
                }
            }
        }
Ejemplo n.º 2
0
        private void addBaffleX(Body body)
        {
            try
            {
                if (body.GetUserData() == null)
                {
                    return;
                }
                GizmoComponents myGizmo = (GizmoComponents)body.GetUserData();
                //添加Shape
                addPolygon(body);

                BodyDef kinematicBodyDef = new BodyDef();
                kinematicBodyDef.Position.Set(0, myGizmo.PositionY);
                Body kinematicBody = myWorld.CreateBody(kinematicBodyDef);

                PrismaticJointDef jointDef = new PrismaticJointDef();
                Vec2 worldAxis             = new Vec2(1, 0);
                jointDef.Initialize(body, kinematicBody, kinematicBody.GetWorldCenter(), worldAxis);
                jointDef.LowerTranslation = -100f;
                jointDef.UpperTranslation = 100f;
                jointDef.EnableLimit      = true;
                PrismaticJoint joint = (PrismaticJoint)myWorld.CreateJoint(jointDef);
            }
            catch (Exception e)
            {
                MessageBox.Show("PhysicalWorld-addBaffleX函数出错");
            }
        }
Ejemplo n.º 3
0
    void Start()
    {
        if (connectedBody == null)
        {
            return;
        }
        var jointDef = new PrismaticJointDef();

#if USING_BOX2DX
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor.ToVec2(), axis.ToVec2());
#else
        jointDef.Initialize(GetComponent <Box2DBody>(), connectedBody, anchor, axis);
#endif
        jointDef.MaxMotorForce = maxMotorForce;
        jointDef.EnableMotor   = enableMotor;

        joint = (PrismaticJoint)Box2DWorld.Instance().CreateJoint(jointDef);
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a distance joint between two bodies
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <param name="anchor"></param>
        /// <param name="axis"></param>
        public void joinBodies_Prismatic(Body b1, Body b2, Vector2 anchor, Vector2 axis)
        {
            PrismaticJointDef pjd = new PrismaticJointDef();

            pjd.upperTranslation = 1;
            pjd.lowerTranslation = -1;
            pjd.enableMotor      = true;
            pjd.maxMotorForce    = 1000;
            pjd.motorSpeed       = 1;
            pjd.Initialize(b1, b2, anchor, axis);
            PrismaticJoint pj = physicsWorld.CreateJoint(pjd) as PrismaticJoint;
        }
Ejemplo n.º 5
0
    // Use this for initialization
    protected override IntPtr Init()
    {
        PrismaticJointDef jd = new PrismaticJointDef(other.body, body.body);

        jd.Initialize(other.body, body.body, anchor, axis);
        jd.enableLimit      = enableLimit;
        jd.lowerTranslation = lowerTranslation;
        jd.upperTranslation = upperTranslation;
        jd.enableMotor      = enableMotor;
        jd.maxMotorForce    = maxMotorForce;
        jd.motorSpeed       = motorSpeed;
        return(API.CreatePrismaticJoint(B2DWorld.instance.world, jd));
    }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a distance joint between two bodies
        /// </summary>
        /// <param name="b1"></param>
        /// <param name="b2"></param>
        /// <param name="anchor"></param>
        /// <param name="axis"></param>
        /// <param name="uppertranslation"></param>
        /// <param name="lowertranslation"></param>
        /// <param name="motorspeed"></param>
        public void joinBodies_Prismatic(Body b1, Body b2, Vector2 anchor, Vector2 axis, float uppertranslation, float lowertranslation, float motorspeed)
        {
            PrismaticJointDef pjd = new PrismaticJointDef();

            pjd.Initialize(b1, b2, anchor, axis);
            if (motorspeed != 0)
            {
                pjd.enableMotor = true;
            }
            pjd.motorSpeed       = motorspeed;
            pjd.lowerTranslation = lowertranslation;
            pjd.upperTranslation = uppertranslation;

            PrismaticJoint pj = physicsWorld.CreateJoint(pjd) as PrismaticJoint;
        }
Ejemplo n.º 7
0
        public Prismatic()
        {
            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(2.0f, 0.5f);
                shape.Density = 5;

                BodyDef bd = new BodyDef();
                bd.type = BodyType._dynamicBody;
                bd.Position.Set(-10.0f, 10.0f);
                bd.angle      = 0.5f * (float)Math.PI;
                bd.allowSleep = false;
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vec2 axis = new Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(1.0f, 0.0f));

                pjd.motorSpeed       = 10.0f;
                pjd.maxMotorForce    = 10000.0f;
                pjd.enableMotor      = true;
                pjd.lowerTranslation = 0.0f;
                pjd.upperTranslation = 20.0f;
                pjd.enableLimit      = true;

                m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
Ejemplo n.º 8
0
        public Prismatic()
        {
            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(2.0f, 0.5f);

                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-10.0f, 10.0f);
                bd.Angle      = 0.5f * Settings.Pi;
                bd.AllowSleep = false;
                var body = World.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                var pjd = new PrismaticJointDef();

                // Bouncy limit
                var axis = new Vector2(2.0f, 1.0f);

                axis = Vector2.Normalize(axis);
                pjd.Initialize(ground, body, new Vector2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vector2(-10.0f, 10.0f), new Vector2(1.0f, 0.0f));

                pjd.MotorSpeed       = 10.0f;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = true;
                pjd.LowerTranslation = 0.0f;
                pjd.UpperTranslation = 20.0f;
                pjd.EnableLimit      = true;

                _joint = (PrismaticJoint)World.CreateJoint(pjd);
            }
        }
        public PrismaticJointTestBase()
        {
            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);
            }

            EnableLimit = true;
            EnableMotor = false;
            MotorSpeed  = 10.0f;

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

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 10.0f);
                bd.Angle      = 0.5f * Settings.Pi;
                bd.AllowSleep = false;
                var body = World.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

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

                pjd.MotorSpeed       = MotorSpeed;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = EnableMotor;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit      = EnableLimit;

                Joint = (PrismaticJoint)World.CreateJoint(pjd);
            }
        }
Ejemplo n.º 10
0
        public Prismatic()
        {
            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.CreateShape(sd);
            }

            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(2.0f, 0.5f);
                sd.Density  = 5.0f;
                sd.Friction = 0.05f;

                BodyDef bd = new BodyDef();
                bd.Position.Set(-10.0f, 10.0f);
                bd.Angle = 0.5f * Box2DX.Common.Settings.Pi;
                Body body = _world.CreateBody(bd);
                body.CreateShape(sd);
                body.SetMassFromShapes();

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), new Vec2(1.0f, 0.0f));

                // Non-bouncy limit
                //pjd.Initialize(ground, body, Vec2(-10.0f, 10.0f), Vec2(1.0f, 0.0f));

                pjd.MotorSpeed       = 10.0f;
                pjd.MaxMotorForce    = 1000.0f;
                pjd.EnableMotor      = true;
                pjd.LowerTranslation = 0.0f;
                pjd.UpperTranslation = 20.0f;
                pjd.EnableLimit      = true;

                _joint = (PrismaticJoint)_world.CreateJoint(pjd);
            }
        }
Ejemplo n.º 11
0
        private PrismaticJointTest()
        {
            Body ground;

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

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

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

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

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

                PrismaticJointDef pjd = new PrismaticJointDef();

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

                pjd.MotorSpeed       = _motorSpeed;
                pjd.MaxMotorForce    = 10000.0f;
                pjd.EnableMotor      = _enableMotor;
                pjd.LowerTranslation = -10.0f;
                pjd.UpperTranslation = 10.0f;
                pjd.EnableLimit      = _enableLimit;

                _joint = (PrismaticJoint)JointFactory.CreateFromDef(World, pjd);
            }
        }
Ejemplo n.º 12
0
        public Prismatic()
	    {
		    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(2.0f, 0.5f);

			    BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
			    bd.position = new Vector2(-10.0f, 10.0f);
			    bd.angle = 0.5f * (float)Settings.b2_pi;
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

			    // Bouncy limit
                Vector2 axis = new Vector2(2, 1);
			    axis.Normalize();
			    pjd.Initialize(ground, body, new Vector2(0.0f, 0.0f), axis);

			    // Non-bouncy limit
			    //pjd.Initialize(ground, body, new Vector2(-10.0f, 10.0f), new Vector2(1.0f, 0.0f));

			    pjd.motorSpeed = 10.0f;
			    pjd.maxMotorForce = 1000.0f;
			    pjd.enableMotor = true;
			    pjd.lowerTranslation = 0.0f;
			    pjd.upperTranslation = 20.0f;
			    pjd.enableLimit = true;

			    _joint = (PrismaticJoint)_world.CreateJoint(pjd);
		    }
	    }
Ejemplo n.º 13
0
        public Prismatic()
        {
            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(2.0, 0.5);

                BodyDef bd = new BodyDef();
                bd.type     = BodyType.Dynamic;
                bd.position = new Vector2(-10.0, 10.0);
                bd.angle    = 0.5 * Alt.Box2D.Settings.b2_pi;
                Body body = _world.CreateBody(bd);
                body.CreateFixture(shape, 5.0);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vector2 axis = new Vector2(2, 1);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vector2(0.0, 0.0), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vector2(-10.0, 10.0), new Vector2(1.0, 0.0));

                pjd.motorSpeed       = 10.0;
                pjd.maxMotorForce    = 1000.0;
                pjd.enableMotor      = true;
                pjd.lowerTranslation = 0.0;
                pjd.upperTranslation = 20.0;
                pjd.enableLimit      = true;

                _joint = (PrismaticJoint)_world.CreateJoint(pjd);
            }
        }
Ejemplo n.º 14
0
        public SliderCrank()
        {
            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);
            }

            {
                Body prevBody = ground;

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

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

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

                    prevBody = body;
                }

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

                    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);
                }
            }
        }
        /// <summary>
        /// Add objects to the Box2d world.
        /// </summary>
        protected override void PopulateWorld()
        {
            // ==== Define the ground body ====
            BodyDef groundBodyDef = new BodyDef();

            groundBodyDef.Position.Set(0f, -0.25f);

            // Call the body factory which creates the ground box shape.
            // The body is also added to the world.
            Body groundBody = _world.CreateBody(groundBodyDef);

            // Define the ground box shape.
            PolygonDef groundShapeDef = new PolygonDef();

            // The extents are the half-widths of the box.
            groundShapeDef.SetAsBox(_trackLengthHalf + 1f, 0.25f);
            groundShapeDef.Friction            = _simParams._defaultFriction;
            groundShapeDef.Restitution         = _simParams._defaultRestitution;
            groundShapeDef.Filter.CategoryBits = 0x3;

            // Add the ground shape to the ground body.
            groundBody.CreateShape(groundShapeDef);

            // ==== Define the cart body.
            BodyDef cartBodyDef = new BodyDef();

            cartBodyDef.Position.Set(0f, 0.15f);

            // Create cart body.
            _cartBody = _world.CreateBody(cartBodyDef);
            PolygonDef cartShapeDef = new PolygonDef();

            cartShapeDef.SetAsBox(0.5f, 0.125f);
            cartShapeDef.Friction    = 0f;
            cartShapeDef.Restitution = 0f;
            cartShapeDef.Density     = 16f;
            _cartBody.CreateShape(cartShapeDef);
            _cartBody.SetMassFromShapes();

            // Fix cart to 'track' (prismatic joint).
            PrismaticJointDef cartTrackJointDef = new PrismaticJointDef();

            cartTrackJointDef.EnableMotor      = true;
            cartTrackJointDef.LowerTranslation = -_trackLengthHalf;
            cartTrackJointDef.UpperTranslation = _trackLengthHalf;
            cartTrackJointDef.EnableLimit      = true;
            cartTrackJointDef.Initialize(groundBody, _cartBody, new Vec2(0f, 0f), new Vec2(1f, 0f));
            _cartTrackJoint = (PrismaticJoint)_world.CreateJoint(cartTrackJointDef);

            // ===== Create arm1.
            const float poleRadius  = 0.025f;   // Half the thickness of the pole.
            Vec2        arm1PosBase = new Vec2(0f, 0.275f);
            Body        arm1Body    = CreatePole(arm1PosBase, _cartJointInitialAngle, poleRadius, 0f, 0f, 2f, 0x0);

            // Join arm1 to cart.
            RevoluteJointDef poleJointDef = new RevoluteJointDef();

            poleJointDef.CollideConnected = false;
            poleJointDef.EnableMotor      = false;
            poleJointDef.MaxMotorTorque   = 0f;
            poleJointDef.Initialize(_cartBody, arm1Body, arm1PosBase);
            _cartJoint = (RevoluteJoint)_world.CreateJoint(poleJointDef);

            // ===== Create arm2.
            Vec2 arm2PosBase = CalcPoleEndPos(arm1Body);
            Body arm2Body    = CreatePole(arm2PosBase, _elbowJointInitialAngle, poleRadius, 0f, 0f, 2f, 0x0);

            _arm2Body = arm2Body;

            // Join arm2 to arm1.
            poleJointDef.CollideConnected = false;
            poleJointDef.EnableMotor      = false;
            poleJointDef.MaxMotorTorque   = 0f;
            poleJointDef.Initialize(arm1Body, arm2Body, arm2PosBase);
            _elbowJoint = (RevoluteJoint)_world.CreateJoint(poleJointDef);
        }
Ejemplo n.º 16
0
        public BodyTypes()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

                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 * (double)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);
            }
        }
Ejemplo n.º 17
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);
		    }
	    }
Ejemplo n.º 18
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();
            }
        }
Ejemplo n.º 19
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);
            }
        }
Ejemplo n.º 20
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);
            }
        }
Ejemplo n.º 21
0
        public void JointReactions()
        {
            var   gravity = new Vector2(0, -10.0f);
            World world   = new World(gravity);

            BodyDef bodyDef = new BodyDef();
            Body    ground  = world.CreateBody(bodyDef);

            CircleShape circle = new CircleShape();

            circle.Radius = 1.0f;

            FixtureDef fixtureDef = new FixtureDef();

            // Disable collision
            fixtureDef.Filter.MaskBits = 0;
            fixtureDef.Density         = 1.0f;
            fixtureDef.Shape           = circle;

            bodyDef.BodyType = BodyType.DynamicBody;
            bodyDef.Position.Set(-2.0f, 3.0f);

            var bodyA = world.CreateBody(bodyDef);
            var bodyB = world.CreateBody(bodyDef);
            var bodyC = world.CreateBody(bodyDef);

            circle.ComputeMass(out var massData, fixtureDef.Density);
            var mg = massData.Mass * gravity.Y;

            bodyA.CreateFixture(fixtureDef);
            bodyB.CreateFixture(fixtureDef);
            bodyC.CreateFixture(fixtureDef);

            DistanceJointDef distanceJointDef = new DistanceJointDef();

            distanceJointDef.Initialize(ground, bodyA, bodyDef.Position + new Vector2(0.0f, 4.0f), bodyDef.Position);
            distanceJointDef.MinLength = distanceJointDef.Length;
            distanceJointDef.MaxLength = distanceJointDef.Length;

            PrismaticJointDef prismaticJointDef = new PrismaticJointDef();

            prismaticJointDef.Initialize(ground, bodyB, bodyDef.Position, new Vector2(1.0f, 0.0f));

            RevoluteJointDef revoluteJointDef = new RevoluteJointDef();

            revoluteJointDef.Initialize(ground, bodyC, bodyDef.Position);

            var distanceJoint  = (DistanceJoint)world.CreateJoint(distanceJointDef);
            var prismaticJoint = (PrismaticJoint)world.CreateJoint(prismaticJointDef);
            var revoluteJoint  = (RevoluteJoint)world.CreateJoint(revoluteJointDef);

            const float timeStep           = 1 / 60f;
            const float invTimeStep        = 60.0f;
            const int   velocityIterations = 6;
            const int   positionIterations = 2;

            world.Step(timeStep, velocityIterations, positionIterations);

            const float tol = 1e-5f;
            {
                var F = distanceJoint.GetReactionForce(invTimeStep);
                var T = distanceJoint.GetReactionTorque(invTimeStep);
                F.X.ShouldBe(0.0f);
                Math.Abs(F.Y + mg).ShouldBeLessThan(tol);
                T.ShouldBe(0.0f);
            }

            {
                var F = prismaticJoint.GetReactionForce(invTimeStep);
                var T = prismaticJoint.GetReactionTorque(invTimeStep);
                F.X.ShouldBe(0.0f);
                Math.Abs(F.Y + mg).ShouldBeLessThan(tol);
                T.ShouldBe(0.0f);
            }

            {
                var F = revoluteJoint.GetReactionForce(invTimeStep);
                var T = revoluteJoint.GetReactionTorque(invTimeStep);
                F.X.ShouldBe(0.0f);
                Math.Abs(F.Y + mg).ShouldBeLessThan(tol);
                T.ShouldBe(0.0f);
            }
        }
Ejemplo n.º 22
0
        public static Joint AddJoint(this IJointable ithis, V2DJoint joint, float offsetX, float offsetY)
        {
            Joint    result   = null;
            JointDef jointDef = null;
            //Body targ0 = ithis.VScreen.bodyMap[joint.Body1];
            //Body targ1 = ithis.VScreen.bodyMap[joint.Body2];
            Body targ0 = GetBody(ithis, joint.Body1);
            Body targ1 = GetBody(ithis, joint.Body2);

            // gears need the first body static
            if (targ0 != null && targ1 != null && targ1.GetType() == BodyType.Static && targ0.GetType() != BodyType.Static)
            {
                Body temp = targ0;
                targ0 = targ1;
                targ1 = temp;
            }

            Vector2 pt0 = new Vector2(joint.X + offsetX, joint.Y + offsetY);

            string name = joint.Name;

            Vector2 anchor0 = new Vector2(pt0.X / V2DScreen.WorldScale, pt0.Y / V2DScreen.WorldScale);
            Vector2 anchor1 = new Vector2();

            switch (joint.Type)
            {
            case V2DJointKind.Distance:
                Vector2 pt1 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                anchor1 = new Vector2(pt1.X / V2DScreen.WorldScale, pt1.Y / V2DScreen.WorldScale);

                DistanceJointDef dj = new DistanceJointDef();
                dj.Initialize(targ0, targ1, anchor0, anchor1);
                dj.collideConnected = joint.CollideConnected;
                dj.dampingRatio     = joint.DampingRatio;
                dj.frequencyHz      = joint.FrequencyHz;
                if (joint.Length != -1)
                {
                    dj.length = joint.Length / V2DScreen.WorldScale;
                }

                jointDef = dj;
                break;

            case V2DJointKind.Revolute:
                float rot0 = joint.Min;                         //(typeof(joint["min"]) == "string") ? parseFloat(joint["min"]) / 180 * Math.PI : joint["min"];
                float rot1 = joint.Max;                         //(typeof(joint["max"]) == "string") ? parseFloat(joint["max"]) / 180 * Math.PI : joint["max"];

                RevoluteJointDef rj = new RevoluteJointDef();
                rj.Initialize(targ0, targ1, anchor0);
                rj.lowerAngle = rot0;
                rj.upperAngle = rot1;

                rj.enableLimit    = rot0 != 0 && rot1 != 0;
                rj.maxMotorTorque = joint.MaxMotorTorque;
                rj.motorSpeed     = joint.MotorSpeed;
                rj.enableMotor    = joint.EnableMotor;

                jointDef = rj;
                break;

            case V2DJointKind.Prismatic:
                float axisX = joint.AxisX;
                float axisY = joint.AxisY;
                float min   = joint.Min;
                float max   = joint.Max;

                PrismaticJointDef pj        = new PrismaticJointDef();
                Vector2           worldAxis = new Vector2(axisX, axisY);
                pj.Initialize(targ0, targ1, anchor0, worldAxis);
                pj.lowerTranslation = min / V2DScreen.WorldScale;
                pj.upperTranslation = max / V2DScreen.WorldScale;

                pj.enableLimit   = joint.EnableLimit;
                pj.maxMotorForce = joint.MaxMotorTorque;
                pj.motorSpeed    = joint.MotorSpeed;
                pj.enableMotor   = joint.EnableMotor;

                jointDef = pj;
                break;

            case V2DJointKind.Pully:
                Vector2 pt2 = new Vector2(joint.X2 + offsetX, joint.Y2 + offsetY);
                anchor1 = new Vector2(pt2.X / V2DScreen.WorldScale, pt2.Y / V2DScreen.WorldScale);

                Vector2 groundAnchor0 = new Vector2(joint.GroundAnchor1X / V2DScreen.WorldScale, joint.GroundAnchor1Y / V2DScreen.WorldScale);

                Vector2 groundAnchor1 = new Vector2(joint.GroundAnchor2X / V2DScreen.WorldScale, joint.GroundAnchor2Y / V2DScreen.WorldScale);

                float max0 = joint.MaxLength1;
                float max1 = joint.MaxLength2;

                float rat = joint.Ratio;

                PulleyJointDef puj = new PulleyJointDef();
                puj.Initialize(targ0, targ1, groundAnchor0, groundAnchor1, anchor0, anchor1, rat);
                puj.maxLengthA = (max0 + max1) / V2DScreen.WorldScale;
                puj.maxLengthB = (max0 + max1) / V2DScreen.WorldScale;

                puj.collideConnected = joint.CollideConnected;

                jointDef = puj;
                break;

            case V2DJointKind.Gear:
                GearJointDef gj = new GearJointDef();
                gj.bodyA  = targ0;
                gj.bodyB  = targ1;
                gj.joint1 = GetFirstGearableJoint(targ0.GetJointList());
                gj.joint2 = GetFirstGearableJoint(targ1.GetJointList());
                gj.ratio  = joint.Ratio;
                jointDef  = gj;
                break;
            }

            if (jointDef != null)
            {
                result = SetJointWithReflection(ithis, name, jointDef);

                if (result != null)
                {
                    Dictionary <string, string> dict = new Dictionary <string, string>();
                    dict["name"] = name;
                    result.SetUserData(dict);
                }
            }

            return(result);
        }
Ejemplo n.º 23
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);
                }
            }
        }
Ejemplo n.º 24
0
        // Main...
        public ElasticBody()
        {
            // Bottom static body
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(50.0f, 2.0f);
                sd.Friction    = 0.1f;
                sd.Restitution = 0.1f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-1.0f, -7.5f);
                _ground = _world.CreateBody(bd);
                _ground.CreateFixture(sd);
            }
            // Upper static body
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(20.0f, 0.50f, new Vec2(0.0f, 0.0f), 0.047f * Box2DNet.Common.Settings.Pi);
                sd.Friction    = 0.01f;
                sd.Restitution = 0.001f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-20.0f, 93.0f);
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);
                sd.SetAsBox(15.0f, 0.50f, new Vec2(-15.0f, 12.5f), 0.0f);
                g.CreateFixture(sd);

                sd.SetAsBox(20.0f, 0.5f, new Vec2(0.0f, -25.0f), -0.5f);
                g.CreateFixture(sd);
            }
            // Left channel left wall
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.7f, 55.0f);
                sd.Friction    = 0.1f;
                sd.Restitution = 0.1f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-49.3f, 50.0f);
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);
            }
            // Right wall
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.7f, 55.0f);
                sd.Friction    = 0.1f;
                sd.Restitution = 0.1f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(45.0f, 50.0f);
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);
            }
            // Left channel right upper wall
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.5f, 20.0f);
                sd.Friction    = 0.05f;
                sd.Restitution = 0.01f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-42.0f, 70.0f);
                bd.Angle = -0.03f * Box2DNet.Common.Settings.Pi;
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);
            }
            // Left channel right lower wall
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.50f, 23.0f);
                sd.Friction    = 0.05f;
                sd.Restitution = 0.01f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-44.0f, 27.0f);
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);
                // Bottom motors
                CircleDef cd = new CircleDef();
                cd.Radius      = 3.0f;
                cd.Density     = 15.0f;
                cd.Friction    = 1.0f;
                cd.Restitution = 0.2f;
                // 1.
                bd.Position.Set(-40.0f, 2.5f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                RevoluteJointDef jr = new RevoluteJointDef();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                jr.MaxMotorTorque = 30000.0f;
                jr.EnableMotor    = true;
                jr.MotorSpeed     = 20.0f;
                _world.CreateJoint(jr);
                // 1. left down
                bd.Position.Set(-46.0f, -2.5f);
                cd.Radius = 1.5f; jr.MotorSpeed = -20.0f;
                body      = _world.CreateBody(bd);
                body.CreateFixture(cd);
                sd.SetAsBox(2.0f, 0.50f);
                body.CreateFixture(sd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter());
                _world.CreateJoint(jr);
                // 2.
                cd.Radius = 3.0f; jr.MotorSpeed = 20.0f;
                bd.Position.Set(-32.0f, 2.5f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 3.
                jr.MotorSpeed = 20.0f;
                bd.Position.Set(-24.0f, 1.5f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 4.
                bd.Position.Set(-16.0f, 0.8f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 5.
                bd.Position.Set(-8.0f, 0.5f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 6.
                bd.Position.Set(0.0f, 0.1f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 7.
                bd.Position.Set(8.0f, -0.5f);
                body = _world.CreateBody(bd);
                body.CreateFixture(cd);
                sd.SetAsBox(3.7f, 0.5f);
                body.CreateFixture(sd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f));
                _world.CreateJoint(jr);
                // 8. right rotator
                sd.SetAsBox(5.0f, 0.5f);
                sd.Density = 2.0f;
                bd.Position.Set(18.0f, 1.0f);
                Body rightmotor = _world.CreateBody(bd);
                rightmotor.CreateFixture(sd);
                sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi / 3.0f);
                rightmotor.CreateFixture(sd);
                sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi * 2.0f / 3.0f);
                rightmotor.CreateFixture(sd);
                cd.Radius = 4.2f;
                rightmotor.CreateFixture(cd);
                rightmotor.SetMassFromShapes();
                jr.Initialize(g, rightmotor, rightmotor.GetWorldCenter());
                jr.MaxMotorTorque = 70000.0f;
                jr.MotorSpeed     = -4.0f;
                _world.CreateJoint(jr);
                // 9. left rotator
                sd.SetAsBox(8.5f, 0.5f);
                sd.Density = 2.0f;
                bd.Position.Set(-34.0f, 17.0f);
                body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                sd.SetAsBox(8.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DNet.Common.Settings.Pi * .5f);
                body.CreateFixture(sd);
                cd.Radius   = 7.0f;
                cd.Friction = 0.9f;
                body.CreateFixture(cd);
                body.SetMassFromShapes();
                jr.Initialize(g, body, body.GetWorldCenter());
                jr.MaxMotorTorque = 100000.0f;
                jr.MotorSpeed     = -5.0f;
                _world.CreateJoint(jr);
                // big compressor
                sd.SetAsBox(3.0f, 4.0f);
                sd.Density = 10.0f;
                bd.Position.Set(-16.0f, 17.0f);
                Body hammerleft = _world.CreateBody(bd);
                hammerleft.CreateFixture(sd);
                hammerleft.SetMassFromShapes();
                DistanceJointDef jd = new DistanceJointDef();
                jd.Initialize(body, hammerleft, body.GetWorldCenter() + new Vec2(0.0f, 6.0f), hammerleft.GetWorldCenter());
                _world.CreateJoint(jd);

                bd.Position.Set(4.0f, 17.0f);
                Body hammerright = _world.CreateBody(bd);
                hammerright.CreateFixture(sd);
                hammerright.SetMassFromShapes();
                jd.Initialize(body, hammerright, body.GetWorldCenter() - new Vec2(0.0f, 6.0f), hammerright.GetWorldCenter());
                _world.CreateJoint(jd);
                // pusher
                sd.SetAsBox(6.0f, 0.75f);
                bd.Position.Set(-21.0f, 9.0f);
                Body pusher = _world.CreateBody(bd);
                pusher.CreateFixture(sd);
                sd.SetAsBox(2.0f, 1.5f, new Vec2(-5.0f, 0.0f), 0.0f);
                pusher.SetMassFromShapes();
                pusher.CreateFixture(sd);
                jd.Initialize(rightmotor, pusher, rightmotor.GetWorldCenter() + new Vec2(-8.0f, 0.0f),
                              pusher.GetWorldCenter() + new Vec2(5.0f, 0.0f));
                _world.CreateJoint(jd);
            }
            // Static bodies above motors
            {
                PolygonDef sd = new PolygonDef();
                CircleDef  cd = new CircleDef();
                sd.SetAsBox(9.0f, 0.5f);
                sd.Friction    = 0.05f;
                sd.Restitution = 0.01f;
                BodyDef bd = new BodyDef();
                bd.Position.Set(-15.5f, 12.0f);
                bd.Angle = 0.0f;
                Body g = _world.CreateBody(bd);
                g.CreateFixture(sd);

                sd.SetAsBox(8.0f, 0.5f, new Vec2(23.0f, 0.0f), 0.0f);
                g.CreateFixture(sd);
                // compressor statics
                sd.SetAsBox(7.0f, 0.5f, new Vec2(-2.0f, 9.0f), 0.0f);
                g.CreateFixture(sd);
                sd.SetAsBox(9.0f, 0.5f, new Vec2(22.0f, 9.0f), 0.0f);
                g.CreateFixture(sd);

                sd.SetAsBox(19.0f, 0.5f, new Vec2(-9.0f, 15.0f), -0.05f);
                g.CreateFixture(sd);
                sd.SetAsBox(4.7f, 0.5f, new Vec2(15.0f, 11.5f), -0.5f);
                g.CreateFixture(sd);
                // below compressor
                sd.SetAsBox(26.0f, 0.3f, new Vec2(17.0f, -4.4f), -0.02f);
                g.CreateFixture(sd);
                cd.Radius        = 1.0f; cd.Friction = 1.0f;
                cd.LocalPosition = new Vec2(29.0f, -6.0f);
                g.CreateFixture(cd);
                cd.Radius        = 0.7f;
                cd.LocalPosition = new Vec2(-2.0f, -4.5f);
                g.CreateFixture(cd);
            }
            // Elevator
            {
                BodyDef    bd = new BodyDef();
                CircleDef  cd = new CircleDef();
                PolygonDef sd = new PolygonDef();

                bd.Position.Set(40.0f, 4.0f);
                _elev = _world.CreateBody(bd);

                sd.SetAsBox(0.5f, 2.5f, new Vec2(3.0f, -3.0f), 0.0f);
                sd.Density  = 1.0f;
                sd.Friction = 0.01f;
                _elev.CreateFixture(sd);
                sd.SetAsBox(7.0f, 0.5f, new Vec2(-3.5f, -5.5f), 0.0f);
                _elev.CreateFixture(sd);
                sd.SetAsBox(0.5f, 2.5f, new Vec2(-11.0f, -3.5f), 0.0f);
                _elev.CreateFixture(sd);
                _elev.SetMassFromShapes();

                PrismaticJointDef jp = new PrismaticJointDef();
                jp.Initialize(_ground, _elev, bd.Position, new Vec2(0.0f, 1.0f));
                jp.LowerTranslation = 0.0f;
                jp.UpperTranslation = 100.0f;
                jp.EnableLimit      = true;
                jp.EnableMotor      = true;
                jp.MaxMotorForce    = 10000.0f;
                jp.MotorSpeed       = 0.0f;
                _joint_elev         = (PrismaticJoint)_world.CreateJoint(jp);

                // Korb
                sd.SetAsBox(2.3f, 0.5f, new Vec2(1.0f, 0.0f), 0.0f);
                sd.Density = 0.5f;
                bd.Position.Set(29.0f, 6.5f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(sd);
                sd.SetAsBox(2.5f, 0.5f, new Vec2(3.0f, -2.0f), Box2DNet.Common.Settings.Pi / 2.0f);
                body.CreateFixture(sd);
                sd.SetAsBox(4.6f, 0.5f, new Vec2(7.8f, -4.0f), 0.0f);
                body.CreateFixture(sd);
                sd.SetAsBox(0.5f, 4.5f, new Vec2(12.0f, 0.0f), 0.0f);
                body.CreateFixture(sd);

                sd.SetAsBox(0.5f, 0.5f, new Vec2(13.0f, 4.0f), 0.0f);
                body.CreateFixture(sd);

                cd.Radius        = 0.7f; cd.Density = 1.0f; cd.Friction = 0.01f;
                cd.LocalPosition = new Vec2(0.0f, 0.0f);
                body.CreateFixture(cd);
                body.SetMassFromShapes();

                RevoluteJointDef jr = new RevoluteJointDef();
                jr.Initialize(_elev, body, bd.Position);
                jr.EnableLimit      = true;
                jr.LowerAngle       = -0.2f;
                jr.UpperAngle       = Box2DNet.Common.Settings.Pi * 1.1f;
                jr.CollideConnected = true;
                _world.CreateJoint(jr);
                // upper body exit
                sd.SetAsBox(14.0f, 0.5f, new Vec2(-3.5f, -10.0f), 0.0f);
                bd.Position.Set(17.5f, 96.0f);
                body = _world.CreateBody(bd);
                body.CreateFixture(sd);
            }
            // "Elastic body" 64 bodies - something like a lin. elastic compound
            // connected via dynamic forces (springs)
            {
                PolygonDef sd = new PolygonDef();
                sd.SetAsBox(0.55f, 0.55f);
                sd.Density           = 1.5f;
                sd.Friction          = 0.01f;
                sd.Filter.GroupIndex = -1;
                Vec2    startpoint = new Vec2(30.0f, 20.0f);
                BodyDef bd         = new BodyDef();
                bd.IsBullet   = false;
                bd.AllowSleep = false;
                for (int i = 0; i < 8; ++i)
                {
                    for (int j = 0; j < 8; ++j)
                    {
                        bd.Position.Set(j * 1.02f, 2.51f + 1.02f * i);
                        bd.Position += startpoint;
                        Body body = _world.CreateBody(bd);
                        bodies[8 * i + j] = body;
                        body.CreateFixture(sd);
                        body.SetMassFromShapes();
                    }
                }
            }
        }
Ejemplo n.º 25
0
        public BodyTypes()
        {
            Body ground;
            {
                var bd = new BodyDef();
                ground = World.CreateBody(bd);

                var shape = new EdgeShape();
                shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f));

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

                ground.CreateFixture(fd);
            }

            // Define attachment
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 3.0f);
                _attachment = World.CreateBody(bd);

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

            // Define platform
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(-4.0f, 5.0f);
                _platform = World.CreateBody(bd);

                var shape = new PolygonShape();
                shape.SetAsBox(0.5f, 4.0f, new Vector2(4.0f, 0.0f), 0.5f * Settings.Pi);

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

                var rjd = new RevoluteJointDef();
                rjd.Initialize(_attachment, _platform, new Vector2(0.0f, 5.0f));
                rjd.MaxMotorTorque = 50.0f;
                rjd.EnableMotor    = true;
                World.CreateJoint(rjd);

                var 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
            {
                var bd = new BodyDef();
                bd.BodyType = BodyType.DynamicBody;
                bd.Position.Set(0.0f, 8.0f);
                var body = World.CreateBody(bd);

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

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

                body.CreateFixture(fd);
            }
        }
Ejemplo n.º 26
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);
            }
        }
Ejemplo n.º 27
0
        public SliderCrank()
        {
            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);
            }

            {
                Body prevBody = ground;

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 7.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

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

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 13.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

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

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.type          = BodyType._dynamicBody;
                    bd.fixedRotation = true;
                    bd.Position.Set(0.0f, 17.0f);
                    Body body = m_world.CreateBody(bd);
                    body.CreateFixture(shape);

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

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

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

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

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

                    BodyDef bd = new BodyDef();
                    bd.type = BodyType._dynamicBody;
                    bd.Position.Set(0.0f, 23.0f);
                    Body body = m_world.CreateBody(bd);
                    shape.Density = 2;
                    body.CreateFixture(shape);
                }
            }
        }
Ejemplo n.º 28
0
        public Gears()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

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

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

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

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

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

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

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

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

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

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

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

                GearJointDef jd5 = new GearJointDef();
                jd5.BodyA  = body2;
                jd5.BodyB  = body3;
                jd5.JointA = m_joint2;
                jd5.JointB = m_joint3;
                jd5.Ratio  = -1.0f / circle2.Radius;
                m_joint5   = (GearJoint)m_world.CreateJoint(jd5);
            }
        }
Ejemplo n.º 29
0
        public SliderCrank()
        {
            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 prevBody = ground;

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

                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position = new Vector2(0.0f, 7.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 5.0f));
                    rjd.MotorSpeed     = 1.0f * Settings.Pi;
                    rjd.MaxMotorTorque = 10000.0f;
                    rjd.EnableMotor    = true;
                    _joint1            = (RevoluteJoint)World.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    var bd = new BodyDef {
                        BodyType = BodyType.DynamicBody, Position = new Vector2(0.0f, 13.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 9.0f));
                    rjd.EnableMotor = false;
                    World.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    var bd = new BodyDef
                    {
                        BodyType = BodyType.DynamicBody, FixedRotation = true,
                        Position = new Vector2(0.0f, 17.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(0.0f, 17.0f));
                    World.CreateJoint(rjd);

                    var 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
                {
                    var shape = new PolygonShape();
                    shape.SetAsBox(1.5f, 1.5f);

                    var bd = new BodyDef();
                    bd.BodyType = BodyType.DynamicBody;
                    bd.Position = new Vector2(0.0f, 23.0f);
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }
Ejemplo n.º 30
0
        private SliderCrank1Test()
        {
            Body ground;
            {
                BodyDef bd = new BodyDef();
                bd.Position = new Vector2(0.0f, 17.0f);
                ground      = BodyFactory.CreateFromDef(World, bd);
            }

            {
                Body prevBody = ground;

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

                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(-8.0f, 20.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(shape);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(-12.0f, 20.0f));
                    JointFactory.CreateFromDef(World, rjd);

                    prevBody = body;
                }

                // Define connecting rod
                {
                    PolygonShape shape = new PolygonShape(2.0f);
                    shape.SetAsBox(8.0f, 1.0f);

                    BodyDef bd = new BodyDef();
                    bd.Type     = BodyType.Dynamic;
                    bd.Position = new Vector2(4.0f, 20.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(shape);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(-4.0f, 20.0f));
                    JointFactory.CreateFromDef(World, rjd);

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.Type          = BodyType.Dynamic;
                    bd.FixedRotation = true;
                    bd.Position      = new Vector2(12.0f, 20.0f);
                    Body body = BodyFactory.CreateFromDef(World, bd);
                    body.AddFixture(shape);

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(12.0f, 20.0f));
                    JointFactory.CreateFromDef(World, rjd);

                    PrismaticJointDef pjd = new PrismaticJointDef();
                    pjd.Initialize(ground, body, new Vector2(12.0f, 17.0f), new Vector2(1.0f, 0.0f));
                    JointFactory.CreateFromDef(World, pjd);
                }
            }
        }
Ejemplo n.º 31
0
        public Gears()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

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

            {
                CircleShape circle1 = new CircleShape();
                circle1.m_radius = 1.0f;
                circle1.Density  = 5;

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

                CircleShape circle2 = new CircleShape();
                circle2.m_radius = 2.0f;
                circle2.Density  = 5;

                BodyDef bd1 = new BodyDef();
                bd1.type = BodyType._staticBody;
                bd1.Position.Set(10.0f, 9.0f);
                Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1);

                BodyDef bd2 = new BodyDef();
                bd2.type = BodyType._dynamicBody;
                bd2.Position.Set(10.0f, 8.0f);
                Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(box);

                BodyDef bd3 = new BodyDef();
                bd3.type = BodyType._dynamicBody;
                bd3.Position.Set(10.0f, 6.0f);
                Body body3 = m_world.CreateBody(bd3);
                body3.CreateFixture(circle2);

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

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

                GearJointDef jd4 = new GearJointDef();
                jd4.bodyA  = body1;
                jd4.bodyB  = body3;
                jd4.joint1 = joint1;
                jd4.joint2 = joint2;
                jd4.ratio  = circle2.m_radius / circle1.m_radius;
                m_world.CreateJoint(jd4);
            }

            {
                CircleShape circle1 = new CircleShape();
                circle1.m_radius = 1.0f;
                circle1.Density  = 5;

                CircleShape circle2 = new CircleShape();
                circle2.m_radius = 2.0f;
                circle2.Density  = 5;

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

                BodyDef bd1 = new BodyDef();
                bd1.type = BodyType._dynamicBody;
                bd1.Position.Set(-3.0f, 12.0f);
                Body body1 = m_world.CreateBody(bd1);
                body1.CreateFixture(circle1);

                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();
                m_joint1           = (RevoluteJoint)m_world.CreateJoint(jd1);

                BodyDef bd2 = new BodyDef();
                bd2.type = BodyType._dynamicBody;
                bd2.Position.Set(0.0f, 12.0f);
                Body body2 = m_world.CreateBody(bd2);
                body2.CreateFixture(circle2);

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

                BodyDef bd3 = new BodyDef();
                bd3.type = BodyType._dynamicBody;
                bd3.Position.Set(2.5f, 12.0f);
                Body body3 = m_world.CreateBody(bd3);
                box.Density = 5;
                body3.CreateFixture(box);

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

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

                GearJointDef jd4 = new GearJointDef();
                jd4.bodyA  = body1;
                jd4.bodyB  = body2;
                jd4.joint1 = m_joint1;
                jd4.joint2 = m_joint2;
                jd4.ratio  = circle2.m_radius / circle1.m_radius;
                m_joint4   = (GearJoint)m_world.CreateJoint(jd4);

                GearJointDef jd5 = new GearJointDef();
                jd5.bodyA  = body2;
                jd5.bodyB  = body3;
                jd5.joint1 = m_joint2;
                jd5.joint2 = m_joint3;
                jd5.ratio  = -1.0f / circle2.m_radius;
                m_joint5   = (GearJoint)m_world.CreateJoint(jd5);
            }
        }
Ejemplo n.º 32
0
        public Gears()
        {
            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);
            }

            {
                CircleDef circle1 = new CircleDef();
                circle1.Radius  = 1.0f;
                circle1.Density = 5.0f;

                CircleDef circle2 = new CircleDef();
                circle2.Radius  = 2.0f;
                circle2.Density = 5.0f;

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

                BodyDef bd1 = new BodyDef();
                bd1.Position.Set(-3.0f, 12.0f);
                Body body1 = _world.CreateBody(bd1);
                body1.CreateShape(circle1);
                body1.SetMassFromShapes();

                RevoluteJointDef jd1 = new RevoluteJointDef();
                jd1.Body1          = ground;
                jd1.Body2          = body1;
                jd1.LocalAnchor1   = ground.GetLocalPoint(bd1.Position);
                jd1.LocalAnchor2   = body1.GetLocalPoint(bd1.Position);
                jd1.ReferenceAngle = body1.GetAngle() - ground.GetAngle();
                _joint1            = (RevoluteJoint)_world.CreateJoint(jd1);

                BodyDef bd2 = new BodyDef();
                bd2.Position.Set(0.0f, 12.0f);
                Body body2 = _world.CreateBody(bd2);
                body2.CreateShape(circle2);
                body2.SetMassFromShapes();

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

                BodyDef bd3 = new BodyDef();
                bd3.Position.Set(2.5f, 12.0f);
                Body body3 = _world.CreateBody(bd3);
                body3.CreateShape(box);
                body3.SetMassFromShapes();

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

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

                GearJointDef jd4 = new GearJointDef();
                jd4.Body1  = body1;
                jd4.Body2  = body2;
                jd4.Joint1 = _joint1;
                jd4.Joint2 = _joint2;
                jd4.Ratio  = circle2.Radius / circle1.Radius;
                _joint4    = (GearJoint)_world.CreateJoint(jd4);

                GearJointDef jd5 = new GearJointDef();
                jd5.Body1  = body2;
                jd5.Body2  = body3;
                jd5.Joint1 = _joint2;
                jd5.Joint2 = _joint3;
                jd5.Ratio  = -1.0f / circle2.Radius;
                _joint5    = (GearJoint)_world.CreateJoint(jd5);
            }
        }
Ejemplo n.º 33
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);
        }
        public SliderCrank1()
        {
            Body ground;
            {
                var bd = new BodyDef {
                    Position = new Vector2(0.0f, 17.0f)
                };
                ground = World.CreateBody(bd);
            }

            {
                var prevBody = ground;

                // Define crank.
                {
                    var shape = new PolygonShape();
                    shape.SetAsBox(4.0f, 1.0f);

                    var bd = new BodyDef {
                        BodyType = BodyType.DynamicBody, Position = new Vector2(-8.0f, 20.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(-12.0f, 20.0f));
                    World.CreateJoint(rjd);

                    prevBody = body;
                }

                // Define connecting rod
                {
                    var shape = new PolygonShape();
                    shape.SetAsBox(8.0f, 1.0f);

                    var bd = new BodyDef {
                        BodyType = BodyType.DynamicBody, Position = new Vector2(4.0f, 20.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(-4.0f, 20.0f));
                    World.CreateJoint(rjd);

                    prevBody = body;
                }

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

                    var bd = new BodyDef
                    {
                        BodyType = BodyType.DynamicBody, FixedRotation = true,
                        Position = new Vector2(12.0f, 20.0f)
                    };
                    var body = World.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

                    var rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vector2(12.0f, 20.0f));
                    World.CreateJoint(rjd);

                    var pjd = new PrismaticJointDef();
                    pjd.Initialize(ground, body, new Vector2(12.0f, 17.0f), new Vector2(1.0f, 0.0f));
                    World.CreateJoint(pjd);
                }
            }
        }