internal RevoluteJoint(RevoluteJointDef def)
            : base(def)
        {
            _localAnchor1   = def.localAnchorA;
            _localAnchor2   = def.localAnchorB;
            _referenceAngle = def.referenceAngle;

            _impulse      = Vector3.zero;
            _motorImpulse = 0.0f;

            _lowerAngle     = def.lowerAngle;
            _upperAngle     = def.upperAngle;
            _maxMotorTorque = def.maxMotorTorque;
            _motorSpeed     = def.motorSpeed;
            _enableLimit    = def.enableLimit;
            _enableMotor    = def.enableMotor;
            _limitState     = LimitState.Inactive;
        }
Exemple #2
0
		public Chain()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

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

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

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

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

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

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

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

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

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

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

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

			shape.SetAsBox(offset, 0.25f * a, new Vec2(0, -a), 0.0f);
			body.CreateFixture(shape);

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

			RevoluteJointDef jointDef = new RevoluteJointDef();
			jointDef.bodyA = body;
			jointDef.localAnchorB = h;

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

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

			return body;
		}
Exemple #4
0
		public Tumbler()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);
			}

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

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

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

			m_count = 0;
		}
		public MobileBalanced()
		{
			Body ground;

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

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

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

			RevoluteJointDef jointDef = new RevoluteJointDef();
			jointDef.bodyA = ground;
			jointDef.bodyB = root;
			jointDef.localAnchorA.SetZero();
			jointDef.localAnchorB = h;
			m_world.CreateJoint(jointDef);
		}
Exemple #6
0
		public Revolute()
		{
			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));

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				//fd.Filter.CategoryBits = 2;

				ground.CreateFixture(fd);
			}

			{
				CircleShape shape = new CircleShape();
				shape.m_radius = 0.5f;
				shape.Density = 5;

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

				RevoluteJointDef rjd = new RevoluteJointDef();

				bd.Position.Set(-10.0f, 20.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(shape);

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

				rjd.Initialize(ground, body, new Vec2(-10.0f, 12.0f));
				rjd.motorSpeed = 1.0f * (float)Math.PI;
				rjd.maxMotorTorque = 10000.0f;
				rjd.enableMotor = false;
				rjd.lowerAngle = -0.25f * (float)Math.PI;
				rjd.upperAngle = 0.5f * (float)Math.PI;
				rjd.enableLimit = true;
				rjd.collideConnected = true;

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

			{
				CircleShape circle_shape = new CircleShape();
				circle_shape.m_radius = 3.0f;

				BodyDef circle_bd = new BodyDef();
				circle_bd.type = BodyType._dynamicBody;
				circle_bd.Position.Set(5.0f, 30.0f);

				FixtureDef fd = new FixtureDef();
				fd.Density = 5.0f;
				fd.Filter.MaskBits = 1;
				fd.shape = circle_shape;

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

				PolygonShape polygon_shape = new PolygonShape();
				polygon_shape.SetAsBox(10.0f, 0.2f, new Vec2(-10.0f, 0.0f), 0.0f);
				polygon_shape.Density = 2;

				BodyDef polygon_bd = new BodyDef();
				polygon_bd.Position.Set(20.0f, 10.0f);
				polygon_bd.type = BodyType._dynamicBody;
				polygon_bd.bullet = true;
				Body polygon_body = m_world.CreateBody(polygon_bd);
				
				polygon_body.CreateFixture(polygon_shape);

				RevoluteJointDef rjd = new RevoluteJointDef();
				rjd.Initialize(ground, polygon_body, new Vec2(20.0f, 10.0f));
				rjd.lowerAngle = -0.25f * (float)Math.PI;
				rjd.upperAngle = 0.0f * (float)Math.PI;
				rjd.enableLimit = true;
				m_world.CreateJoint(rjd);
			}

			// Tests mass computation of a small object far from the origin
			{
				BodyDef bodyDef = new BodyDef();
				bodyDef.type = BodyType._dynamicBody;
				Body body = m_world.CreateBody(bodyDef);
		
				PolygonShape polyShape = new PolygonShape();
				Vec2[] verts = new Vec2[3];
				verts[0].Set( 17.63f, 36.31f );
				verts[1].Set( 17.52f, 36.69f );
				verts[2].Set( 17.19f, 36.36f );
				polyShape.Set(verts, 3);
		
				FixtureDef polyFixtureDef = new FixtureDef();
				polyFixtureDef.shape = polyShape;
				polyFixtureDef.Density = 1;

				body.CreateFixture(polyFixtureDef);	//assertion hits inside here
			}

		}
Exemple #7
0
		public BodyTypes()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

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

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

				ground.CreateFixture(fd);
			}

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

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

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

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

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

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

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

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

				m_world.CreateJoint(pjd);

				m_speed = 3.0f;
			}

			// Create a payload
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 8.0f);
				Body body = m_world.CreateBody(bd);

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

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

				body.CreateFixture(fd);
			}
		}
		public RopeJointTest()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

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

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

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 20.0f;
				fd.friction = 0.2f;
				fd.Filter.CategoryBits = 0x0001;
				fd.Filter.MaskBits = 0xFFFF & ~0x0002;

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

				const int N = 10;
				const float y = 15.0f;
				m_ropeDef.localAnchorA.Set(0.0f, y);

				Body prevBody = ground;
				for (int i = 0; i < N; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(0.5f + 1.0f * i, y);
					if (i == N - 1)
					{
						shape.SetAsBox(1.5f, 1.5f);
						fd.Density = 100.0f;
						fd.Filter.CategoryBits = 0x0002;
						bd.Position.Set(1.0f * i, y);
						bd.angularDamping = 0.4f;
					}

					Body body = m_world.CreateBody(bd);

					body.CreateFixture(fd);

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

					prevBody = body;
				}

				m_ropeDef.localAnchorB.SetZero();

				float extraLength = 0.01f;
				m_ropeDef.maxLength = N - 1.0f + extraLength;
				m_ropeDef.bodyB = prevBody;
			}

			{
				m_ropeDef.bodyA = ground;
				m_rope = m_world.CreateJoint(m_ropeDef);
			}
		}
Exemple #9
0
		public Car()
		{		
			m_hz = 4.0f;
			m_zeta = 0.7f;
			m_speed = 50.0f;

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

				EdgeShape shape = new EdgeShape();

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 0.0f;
				fd.friction = 0.6f;

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

				float[] hs = new float[]{0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f};

				float x = 20.0f, y1 = 0.0f, dx = 5.0f;

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

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

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

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

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

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

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

			// Teeter
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(140.0f, 1.0f);
				bd.type = BodyType._dynamicBody;
				Body body = m_world.CreateBody(bd);

				PolygonShape box = new PolygonShape();
				box.SetAsBox(10.0f, 0.25f);
				box.Density = 1;
				body.CreateFixture(box);

				RevoluteJointDef jd = new RevoluteJointDef();
				jd.Initialize(ground, body, body.GetPosition());
				jd.lowerAngle = -8.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 8.0f * (float)Math.PI / 180.0f;
				jd.enableLimit = true;
				m_world.CreateJoint(jd);

				body.ApplyAngularImpulse(100.0f, true);
			}

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

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

				RevoluteJointDef jd = new RevoluteJointDef();

				Body prevBody = ground;
				for (int i = 0; i < N; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(161.0f + 2.0f * i, -0.125f);
					Body body = m_world.CreateBody(bd);
					body.CreateFixture(fd);

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

					prevBody = body;
				}

				Vec2 anchor2 = new Vec2(160.0f + 2.0f * N, -0.125f);
				jd.Initialize(prevBody, ground, anchor2);
				m_world.CreateJoint(jd);
			}

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

				Body body = null;
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;

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

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

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

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

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

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

				CircleShape circle = new CircleShape();
				circle.m_radius = 0.4f;

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(0.0f, 1.0f);
				m_car = m_world.CreateBody(bd);
				m_car.CreateFixture(chassis);

				FixtureDef fd = new FixtureDef();
				fd.shape = circle;
				fd.Density = 1.0f;
				fd.friction = 0.9f;

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

				bd.Position.Set(1.0f, 0.4f);
				m_wheel2 = m_world.CreateBody(bd);
				m_wheel2.CreateFixture(fd);

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

				jd.Initialize(m_car, m_wheel1, m_wheel1.GetPosition(), axis);
				jd.motorSpeed = 0.0f;
				jd.maxMotorTorque = 20.0f;
				jd.enableMotor = true;
				jd.frequencyHz = m_hz;
				jd.dampingRatio = m_zeta;
				m_spring1 = (WheelJoint)m_world.CreateJoint(jd);

				jd.Initialize(m_car, m_wheel2, m_wheel2.GetPosition(), axis);
				jd.motorSpeed = 0.0f;
				jd.maxMotorTorque = 10.0f;
				jd.enableMotor = false;
				jd.frequencyHz = m_hz;
				jd.dampingRatio = m_zeta;
				m_spring2 = (WheelJoint)m_world.CreateJoint(jd);
			}
		}
Exemple #10
0
		internal RevoluteJoint(RevoluteJointDef def): base(def)
		{
			m_localAnchorA = def.localAnchorA;
			m_localAnchorB = def.localAnchorB;
			m_referenceAngle = def.referenceAngle;

			m_impulse.SetZero();
			m_motorImpulse = 0.0f;

			m_lowerAngle = def.lowerAngle;
			m_upperAngle = def.upperAngle;
			m_maxMotorTorque = def.maxMotorTorque;
			m_motorSpeed = def.motorSpeed;
			m_enableLimit = def.enableLimit;
			m_enableMotor = def.enableMotor;
			m_limitState = LimitState.e_inactiveLimit;
		}
Exemple #11
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);
			}
		}
Exemple #12
0
		public 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] = new Vec2(0, 0);
				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] = new Vec2(0, 0);
				vertices[1] = p6 - p4;
				vertices[2] = p5 - p4;
				poly2.Set(vertices, 3);
			}

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

			BodyDef bd1 = new BodyDef();
			BodyDef bd2 = new BodyDef();
			bd1.type = BodyType._dynamicBody;
			bd2.type = BodyType._dynamicBody;
			bd1.Position = m_offset;
			bd2.Position = p4 + m_offset;

			bd1.angularDamping = 10.0f;
			bd2.angularDamping = 10.0f;

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

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

			DistanceJointDef djd = new DistanceJointDef();

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

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

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

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

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

			RevoluteJointDef rjd = new RevoluteJointDef();

			rjd.Initialize(body2, m_chassis, p4 + m_offset);
			m_world.CreateJoint(rjd);
		}
Exemple #13
0
		public TheoJansen()
		{
			m_offset.Set(0.0f, 8.0f);
			m_motorSpeed = 2.0f;
			m_motorOn = true;
			Vec2 pivot = new Vec2(0.0f, 0.8f);

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

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

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

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

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

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

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

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

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

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

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

			{
				RevoluteJointDef jd = new RevoluteJointDef();
				jd.Initialize(m_wheel, m_chassis, pivot + m_offset);
				jd.collideConnected = false;
				jd.motorSpeed = m_motorSpeed;
				jd.maxMotorTorque = 400.0f;
				jd.enableMotor = m_motorOn;
				m_motorJoint = (RevoluteJoint)m_world.CreateJoint(jd);
			}

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

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

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

			m_wheel.SetTransform(m_wheel.GetPosition(), -120.0f * (float)Math.PI / 180.0f);
			CreateLeg(-1.0f, wheelAnchor);
			CreateLeg(1.0f, wheelAnchor);
		}
Exemple #14
0
		Bridge()
		{
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

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

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

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

				RevoluteJointDef jd = new RevoluteJointDef();

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

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

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

				Vec2 anchor2 = new Vec2(-15.0f + 1.0f * e_count, 5.0f);
				jd.Initialize(prevBody, ground, anchor2);
				m_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.type = BodyType._dynamicBody;
				bd.Position.Set(-8.0f + 8.0f * i, 12.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-6.0f + 6.0f * i, 10.0f);
				Body body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}
		}
Exemple #15
0
		public Dominos()
		{
			Body b1;
			{
				EdgeShape shape = new EdgeShape();
				shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
				shape.Density = 0;

				BodyDef bd = new BodyDef();
				b1 = m_world.CreateBody(bd);
				b1.CreateFixture(shape);
			}

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

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

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

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

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

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

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

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

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-0.9f, 1.0f);
				bd.angle = -0.15f;

				b3 = m_world.CreateBody(bd);
				b3.CreateFixture(shape);
			}

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

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

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(-10.0f, 15.0f);
				b4 = m_world.CreateBody(bd);
				shape.Density = 10;
				shape.Density = 10;
				b4.CreateFixture(shape);
			}

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

			Body b5;
			{
				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(6.5f, 3.0f);
				b5 = m_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);
			m_world.CreateJoint(jd);

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

				BodyDef bd = new BodyDef();
				bd.type = BodyType._dynamicBody;
				bd.Position.Set(6.5f, 4.1f);
				b6 = m_world.CreateBody(bd);
				shape.Density = 30;
				b6.CreateFixture(shape);
			}

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

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

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

				b7 = m_world.CreateBody(bd);
				shape.Density = 10;
				b7.CreateFixture(shape);
			}

			DistanceJointDef djd = new DistanceJointDef();
			djd.bodyA = b3;
			djd.bodyB = b7;
			djd.localAnchorA.Set(6.0f, 0.0f);
			djd.localAnchorB.Set(0.0f, -1.0f);
			Vec2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA);
			djd.length = d.Length();
			m_world.CreateJoint(djd);

			{
				float radius = 0.2f;

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

				for (int i = 0; i < 4; ++i)
				{
					BodyDef bd = new BodyDef();
					bd.type = BodyType._dynamicBody;
					bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f);
					Body body = m_world.CreateBody(bd);
					shape.Density = 10;
					body.CreateFixture(shape);
				}
			}
		}
Exemple #16
0
		public Pinball()
		{
			// Ground body
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

				Vec2[] vs = new Vec2[5];
				vs[0].Set(0.0f, -2.0f);
				vs[1].Set(8.0f, 6.0f);
				vs[2].Set(8.0f, 20.0f);
				vs[3].Set(-8.0f, 20.0f);
				vs[4].Set(-8.0f, 6.0f);

				ChainShape loop = new ChainShape();
				loop.CreateLoop(vs, 5);
				FixtureDef fd = new FixtureDef();
				fd.shape = loop;
				fd.Density = 0.0f;
				ground.CreateFixture(fd);
			}

			// Flippers
			{
				Vec2 p1 = new Vec2(-2.0f, 0.0f);
				Vec2 p2 = new Vec2(2.0f, 0.0f);

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

				bd.Position = p1;
				Body leftFlipper = m_world.CreateBody(bd);

				bd.Position = p2;
				Body rightFlipper = m_world.CreateBody(bd);

				PolygonShape box = new PolygonShape();
				box.SetAsBox(1.75f, 0.1f);

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

				leftFlipper.CreateFixture(fd);
				rightFlipper.CreateFixture(fd);

				RevoluteJointDef jd = new RevoluteJointDef();
				jd.bodyA = ground;
				jd.localAnchorB.SetZero();
				jd.enableMotor = true;
				jd.maxMotorTorque = 1000.0f;
				jd.enableLimit = true;

				jd.motorSpeed = 0.0f;
				jd.localAnchorA = p1;
				jd.bodyB = leftFlipper;
				jd.lowerAngle = -30.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 5.0f * (float)Math.PI / 180.0f;
				m_leftJoint = (RevoluteJoint)m_world.CreateJoint(jd);

				jd.motorSpeed = 0.0f;
				jd.localAnchorA = p2;
				jd.bodyB = rightFlipper;
				jd.lowerAngle = -5.0f * (float)Math.PI / 180.0f;
				jd.upperAngle = 30.0f * (float)Math.PI / 180.0f;
				m_rightJoint = (RevoluteJoint)m_world.CreateJoint(jd);
			}

			// Circle character
			{
				BodyDef bd = new BodyDef();
				bd.Position.Set(1.0f, 15.0f);
				bd.type = BodyType._dynamicBody;
				bd.bullet = true;

				m_ball = m_world.CreateBody(bd);

				CircleShape shape = new CircleShape();
				shape.m_radius = 0.2f;

				FixtureDef fd = new FixtureDef();
				fd.shape = shape;
				fd.Density = 1.0f;
				m_ball.CreateFixture(fd);
			}

			m_button = false;
		}
Exemple #17
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);
				}
			}
		}