/** Handles anchoring the first and last pieces of the bridge
  * to the world versus to other bridge pieces
  */
 private void CreateEndJoint(World world, bool leftEnd)
 {
     RevoluteJointDef joint = new RevoluteJointDef();
     Vector2 anchor = Position - new Vector2(width/2, 0);
     if (!leftEnd)
         anchor = Position + new Vector2(width / 2, 0);
     joint.Initialize(Body, world.GetGroundBody(), Utils.Convert(anchor));
     world.CreateJoint(joint);
 }
        public override void Update(CASSWorld world, float dt)
        {
            if (hack)
            {
                RevoluteJointDef jointDef = new RevoluteJointDef();
                jointDef.Initialize(Body, theWholeWideWorld.GetGroundBody(), Common.Utils.Convert(Position));
                theWholeWideWorld.CreateJoint(jointDef);

                hack = false;
            }

            base.Update(world, dt);
        }
Пример #3
0
 public RevoluteJoint(RevoluteJointDef def)
     : base(def)
 {
     this._localAnchor1 = def.LocalAnchor1;
     this._localAnchor2 = def.LocalAnchor2;
     this._referenceAngle = def.ReferenceAngle;
     this._impulse = default(Vec3);
     this._motorImpulse = 0f;
     this._lowerAngle = def.LowerAngle;
     this._upperAngle = def.UpperAngle;
     this._maxMotorTorque = def.MaxMotorTorque;
     this._motorSpeed = def.MotorSpeed;
     this._enableLimit = def.EnableLimit;
     this._enableMotor = def.EnableMotor;
 }
        public RevoluteJoint(RevoluteJointDef def)
            : base(def)
        {
            _localAnchor1 = def.LocalAnchor1;
            _localAnchor2 = def.LocalAnchor2;
            _referenceAngle = def.ReferenceAngle;

            _impulse = new Vec3();
            _motorImpulse = 0.0f;

            _lowerAngle = def.LowerAngle;
            _upperAngle = def.UpperAngle;
            _maxMotorTorque = def.MaxMotorTorque;
            _motorSpeed = def.MotorSpeed;
            _enableLimit = def.EnableLimit;
            _enableMotor = def.EnableMotor;
            _limitState = LimitState.InactiveLimit;
        }
Пример #5
0
		public Bridge()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

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

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

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

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

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

					prevBody = body;
				}

				Vec2 anchor_ = new Vec2(-15.0f + 1.0f * numPlanks, 5.0f);
				jd.Initialize(prevBody, ground, anchor_);
				_world.CreateJoint(jd);
			}
		}
Пример #6
0
		public Revolute()
		{
			Body ground = null;
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(50.0f, 10.0f);

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

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

				BodyDef bd = new BodyDef();

				RevoluteJointDef rjd = new RevoluteJointDef();

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

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

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

				_joint = (RevoluteJoint)_world.CreateJoint(rjd);
			}
		}
Пример #7
0
        public Chain()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

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

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

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

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

                    prevBody = body;
                }
            }
        }
Пример #8
0
		public Chain()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(0.0f, -10.0f);
				ground = _world.CreateBody(bd);

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

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

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

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

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

					prevBody = body;
				}
			}
		}
Пример #9
0
        public Dominos()
        {
            Body b1;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-1.5f, 10.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape,0);
            }

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

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

                for (int i = 0; i < 10; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(-6.0f + 1.0f * i, 11.25f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(fd);
                }
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(1.0f, 6.0f);
                Body ground = _world.CreateBody(bd);
                ground.CreateFixture(shape,0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-7.0f, 4.0f);
                 b2 = _world.CreateBody(bd);
                b2.CreateFixture(shape,0);
            }

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-0.9f, 1.0f);
                bd.Angle = -0.15f;

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

            RevoluteJointDef jd = new RevoluteJointDef();
            Vec2 anchor = new Vec2();

            anchor.Set(-2.0f, 1.0f);
            jd.Initialize(b1, b3, anchor);
            jd.CollideConnected = true;
            _world.CreateJoint(jd);

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

                BodyDef bd  = new BodyDef();
                bd.Position.Set(-10.0f, 15.0f);
                b4 = _world.CreateBody(bd);
                b4.CreateFixture(shape, 10.0f);
            }

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

            Body b5;
            {
                BodyDef bd = new BodyDef();
                bd.Position.Set(6.5f, 3.0f);
                b5 = _world.CreateBody(bd);

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

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

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

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

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

            }

            anchor.Set(6.0f, 2.0f);
            jd.Initialize(b1, b5, anchor);
            _world.CreateJoint(jd);

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(6.5f, 4.1f);
                b6 = _world.CreateBody(bd);
                b6.CreateFixture(shape, 30.0f);
            }

            anchor.Set(7.5f, 4.0f);
            jd.Initialize(b5, b6, anchor);
            _world.CreateJoint(jd);

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(7.4f, 1.0f);

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

            DistanceJointDef djd  =new DistanceJointDef();
            djd.Body1 = b3;
            djd.Body2 = b7;
            djd.LocalAnchor1.Set(6.0f, 0.0f);
            djd.LocalAnchor2.Set(0.0f, -1.0f);
            Vec2 d = djd.Body2.GetWorldPoint(djd.LocalAnchor2) - djd.Body1.GetWorldPoint(djd.LocalAnchor1);
            djd.Length = d.Length();
            _world.CreateJoint(djd);

            {
                float radius = 0.2f;

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

                for (int i = 0; i < 4; ++i)
                {
                    BodyDef bd = new BodyDef();
                    bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 10.0f);
                }
            }
        }
Пример #10
0
		public Car()
		{
			{	// car body
				PolygonDef poly1 = new PolygonDef(), poly2 = new PolygonDef();

				// bottom half
				poly1.VertexCount = 5;
				poly1.Vertices[4].Set(-2.2f, -0.74f);
				poly1.Vertices[3].Set(-2.2f, 0);
				poly1.Vertices[2].Set(1.0f, 0);
				poly1.Vertices[1].Set(2.2f, -0.2f);
				poly1.Vertices[0].Set(2.2f, -0.74f);
				poly1.Filter.GroupIndex = -1;

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

				// top half
				poly2.VertexCount = 4;
				poly2.Vertices[3].Set(-1.7f, 0);
				poly2.Vertices[2].Set(-1.3f, 0.7f);
				poly2.Vertices[1].Set(0.5f, 0.74f);
				poly2.Vertices[0].Set(1.0f, 0);
				poly2.Filter.GroupIndex = -1;

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

				BodyDef bd = new BodyDef();
				bd.Position.Set(-35.0f, 2.8f);

				_vehicle = _world.CreateBody(bd);
				_vehicle.CreateShape(poly1);
				_vehicle.CreateShape(poly2);
				_vehicle.SetMassFromShapes();
			}

			{	// vehicle wheels
				CircleDef circ = new CircleDef();
				circ.Density = 40.0f;
				circ.Radius = 0.38608f;
				circ.Friction = 0.8f;
				circ.Filter.GroupIndex = -1;

				BodyDef bd = new BodyDef();
				bd.AllowSleep = false;
				bd.Position.Set(-33.8f, 2.0f);

				_rightWheel = _world.CreateBody(bd);
				_rightWheel.CreateShape(circ);
				_rightWheel.SetMassFromShapes();

				bd.Position.Set(-36.2f, 2.0f);
				_leftWheel = _world.CreateBody(bd);
				_leftWheel.CreateShape(circ);
				_leftWheel.SetMassFromShapes();
			}

			{	// join wheels to chassis
				Vec2 anchor = new Vec2();
				RevoluteJointDef jd = new RevoluteJointDef();
				jd.Initialize(_vehicle, _leftWheel, _leftWheel.GetWorldCenter());
				jd.CollideConnected = false;
				jd.EnableMotor = true;
				jd.MaxMotorTorque = 10.0f;
				jd.MotorSpeed = 0.0f;
				_leftJoint = (RevoluteJoint)_world.CreateJoint(jd);

				jd.Initialize(_vehicle, _rightWheel, _rightWheel.GetWorldCenter());
				jd.CollideConnected = false;
				_rightJoint = (RevoluteJoint)_world.CreateJoint(jd);
			}

			{	// ground
				PolygonDef box = new PolygonDef();
				box.SetAsBox(19.5f, 0.5f);
				box.Friction = 0.62f;

				BodyDef bd = new BodyDef();
				bd.Position.Set(-25.0f, 1.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}

			{	// more ground
				PolygonDef box = new PolygonDef();
				BodyDef bd = new BodyDef();

				box.SetAsBox(9.5f, 0.5f, Vec2.Zero, 0.1f * Box2DX.Common.Settings.Pi);
				box.Friction = 0.62f;
				bd.Position.Set(27.0f - 30.0f, 3.1f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}

			{	// more ground
				PolygonDef box = new PolygonDef();
				BodyDef bd = new BodyDef();

				box.SetAsBox(9.5f, 0.5f, Vec2.Zero, -0.1f * Box2DX.Common.Settings.Pi);
				box.Friction = 0.62f;
				bd.Position.Set(55.0f - 30.0f, 3.1f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}

			{	// more ground
				PolygonDef box = new PolygonDef();
				BodyDef bd = new BodyDef();

				box.SetAsBox(9.5f, 0.5f, Vec2.Zero, 0.03f * Box2DX.Common.Settings.Pi);
				box.Friction = 0.62f;
				bd.Position.Set(41.0f, 2.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}

			{	// more ground
				PolygonDef box = new PolygonDef();
				BodyDef bd = new BodyDef();

				box.SetAsBox(5.0f, 0.5f, Vec2.Zero, 0.15f * Box2DX.Common.Settings.Pi);
				box.Friction = 0.62f;
				bd.Position.Set(50.0f, 4.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}

			{	// more ground
				PolygonDef box = new PolygonDef();
				BodyDef bd = new BodyDef();

				box.SetAsBox(20.0f, 0.5f);
				box.Friction = 0.62f;
				bd.Position.Set(85.0f, 2.0f);

				Body ground = _world.CreateBody(bd);
				ground.CreateShape(box);
			}
		}
Пример #11
0
        /// <summary>
        /// Add objects to the Box2d world.
        /// </summary>
        protected override void PopulateWorld()
        {
		// ==== Define the ground body ====
			BodyDef groundBodyDef = new BodyDef();
			groundBodyDef.Position.Set(_trackLengthHalf, -1f);

   			// 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, 1f);
            groundShapeDef.Friction = _simParams._defaultFriction;
            groundShapeDef.Restitution = _simParams._defaultRestitution;
            groundShapeDef.Filter.CategoryBits = 0x3;

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

        // ==== Define walker torso.
            BodyDef torsoBodyDef = new BodyDef();
            torsoBodyDef.Position.Set(0f, 1.45f);
            torsoBodyDef.IsBullet = true;

            // Create walker torso.
            _torsoBody = _world.CreateBody(torsoBodyDef);
            PolygonDef torsoShapeDef = new PolygonDef();
            torsoShapeDef.SetAsBox(0.10f, 0.45f);
            torsoShapeDef.Friction = _simParams._defaultFriction;
            torsoShapeDef.Restitution = 0f;
            torsoShapeDef.Density = 2f;
            torsoShapeDef.Filter.CategoryBits = 0x2;
            _torsoBody.CreateShape(torsoShapeDef);
            _torsoBody.SetMassFromShapes();

        // ===== Create legs.
            // Leg joint definition.
            RevoluteJointDef jointDef = new RevoluteJointDef();
            jointDef.CollideConnected = false;
            jointDef.EnableMotor = true;
            jointDef.MaxMotorTorque = 0f;

            // Other re-usable stuff .
            const float legRadius = 0.05f;	// Half the thickness of the leg
            Vec2 upperLegPosBase = new Vec2(0f, 1.05f);
            Vec2 lowerLegPosBase = new Vec2(0f, 0.55f);

        // ===== Create left leg.
            // Upper leg.
            Body upperLeftLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to torso (hip joint)
            jointDef.Initialize(_torsoBody, upperLeftLegBody, upperLegPosBase);
            _leftHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

            // Lower leg.
            _leftLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to upper leg (knee joint)
            jointDef.Initialize(upperLeftLegBody, _leftLowerLegBody, lowerLegPosBase);
            _leftKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

        // ===== Create right leg.
            // Upper leg.
            Body upperRightLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to torso (hip joint)
            jointDef.Initialize(_torsoBody, upperRightLegBody, upperLegPosBase);
            _rightHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef);

            // Lower leg.
            _rightLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1);
            // Join to upper leg (knee joint)
            jointDef.Initialize(upperRightLegBody, _rightLowerLegBody, lowerLegPosBase);
            _rightKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef);
        }
Пример #12
0
        public Bridge()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

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

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

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

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

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

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

                    prevBody = body;
                }

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

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

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);
            }

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
                Body body = _world.CreateBody(bd);
                body.CreateFixture(fd);
            }
        }
        /// <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);
        }
Пример #14
0
        private void CreateLeg(float s, Vec2 wheelAnchor)
        {
            Vec2 p1 = new Vec2(5.4f * s, -6.1f);
            Vec2 p2 = new Vec2(7.2f * s, -1.2f);
            Vec2 p3 = new Vec2(4.3f * s, -1.9f);
            Vec2 p4 = new Vec2(3.1f * s, 0.8f);
            Vec2 p5 = new Vec2(6.0f * s, 1.5f);
            Vec2 p6 = new Vec2(2.5f * s, 3.7f);

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

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

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

                vertices[0] = p1;
                vertices[1] = p2;
                vertices[2] = p3;
                poly1.Set(vertices, 3);

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

                vertices[0] = p1;
                vertices[1] = p3;
                vertices[2] = p2;
                poly1.Set(vertices, 3);

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

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

            BodyDef bd1 = new BodyDef();
            BodyDef bd2 = new BodyDef();
            bd1.Position = _offset;
            bd2.Position = p4 + _offset;

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

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

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

            DistanceJointDef djd = new DistanceJointDef();

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

            djd.Initialize(body1, body2, p2 + _offset, p5 + _offset);
            _world.CreateJoint(djd);

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

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

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

            RevoluteJointDef rjd = new RevoluteJointDef();

            rjd.Initialize(body2, _chassis, p4 + _offset);
            _world.CreateJoint(rjd);
        }
Пример #15
0
        public Gears()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _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);
            }

            {
                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.Position.Set(-3.0f, 12.0f);
                Body body1 = _world.CreateBody(bd1);
                body1.CreateFixture(circle1, 5.0f);

                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.CreateFixture(circle2, 5.0f);

                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.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;

                _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);
            }
        }
Пример #16
0
        public PlayerCharacter(World _world, Vector2 _position)
            : base(_world, _position)
        {
            playerIndex = playerCount++;
            color = PlayerCharacter.Colors[playerIndex];
            accelerate = 0;
            rotate = 0;

            isDead = false;

            this.angVelocity = 0;
            //build the unicycle
            CircleDef circleDef = new CircleDef();
            circleDef.Radius = 1;
            circleDef.Density = 1f;
            circleDef.Friction = 1.0f;
            circleDef.Restitution = 0.0f;
            circleDef.LocalPosition.Set(0, 0);

            wheel = body.CreateShape(circleDef);
            body.SetMassFromShapes();
            body.SetUserData(this); // link body and this to register collisions in this

            //build the head and connect with the wheel
            BodyDef bodydef = new BodyDef();
            bodydef.Position = _position + new Vector2(0.0f, 3.5f);
            bodydef.Angle = 0f;

            chest = _world.CreateBody(bodydef);

            //add the head
            circleDef.Density = 0.0001f;
            circleDef.Radius = 0.75f;
            circleDef.LocalPosition.Set(0, 3);
            head = chest.CreateShape(circleDef);

            rotationHead = _position;

            PolygonDef Boxdef = new PolygonDef();
            Boxdef.SetAsBox(1, 1.5f);
            Boxdef.Density = 0.25f;
            Boxdef.Friction = 0.4f;
            Box2DX.Collision.Shape s2 = chest.CreateShape(Boxdef);
            chest.SetMassFromShapes();
            chest.SetUserData(this);

            //Jointshit
            RevoluteJointDef jointDefKW = new RevoluteJointDef();
            jointDefKW.Body2 = chest;
            jointDefKW.Body1 = body;
            jointDefKW.CollideConnected = false;
            jointDefKW.LocalAnchor2 = new Vector2(-0.0f, -3.8f);
            jointDefKW.LocalAnchor1 = new Vector2(0, 0);
            jointDefKW.EnableLimit = false;

            _world.CreateJoint(jointDefKW);

            // add visuals
            Texture wheelTexture = AssetManager.getTexture(AssetManager.TextureName.ShoopWheel);
            wheelSprite = new AnimatedSprite(wheelTexture, 1.0f, 1, (Vector2)wheelTexture.Size);
            wheelSprite.Scale = Constants.windowScaleFactor * new Vector2(0.2f, 0.2f); //(Vector2.One / (Vector2)wheelTexture.Size * 2F * circleDef.Radius).toScreenCoord() - Vector2.Zero.toScreenCoord();//new Vector2(0.08f, 0.08f);
            wheelSprite.Origin = ((Vector2)wheelSprite.spriteSize) / 2F;

            sheepSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.ShoopInfronUnicycle));
            sheepSprite.Origin = ((Vector2)sheepSprite.Texture.Size) / 2F;
            sheepSprite.Scale = Constants.windowScaleFactor *  new Vector2(_position.X > Constants.worldSizeX / 2F ? -0.2f : 0.2f, 0.2f);
        }
        private void SetupJointsHelper(World world)
        {
            if (children.Count == 0)
            {
                //If there are no children left, set up the final plank and stop recursing
                CreateEndJoint(world, false);
                return;
            }

            //nextBoard is the next piece of the bridge
            RopeBridge nextBoard = children[0] as RopeBridge;

            // Attach this board's body to nextBoard's body with a joint
            /////////////////////////////////////////////////

            RevoluteJointDef joint = new RevoluteJointDef();
            Vector2 anchor = Position + new Vector2(width / 2, 0);
            joint.Initialize(Body, nextBoard.Body, Utils.Convert(anchor));
            world.CreateJoint(joint);

            /////////////////////////////////////////////////

            nextBoard.SetupJointsHelper(world);
        }
Пример #18
0
        public Car(Physics ph,CarParams p,Pose pose)
        {
            this.p = p;

            car_angle0 = Helper.GetRelAngle(new Vec2(1, 0), forwardVec);

            body = ph.CreateBox(new Pose { xc = pose.xc, yc = pose.yc }, new Box2DX.Common.Vec2 { X = p.w, Y = p.h }, new BodyBehaviour { isDynamic = true, k = 0.98f * p.mass });

            bodyFW1 = ph.CreateBox(new Pose { xc = pose.xc, yc = pose.yc + p.h_base / 2 }, new Box2DX.Common.Vec2 { X = p.w / 10, Y = p.h / 10 }, new BodyBehaviour { isDynamic = true, k = 0.01f * p.mass });
            bodyBW1 = ph.CreateBox(new Pose { xc = pose.xc, yc = pose.yc + -p.h_base / 2 }, new Box2DX.Common.Vec2 { X = p.w / 10, Y = p.h / 10 }, new BodyBehaviour { isDynamic = true, k = 0.01f * p.mass });

            var jFW1def = new RevoluteJointDef();
            jFW1def.Initialize(body, bodyFW1, bodyFW1.GetWorldCenter());
            jFW1def.EnableMotor = true;
            jFW1def.MaxMotorTorque = 1000;
            jFW1def.EnableLimit = true;
            jFW1def.LowerAngle = -p.max_steer_angle * Helper.angle_to_rad;
            jFW1def.UpperAngle = p.max_steer_angle * Helper.angle_to_rad;

            jFW1 = (RevoluteJoint)ph.world.CreateJoint(jFW1def);

            var jBW1def = new PrismaticJointDef();
            jBW1def.Initialize(body, bodyBW1, bodyBW1.GetWorldCenter(), new Vec2(1, 0));
            jBW1def.EnableLimit = true;
            jBW1def.UpperTranslation = jBW1def.LowerTranslation = 0;
            jBW1 = (PrismaticJoint)ph.world.CreateJoint(jBW1def);

            //LidarParams lp = new LidarParams() { dir_deg = 0, d0 = 2.2f, dist = 20, fov_deg = 60, x0 = 0, y0 = 0, num_rays = 20 };
            var p1 = new LidarParams();
            p1.InitDefault();
            p1.d0 = p.h / 2 + 0.2f;
            InitLidar(p1);

            body.SetAngle(pose.angle_rad);

            //rcs = new RCS(this);
        }
Пример #19
0
		public RevoluteJoint(RevoluteJointDef def)
			: base(def)
		{
			_localAnchor1 = def.LocalAnchor1;
			_localAnchor2 = def.LocalAnchor2;
			_referenceAngle = def.ReferenceAngle;

			_pivotForce.Set(0.0f, 0.0f);
			_motorForce = 0.0f;
			_limitForce = 0.0f;
			_limitPositionImpulse = 0.0f;

			_lowerAngle = def.LowerAngle;
			_upperAngle = def.UpperAngle;
			_maxMotorTorque = def.MaxMotorTorque;
			_motorSpeed = def.MotorSpeed;
			_enableLimit = def.EnableLimit;
			_enableMotor = def.EnableMotor;
		}
Пример #20
0
		public MotorsAndLimits()
		{
			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();

				RevoluteJointDef rjd = new RevoluteJointDef();

				Body body = null;
				Body prevBody = ground;
				const float y = 8.0f;

				bd.Position.Set(3.0f, y);
				body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

				rjd.Initialize(prevBody, body, new Vec2(0.0f, y));
				rjd.MotorSpeed = 1.0f * Box2DX.Common.Settings.Pi;
				rjd.MaxMotorTorque = 10000.0f;
				rjd.EnableMotor = true;

				_joint1 = (RevoluteJoint)_world.CreateJoint(rjd);

				prevBody = body;

				bd.Position.Set(9.0f, y);
				body = _world.CreateBody(bd);
				body.CreateShape(sd);
				body.SetMassFromShapes();

				rjd.Initialize(prevBody, body, new Vec2(6.0f, y));
				rjd.MotorSpeed = 0.5f * Box2DX.Common.Settings.Pi;
				rjd.MaxMotorTorque = 2000.0f;
				rjd.EnableMotor = true;
				rjd.LowerAngle = -0.5f * Box2DX.Common.Settings.Pi;
				rjd.UpperAngle = 0.5f * Box2DX.Common.Settings.Pi;
				rjd.EnableLimit = true;

				_joint2 = (RevoluteJoint)_world.CreateJoint(rjd);

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

				PrismaticJointDef pjd = new PrismaticJointDef();
				pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new 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;

				_joint3 = (PrismaticJoint)_world.CreateJoint(pjd);
			}
		}
Пример #21
0
        protected override void CreatePhysics(World world, float positionX, float positionY)
        {
            var frontWheelJointDef = new RevoluteJointDef();
            frontWheelJointDef.Initialize(CarBody.Body, FrontWheel.Body, FrontWheel.Body.GetWorldCenter());
            frontWheelJoint = (RevoluteJoint)world.CreateJoint(frontWheelJointDef);

            var backWheelJointDef = new RevoluteJointDef();
            backWheelJointDef.Initialize(CarBody.Body, BackWheel.Body, BackWheel.Body.GetWorldCenter());
            backWheelJoint = (RevoluteJoint)world.CreateJoint(backWheelJointDef);

            var poleJointDef = new RevoluteJointDef();

            var anchor = this.Pole.Body.GetWorldCenter();
            anchor.Y -= Pole.Height  ;
            poleJointDef.CollideConnected = false;
            poleJointDef.EnableLimit = true;
            poleJointDef.LowerAngle = Helper.DegreesToRad(-90);
            poleJointDef.UpperAngle = Helper.DegreesToRad(90);
            poleJointDef.Initialize(CarBody.Body, Pole.Body, anchor);
            poleJoint = (RevoluteJoint)world.CreateJoint(poleJointDef);
        }
Пример #22
0
 public JointTest()
 {
     RevoluteJointDef jointDef = new RevoluteJointDef();
 }
Пример #23
0
        public TheoJansen()
        {
            _offset.Set(0.0f, 8.0f);
            _motorSpeed = 2.0f;
            _motorOn = true;
            Vec2 pivot = new Vec2(0.0f, 0.8f);

            // Ground
            {
                BodyDef bd = new BodyDef();
                Body ground = _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);

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

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

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

                BodyDef bd = new BodyDef();
                bd.Position.Set(-40.0f + 2.0f * i, 0.5f);

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

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

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.Position = pivot + _offset;
                _chassis = _world.CreateBody(bd);
                _chassis.CreateFixture(sd);
            }

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

                FixtureDef sd = new FixtureDef();
                sd.Density = 1.0f;
                sd.Shape = shape;
                sd.Filter.GroupIndex = -1;
                BodyDef bd = new BodyDef();
                bd.Position = pivot + _offset;
                _wheel = _world.CreateBody(bd);
                _wheel.CreateFixture(sd);
            }

            {
                RevoluteJointDef jd = new RevoluteJointDef();
                jd.Initialize(_wheel, _chassis, pivot + _offset);
                jd.CollideConnected = false;
                jd.MotorSpeed = _motorSpeed;
                jd.MaxMotorTorque = 400.0f;
                jd.EnableMotor = _motorOn;
                _motorJoint = (RevoluteJoint)_world.CreateJoint(jd);
            }

            Vec2 wheelAnchor;

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

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

            _wheel.SetTransform(_wheel.GetPosition(), 120.0f * Box2DX.Common.Settings.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);

            _wheel.SetTransform(_wheel.GetPosition(), -120.0f * Box2DX.Common.Settings.PI / 180.0f);
            CreateLeg(-1.0f, wheelAnchor);
            CreateLeg(1.0f, wheelAnchor);
        }
Пример #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.CreateShape(sd);
			}
			// Upper static body
			{
				PolygonDef sd = new PolygonDef();
				sd.SetAsBox(20.0f, 0.50f, new Vec2(0.0f, 0.0f), 0.047f * Box2DX.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.CreateShape(sd);
				sd.SetAsBox(15.0f, 0.50f, new Vec2(-15.0f, 12.5f), 0.0f);
				g.CreateShape(sd);

				sd.SetAsBox(20.0f, 0.5f, new Vec2(0.0f, -25.0f), -0.5f);
				g.CreateShape(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.CreateShape(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.CreateShape(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 * Box2DX.Common.Settings.Pi;
				Body g = _world.CreateBody(bd);
				g.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(cd);
				sd.SetAsBox(2.0f, 0.50f);
				body.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(cd);
				sd.SetAsBox(3.7f, 0.5f);
				body.CreateShape(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.CreateShape(sd);
				sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi / 3.0f);
				rightmotor.CreateShape(sd);
				sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi * 2.0f / 3.0f);
				rightmotor.CreateShape(sd);
				cd.Radius = 4.2f;
				rightmotor.CreateShape(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.CreateShape(sd);
				sd.SetAsBox(8.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi * .5f);
				body.CreateShape(sd);
				cd.Radius = 7.0f;
				cd.Friction = 0.9f;
				body.CreateShape(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.CreateShape(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.CreateShape(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.CreateShape(sd);
				sd.SetAsBox(2.0f, 1.5f, new Vec2(-5.0f, 0.0f), 0.0f);
				pusher.SetMassFromShapes();
				pusher.CreateShape(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.CreateShape(sd);

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

				sd.SetAsBox(19.0f, 0.5f, new Vec2(-9.0f, 15.0f), -0.05f);
				g.CreateShape(sd);
				sd.SetAsBox(4.7f, 0.5f, new Vec2(15.0f, 11.5f), -0.5f);
				g.CreateShape(sd);
				// below compressor
				sd.SetAsBox(26.0f, 0.3f, new Vec2(17.0f, -4.4f), -0.02f);
				g.CreateShape(sd);
				cd.Radius = 1.0f; cd.Friction = 1.0f;
				cd.LocalPosition = new Vec2(29.0f, -6.0f);
				g.CreateShape(cd);
				cd.Radius = 0.7f;
				cd.LocalPosition = new Vec2(-2.0f, -4.5f);
				g.CreateShape(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.CreateShape(sd);
				sd.SetAsBox(7.0f, 0.5f, new Vec2(-3.5f, -5.5f), 0.0f);
				_elev.CreateShape(sd);
				sd.SetAsBox(0.5f, 2.5f, new Vec2(-11.0f, -3.5f), 0.0f);
				_elev.CreateShape(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.CreateShape(sd);
				sd.SetAsBox(2.5f, 0.5f, new Vec2(3.0f, -2.0f), Box2DX.Common.Settings.Pi / 2.0f);
				body.CreateShape(sd);
				sd.SetAsBox(4.6f, 0.5f, new Vec2(7.8f, -4.0f), 0.0f);
				body.CreateShape(sd);
				sd.SetAsBox(0.5f, 4.5f, new Vec2(12.0f, 0.0f), 0.0f);
				body.CreateShape(sd);

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

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

				RevoluteJointDef jr = new RevoluteJointDef();
				jr.Initialize(_elev, body, bd.Position);
				jr.EnableLimit = true;
				jr.LowerAngle = -0.2f;
				jr.UpperAngle = Box2DX.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.CreateShape(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.CreateShape(sd);
						body.SetMassFromShapes();
					}
				}
			}
		}
Пример #25
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.CreateShape(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.CreateShape(sd);
				body.SetMassFromShapes();

				rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f));
				rjd.MotorSpeed = 1.0f * Box2DX.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.CreateShape(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.CreateShape(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.CreateShape(sd);
				body.SetMassFromShapes();
			}
		}
Пример #26
0
        public SliderCrank()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = _world.CreateBody(bd);

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

                Body prevBody = ground;

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

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

                    RevoluteJointDef rjd = new RevoluteJointDef();
                    rjd.Initialize(prevBody, body, new Vec2(0.0f, 5.0f));
                    rjd.MotorSpeed = 1.0f * Box2DX.Common.Settings.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.Position.Set(0.0f, 13.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

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

                    prevBody = body;
                }

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

                    BodyDef bd = new BodyDef();
                    bd.Position.Set(0.0f, 17.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);

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

                    BodyDef bd = new BodyDef();
                    bd.Position.Set(0.0f, 23.0f);
                    Body body = _world.CreateBody(bd);
                    body.CreateFixture(shape, 2.0f);
                }
            }
        }