public LineJoint() { Body ground = null; { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 2.0f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 7.0f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 1.0f); LineJointDef jd = new LineJointDef(); Vector2 axis = new Vector2(2.0f, 1.0f); axis.Normalize(); jd.Initialize(ground, body, new Vector2(0.0f, 8.5f), axis); jd.motorSpeed = 0.0f; jd.maxMotorForce = 100.0f; jd.enableMotor = true; jd.lowerTranslation = -4.0f; jd.upperTranslation = 4.0f; jd.enableLimit = true; _world.CreateJoint(jd); } }
public Breakable() { // Ground body { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Breakable dynamic body { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 40.0f); bd.angle = 0.25f * Settings.b2_pi; _body1 = _world.CreateBody(bd); _shape1 = new PolygonShape(); _shape1.SetAsBox(0.5f, 0.5f, new Vector2(-0.5f, 0.0f), 0.0f); _piece1 = _body1.CreateFixture(_shape1, 1.0f); _shape2 = new PolygonShape(); _shape2.SetAsBox(0.5f, 0.5f, new Vector2(0.5f, 0.0f), 0.0f); _piece2 = _body1.CreateFixture(_shape2, 1.0f); } _break = false; _broke = false; }
VaryingRestitution() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { CircleShape shape = new CircleShape(); shape._radius = 1.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; float[] restitution = new float[7] {0.0f, 0.1f, 0.3f, 0.5f, 0.75f, 0.9f, 1.0f}; for (int i = 0; i < 7; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f + 3.0f * i, 20.0f); Body body = _world.CreateBody(bd); fd.restitution = restitution[i]; body.CreateFixture(fd); } } }
public SphereStack() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { CircleShape shape = new CircleShape(); shape._radius = 1.0f; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 4.0f + 3.0f * i); _bodies[i] = _world.CreateBody(bd); _bodies[i].CreateFixture(shape, 1.0f); //_bodies[i].SetLinearVelocity(new Vector2(0.0f, -100.0f)); } } }
public SensorTest() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } #if false { FixtureDef sd; sd.SetAsBox(10.0f, 2.0f, new Vector2(0.0f, 20.0f), 0.0f); sd.isSensor = true; _sensor = ground.CreateFixture(&sd); } #else { CircleShape shape = new CircleShape(); shape._radius = 5.0f; shape._p = new Vector2(0.0f, 10.0f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.isSensor = true; _sensor = ground.CreateFixture(fd); } #endif } { CircleShape shape = new CircleShape(); shape._radius = 1.0f; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f + 3.0f * i, 20.0f); bd.userData = i; _touching[i] = false; _bodies[i] = _world.CreateBody(bd); _bodies[i].CreateFixture(shape, 1.0f); } } }
public ContinuousTest() { { BodyDef bd = new BodyDef(); bd.position = new Vector2(0.0f, 0.0f); Body body = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f)); body.CreateFixture(shape, 1.0f); shape.SetAsBox(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0.0f); body.CreateFixture(shape, 1.0f); } #if true { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 20.0f); //bd.angle = 0.1f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.0f, 0.1f); _body = _world.CreateBody(bd); _body.CreateFixture(shape, 1.0f); Launch(); } #else { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 0.5f); Body body = _world.CreateBody(bd); CircleShape shape = new CircleShape(); shape._p = new Vector2Zero(); shape._radius = 0.5f; body.CreateFixture(shape, 1.0f); //bd.bullet = true; bd.position = new Vector2(0.0f, 10.0f); body = _world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.SetLinearVelocity(new Vector2(0.0f, -100.0f)); } #endif }
VerticalStack() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(20.0f, 0.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); } float[] xs = new float[5] {0.0f, -10.0f, -5.0f, 5.0f, 10.0f}; for (int j = 0; j < (int)StackOptions.e_columnCount; ++j) { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < (int)StackOptions.e_rowCount; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; int n = j * e_rowCount + i; Debug.Assert(n < e_rowCount * e_columnCount); _indices[n] = n; bd.userData = _indices[n]; float x = 0.0f; //float x = Rand.RandomFloat-0.02f, 0.02f); //float x = i % 2 == 0 ? -0.025f : 0.025f; bd.position = new Vector2(xs[j] + x, 0.752f + 1.54f * i); Body body = _world.CreateBody(bd); _bodies[n] = body; body.CreateFixture(fd); } } _bullet = null; }
public Confined() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); // Floor shape.SetAsEdge(new Vector2(-10.0f, 0.0f), new Vector2(10.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); // Left wall shape.SetAsEdge(new Vector2(-10.0f, 0.0f), new Vector2(-10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); // Right wall shape.SetAsEdge(new Vector2(10.0f, 0.0f), new Vector2(10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); // Roof shape.SetAsEdge(new Vector2(-10.0f, 20.0f), new Vector2(10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); } float radius = 0.5f; CircleShape shape2 = new CircleShape(); shape2._p = Vector2.Zero; shape2._radius = radius; FixtureDef fd = new FixtureDef(); fd.shape = shape2; fd.density = 1.0f; fd.friction = 0.1f; for (int j = 0; j < e_columnCount; ++j) { for (int i = 0; i < e_rowCount; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } } _world.Gravity = new Vector2(0.0f, 0.0f); }
public Bridge() { Body ground; { PolygonDef sd = new PolygonDef(); sd.ShapeType = ShapeType.e_polygonShape; sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position = new Vector(0, -10); ground = world.CreateBody(bd); ground.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.125f); sd.Density = 20.0f; sd.Friction = 0.2f; BodyDef bd = new BodyDef(); bd.BodyType = BodyType.e_dynamicBody; RevoluteJointDef jd = new RevoluteJointDef(); const float numPlanks = 30; Body prevBody = ground; for (float i = 0; i < numPlanks; ++i) { bd.Position = new Vector(-14.5f + i, 5); Body body = world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); Vector anchor = new Vector(-15 + i, 5); jd.Initialize(prevBody, body, anchor); world.CreateJoint(jd); prevBody = body; } Vector anchor2 = new Vector(-15 + numPlanks, 5); jd.Initialize(prevBody, ground, anchor2); world.CreateJoint(jd); } }
public Prismatic() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.0f, 0.5f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f, 10.0f); bd.angle = 0.5f * (float)Settings.b2_pi; Body body = _world.CreateBody(bd); body.CreateFixture(shape, 5.0f); PrismaticJointDef pjd = new PrismaticJointDef(); // Bouncy limit Vector2 axis = new Vector2(2, 1); axis.Normalize(); pjd.Initialize(ground, body, new Vector2(0.0f, 0.0f), axis); // Non-bouncy limit //pjd.Initialize(ground, body, new Vector2(-10.0f, 10.0f), new Vector2(1.0f, 0.0f)); pjd.motorSpeed = 10.0f; pjd.maxMotorForce = 1000.0f; pjd.enableMotor = true; pjd.lowerTranslation = 0.0f; pjd.upperTranslation = 20.0f; pjd.enableLimit = true; _joint = (PrismaticJoint)_world.CreateJoint(pjd); } }
public OneSidedPlatform() { // Ground { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Platform { BodyDef bd = new BodyDef(); bd.position = new Vector2(0.0f, 10.0f); Body body = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(3.0f, 0.5f); _platform = body.CreateFixture(shape, 0.0f); _bottom = 10.0f - 0.5f; _top = 10.0f + 0.5f; } // Actor { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.0f, 12.0f); Body body = _world.CreateBody(bd); _radius = 0.5f; CircleShape shape = new CircleShape(); shape._radius = _radius; _character = body.CreateFixture(shape, 20.0f); body.SetLinearVelocity(new Vector2(0.0f, -50.0f)); _state = State.e_unknown; } }
Pulleys() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { float a = 2.0f; float b = 4.0f; float y = 16.0f; float L = 12.0f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, b); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f, y); Body body1 = _world.CreateBody(bd); body1.CreateFixture(shape, 5.0f); bd.position = new Vector2(10.0f, y); Body body2 = _world.CreateBody(bd); body2.CreateFixture(shape, 5.0f); PulleyJointDef pulleyDef = new PulleyJointDef(); Vector2 anchor1 = new Vector2(-10.0f, y + b); Vector2 anchor2 = new Vector2(10.0f, y + b); Vector2 groundAnchor1 = new Vector2(-10.0f, y + b + L); Vector2 groundAnchor2 = new Vector2(10.0f, y + b + L); pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 2.0f); _joint1 = (PulleyJoint)_world.CreateJoint(pulleyDef); } }
public Chain() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.6f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); jd.collideConnected = false; float y = 25.0f; Body prevBody = ground; for (int i = 0; i < 30; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(0.5f + i, y); Body body = _world.CreateBody(bd); body.CreateFixture(fd); Vector2 anchor = new Vector2((float)i, y); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); prevBody = body; } } }
public Revolute() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { CircleShape shape = new CircleShape(); shape._radius = 0.5f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; RevoluteJointDef rjd = new RevoluteJointDef(); bd.position = new Vector2(0.0f, 20.0f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 5.0f); float w = 100.0f; body.SetAngularVelocity(w); body.SetLinearVelocity(new Vector2(-8.0f * w, 0.0f)); rjd.Initialize(ground, body, new Vector2(0.0f, 12.0f)); rjd.motorSpeed = 1.0f * (float)Settings.b2_pi; rjd.maxMotorTorque = 10000.0f; rjd.enableMotor = false; rjd.lowerAngle = -0.25f * (float)Settings.b2_pi; rjd.upperAngle = 0.5f * (float)Settings.b2_pi; rjd.enableLimit = true; rjd.collideConnected = true; _joint = (RevoluteJoint)_world.CreateJoint(rjd); } }
public Pyramid() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { float a = 0.5f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, a); Vector2 x = new Vector2(-7.0f, 0.75f); Vector2 y; Vector2 deltaX = new Vector2(0.5625f, 1.25f); Vector2 deltaY = new Vector2(1.125f, 0.0f); for (int i = 0; i < e_count; ++i) { y = x; for (int j = i; j < e_count; ++j) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = y; Body body = _world.CreateBody(bd); body.CreateFixture(shape, 5.0f); y += deltaY; } x += deltaX; } } }
public ShapeEditing() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } BodyDef bodydef = new BodyDef(); bodydef.type = BodyType.Dynamic; bodydef.position = new Vector2(0.0f, 10.0f); _body = _world.CreateBody(bodydef); PolygonShape shape2 = new PolygonShape(); shape2.SetAsBox(4.0f, 4.0f, new Vector2(0.0f, 0.0f), 0.0f); _fixture1 = _body.CreateFixture(shape2, 10.0f); _fixture2 = null; }
public CollisionProcessing() { // Ground body { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f)); FixtureDef sd = new FixtureDef(); sd.shape = shape; BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); ground.CreateFixture(sd); } float xLo = -5.0f, xHi = 5.0f; float yLo = 2.0f, yHi = 35.0f; // Small triangle Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-1.0f, 0.0f); vertices[1] = new Vector2(1.0f, 0.0f); vertices[2] = new Vector2(0.0f, 2.0f); PolygonShape polygon = new PolygonShape(); polygon.Set(vertices, 3); FixtureDef triangleShapeDef = new FixtureDef(); triangleShapeDef.shape = polygon; triangleShapeDef.density = 1.0f; BodyDef triangleBodyDef = new BodyDef(); triangleBodyDef.type = BodyType.Dynamic; triangleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body1 = _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); triangleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body2 = _world.CreateBody(triangleBodyDef); body2.CreateFixture(triangleShapeDef); // Small box polygon.SetAsBox(1.0f, 0.5f); FixtureDef boxShapeDef = new FixtureDef(); boxShapeDef.shape = polygon; boxShapeDef.density = 1.0f; BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.Dynamic; boxBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body3 = _world.CreateBody(boxBodyDef); body3.CreateFixture(boxShapeDef); // Large box (recycle definitions) polygon.SetAsBox(2.0f, 1.0f); boxBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body4 = _world.CreateBody(boxBodyDef); body4.CreateFixture(boxShapeDef); // Small circle CircleShape circle = new CircleShape(); circle._radius = 1.0f; FixtureDef circleShapeDef = new FixtureDef(); circleShapeDef.shape = circle; circleShapeDef.density = 1.0f; BodyDef circleBodyDef = new BodyDef(); circleBodyDef.type = BodyType.Dynamic; circleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body5 = _world.CreateBody(circleBodyDef); body5.CreateFixture(circleShapeDef); // Large circle circle._radius *= 2.0f; circleBodyDef.position = new Vector2(Rand.RandomFloat(xLo, xHi), Rand.RandomFloat(yLo, yHi)); Body body6 = _world.CreateBody(circleBodyDef); body6.CreateFixture(circleShapeDef); }
public Dominos() { Body b1 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); BodyDef bd = new BodyDef(); b1 = _world.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-1.5f, 10.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.1f; for (int i = 0; i < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-6.0f + 1.0f * i, 11.25f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, Vector2.Zero, 0.3f); BodyDef bd = new BodyDef(); bd.position = new Vector2(1.0f, 6.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } Body b2 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-7.0f, 4.0f); b2 = _world.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } Body b3 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-0.9f, 1.0f); bd.angle = -0.15f; b3 = _world.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } RevoluteJointDef jd = new RevoluteJointDef(); Vector2 anchor; anchor = new Vector2(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.collideConnected = true; _world.CreateJoint(jd); Body b4 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-10.0f, 15.0f); b4 = _world.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor = new Vector2(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); _world.CreateJoint(jd); Body b5 = null; { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 3.0f); b5 = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 10.0f; fd.friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new Vector2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vector2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor = new Vector2(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); _world.CreateJoint(jd); Body b6 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(6.5f, 4.1f); b6 = _world.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor = new Vector2(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); _world.CreateJoint(jd); Body b7 = null; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(7.4f, 1.0f); b7 = _world.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } DistanceJointDef djd = new DistanceJointDef(); djd.bodyA = b3; djd.bodyB = b7; djd.localAnchorA = new Vector2(6.0f, 0.0f); djd.localAnchorB = new Vector2(0.0f, -1.0f); Vector2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length(); _world.CreateJoint(djd); { float radius = 0.2f; CircleShape shape = new CircleShape(); shape._radius = radius; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(5.9f + 2.0f * radius * i, 2.4f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
VaryingFriction() { { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-4.0f, 22.0f); bd.angle = -0.25f; Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.0f); BodyDef bd = new BodyDef(); bd.position = new Vector2(10.5f, 19.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(4.0f, 14.0f); bd.angle = 0.25f; Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.0f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-10.5f, 11.0f); Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); BodyDef bd = new BodyDef(); bd.position = new Vector2(-4.0f, 6.0f); bd.angle = -0.25f; Body ground = _world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 25.0f; float[] friction = new float[5] {0.75f, 0.5f, 0.35f, 0.1f, 0.0f}; for (int i = 0; i < 5; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-15.0f + 4.0f * i, 28.0f); Body body = _world.CreateBody(bd); fd.friction = friction[i]; body.CreateFixture(fd); } } }
public Tiles() { var fixtureCount = 0; var timer = Stopwatch.StartNew(); { var a = 0.5f; var bd = new BodyDef(); bd.Position.Y = -a; var ground = World.CreateBody(bd); var N = 200; var M = 10; var position = new Vector2(); position.Y = 0.0f; for (var j = 0; j < M; ++j) { position.X = -N * a; for (var i = 0; i < N; ++i) { var shape = new PolygonShape(); shape.SetAsBox(a, a, position, 0.0f); ground.CreateFixture(shape, 0.0f); ++fixtureCount; position.X += 2.0f * a; } position.Y -= 2.0f * a; } } { var a = 0.5f; var shape = new PolygonShape(); shape.SetAsBox(a, a); var x = new Vector2(-7.0f, 0.75f); var y = new Vector2(); var deltaX = new Vector2(0.5625f, 1.25f); var deltaY = new Vector2(1.125f, 0.0f); for (int i = 0; i < Count; ++i) { y = x; for (var j = i; j < Count; ++j) { var bd = new BodyDef { BodyType = BodyType.DynamicBody, Position = y }; var body = World.CreateBody(bd); if (_bodies[_bodyIndex] != null) { World.DestroyBody(_bodies[_bodyIndex]); _bodies[_bodyIndex] = null; } _bodies[_bodyIndex] = body; _bodyIndex = (_bodyIndex + 1) % MaxBodies; body.CreateFixture(shape, 5.0f); ++fixtureCount; y += deltaY; } x += deltaX; } } CreateTime = timer.ElapsedMilliseconds; FixtureCount = fixtureCount; }
public RayCast() { // Ground body { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var vertices = new Vector2[3]; vertices[0] = new Vector2(-0.5f, 0.0f); vertices[1] = new Vector2(0.5f, 0.0f); vertices[2] = new Vector2(0.0f, 1.5f); _polygons[0] = new PolygonShape(); _polygons[0].Set(vertices, 3); } { var vertices2 = new Vector2[3]; vertices2[0] = new Vector2(-0.1f, 0.0f); vertices2[1] = new Vector2(0.1f, 0.0f); vertices2[2] = new Vector2(0.0f, 1.5f); _polygons[1] = new PolygonShape(); _polygons[1].Set(vertices2, 3); } { float w = 1.0f; float b = w / (2.0f + (float)Math.Sqrt(2.0)); float s = (float)Math.Sqrt(2.0) * b; var vertices3 = new Vector2[8]; vertices3[0] = new Vector2(0.5f * s, 0.0f); vertices3[1] = new Vector2(0.5f * w, b); vertices3[2] = new Vector2(0.5f * w, b + s); vertices3[3] = new Vector2(0.5f * s, w); vertices3[4] = new Vector2(-0.5f * s, w); vertices3[5] = new Vector2(-0.5f * w, b + s); vertices3[6] = new Vector2(-0.5f * w, b); vertices3[7] = new Vector2(-0.5f * s, 0.0f); _polygons[2] = new PolygonShape(); _polygons[2].Set(vertices3, 8); } { _polygons[3] = new PolygonShape(); _polygons[3].SetAsBox(0.5f, 0.5f); } { _circle = new CircleShape(); _circle._radius = 0.5f; } _bodyIndex = 0; _angle = 0.0f; _mode = RayCastMode.Closest; }
public Cantilever() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-14.5f + 1.0f * i, 5.0f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); prevBody = body; } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-14.5f + 1.0f * i, 15.0f); bd.inertiaScale = 10.0f; Body body = _world.CreateBody(bd); body.CreateFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); prevBody = body; } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-4.5f + 1.0f * i, 5.0f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); } prevBody = body; } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(5.5f + 1.0f * i, 10.0f); bd.inertiaScale = 10.0f; Body body = _world.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); jd.Initialize(prevBody, body, anchor); _world.CreateJoint(jd); } prevBody = body; } } for (int i = 0; i < 2; ++i) { Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-0.5f, 0.0f); vertices[1] = new Vector2(0.5f, 0.0f); vertices[2] = new Vector2(0.0f, 1.5f); PolygonShape shape = new PolygonShape(); shape.Set(vertices, 3); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-8.0f + 8.0f * i, 12.0f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } for (int i = 0; i < 2; ++i) { CircleShape shape = new CircleShape(); shape._radius = 0.5f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-6.0f + 6.0f * i, 10.0f); Body body = _world.CreateBody(bd); body.CreateFixture(fd); } }
public PhysicsProcessBox2D(Settings.BallReboundType ballRebound, bool shouldAttack) { this.shouldAttack = shouldAttack; BodyDef ballDef = new BodyDef { AngularDamping = 0.01f, Position = new Vec2(FieldWidth / 2, 2) }; this.ball = this.world.CreateBody(ballDef); this.ball.SetLinearVelocity(new Vec2(new Random().Next(-20, 21) / 8f, -2)); CircleDef ballShapeDef = new CircleDef { Radius = BallRadius, Density = BallDensity, Friction = BallFriction, Restitution = ballRebound == Settings.BallReboundType.High ? 0.87f : ballRebound == Settings.BallReboundType.Medium ? 0.6f : 0.4f }; this.ball.CreateShape(ballShapeDef); this.ball.SetMassFromShapes(); BodyDef groundDef = new BodyDef { Position = new Vec2(FieldWidth / 2, FieldHeight - 0.37f) }; this.ground = this.world.CreateBody(groundDef); PolygonDef groundShapeDef = new PolygonDef { Friction = 1f }; groundShapeDef.SetAsBox(FieldWidth / 2, GroundThickness / 2); this.ground.CreateShape(groundShapeDef); BodyDef leftWallDef = new BodyDef { Position = new Vec2(-WallThickness / 2, FieldHeight / 2) }; this.leftWall = this.world.CreateBody(leftWallDef); PolygonDef leftWallShapeDef = new PolygonDef(); leftWallShapeDef.SetAsBox(WallThickness / 2, FieldHeight / 2); this.leftWall.CreateShape(leftWallShapeDef); BodyDef rightWallDef = new BodyDef { Position = new Vec2(FieldWidth + (WallThickness / 2), FieldHeight / 2) }; this.rightWall = this.world.CreateBody(rightWallDef); PolygonDef rightWallShapeDef = new PolygonDef(); rightWallShapeDef.SetAsBox(WallThickness / 2, FieldHeight / 2); this.rightWall.CreateShape(rightWallShapeDef); BodyDef roofDef = new BodyDef { Position = new Vec2(FieldWidth / 2, -WallThickness / 2) }; this.roof = this.world.CreateBody(roofDef); PolygonDef roofShapeDef = new PolygonDef(); roofShapeDef.SetAsBox(FieldWidth / 2, WallThickness / 2); this.roof.CreateShape(roofShapeDef); BodyDef playerLeftDef = new BodyDef { FixedRotation = true, Position = new Vec2(PlayerXPosition, PlayerYPosition) }; this.playerLeft = this.world.CreateBody(playerLeftDef); PolygonDef playerBody = new PolygonDef { Friction = 2, Density = 1.3f, Restitution = 0, UserData = "Body" }; playerBody.SetAsBox(PlayerThickness / 2, PlayerBodyHeight / 2); this.playerLeft.CreateShape(playerBody); CircleDef playerHead = new CircleDef { Friction = 1, Density = 1, Restitution = 0, Radius = PlayerHeadRadius, LocalPosition = new Vec2(0, -0.35f), UserData = "Head" }; this.playerLeft.CreateShape(playerHead); this.playerLeft.SetMassFromShapes(); BodyDef playerRightDef = new BodyDef { FixedRotation = true, Position = new Vec2(FieldWidth - PlayerXPosition, PlayerYPosition) }; this.playerRight = this.world.CreateBody(playerRightDef); this.playerRight.CreateShape(playerBody); this.playerRight.CreateShape(playerHead); this.playerRight.SetMassFromShapes(); BodyDef goalLeftDef = new BodyDef { Position = new Vec2(0.5f, 5.035f) }; this.goalLeft = this.world.CreateBody(goalLeftDef); CircleDef goalLeftBarDef = new CircleDef { Radius = 0.075f, LocalPosition = new Vec2(0.425f, 0), Friction = 1, Restitution = 0, UserData = "Post" }; PolygonDef goalLeftLineDef = new PolygonDef { Friction = 1, Restitution = 0 }; goalLeftLineDef.SetAsBox(0.46f, 0.001f, new Vec2(-0.04f, -0.069f), 0); this.goalLeft.CreateShape(goalLeftBarDef); this.goalLeft.CreateShape(goalLeftLineDef); BodyDef goalRightDef = new BodyDef { Position = new Vec2(13.16f, 5.035f) }; this.goalRight = this.world.CreateBody(goalRightDef); CircleDef goalRightBarDef = new CircleDef { Radius = 0.075f, LocalPosition = new Vec2(-0.425f, 0), Friction = 1, Restitution = 0, UserData = "Post" }; PolygonDef goalRightLineDef = new PolygonDef { Friction = 1, Restitution = 0 }; goalRightLineDef.SetAsBox(0.46f, 0.001f, new Vec2(0.04f, -0.069f), 0); this.goalRight.CreateShape(goalRightBarDef); this.goalRight.CreateShape(goalRightLineDef); BodyDef legLeftDef = new BodyDef { Position = new Vec2(playerLeftDef.Position.X, playerLeftDef.Position.Y + 0.2f) }; this.legLeft = this.world.CreateBody(legLeftDef); PolygonDef legLeftShapeDef = new PolygonDef { Density = 1.4f, Friction = 3, Restitution = 0, Vertices = new Vec2[] { new Vec2(0.12f, 0.03f), new Vec2(-0.07f, -0.06f), new Vec2(-0.03f, -0.15f) }, VertexCount = 3 }; this.legLeft.CreateShape(legLeftShapeDef); this.legLeft.SetMassFromShapes(); RevoluteJointDef jointLeftDef = new RevoluteJointDef { Body1 = this.playerLeft, Body2 = this.legLeft, LocalAnchor1 = new Vec2(0.05f, -0.4f), LocalAnchor2 = new Vec2(0, -0.64f), LowerAngle = AngleConversion.DegreeToRadian(-18), UpperAngle = AngleConversion.DegreeToRadian(13), EnableMotor = true, EnableLimit = true, MaxMotorTorque = 0.08f }; this.jointLeft = this.world.CreateJoint(jointLeftDef); BodyDef legRightDef = new BodyDef { Position = new Vec2(playerRightDef.Position.X, playerRightDef.Position.Y + 0.2f) }; this.legRight = this.world.CreateBody(legRightDef); PolygonDef legRightShapeDef = new PolygonDef { Density = 1.4f, Friction = 3, Restitution = 0, Vertices = new Vec2[] { new Vec2(-0.12f, 0.03f), new Vec2(0.03f, -0.15f), new Vec2(0.07f, -0.06f) }, VertexCount = 3 }; this.legRight.CreateShape(legRightShapeDef); this.legRight.SetMassFromShapes(); RevoluteJointDef jointRightDef = new RevoluteJointDef { Body1 = this.playerRight, Body2 = this.legRight, LocalAnchor1 = new Vec2(-0.05f, -0.4f), LocalAnchor2 = new Vec2(0, -0.64f), LowerAngle = AngleConversion.DegreeToRadian(-13), UpperAngle = AngleConversion.DegreeToRadian(18), EnableMotor = true, EnableLimit = true, MaxMotorTorque = 0.08f }; this.jointRight = this.world.CreateJoint(jointRightDef); this.playerLeftTimers.Defend = 12000; this.playerRightTimers.Defend = 12000; this.listener.OnResult += this.BallContact; this.listener.OnPersist += this.BallContacting; this.listener.OnAdd += this.BallContacting; this.listener.OnAdd += this.PlayerLeftContact; this.listener.OnAdd += this.PlayerRightContact; this.listener.OnAdd += this.PlayersTackle; this.listener.OnRemove += this.PlayerLeftContactRemove; this.listener.OnRemove += this.PlayerRightContactRemove; this.world.SetContactListener(this.listener); }
private BodyTypesTest() { Body ground; { BodyDef bd = new BodyDef(); ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); FixtureDef fd = new FixtureDef(); fd.Shape = shape; ground.AddFixture(fd); } // Define attachment { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(0.0f, 3.0f); _attachment = BodyFactory.CreateFromDef(World, bd); PolygonShape shape = new PolygonShape(2.0f); shape.SetAsBox(0.5f, 2.0f); _attachment.AddFixture(shape); } // Define platform { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-4.0f, 5.0f); _platform = BodyFactory.CreateFromDef(World, bd); PolygonShape shape = new PolygonShape(2.0f); shape.SetAsBox(0.5f, 4.0f, new Vector2(4.0f, 0.0f), 0.5f * MathConstants.Pi); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.6f; _platform.AddFixture(fd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(_attachment, _platform, new Vector2(0.0f, 5.0f)); rjd.MaxMotorTorque = 50.0f; rjd.EnableMotor = true; JointFactory.CreateFromDef(World, rjd); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.Initialize(ground, _platform, new Vector2(0.0f, 5.0f), new Vector2(1.0f, 0.0f)); pjd.MaxMotorForce = 1000.0f; pjd.EnableMotor = true; pjd.LowerTranslation = -10.0f; pjd.UpperTranslation = 10.0f; pjd.EnableLimit = true; JointFactory.CreateFromDef(World, pjd); _speed = 3.0f; } // Create a payload { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(0.0f, 8.0f); Body body = BodyFactory.CreateFromDef(World, bd); PolygonShape shape = new PolygonShape(2.0f); shape.SetAsBox(0.75f, 0.75f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.6f; body.AddFixture(fd); } }
public VaryingFriction() { { var bd = new BodyDef(); var ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); var bd = new BodyDef(); bd.Position.Set(-4.0f, 22.0f); bd.Angle = -0.25f; var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.0f); var bd = new BodyDef(); bd.Position.Set(10.5f, 19.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); var bd = new BodyDef(); bd.Position.Set(4.0f, 14.0f); bd.Angle = 0.25f; var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.0f); var bd = new BodyDef(); bd.Position.Set(-10.5f, 11.0f); var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(13.0f, 0.25f); var bd = new BodyDef(); bd.Position.Set(-4.0f, 6.0f); bd.Angle = -0.25f; var ground = World.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 25.0f; float[] friction = {0.75f, 0.5f, 0.35f, 0.1f, 0.0f}; for (var i = 0; i < 5; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-15.0f + 4.0f * i, 28.0f); var body = World.CreateBody(bd); fd.Friction = friction[i]; body.CreateFixture(fd); } } }
public Biped(World w, Vec2 position) { _world = w; BipedDef def = new BipedDef(); BodyDef bd = new BodyDef(); // create body parts bd = def.LFootDef; bd.Position += position; LFoot = w.CreateBody(bd); LFoot.CreateFixture(def.LFootPoly); LFoot.SetMassFromShapes(); bd = def.RFootDef; bd.Position += position; RFoot = w.CreateBody(bd); RFoot.CreateFixture(def.RFootPoly); RFoot.SetMassFromShapes(); bd = def.LCalfDef; bd.Position += position; LCalf = w.CreateBody(bd); LCalf.CreateFixture(def.LCalfPoly); LCalf.SetMassFromShapes(); bd = def.RCalfDef; bd.Position += position; RCalf = w.CreateBody(bd); RCalf.CreateFixture(def.RCalfPoly); RCalf.SetMassFromShapes(); bd = def.LThighDef; bd.Position += position; LThigh = w.CreateBody(bd); LThigh.CreateFixture(def.LThighPoly); LThigh.SetMassFromShapes(); bd = def.RThighDef; bd.Position += position; RThigh = w.CreateBody(bd); RThigh.CreateFixture(def.RThighPoly); RThigh.SetMassFromShapes(); bd = def.PelvisDef0; bd.Position += position; Pelvis = w.CreateBody(bd); Pelvis.CreateFixture(def.PelvisPoly); Pelvis.SetMassFromShapes(); bd = def.PelvisDef; bd.Position += position; Stomach = w.CreateBody(bd); Stomach.CreateFixture(def.StomachPoly); Stomach.SetMassFromShapes(); bd = def.ChestDef; bd.Position += position; Chest = w.CreateBody(bd); Chest.CreateFixture(def.ChestPoly); Chest.SetMassFromShapes(); bd = def.NeckDef; bd.Position += position; Neck = w.CreateBody(bd); Neck.CreateFixture(def.NeckPoly); Neck.SetMassFromShapes(); bd = def.HeadDef; bd.Position += position; Head = w.CreateBody(bd); Head.CreateFixture(def.HeadCirc); Head.SetMassFromShapes(); bd = def.LUpperArmDef; bd.Position += position; LUpperArm = w.CreateBody(bd); LUpperArm.CreateFixture(def.LUpperArmPoly); LUpperArm.SetMassFromShapes(); bd = def.RUpperArmDef; bd.Position += position; RUpperArm = w.CreateBody(bd); RUpperArm.CreateFixture(def.RUpperArmPoly); RUpperArm.SetMassFromShapes(); bd = def.LForearmDef; bd.Position += position; LForearm = w.CreateBody(bd); LForearm.CreateFixture(def.LForearmPoly); LForearm.SetMassFromShapes(); bd = def.RForearmDef; bd.Position += position; RForearm = w.CreateBody(bd); RForearm.CreateFixture(def.RForearmPoly); RForearm.SetMassFromShapes(); bd = def.LHandDef; bd.Position += position; LHand = w.CreateBody(bd); LHand.CreateFixture(def.LHandPoly); LHand.SetMassFromShapes(); bd = def.RHandDef; bd.Position += position; RHand = w.CreateBody(bd); RHand.CreateFixture(def.RHandPoly); RHand.SetMassFromShapes(); // link body parts def.LAnkleDef.Body1 = LFoot; def.LAnkleDef.Body2 = LCalf; def.RAnkleDef.Body1 = RFoot; def.RAnkleDef.Body2 = RCalf; def.LKneeDef.Body1 = LCalf; def.LKneeDef.Body2 = LThigh; def.RKneeDef.Body1 = RCalf; def.RKneeDef.Body2 = RThigh; def.LHipDef.Body1 = LThigh; def.LHipDef.Body2 = Pelvis; def.RHipDef.Body1 = RThigh; def.RHipDef.Body2 = Pelvis; def.LowerAbsDef.Body1 = Pelvis; def.LowerAbsDef.Body2 = Stomach; def.UpperAbsDef.Body1 = Stomach; def.UpperAbsDef.Body2 = Chest; def.LowerNeckDef.Body1 = Chest; def.LowerNeckDef.Body2 = Neck; def.UpperNeckDef.Body1 = Chest; def.UpperNeckDef.Body2 = Head; def.LShoulderDef.Body1 = Chest; def.LShoulderDef.Body2 = LUpperArm; def.RShoulderDef.Body1 = Chest; def.RShoulderDef.Body2 = RUpperArm; def.LElbowDef.Body1 = LForearm; def.LElbowDef.Body2 = LUpperArm; def.RElbowDef.Body1 = RForearm; def.RElbowDef.Body2 = RUpperArm; def.LWristDef.Body1 = LHand; def.LWristDef.Body2 = LForearm; def.RWristDef.Body1 = RHand; def.RWristDef.Body2 = RForearm; // create joints LAnkle = (RevoluteJoint)w.CreateJoint(def.LAnkleDef); RAnkle = (RevoluteJoint)w.CreateJoint(def.RAnkleDef); LKnee = (RevoluteJoint)w.CreateJoint(def.LKneeDef); RKnee = (RevoluteJoint)w.CreateJoint(def.RKneeDef); LHip = (RevoluteJoint)w.CreateJoint(def.LHipDef); RHip = (RevoluteJoint)w.CreateJoint(def.RHipDef); LowerAbs = (RevoluteJoint)w.CreateJoint(def.LowerAbsDef); UpperAbs = (RevoluteJoint)w.CreateJoint(def.UpperAbsDef); LowerNeck = (RevoluteJoint)w.CreateJoint(def.LowerNeckDef); UpperNeck = (RevoluteJoint)w.CreateJoint(def.UpperNeckDef); LShoulder = (RevoluteJoint)w.CreateJoint(def.LShoulderDef); RShoulder = (RevoluteJoint)w.CreateJoint(def.RShoulderDef); LElbow = (RevoluteJoint)w.CreateJoint(def.LElbowDef); RElbow = (RevoluteJoint)w.CreateJoint(def.RElbowDef); LWrist = (RevoluteJoint)w.CreateJoint(def.LWristDef); RWrist = (RevoluteJoint)w.CreateJoint(def.RWristDef); }
public ApplyForce() { World.Gravity = new Vector2(0.0f, 0.0f); const float restitution = 0.4f; Body ground; { var bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); ground = World.CreateBody(bd); var shape = new EdgeShape(); var sd = new FixtureDef(); sd.Shape = shape; sd.Density = 0.0f; sd.Restitution = restitution; // Left vertical shape.SetTwoSided(new Vector2(-20.0f, -20.0f), new Vector2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.SetTwoSided(new Vector2(20.0f, -20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.SetTwoSided(new Vector2(-20.0f, 20.0f), new Vector2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.SetTwoSided(new Vector2(-20.0f, -20.0f), new Vector2(20.0f, -20.0f)); ground.CreateFixture(sd); } { var xf1 = new Transform(); xf1.Rotation.Set(0.3524f * Settings.Pi); xf1.Position = xf1.Rotation.GetXAxis(); var vertices = new Vector2[3]; vertices[0] = MathUtils.Mul(xf1, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(xf1, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(xf1, new Vector2(0.0f, 0.5f)); var poly1 = new PolygonShape(); poly1.Set(vertices); var sd1 = new FixtureDef(); sd1.Shape = poly1; sd1.Density = 2.0f; var xf2 = new Transform(); xf2.Rotation.Set(-0.3524f * Settings.Pi); xf2.Position = -xf2.Rotation.GetXAxis(); vertices[0] = MathUtils.Mul(xf2, new Vector2(-1.0f, 0.0f)); vertices[1] = MathUtils.Mul(xf2, new Vector2(1.0f, 0.0f)); vertices[2] = MathUtils.Mul(xf2, new Vector2(0.0f, 0.5f)); var poly2 = new PolygonShape(); poly2.Set(vertices); var sd2 = new FixtureDef(); sd2.Shape = poly2; sd2.Density = 2.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position = new Vector2(0.0f, 3.0f); bd.Angle = Settings.Pi; bd.AllowSleep = false; _body = World.CreateBody(bd); _body.CreateFixture(sd1); _body.CreateFixture(sd2); var gravity = 10.0f; var I = _body.Inertia; var mass = _body.Mass; // Compute an effective radius that can be used to // set the max torque for a friction joint // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) var radius = (float)Math.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.BodyA = ground; jd.BodyB = _body; jd.LocalAnchorA.SetZero(); jd.LocalAnchorB = _body.GetLocalCenter(); jd.CollideConnected = true; jd.MaxForce = 0.5f * mass * gravity; jd.MaxTorque = 0.2f * mass * radius * gravity; World.CreateJoint(jd); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; fd.Friction = 0.3f; for (var i = 0; i < 10; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(0.0f, 7.0f + 1.54f * i); var body = World.CreateBody(bd); body.CreateFixture(fd); var gravity = 10.0f; var I = body.Inertia; var mass = body.Mass; // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) var radius = (float)Math.Sqrt(2.0f * I / mass); var jd = new FrictionJointDef(); jd.LocalAnchorA.SetZero(); jd.LocalAnchorB.SetZero(); jd.BodyA = ground; jd.BodyB = body; jd.CollideConnected = true; jd.MaxForce = mass * gravity; jd.MaxTorque = 0.1f * mass * radius * gravity; World.CreateJoint(jd); } } }
public RopeJointTest() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.Set(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; fd.Friction = 0.2f; var filter = fd.Filter; filter.CategoryBits = 0x0001; filter.MaskBits = 0xFFFF & ~0x0002; fd.Filter = filter; var jd = new RevoluteJointDef(); jd.CollideConnected = false; const int N = 10; const float y = 15.0f; _ropeDef.LocalAnchorA.Set(0.0f, y); var prevBody = ground; for (var i = 0; i < N; ++i) { var bd = new BodyDef(); bd.BodyType = 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; filter = fd.Filter; filter.CategoryBits = 0x0002; fd.Filter = filter; bd.Position.Set(1.0f * i, y); bd.AngularDamping = 0.4f; } var body = World.CreateBody(bd); body.CreateFixture(fd); var anchor = new Vector2(i, y); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); prevBody = body; } _ropeDef.LocalAnchorB.SetZero(); var extraLength = 0.01f; _ropeDef.MaxLength = N - 1.0f + extraLength; _ropeDef.BodyB = prevBody; } { _ropeDef.BodyA = ground; _rope = World.CreateJoint(_ropeDef); } }
public ApplyForce() { _world.Gravity = new Vec2(0.0f, 0.0f); const float k_restitution = 0.4f; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); Body ground = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.Density = 0.0f; sd.Restitution = k_restitution; sd.SetAsBox(0.2f, 20.0f, new Vec2(-20.0f, 0.0f), 0.0f); ground.CreateShape(sd); sd.SetAsBox(0.2f, 20.0f, new Vec2(20.0f, 0.0f), 0.0f); ground.CreateShape(sd); sd.SetAsBox(0.2f, 20.0f, new Vec2(0.0f, -20.0f), 0.5f * Box2DX.Common.Settings.Pi); ground.CreateShape(sd); sd.SetAsBox(0.2f, 20.0f, new Vec2(0.0f, 20.0f), -0.5f * Box2DX.Common.Settings.Pi); ground.CreateShape(sd); } { XForm xf1 = new XForm(); xf1.R.Set(0.3524f * Box2DX.Common.Settings.Pi); xf1.Position = Box2DX.Common.Math.Mul(xf1.R, new Vec2(1.0f, 0.0f)); PolygonDef sd1 = new PolygonDef(); sd1.VertexCount = 3; sd1.Vertices[0] = Box2DX.Common.Math.Mul(xf1, new Vec2(-1.0f, 0.0f)); sd1.Vertices[1] = Box2DX.Common.Math.Mul(xf1, new Vec2(1.0f, 0.0f)); sd1.Vertices[2] = Box2DX.Common.Math.Mul(xf1, new Vec2(0.0f, 0.5f)); sd1.Density = 2.0f; XForm xf2 = new XForm(); xf2.R.Set(-0.3524f * Box2DX.Common.Settings.Pi); xf2.Position = Box2DX.Common.Math.Mul(xf2.R, new Vec2(-1.0f, 0.0f)); PolygonDef sd2 = new PolygonDef(); sd2.VertexCount = 3; sd2.Vertices[0] = Box2DX.Common.Math.Mul(xf2, new Vec2(-1.0f, 0.0f)); sd2.Vertices[1] = Box2DX.Common.Math.Mul(xf2, new Vec2(1.0f, 0.0f)); sd2.Vertices[2] = Box2DX.Common.Math.Mul(xf2, new Vec2(0.0f, 0.5f)); sd2.Density = 2.0f; BodyDef bd = new BodyDef(); bd.AngularDamping = 2.0f; bd.LinearDamping = 0.1f; bd.Position.Set(0.0f, 1.05f); bd.Angle = Box2DX.Common.Settings.Pi; _body = _world.CreateBody(bd); _body.CreateShape(sd1); _body.CreateShape(sd2); _body.SetMassFromShapes(); } }
public EdgeTest() { var vertices = new Vector2[] { new Vector2(10.0f, -4.0f), new Vector2(10.0f, 0.0f), new Vector2(6.0f, 0.0f), new Vector2(4.0f, 2.0f), new Vector2(2.0f, 0.0f), new Vector2(-2.0f, 0.0f), new Vector2(-6.0f, 0.0f), new Vector2(-8.0f, -3.0f), new Vector2(-10.0f, 0.0f), new Vector2(-10.0f, -4.0f) }; Offset1.Set(0.0f, 8.0f); Offset2.Set(0.0f, 16.0f); { var v1 = vertices[0] + Offset1; var v2 = vertices[1] + Offset1; var v3 = vertices[2] + Offset1; var v4 = vertices[3] + Offset1; var v5 = vertices[4] + Offset1; var v6 = vertices[5] + Offset1; var v7 = vertices[6] + Offset1; var v8 = vertices[7] + Offset1; var v9 = vertices[8] + Offset1; var v10 = vertices[9] + Offset1; BodyDef bd = new BodyDef(); Body ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.SetOneSided(v10, v1, v2, v3); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v1, v2, v3, v4); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v2, v3, v4, v5); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v3, v4, v5, v6); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v4, v5, v6, v7); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v5, v6, v7, v8); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v6, v7, v8, v9); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v7, v8, v9, v10); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v8, v9, v10, v1); ground.CreateFixture(shape, 0.0f); shape.SetOneSided(v9, v10, v1, v2); ground.CreateFixture(shape, 0.0f); } { var v1 = vertices[0] + Offset2; var v2 = vertices[1] + Offset2; var v3 = vertices[2] + Offset2; var v4 = vertices[3] + Offset2; var v5 = vertices[4] + Offset2; var v6 = vertices[5] + Offset2; var v7 = vertices[6] + Offset2; var v8 = vertices[7] + Offset2; var v9 = vertices[8] + Offset2; var v10 = vertices[9] + Offset2; BodyDef bd = new BodyDef(); Body ground = World.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(v1, v2); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v2, v3); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v3, v4); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v4, v5); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v5, v6); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v6, v7); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v7, v8); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v8, v9); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v9, v10); ground.CreateFixture(shape, 0.0f); shape.SetTwoSided(v10, v1); ground.CreateFixture(shape, 0.0f); } Body1 = null; Body2 = null; CreateBoxes(); Boxes = true; }
void CreateLeg(float s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); FixtureDef fd1 = new FixtureDef(); FixtureDef fd2 = new FixtureDef(); fd1.filter.groupIndex = -1; fd2.filter.groupIndex = -1; fd1.density = 1.0f; fd2.density = 1.0f; PolygonShape poly1 = new PolygonShape(); PolygonShape poly2 = new PolygonShape(); Vector2[] vertices = new Vector2[3]; if (s > 0.0f) { vertices[0] = p1; vertices[1] = p2; vertices[2] = p3; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Set(vertices, 3); } else { vertices[0] = p1; vertices[1] = p3; vertices[2] = p2; poly1.Set(vertices, 3); vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Set(vertices, 3); } fd1.shape = poly1; fd2.shape = poly2; BodyDef bd1 = new BodyDef(); BodyDef bd2 = new BodyDef(); bd1.type = BodyType.Dynamic; bd2.type = BodyType.Dynamic; bd1.position = _offset; bd2.position = p4 + _offset; bd1.angularDamping = 10.0f; bd2.angularDamping = 10.0f; Body body1 = _world.CreateBody(bd1); Body body2 = _world.CreateBody(bd2); body1.CreateFixture(fd1); body2.CreateFixture(fd2); DistanceJointDef djd = new DistanceJointDef(); // Using a soft distanceraint can reduce some jitter. // It also makes the structure seem a bit more fluid by // acting like a suspension system. djd.dampingRatio = 0.5f; djd.frequencyHz = 10.0f; djd.Initialize(body1, body2, p2 + _offset, p5 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, body2, p3 + _offset, p4 + _offset); _world.CreateJoint(djd); djd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); djd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); _world.CreateJoint(djd); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(body2, _chassis, p4 + _offset); _world.CreateJoint(rjd); }
protected override void Create() { // Ground body { var bd = new BodyDef(); var ground = World.CreateBody(bd); var x1 = -20.0f; var y1 = 2.0f * (float)Math.Cos(x1 / 10.0f * Settings.Pi); for (var i = 0; i < 80; ++i) { var x2 = x1 + 0.5f; var y2 = 2.0f * (float)Math.Cos(x2 / 10.0f * Settings.Pi); var shape = new EdgeShape(); shape.Set(new Vector2(x1, y1), new Vector2(x2, y2)); ground.CreateFixture(shape, 0.0f); x1 = x2; y1 = y2; } } { var vertices = new Vector2[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); _polygons[0].Set(vertices); } { var vertices = new Vector2[3]; vertices[0].Set(-0.1f, 0.0f); vertices[1].Set(0.1f, 0.0f); vertices[2].Set(0.0f, 1.5f); _polygons[1].Set(vertices); } { var w = 1.0f; var b = w / (2.0f + (float)Math.Sqrt(2.0f)); var s = (float)Math.Sqrt(2.0f) * b; var vertices = new Vector2[8]; vertices[0].Set(0.5f * s, 0.0f); vertices[1].Set(0.5f * w, b); vertices[2].Set(0.5f * w, b + s); vertices[3].Set(0.5f * s, w); vertices[4].Set(-0.5f * s, w); vertices[5].Set(-0.5f * w, b + s); vertices[6].Set(-0.5f * w, b); vertices[7].Set(-0.5f * s, 0.0f); _polygons[2].Set(vertices); } { _polygons[3].SetAsBox(0.5f, 0.5f); } { _circle.Radius = 0.5f; } _bodyIndex = 0; _angle = 0.0f; }
public override void Step(Framework.Settings settings) { base.Step(settings); _debugDraw.DrawString(50, _textLine, "Press: (,) to launch a bullet."); if (_stepCount == 300) { if (_bullet != null) { _world.DestroyBody(_bullet); _bullet = null; } { CircleShape shape = new CircleShape(); shape._radius = 0.25f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.bullet = true; bd.position = new Vector2(-31.0f, 5.0f); _bullet = _world.CreateBody(bd); _bullet.CreateFixture(fd); _bullet.SetLinearVelocity(new Vector2(400.0f, 0.0f)); } } _textLine += 15; }
/// <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); }
// don't bother caching multi apparel ones for now public static ApparelSlot Create(BodyDef def, IEnumerable <ThingDef> apparel) { return(new ApparelSlot(def, apparel)); }
public DataLoader() { Race human = new Race(); human.DefName = "Human"; human.Label = "Human"; Genders.Add("Female"); Genders.Add("Male"); human.BodyType.Add("Male"); human.BodyType.Add("Female"); human.BodyType.Add("Average"); human.BodyType.Add("Thin"); human.BodyType.Add("Hulk"); human.BodyType.Add("Fat"); human.HairsByGender["Female"] = new List <Hair>(); human.HairsByGender["Male"] = new List <Hair>(); foreach (var firstType in new string[] { "Average", "Narrow" }) { foreach (var secondType in new string[] { "Normal", "Pointy", "Wide" }) { human.HeadType.Add(new CrownType { CrownFirstType = firstType, CrownSubType = secondType }); } } Quality.Add("Awful"); Quality.Add("Poor"); Quality.Add("Normal"); Quality.Add("Good"); Quality.Add("Excellent"); Quality.Add("Masterwork"); Quality.Add("Legendary"); RaceDictionary[human.DefName] = human; ResourceLoader.Load(); var allXmlFiles = Directory.GetFiles("Mods", "*.xml", SearchOption.AllDirectories); var steamModsDir = GetSteamWorkshopLocation(); if (Directory.Exists(steamModsDir)) { allXmlFiles = allXmlFiles.Concat(Directory.GetFiles(steamModsDir, "*.xml", SearchOption.AllDirectories)).ToArray(); } List <Hair> allHairs = new List <Hair>(); foreach (var xmlFile in allXmlFiles) { var modxmlFile = xmlFile; if (modxmlFile.StartsWith(steamModsDir)) { modxmlFile = xmlFile.Replace(steamModsDir, ""); } string[] pathComponents = modxmlFile.Split('\\'); if (!pathComponents[2].Equals("Defs")) { continue; } using (var fileStream = File.OpenRead(xmlFile)) { var docRoot = XDocument.Load(fileStream).Root; CurrentDocumentPath = xmlFile; var workTypeDefsRoot = docRoot.XPathSelectElements("WorkTypeDef/workTags/.."); if (workTypeDefsRoot.Count() != 0) { var workTypeDefs = from workTypeDef in workTypeDefsRoot select new WorkType { DefName = workTypeDef.Element("defName").GetValue(), FullName = workTypeDef.Element("gerundLabel").GetValue(), WorkTags = workTypeDef.Element("workTags") .Elements("li") .Select(element => element.GetValue()).ToArray() }; WorkTypes.AddRange(workTypeDefs); } foreach (var raceVars in docRoot.Descendants("AlienRace.ThingDef_AlienRace")) { Race race = new Race(); race.DefName = raceVars.Element("defName").GetValue(); race.Label = raceVars.Element("label").GetValue(); var alienraceElement = raceVars.Element("alienrace"); if (alienraceElement == null) { alienraceElement = raceVars.Element("alienRace"); } race.HairsByGender["Female"] = new List <Hair>(); race.HairsByGender["Male"] = new List <Hair>(); foreach (var bodyType in alienraceElement.XPathSelectElements("generalSettings/alienPartGenerator/alienbodytypes/li")) { race.BodyType.Add(bodyType.GetValue()); } foreach (var crownType in alienraceElement.XPathSelectElements("generalSettings/alienPartGenerator/aliencrowntypes/li")) { string[] crownStrings = crownType.GetValue().Split('_'); race.HeadType.Add(new CrownType { CrownFirstType = crownStrings[0], CrownSubType = crownStrings[1] }); } if (race.HeadType.Count == 0) { race.HeadType.Add(new CrownType { CrownFirstType = "Average", CrownSubType = "Normal" }); } var useGenderedHeads = alienraceElement.XPathSelectElement("generalSettings/alienPartGenerator/UseGenderedHeads"); if (useGenderedHeads != null) { race.UseGenderedHeads = Convert.ToBoolean(useGenderedHeads.GetValue()); } foreach (var path in alienraceElement.XPathSelectElement("graphicPaths/li").Elements()) { race.GraphicPaths[path.Name.ToString().ToLower()] = path.GetValue(); } RaceDictionary[race.DefName] = race; } foreach (var hairVars in docRoot.Descendants("HairDef")) { Hair hair = new Hair( hairVars.Element("hairGender").GetValue(), hairVars.Element("label").GetValue(), hairVars.Element("defName").GetValue(), hairVars.XPathSelectElements("hairTags/li")); allHairs.Add(hair); } foreach (var traitDef in docRoot.Descendants("TraitDef")) { var traits = (from trait in traitDef.XPathSelectElements("degreeDatas/li") select new TraitDef { Def = traitDef.Element("defName").Value, Label = textInfo.ToTitleCase(trait.Element("label").Value), Degree = trait.Element("degree") != null ? trait.Element("degree").Value : "0", Description = trait.Element("description").Value }); foreach (var trait in traits) { if (!Traits.ContainsKey(trait.Def + trait.Degree)) { Traits.Add(trait.Def + trait.Degree, trait); } } } foreach (var hediffRoot in docRoot.XPathSelectElements("HediffDef/hediffClass/..")) { var parentClass = hediffRoot.Element("hediffClass").Value; var parentName = hediffRoot.Attribute("Name") != null?hediffRoot.Attribute("Name").Value : "None"; Hediff coreHediff; if (!Hediffs.TryGetValue(parentClass, out coreHediff)) { Hediffs.Add(parentClass, coreHediff = new Hediff(parentClass, parentName)); } var hediffs = (from hediff in docRoot.XPathSelectElements("//HediffDef[boolean(@ParentName) and not(@Abstract)]") .Where(x => x.Attribute("ParentName").Value == parentName) select new HediffDef { ParentClass = parentClass, ParentName = hediff.Attribute("ParentName").Value, Def = hediff.Element("defName").Value, Label = textInfo.ToTitleCase(hediff.Element("label").Value), }); foreach (var hediff in hediffs) { coreHediff.SubDiffs[hediff.Def] = hediff; } } foreach (var def in docRoot.Descendants("AlienRace.BackstoryDef")) { if (def.Attribute("Abstract") == null || def.Attribute("Abstract").Value != "True") { Backstory backstory = new Backstory { Id = (string)def.Element("defName"), Title = (string)def.Element("title"), DisplayTitle = "(AlienRace)" + (string)def.Element("title"), TitleShort = (string)def.Element("titleShort"), Description = (string)def.Element("baseDescription"), Slot = (string)def.Element("slot"), SkillGains = new Dictionary <string, int>(), WorkDisables = new List <string>() }; foreach (var skillGain in def.XPathSelectElements("skillGains/li")) { string defName = (string)skillGain.Element("defName"); int amount = Convert.ToInt32(skillGain.Element("amount").GetValue()); backstory.SkillGains.Add(defName, amount); } foreach (var workDisables in def.XPathSelectElements("workDisables/li")) { backstory.WorkDisables.Add(workDisables.GetValue()); } ResourceLoader.Backstories[backstory.Id] = backstory; if (string.IsNullOrEmpty(backstory.Slot)) { ResourceLoader.ChildhoodStories.Add(backstory); ResourceLoader.AdulthoodStories.Add(backstory); } else if (backstory.Slot == "Childhood") { ResourceLoader.ChildhoodStories.Add(backstory); } else { ResourceLoader.AdulthoodStories.Add(backstory); } } } foreach (var relationDef in docRoot.XPathSelectElements("PawnRelationDef")) { var pawnRelation = new PawnRelationDef(relationDef); PawnRelationDefs.Add(pawnRelation.DefName, pawnRelation); } foreach (var thingDef in docRoot.XPathSelectElements("ThingDef")) { ThingDef def = new ThingDef(thingDef); if (ThingDefs.TryGetValue(def.Name, out ThingDef value)) { value.updateDef(thingDef); } else { ThingDefs.Add(def.Name, def); } } foreach (var bodyDefElement in docRoot.XPathSelectElements("BodyDef")) { BodyDef bodyDef = new BodyDef(bodyDefElement); if (bodyDef.DefName != null) { BodyDefsByDef[bodyDef.DefName] = bodyDef; } } fileStream.Close(); } } Dictionary <string, Race> tempRaceDic = RaceDictionary.Values.ToDictionary(x => x.Label.ToLower(), x => x); foreach (Hair hair in allHairs) { List <Race> races = new List <Race>(); foreach (var hairTags in hair.HairTags) { Race race; if (tempRaceDic.TryGetValue(hairTags, out race)) { races.Add(race); } } if (races.Count == 0) { races.Add(RaceDictionary["Human"]); if (RaceDictionary.TryGetValue("Alien_Orassan", out var race)) { races.Add(race); } } foreach (var race in races) { if (hair.Gender.Equals("Any") || hair.Gender.Contains("Usually")) { foreach (var list in race.HairsByGender.Values.ToList()) { list.Add(hair); } } else { List <Hair> hairListForGender; if (race.HairsByGender.TryGetValue(hair.Gender, out hairListForGender)) { hairListForGender.Add(hair); } } } } foreach (ThingDef thingDef in ThingDefs.Values) { if (thingDef.ParentName != null && ThingDefs.TryGetValue(thingDef.ParentName, out ThingDef value)) { thingDef.Parent = value; } if (thingDef.DefName != null && !ThingDefsByDefName.ContainsKey(thingDef.DefName)) { ThingDefsByDefName.Add(thingDef.DefName, thingDef); } foreach (string stuffPropCat in thingDef.StuffPropsCategories) { if (ThingDefsByStuffCategory.TryGetValue(stuffPropCat, out var list)) { list.Add(thingDef); } else { List <ThingDef> thingDefs = new List <ThingDef>(); thingDefs.Add(thingDef); ThingDefsByStuffCategory.Add(stuffPropCat, thingDefs); } } } ResourceLoader.ChildhoodStories = ResourceLoader.ChildhoodStories.OrderBy(x => x.DisplayTitle).ToList(); ResourceLoader.AdulthoodStories = ResourceLoader.AdulthoodStories.OrderBy(x => x.DisplayTitle).ToList(); }
/// <summary> /// Called to setup the arena boundaries and bind to all of the physics stuff. /// </summary> public virtual void Bind() { float width = dimensions.X, height = dimensions.Y; worldAABB = new AABB(); worldAABB.LowerBound.Set(-width / 2 - edgeTolerance, -edgeTolerance); worldAABB.UpperBound.Set(width / 2 + edgeTolerance, height + edgeTolerance); Vec2 gravity = new Vec2(this.gravity.X, this.gravity.Y); bool doSleep = true; world = new World(worldAABB, gravity, doSleep); world.SetContactFilter(new ContactFilter()); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, 0.0f); Body groundBody = world.CreateBody(groundBodyDef); // Bottom AddBoundaryBlock(groundBody, 0, -(boundaryThickness / 2), width + boundaryThickness * 2, boundaryThickness); // Top AddBoundaryBlock(groundBody, 0, height + boundaryThickness / 2, width + boundaryThickness * 2, boundaryThickness); // Left AddBoundaryBlock(groundBody, -(width / 2) - boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2); // Right AddBoundaryBlock(groundBody, +(width / 2) + boundaryThickness / 2, height / 2, boundaryThickness, height + boundaryThickness * 2); DebugDraw draw = new OpenTKDebugDraw(); draw.Flags = DebugDraw.DrawFlags.Shape; if (Program.DebugDraw) { world.SetDebugDraw(draw); } world.SetContactListener(this); // Old code for reference /* * * * _worldAABB = new AABB(); * _worldAABB.LowerBound.Set(-30.0f, -20.0f); * _worldAABB.UpperBound.Set(30.0f, 40.0f); * Vec2 gravity = new Vec2(); * gravity.Set(0.0f, -10.0f); * bool doSleep = true; * _world = new World(_worldAABB, gravity, doSleep); * _world.SetContactFilter(new ContactFilter()); * * BodyDef groundBodyDef = new BodyDef(); * groundBodyDef.Position.Set(0.0f, 0.0f); * * Body groundBody = _world.CreateBody(groundBodyDef); * * AddBlock(groundBody, 0, -5, 40, 10); * AddBlock(groundBody, -20, 20, 1, 40); * AddBlock(groundBody, 20, 20, 1, 40); * AddBlock(groundBody, 0, 30, 40, 10); * * DebugDraw draw = new OpenTKDebugDraw(); * draw.Flags = DebugDraw.DrawFlags.Shape; * if (Program.DebugDraw) { * _world.SetDebugDraw(draw); * } * * _world.SetContactListener(this); */ }
private BridgeTest() { Body ground; { BodyDef bd = new BodyDef(); ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.AddFixture(shape); } { PolygonShape shape = new PolygonShape(20.0f); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; fd.Friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); Body prevBody = ground; for (int i = 0; i < _count; ++i) { BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-14.5f + 1.0f * i, 5.0f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); JointFactory.CreateFromDef(World, jd); prevBody = body; } Vector2 anchor2 = new Vector2(-15.0f + 1.0f * _count, 5.0f); jd.Initialize(prevBody, ground, anchor2); JointFactory.CreateFromDef(World, jd); } for (int i = 0; i < 2; ++i) { Vertices vertices = new Vertices(3); vertices.Add(new Vector2(-0.5f, 0.0f)); vertices.Add(new Vector2(0.5f, 0.0f)); vertices.Add(new Vector2(0.0f, 1.5f)); PolygonShape shape = new PolygonShape(vertices, 1.0f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-8.0f + 8.0f * i, 12.0f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(fd); } for (int i = 0; i < 3; ++i) { CircleShape shape = new CircleShape(0.5f, 1.0f); FixtureDef fd = new FixtureDef(); fd.Shape = shape; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-6.0f + 6.0f * i, 10.0f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(fd); } }
public SliderCrank2() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var prevBody = ground; // Define crank. { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 2.0f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position = new Vector2(0.0f, 7.0f); var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(0.0f, 5.0f)); rjd.MotorSpeed = 1.0f * Settings.Pi; rjd.MaxMotorTorque = 10000.0f; rjd.EnableMotor = true; _joint1 = (RevoluteJoint)World.CreateJoint(rjd); prevBody = body; } // Define follower. { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 4.0f); var bd = new BodyDef { BodyType = BodyType.DynamicBody, Position = new Vector2(0.0f, 13.0f) }; var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(0.0f, 9.0f)); rjd.EnableMotor = false; World.CreateJoint(rjd); prevBody = body; } // Define piston { var shape = new PolygonShape(); shape.SetAsBox(1.5f, 1.5f); var bd = new BodyDef { BodyType = BodyType.DynamicBody, FixedRotation = true, Position = new Vector2(0.0f, 17.0f) }; var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(0.0f, 17.0f)); World.CreateJoint(rjd); var pjd = new PrismaticJointDef(); pjd.Initialize(ground, body, new Vector2(0.0f, 17.0f), new Vector2(0.0f, 1.0f)); pjd.MaxMotorForce = 1000.0f; pjd.EnableMotor = true; _joint2 = (PrismaticJoint)World.CreateJoint(pjd); } // Create a payload { var shape = new PolygonShape(); shape.SetAsBox(1.5f, 1.5f); var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position = new Vector2(0.0f, 23.0f); var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); } } }
public override bool CanHaveCapacity(BodyDef body) { return(body.HasPartWithTag(BodyPartTagDefOf.BloodPumpingSource)); }
public override bool CanHaveCapacity(BodyDef body) { return(true); }
private void Create(int index) { if (_bodies[_bodyIndex] != null) { _world.DestroyBody(_bodies[_bodyIndex]); _bodies[_bodyIndex] = null; } BodyDef bd = new BodyDef(); float x = Rand.RandomFloat(-10.0f, 10.0f); float y = Rand.RandomFloat(0.0f, 20.0f); bd.position = new Vector2(x, y); bd.angle = Rand.RandomFloat(-(float)Math.PI, (float)Math.PI); bd.userData = index; if (index == 4) { bd.angularDamping = 0.02f; } _bodies[_bodyIndex] = _world.CreateBody(bd); if (index < 4) { FixtureDef fd = new FixtureDef(); fd.shape = _polygons[index]; fd.friction = 0.3f; _bodies[_bodyIndex].CreateFixture(fd); } else { FixtureDef fd = new FixtureDef(); fd.shape = _circle; fd.friction = 0.3f; _bodies[_bodyIndex].CreateFixture(fd); } _bodyIndex = (_bodyIndex + 1) % e_maxBodies; }
Web() { Body ground = null; { BodyDef bd = new BodyDef(); ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-5.0f, 5.0f); _bodies[0] = _world.CreateBody(bd); _bodies[0].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 5.0f); _bodies[1] = _world.CreateBody(bd); _bodies[1].CreateFixture(shape, 5.0f); bd.position = new Vector2(5.0f, 15.0f); _bodies[2] = _world.CreateBody(bd); _bodies[2].CreateFixture(shape, 5.0f); bd.position = new Vector2(-5.0f, 15.0f); _bodies[3] = _world.CreateBody(bd); _bodies[3].CreateFixture(shape, 5.0f); DistanceJointDef jd = new DistanceJointDef(); Vector2 p1, p2, d; jd.frequencyHz = 4.0f; jd.dampingRatio = 0.5f; jd.bodyA = ground; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(-10.0f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[0] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(10.0f, 0.0f); jd.localAnchorB = new Vector2(0.5f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[1] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(10.0f, 20.0f); jd.localAnchorB = new Vector2(0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[2] = _world.CreateJoint(jd); jd.bodyA = ground; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-10.0f, 20.0f); jd.localAnchorB = new Vector2(-0.5f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[3] = _world.CreateJoint(jd); jd.bodyA = _bodies[0]; jd.bodyB = _bodies[1]; jd.localAnchorA = new Vector2(0.5f, 0.0f); jd.localAnchorB = new Vector2(-0.5f, 0.0f);; p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[4] = _world.CreateJoint(jd); jd.bodyA = _bodies[1]; jd.bodyB = _bodies[2]; jd.localAnchorA = new Vector2(0.0f, 0.5f); jd.localAnchorB = new Vector2(0.0f, -0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[5] = _world.CreateJoint(jd); jd.bodyA = _bodies[2]; jd.bodyB = _bodies[3]; jd.localAnchorA = new Vector2(-0.5f, 0.0f); jd.localAnchorB = new Vector2(0.5f, 0.0f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[6] = _world.CreateJoint(jd); jd.bodyA = _bodies[3]; jd.bodyB = _bodies[0]; jd.localAnchorA = new Vector2(0.0f, -0.5f); jd.localAnchorB = new Vector2(0.0f, 0.5f); p1 = jd.bodyA.GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB.GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); _joints[7] = _world.CreateJoint(jd); } }
public Test() { Vector2 gravity; gravity = new Vector2(0.0f, -10.0f); bool doSleep = true; _world = new World(gravity, doSleep); _textLine = 30; _destructionListener.test = this; _world.DestructionListener = _destructionListener; _world.ContactListener = this; _world.DebugDraw = _debugDraw; _bombSpawning = false; _stepCount = 0; BodyDef bodyDef = new BodyDef(); _groundBody = _world.CreateBody(bodyDef); }
public CollisionFiltering() { // Ground body { PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.friction = 0.3f; BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); ground.CreateFixture(sd); } // Small triangle Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-1.0f, 0.0f); vertices[1] = new Vector2(1.0f, 0.0f); vertices[2] = new Vector2(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.Dynamic; triangleBodyDef.position = new Vector2(-5.0f, 2.0f); Body body1 = _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 = new Vector2(-5.0f, 6.0f); triangleBodyDef.fixedRotation = true; // look at me! Body body2 = _world.CreateBody(triangleBodyDef); body2.CreateFixture(triangleShapeDef); { BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-5.0f, 10.0f); Body body = _world.CreateBody(bd); PolygonShape p = new PolygonShape(); p.SetAsBox(0.5f, 1.0f); body.CreateFixture(p, 1.0f); PrismaticJointDef jd = new PrismaticJointDef(); jd.bodyA = body2; jd.bodyB = body; jd.enableLimit = true; jd.localAnchorA = new Vector2(0.0f, 4.0f); jd.localAnchorB = Vector2.Zero; jd.localAxis1 = new Vector2(0.0f, 1.0f); jd.lowerTranslation = -1.0f; jd.upperTranslation = 1.0f; _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.Dynamic; boxBodyDef.position = new Vector2(0.0f, 2.0f); Body body3 = _world.CreateBody(boxBodyDef); body3.CreateFixture(boxShapeDef); // Large box (recycle definitions) polygon.SetAsBox(2.0f, 1.0f); boxShapeDef.filter.groupIndex = k_largeGroup; boxBodyDef.position = new Vector2(0.0f, 6.0f); Body body4 = _world.CreateBody(boxBodyDef); body4.CreateFixture(boxShapeDef); // Small circle CircleShape circle = new CircleShape(); circle._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.Dynamic; circleBodyDef.position = new Vector2(5.0f, 2.0f); Body body5 = _world.CreateBody(circleBodyDef); body5.CreateFixture(circleShapeDef); // Large circle circle._radius *= 2.0f; circleShapeDef.filter.groupIndex = k_largeGroup; circleBodyDef.position = new Vector2(5.0f, 6.0f); Body body6 = _world.CreateBody(circleBodyDef); body6.CreateFixture(circleShapeDef); }
private void Start() { // Ground body { var bd = new BodyDef(); var ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.Set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Collinear edges with no adjacency information. // This shows the problematic case where a box shape can hit // an internal vertex. { var bd = new BodyDef(); var ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.Set(new Vector2(-8.0f, 1.0f), new Vector2(-6.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.Set(new Vector2(-6.0f, 1.0f), new Vector2(-4.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); shape.Set(new Vector2(-4.0f, 1.0f), new Vector2(-2.0f, 1.0f)); ground.CreateFixture(shape, 0.0f); } // Chain shape { var bd = new BodyDef { Angle = 0.25f * Settings.Pi }; var ground = World.CreateBody(bd); var vs = new Vector2[4] { new Vector2(5.0f, 7.0f), new Vector2(6.0f, 8.0f), new Vector2(7.0f, 8.0f), new Vector2(8.0f, 7.0f) }; var shape = new ChainShape(); shape.CreateChain(vs); ground.CreateFixture(shape, 0.0f); } // Square tiles. This shows that adjacency shapes may // have non-smooth collision. There is no solution // to this problem. { var bd = new BodyDef(); var ground = World.CreateBody(bd); var shape = new PolygonShape(); shape.SetAsBox(1.0f, 1.0f, new Vector2(4.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(6.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); shape.SetAsBox(1.0f, 1.0f, new Vector2(8.0f, 3.0f), 0.0f); ground.CreateFixture(shape, 0.0f); } // Square made from an edge loop. Collision should be smooth. { var bd = new BodyDef(); var ground = World.CreateBody(bd); var vs = new Vector2[4] { new Vector2(-1.0f, 3.0f), new Vector2(1.0f, 3.0f), new Vector2(1.0f, 5.0f), new Vector2(-1.0f, 5.0f) }; var shape = new ChainShape(); shape.CreateLoop(vs); ground.CreateFixture(shape, 0.0f); } // Edge loop. Collision should be smooth. { var bd = new BodyDef { Position = new Vector2(-10.0f, 4.0f) }; var ground = World.CreateBody(bd); var vs = new Vector2[10] { new Vector2(0.0f, 0.0f), new Vector2(6.0f, 0.0f), new Vector2(6.0f, 2.0f), new Vector2(4.0f, 1.0f), new Vector2(2.0f, 2.0f), new Vector2(0.0f, 2.0f), new Vector2(-2.0f, 2.0f), new Vector2(-4.0f, 3.0f), new Vector2(-6.0f, 2.0f), new Vector2(-6.0f, 0.0f) }; var shape = new ChainShape(); shape.CreateLoop(vs); ground.CreateFixture(shape, 0.0f); } // Square character 1 { var bd = new BodyDef { Position = new Vector2(-3.0f, 8.0f), BodyType = BodyType.DynamicBody, FixedRotation = true, AllowSleep = false }; var body = World.CreateBody(bd); var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); var fd = new FixtureDef { Shape = shape, Density = 20.0f }; body.CreateFixture(fd); } // Square character 2 { var bd = new BodyDef { Position = new Vector2(-5.0f, 5.0f), BodyType = BodyType.DynamicBody, FixedRotation = true, AllowSleep = false }; var body = World.CreateBody(bd); var shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); var fd = new FixtureDef { Shape = shape, Density = 20.0f }; body.CreateFixture(fd); } // Hexagon character { var bd = new BodyDef { Position = new Vector2(-5.0f, 8.0f), BodyType = BodyType.DynamicBody, FixedRotation = true, AllowSleep = false }; var body = World.CreateBody(bd); Single angle = 0.0f; Single delta = Settings.Pi / 3.0f; var vertices = new Vector2[6]; for (var i = 0; i < 6; ++i) { vertices[i].Set(0.5f * (Single)Math.Cos(angle), 0.5f * (Single)Math.Sin(angle)); angle += delta; } var shape = new PolygonShape(); shape.Set(vertices); var fd = new FixtureDef { Shape = shape, Density = 20.0f }; body.CreateFixture(fd); } // Circle character { var bd = new BodyDef { Position = new Vector2(3.0f, 5.0f), BodyType = BodyType.DynamicBody, FixedRotation = true, AllowSleep = false }; var body = World.CreateBody(bd); var shape = new CircleShape { Radius = 0.5f }; var fd = new FixtureDef { Shape = shape, Density = 20.0f }; body.CreateFixture(fd); } // Circle character { var bd = new BodyDef { Position = new Vector2(-7.0f, 6.0f), BodyType = BodyType.DynamicBody, AllowSleep = false }; _character = World.CreateBody(bd); var shape = new CircleShape { Radius = 0.25f }; var fd = new FixtureDef { Shape = shape, Density = 20.0f, Friction = 1.0f }; _character.CreateFixture(fd); } DrawString("This tests various character collision shapes."); DrawString("Limitation: square and hexagon can snag on aligned boxes."); DrawString("Feature: edge chains have smooth collision inside and out."); }
/// <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); // Add some small mounds/bumps to the ground. for (float x = -1f; x < 40f; x += 0.4f + ((_rng.NextFloat() - 0.5f) * 0.15f)) { WalkerWorldUtils.CreateMound(_world, x, 0f, _simParams._defaultFriction, _simParams._defaultRestitution); } // ==== Define walker torso. float walkerX = 0f; float walkerY = 1.30f;// + ((float)_rng.NextDouble() * 0.1f); BodyDef torsoBodyDef = new BodyDef(); torsoBodyDef.Position.Set(walkerX, walkerY); torsoBodyDef.IsBullet = true; // Create walker torso. _torsoBody = _world.CreateBody(torsoBodyDef); PolygonDef torsoShapeDef = new PolygonDef(); torsoShapeDef.SetAsBox(0.10f, 0.30f); 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(walkerX, walkerY - 0.25f); Vec2 lowerLegPosBase = new Vec2(walkerX, walkerY - 0.75f); // ===== 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); }
private void CreateLeg(float s, Vector2 wheelAnchor) { Vector2 p1 = new Vector2(5.4f * s, -6.1f); Vector2 p2 = new Vector2(7.2f * s, -1.2f); Vector2 p3 = new Vector2(4.3f * s, -1.9f); Vector2 p4 = new Vector2(3.1f * s, 0.8f); Vector2 p5 = new Vector2(6.0f * s, 1.5f); Vector2 p6 = new Vector2(2.5f * s, 3.7f); FixtureDef fd1 = new FixtureDef(), fd2 = new FixtureDef(); fd1.Filter.Group = -1; fd2.Filter.Group = -1; PolygonShape poly1 = new PolygonShape(1.0f), poly2 = new PolygonShape(1.0f); if (s > 0.0f) { Vertices vertices = new Vertices(3); vertices.Add(p1); vertices.Add(p2); vertices.Add(p3); poly1.Vertices = vertices; vertices[0] = Vector2.Zero; vertices[1] = p5 - p4; vertices[2] = p6 - p4; poly2.Vertices = vertices; } else { Vertices vertices = new Vertices(3); vertices.Add(p1); vertices.Add(p3); vertices.Add(p2); poly1.Vertices = vertices; vertices[0] = Vector2.Zero; vertices[1] = p6 - p4; vertices[2] = p5 - p4; poly2.Vertices = vertices; } fd1.Shape = poly1; fd2.Shape = poly2; BodyDef bd1 = new BodyDef(), bd2 = new BodyDef(); bd1.Type = BodyType.Dynamic; bd2.Type = BodyType.Dynamic; bd1.Position = _offset; bd2.Position = p4 + _offset; bd1.AngularDamping = 10.0f; bd2.AngularDamping = 10.0f; Body body1 = BodyFactory.CreateFromDef(World, bd1); Body body2 = BodyFactory.CreateFromDef(World, bd2); body1.AddFixture(fd1); body2.AddFixture(fd2); { DistanceJointDef jd = 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. float dampingRatio = 0.5f; float frequencyHz = 10.0f; jd.Initialize(body1, body2, p2 + _offset, p5 + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out float stiffness, out float damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body1, body2, p3 + _offset, p4 + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body1, _wheel, p3 + _offset, wheelAnchor + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); jd.Initialize(body2, _wheel, p6 + _offset, wheelAnchor + _offset); JointHelper.LinearStiffness(frequencyHz, dampingRatio, jd.BodyA, jd.BodyB, out stiffness, out damping); jd.Stiffness = stiffness; jd.Damping = damping; JointFactory.CreateFromDef(World, jd); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(body2, _chassis, p4 + _offset); JointFactory.CreateFromDef(World, jd); } }
TheoJansen() { _offset = new Vector2(0.0f, 8.0f); _motorSpeed = 2.0f; _motorOn = true; Vector2 pivot = new Vector2(0.0f, 0.8f); // Ground { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(-50.0f, 0.0f), new Vector2(-50.0f, 10.0f)); ground.CreateFixture(shape, 0.0f); shape.SetAsEdge(new Vector2(50.0f, 0.0f), new Vector2(50.0f, 10.0f)); ground.CreateFixture(shape, 0.0f); } // Balls for (int i = 0; i < 40; ++i) { CircleShape shape = new CircleShape(); shape._radius = 0.25f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = new Vector2(-40.0f + 2.0f * i, 0.5f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 1.0f); } // Chassis { PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.5f, 1.0f); FixtureDef sd = new FixtureDef(); sd.density = 1.0f; sd.shape = shape; sd.filter.groupIndex = -1; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = pivot + _offset; _chassis = _world.CreateBody(bd); _chassis.CreateFixture(sd); } { CircleShape shape = new CircleShape(); shape._radius = 1.6f; FixtureDef sd = new FixtureDef(); sd.density = 1.0f; sd.shape = shape; sd.filter.groupIndex = -1; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = pivot + _offset; _wheel = _world.CreateBody(bd); _wheel.CreateFixture(sd); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(_wheel, _chassis, pivot + _offset); jd.collideConnected = false; jd.motorSpeed = _motorSpeed; jd.maxMotorTorque = 400.0f; jd.enableMotor = _motorOn; _motorJoint = (RevoluteJoint)_world.CreateJoint(jd); } Vector2 wheelAnchor; wheelAnchor = pivot + new Vector2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.GetPosition(), 120.0f * (float)Settings.b2_pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.GetPosition(), -120.0f * (float)Settings.b2_pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
private TheoJansenTest() { _offset = new Vector2(0.0f, 8.0f); _motorSpeed = 2.0f; _motorOn = true; Vector2 pivot = new Vector2(0.0f, 0.8f); // Ground { BodyDef bd = new BodyDef(); Body ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-50.0f, 0.0f), new Vector2(50.0f, 0.0f)); ground.AddFixture(shape); shape.SetTwoSided(new Vector2(-50.0f, 0.0f), new Vector2(-50.0f, 10.0f)); ground.AddFixture(shape); shape.SetTwoSided(new Vector2(50.0f, 0.0f), new Vector2(50.0f, 10.0f)); ground.AddFixture(shape); } // Balls for (int i = 0; i < 40; ++i) { CircleShape shape = new CircleShape(1.0f); shape.Radius = 0.25f; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-40.0f + 2.0f * i, 0.5f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(shape); } // Chassis { PolygonShape shape = new PolygonShape(1.0f); shape.SetAsBox(2.5f, 1.0f); FixtureDef sd = new FixtureDef(); sd.Shape = shape; sd.Filter.Group = -1; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = pivot + _offset; _chassis = BodyFactory.CreateFromDef(World, bd); _chassis.AddFixture(sd); } { CircleShape shape = new CircleShape(1.0f); shape.Radius = 1.6f; FixtureDef sd = new FixtureDef(); sd.Shape = shape; sd.Filter.Group = -1; BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = pivot + _offset; _wheel = BodyFactory.CreateFromDef(World, bd); _wheel.AddFixture(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)JointFactory.CreateFromDef(World, jd); } Vector2 wheelAnchor; wheelAnchor = pivot + new Vector2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.Position, 120.0f * MathConstants.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.Position, -120.0f * MathConstants.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
public override void Keyboard(KeyboardState state, KeyboardState oldState) { if (state.IsKeyDown(Keys.OemComma) && oldState.IsKeyUp(Keys.OemComma)) { if (_bullet != null) { _world.DestroyBody(_bullet); _bullet = null; } { CircleShape shape = new CircleShape(); shape._radius = 0.25f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.bullet = true; bd.position = new Vector2(-31.0f, 5.0f); _bullet = _world.CreateBody(bd); _bullet.CreateFixture(fd); _bullet.SetLinearVelocity(new Vector2(400.0f, 0.0f)); } } }
public Cantilever() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; var jd = new WeldJointDef(); var prevBody = ground; for (var i = 0; i < Count; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-14.5f + 1.0f * i, 5.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); var anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); prevBody = body; } } { var shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; var jd = new WeldJointDef(); jd.FrequencyHz = 5.0f; jd.DampingRatio = 0.7f; var prevBody = ground; for (var i = 0; i < 3; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-14.0f + 2.0f * i, 15.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); var anchor = new Vector2(-15.0f + 2.0f * i, 15.0f); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); prevBody = body; } } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; var jd = new WeldJointDef(); var prevBody = ground; for (var i = 0; i < Count; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-4.5f + 1.0f * i, 5.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { var anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); } prevBody = body; } } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; var jd = new WeldJointDef(); jd.FrequencyHz = 8.0f; jd.DampingRatio = 0.7f; var prevBody = ground; for (var i = 0; i < Count; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(5.5f + 1.0f * i, 10.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { var anchor = new Vector2(5.0f + 1.0f * i, 10.0f); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); } prevBody = body; } } for (var i = 0; i < 2; ++i) { var vertices = new Vector2 [3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); var shape = new PolygonShape(); shape.Set(vertices); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-8.0f + 8.0f * i, 12.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); } for (var i = 0; i < 2; ++i) { var shape = new CircleShape(); shape.Radius = 0.5f; var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-6.0f + 6.0f * i, 10.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); } }
private void CheckImpudentDefense(float iterationDuration) { if (this.scored || !this.shouldAttack) { return; } bool playerLeftDefends, playerRightDefends; if (this.playerLeft.GetPosition().X < 4.24f && this.playerRight.GetPosition().X * 1 > 4.24f) { playerLeftDefends = true; } else { playerLeftDefends = false; } if (this.playerRight.GetPosition().X > 9.41f && this.playerLeft.GetPosition().X * 1 < 9.41f) { playerRightDefends = true; } else { playerRightDefends = false; } if (playerLeftDefends && this.leftPostRaiseCount <= 3) { this.playerLeftTimers.Defend += iterationDuration; if (this.playerLeftTimers.Defend < 0) { Vec2 temp = this.goalLeft.GetPosition(); this.world.DestroyBody(this.goalLeft); BodyDef goalDef = new BodyDef() { Position = new Vec2(temp.X, temp.Y - 0.5f) }; this.goalLeft = this.world.CreateBody(goalDef); CircleDef post = new CircleDef() { Radius = 0.075f, LocalPosition = new Vec2(0.425f, 0), Density = 1, Friction = 1, Restitution = 0 }; this.goalLeft.CreateShape(post); PolygonDef net = new PolygonDef() { Density = 1, Friction = 1, Restitution = 0 }; net.SetAsBox(0.46f, 0.001f, new Vec2(-0.04f, -0.069f), 0); this.goalLeft.CreateShape(net); ++this.leftPostRaiseCount; this.playerLeftTimers.Defend = 12000; this.LeftPostRaised?.Invoke(); } } if (playerRightDefends && this.rightPostRaiseCount <= 3) { this.playerRightTimers.Defend += iterationDuration; if (this.playerRightTimers.Defend < 0) { Vec2 temp = this.goalRight.GetPosition(); this.world.DestroyBody(this.goalRight); BodyDef goalDef = new BodyDef() { Position = new Vec2(temp.X, temp.Y - 0.5f) }; this.goalRight = this.world.CreateBody(goalDef); CircleDef post = new CircleDef() { Radius = 0.075f, LocalPosition = new Vec2(-0.425f, 0), Density = 1, Friction = 1, Restitution = 0 }; this.goalRight.CreateShape(post); PolygonDef net = new PolygonDef() { Density = 1, Friction = 1, Restitution = 0 }; net.SetAsBox(0.46f, 0.001f, new Vec2(0.04f, -0.069f), 0); this.goalRight.CreateShape(net); ++this.rightPostRaiseCount; this.playerRightTimers.Defend = 12000; this.RightPostRaised?.Invoke(); } } }
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; }
protected override void Create() { Body ground; { var bd = new BodyDef { Position = new Vector2(0.0f, 17.0f) }; ground = World.CreateBody(bd); } { var prevBody = ground; // Define crank. { var shape = new PolygonShape(); shape.SetAsBox(4.0f, 1.0f); var bd = new BodyDef { BodyType = BodyType.DynamicBody, Position = new Vector2(-8.0f, 20.0f) }; var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(-12.0f, 20.0f)); World.CreateJoint(rjd); prevBody = body; } // Define connecting rod { var shape = new PolygonShape(); shape.SetAsBox(8.0f, 1.0f); var bd = new BodyDef { BodyType = BodyType.DynamicBody, Position = new Vector2(4.0f, 20.0f) }; var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(-4.0f, 20.0f)); World.CreateJoint(rjd); prevBody = body; } // Define piston { var shape = new PolygonShape(); shape.SetAsBox(3.0f, 3.0f); var bd = new BodyDef { BodyType = BodyType.DynamicBody, FixedRotation = true, Position = new Vector2(12.0f, 20.0f) }; var body = World.CreateBody(bd); body.CreateFixture(shape, 2.0f); var rjd = new RevoluteJointDef(); rjd.Initialize(prevBody, body, new Vector2(12.0f, 20.0f)); World.CreateJoint(rjd); var pjd = new PrismaticJointDef(); pjd.Initialize(ground, body, new Vector2(12.0f, 17.0f), new Vector2(1.0f, 0.0f)); World.CreateJoint(pjd); } } }
public KeyPair(BodyDef d, ThingDef a) { bodyDef = d; apparel = a; }
private RevoluteJointTest() { Body ground; { BodyDef bd = new BodyDef(); ground = BodyFactory.CreateFromDef(World, bd); EdgeShape shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); FixtureDef fd = new FixtureDef(); fd.Shape = shape; //fd.Filter.Category = 2; ground.AddFixture(fd); } _enableLimit = true; _enableMotor = false; _motorSpeed = 1.0f; { PolygonShape shape = new PolygonShape(5.0f); shape.SetAsBox(0.25f, 3.0f, new Vector2(0.0f, 3.0f), 0.0f); BodyDef bd = new BodyDef(); bd.Type = BodyType.Dynamic; bd.Position = new Vector2(-10.0f, 20.0f); Body body = BodyFactory.CreateFromDef(World, bd); body.AddFixture(shape); RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(ground, body, new Vector2(-10.0f, 20.5f)); jd.MotorSpeed = _motorSpeed; jd.MaxMotorTorque = 10000.0f; jd.EnableMotor = _enableMotor; jd.LowerAngle = -0.25f * MathConstants.Pi; jd.UpperAngle = 0.5f * MathConstants.Pi; jd.EnableLimit = _enableLimit; _joint1 = (RevoluteJoint)JointFactory.CreateFromDef(World, jd); } { CircleShape circle_shape = new CircleShape(5.0f); circle_shape.Radius = 2.0f; BodyDef circle_bd = new BodyDef(); circle_bd.Type = BodyType.Dynamic; circle_bd.Position = new Vector2(5.0f, 30.0f); FixtureDef fd = new FixtureDef(); fd.Filter.CategoryMask = Category.Cat1; fd.Shape = circle_shape; _ball = BodyFactory.CreateFromDef(World, circle_bd); _ball.AddFixture(fd); PolygonShape polygon_shape = new PolygonShape(2.0f); polygon_shape.SetAsBox(10.0f, 0.5f, new Vector2(-10.0f, 0.0f), 0.0f); BodyDef polygon_bd = new BodyDef(); polygon_bd.Position = new Vector2(20.0f, 10.0f); polygon_bd.Type = BodyType.Dynamic; polygon_bd.IsBullet = true; Body polygon_body = BodyFactory.CreateFromDef(World, polygon_bd); polygon_body.AddFixture(polygon_shape); RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(ground, polygon_body, new Vector2(19.0f, 10.0f)); jd.LowerAngle = -0.25f * MathConstants.Pi; jd.UpperAngle = 0.0f * MathConstants.Pi; jd.EnableLimit = true; jd.EnableMotor = true; jd.MotorSpeed = 0.0f; jd.MaxMotorTorque = 10000.0f; _joint2 = (RevoluteJoint)JointFactory.CreateFromDef(World, jd); } }
public void LaunchBomb(Vector2 position, Vector2 velocity) { if (_bomb != null) { _world.DestroyBody(_bomb); _bomb = null; } BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = position; bd.bullet = true; _bomb = _world.CreateBody(bd); _bomb.SetLinearVelocity(velocity); CircleShape circle = new CircleShape(); circle._radius = 0.3f; FixtureDef fd = new FixtureDef(); fd.shape = circle; fd.density = 20.0f; fd.restitution = 0.0f; Vector2 minV = position - new Vector2(0.3f,0.3f); Vector2 maxV = position + new Vector2(0.3f,0.3f); AABB aabb; aabb.lowerBound = minV; aabb.upperBound = maxV; _bomb.CreateFixture(fd); }
public Bridge() { Body ground; { var bd = new BodyDef(); ground = World.CreateBody(bd); var shape = new EdgeShape(); shape.SetTwoSided(new Vector2(-40.0f, 0.0f), new Vector2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { var shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 20.0f; fd.Friction = 0.2f; var jd = new RevoluteJointDef(); var prevBody = ground; for (var i = 0; i < Count; ++i) { var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-14.5f + 1.0f * i, 5.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); var anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); World.CreateJoint(jd); if (i == Count >> 1) { _middle = body; } prevBody = body; } { var anchor = new Vector2(-15.0f + 1.0f * Count, 5.0f); jd.Initialize(prevBody, ground, anchor); World.CreateJoint(jd); } } for (var i = 0; i < 2; ++i) { var vertices = new Vector2[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); var shape = new PolygonShape(); shape.Set(vertices); var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-8.0f + 8.0f * i, 12.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); } for (var i = 0; i < 3; ++i) { var shape = new CircleShape(); shape.Radius = 0.5f; var fd = new FixtureDef(); fd.Shape = shape; fd.Density = 1.0f; var bd = new BodyDef(); bd.BodyType = BodyType.DynamicBody; bd.Position.Set(-6.0f + 6.0f * i, 10.0f); var body = World.CreateBody(bd); body.CreateFixture(fd); } }