コード例 #1
0
ファイル: Prismatic.cs プロジェクト: CloneDeath/Box2D.Net
		public Prismatic() {
			Body ground = null;
			{
				BodyDef bd = new BodyDef();
				ground = m_world.CreateBody(bd);

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

			{
				PolygonShape shape = new PolygonShape();
				shape.SetAsBox(2.0f, 0.5f);
				shape.Density = 5;

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

				PrismaticJointDef pjd = new PrismaticJointDef();

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

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

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

				m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
			}
		}
コード例 #2
0
		public PrismaticJoint(PrismaticJointDef def) : base(def)
		{
			m_localAnchorA = def.localAnchorA;
			m_localAnchorB = def.localAnchorB;
			m_localXAxisA = def.localAxisA;
			m_localXAxisA.Normalize();
			m_localYAxisA = Utilities.Cross(1.0f, m_localXAxisA);
			m_referenceAngle = def.referenceAngle;

			m_impulse.SetZero();
			m_motorMass = 0.0f;
			m_motorImpulse = 0.0f;

			m_lowerTranslation = def.lowerTranslation;
			m_upperTranslation = def.upperTranslation;
			m_maxMotorForce = def.maxMotorForce;
			m_motorSpeed = def.motorSpeed;
			m_enableLimit = def.enableLimit;
			m_enableMotor = def.enableMotor;
			m_limitState = LimitState.e_inactiveLimit;

			m_axis.SetZero();
			m_perp.SetZero();
		}
コード例 #3
0
ファイル: BodyTypes.cs プロジェクト: CloneDeath/Box2D.Net
		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);
			}
		}
コード例 #4
0
		public CollisionFiltering()
		{
			// Ground body
			{
				EdgeShape shape = new EdgeShape();
				shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

				FixtureDef sd = new FixtureDef();
				sd.shape = shape;
				sd.friction = 0.3f;

				BodyDef bd = new BodyDef();
				Body ground = m_world.CreateBody(bd);
				ground.CreateFixture(sd);
			}

			// Small triangle
			Vec2[] vertices = new Vec2[3];
			vertices[0].Set(-1.0f, 0.0f);
			vertices[1].Set(1.0f, 0.0f);
			vertices[2].Set(0.0f, 2.0f);
			PolygonShape polygon = new PolygonShape();
			polygon.Set(vertices, 3);

			FixtureDef triangleShapeDef = new FixtureDef();
			triangleShapeDef.shape = polygon;
			triangleShapeDef.Density = 1.0f;

			triangleShapeDef.Filter.GroupIndex = k_smallGroup;
			triangleShapeDef.Filter.CategoryBits = k_triangleCategory;
			triangleShapeDef.Filter.MaskBits = k_triangleMask;

			BodyDef triangleBodyDef = new BodyDef();
			triangleBodyDef.type = BodyType._dynamicBody;
			triangleBodyDef.Position.Set(-5.0f, 2.0f);

			Body body1 = m_world.CreateBody(triangleBodyDef);
			body1.CreateFixture(triangleShapeDef);

			// Large triangle (recycle definitions)
			vertices[0] *= 2.0f;
			vertices[1] *= 2.0f;
			vertices[2] *= 2.0f;
			polygon.Set(vertices, 3);
			triangleShapeDef.Filter.GroupIndex = k_largeGroup;
			triangleBodyDef.Position.Set(-5.0f, 6.0f);
			triangleBodyDef.fixedRotation = true; // look at me!

			Body body2 = m_world.CreateBody(triangleBodyDef);
			body2.CreateFixture(triangleShapeDef);

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

				PolygonShape p = new PolygonShape();
				p.SetAsBox(0.5f, 1.0f);
				body.CreateFixture(p);

				PrismaticJointDef jd = new PrismaticJointDef();
				jd.bodyA = body2;
				jd.bodyB = body;
				jd.enableLimit = true;
				jd.localAnchorA.Set(0.0f, 4.0f);
				jd.localAnchorB.SetZero();
				jd.localAxisA.Set(0.0f, 1.0f);
				jd.lowerTranslation = -1.0f;
				jd.upperTranslation = 1.0f;

				m_world.CreateJoint(jd);
			}

			// Small box
			polygon.SetAsBox(1.0f, 0.5f);
			FixtureDef boxShapeDef = new FixtureDef();
			boxShapeDef.shape = polygon;
			boxShapeDef.Density = 1.0f;
			boxShapeDef.restitution = 0.1f;

			boxShapeDef.Filter.GroupIndex = k_smallGroup;
			boxShapeDef.Filter.CategoryBits = k_boxCategory;
			boxShapeDef.Filter.MaskBits = k_boxMask;

			BodyDef boxBodyDef = new BodyDef();
			boxBodyDef.type = BodyType._dynamicBody;
			boxBodyDef.Position.Set(0.0f, 2.0f);

			Body body3 = m_world.CreateBody(boxBodyDef);
			body3.CreateFixture(boxShapeDef);

			// Large box (recycle definitions)
			polygon.SetAsBox(2.0f, 1.0f);
			boxShapeDef.Filter.GroupIndex = k_largeGroup;
			boxBodyDef.Position.Set(0.0f, 6.0f);

			Body body4 = m_world.CreateBody(boxBodyDef);
			body4.CreateFixture(boxShapeDef);

			// Small circle
			CircleShape circle = new CircleShape();
			circle.m_radius = 1.0f;

			FixtureDef circleShapeDef = new FixtureDef();
			circleShapeDef.shape = circle;
			circleShapeDef.Density = 1.0f;

			circleShapeDef.Filter.GroupIndex = k_smallGroup;
			circleShapeDef.Filter.CategoryBits = k_circleCategory;
			circleShapeDef.Filter.MaskBits = k_circleMask;

			BodyDef circleBodyDef = new BodyDef();
			circleBodyDef.type = BodyType._dynamicBody;
			circleBodyDef.Position.Set(5.0f, 2.0f);
		
			Body body5 = m_world.CreateBody(circleBodyDef);
			body5.CreateFixture(circleShapeDef);

			// Large circle
			circle.m_radius *= 2.0f;
			circleShapeDef.Filter.GroupIndex = k_largeGroup;
			circleBodyDef.Position.Set(5.0f, 6.0f);

			Body body6 = m_world.CreateBody(circleBodyDef);
			body6.CreateFixture(circleShapeDef);
		}
コード例 #5
0
ファイル: Gears.cs プロジェクト: CloneDeath/Box2D.Net
		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);
			}
		}
コード例 #6
0
ファイル: SliderCrank.cs プロジェクト: CloneDeath/Box2D.Net
		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);
				}
			}
		}