public ShapeEditing() { { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); } BodyDef bodydef = new BodyDef(); bodydef.Position.Set(0.0f, 10.0f); _body = _world.CreateBody(bodydef); PolygonDef sd_ = new PolygonDef(); sd_.SetAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f); sd_.Density = 10.0f; _shape1 = _body.CreateShape(sd_); _body.SetMassFromShapes(); _shape2 = null; }
public DistanceTest() { PolygonDef sd = new PolygonDef(); sd.SetAsBox(1.0f, 1.0f); sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body1 = _world.CreateBody(bd); _shape1 = _body1.CreateShape(sd); PolygonDef sd2 = new PolygonDef(); sd2.VertexCount = 3; sd2.Vertices[0].Set(-1.0f, 0.0f); sd2.Vertices[1].Set(1.0f, 0.0f); sd2.Vertices[2].Set(0.0f, 15.0f); sd2.Density = 1.0f; BodyDef bd2 = new BodyDef(); bd2.Position.Set(0.0f, 10.0f); _body2 = _world.CreateBody(bd2); _shape2 = _body2.CreateShape(sd2); _body2.SetMassFromShapes(); _world.Gravity = new Vec2(0.0f, 0.0f); }
public Projectile(Player creator, float x, float y, float width, float height) : base(creator, 0, 0) { /* Create New Projectile Body */ BodyDef def = new BodyDef(); def.IsBullet = true; def.Position = creator.body.GetPosition() + new Vec2(x, y); projectile = creator.body.GetWorld().CreateBody(def); /* Create a fixture for the projectile */ PolygonDef fixdef = new PolygonDef(); fixdef.Density = 1.0f; fixdef.SetAsBox(width / 2, height / 2); fixdef.Filter.GroupIndex = creator.ID; fixture = projectile.CreateFixture(fixdef); fixture.Filter.CategoryBits = 0x0004; fixture.Filter.MaskBits = 0xFFFF; /* Made a 2nd fixture, one to observe all collisions */ fixdef.IsSensor = true; fix2 = projectile.CreateFixture(fixdef); fix2.UserData = this; /* Finally, give this projectile some mass */ projectile.SetMassFromShapes(); /* Also, make sure we destroy the projectile when it is time */ this.OnDestroy += Cleanup; }
public PolyCollision() { _localPoints[0].state = ContactState.ContactRemoved; _localPoints[1].state = ContactState.ContactRemoved; { PolygonDef sd = new PolygonDef(); sd.Vertices[0].Set(-9.0f, -1.1f); sd.Vertices[1].Set(7.0f, -1.1f); sd.Vertices[2].Set(5.0f, -0.9f); sd.Vertices[3].Set(-11.0f, -0.9f); sd.VertexCount = 4; sd.Density = 0.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body1 = _world.CreateBody(bd); _body1.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 0.5f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); _body2 = _world.CreateBody(bd); _body2.CreateShape(sd); _body2.SetMassFromShapes(); } _world.Gravity = Vec2.Zero; }
public TimeOfImpact() { { PolygonDef sd = new PolygonDef(); sd.Density = 0.0f; sd.SetAsBox(0.1f, 10.0f, new Vec2(10.0f, 0.0f), 0.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); bd.Angle = 0.0f; _body1 = _world.CreateBody(bd); _shape1 = _body1.CreateShape(sd); } { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.25f, 0.25f); sd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.Position.Set(9.6363468f, 28.050615f); bd.Angle = 1.6408679f; _body2 = _world.CreateBody(bd); _shape2 = (PolygonShape)_body2.CreateShape(sd); _body2.SetMassFromShapes(); } }
public RaycastTest() { Body ground = null; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); ground = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); ground.CreateShape(sd); } { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 1.0f); laserBody = _world.CreateBody(bd); PolygonDef sd = new PolygonDef(); sd.SetAsBox(5.0f, 1.0f); sd.Density = 4.0f; laserBody.CreateShape(sd); laserBody.SetMassFromShapes(); Body body; //Create a few shapes bd.Position.Set(-5.0f, 10.0f); body = _world.CreateBody(bd); CircleDef cd = new CircleDef(); cd.Radius = 3; body.CreateShape(cd); bd.Position.Set(5.0f, 10.0f); body = _world.CreateBody(bd); body.CreateShape(cd); } }
public override void Keyboard(System.Windows.Forms.Keys key) { switch (key) { case System.Windows.Forms.Keys.B: if (_bullet != null) { _world.DestroyBody(_bullet); _bullet = null; } { CircleDef sd = new CircleDef(); sd.Density = 20.0f; sd.Radius = 0.25f; sd.Restitution = 0.05f; BodyDef bd = new BodyDef(); bd.IsBullet = true; bd.Position.Set(-31.0f, 5.0f); _bullet = _world.CreateBody(bd); _bullet.CreateFixture(sd); _bullet.SetMassFromShapes(); _bullet.SetLinearVelocity(new Vec2(400.0f, 0.0f)); } break; } }
protected override void LoadContent() { poligonDef = new PolygonDef(); poligonDef.VertexCount = CollisionBody.Length; poligonDef.Vertices = CollisionBody; poligonDef.Density = 2000f; poligonDef.Friction = 100f; BodyDef bodyDef = new BodyDef(); bodyDef.Position = new Vec2(Position.X, Position.Y); Body = Game1.world.CreateBody(bodyDef); PolygonShape shape = (PolygonShape)Body.CreateShape(poligonDef); Body.SetMassFromShapes(); base.LoadContent(); }
// Main... public ElasticBody() { // Bottom static body { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 2.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(-1.0f, -7.5f); _ground = _world.CreateBody(bd); _ground.CreateShape(sd); } // Upper static body { PolygonDef sd = new PolygonDef(); sd.SetAsBox(20.0f, 0.50f, new Vec2(0.0f, 0.0f), 0.047f * Box2DX.Common.Settings.Pi); sd.Friction = 0.01f; sd.Restitution = 0.001f; BodyDef bd = new BodyDef(); bd.Position.Set(-20.0f, 93.0f); Body g = _world.CreateBody(bd); g.CreateShape(sd); sd.SetAsBox(15.0f, 0.50f, new Vec2(-15.0f, 12.5f), 0.0f); g.CreateShape(sd); sd.SetAsBox(20.0f, 0.5f, new Vec2(0.0f, -25.0f), -0.5f); g.CreateShape(sd); } // Left channel left wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.7f, 55.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(-49.3f, 50.0f); Body g = _world.CreateBody(bd); g.CreateShape(sd); } // Right wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.7f, 55.0f); sd.Friction = 0.1f; sd.Restitution = 0.1f; BodyDef bd = new BodyDef(); bd.Position.Set(45.0f, 50.0f); Body g = _world.CreateBody(bd); g.CreateShape(sd); } // Left channel right upper wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.5f, 20.0f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-42.0f, 70.0f); bd.Angle = -0.03f * Box2DX.Common.Settings.Pi; Body g = _world.CreateBody(bd); g.CreateShape(sd); } // Left channel right lower wall { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.50f, 23.0f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-44.0f, 27.0f); Body g = _world.CreateBody(bd); g.CreateShape(sd); // Bottom motors CircleDef cd = new CircleDef(); cd.Radius = 3.0f; cd.Density = 15.0f; cd.Friction = 1.0f; cd.Restitution = 0.2f; // 1. bd.Position.Set(-40.0f, 2.5f); Body body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); RevoluteJointDef jr = new RevoluteJointDef(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); jr.MaxMotorTorque = 30000.0f; jr.EnableMotor = true; jr.MotorSpeed = 20.0f; _world.CreateJoint(jr); // 1. left down bd.Position.Set(-46.0f, -2.5f); cd.Radius = 1.5f; jr.MotorSpeed = -20.0f; body = _world.CreateBody(bd); body.CreateShape(cd); sd.SetAsBox(2.0f, 0.50f); body.CreateShape(sd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter()); _world.CreateJoint(jr); // 2. cd.Radius = 3.0f; jr.MotorSpeed = 20.0f; bd.Position.Set(-32.0f, 2.5f); body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 3. jr.MotorSpeed = 20.0f; bd.Position.Set(-24.0f, 1.5f); body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 4. bd.Position.Set(-16.0f, 0.8f); body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 5. bd.Position.Set(-8.0f, 0.5f); body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 6. bd.Position.Set(0.0f, 0.1f); body = _world.CreateBody(bd); body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 7. bd.Position.Set(8.0f, -0.5f); body = _world.CreateBody(bd); body.CreateShape(cd); sd.SetAsBox(3.7f, 0.5f); body.CreateShape(sd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter() + new Vec2(0.0f, 1.0f)); _world.CreateJoint(jr); // 8. right rotator sd.SetAsBox(5.0f, 0.5f); sd.Density = 2.0f; bd.Position.Set(18.0f, 1.0f); Body rightmotor = _world.CreateBody(bd); rightmotor.CreateShape(sd); sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi / 3.0f); rightmotor.CreateShape(sd); sd.SetAsBox(4.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi * 2.0f / 3.0f); rightmotor.CreateShape(sd); cd.Radius = 4.2f; rightmotor.CreateShape(cd); rightmotor.SetMassFromShapes(); jr.Initialize(g, rightmotor, rightmotor.GetWorldCenter()); jr.MaxMotorTorque = 70000.0f; jr.MotorSpeed = -4.0f; _world.CreateJoint(jr); // 9. left rotator sd.SetAsBox(8.5f, 0.5f); sd.Density = 2.0f; bd.Position.Set(-34.0f, 17.0f); body = _world.CreateBody(bd); body.CreateShape(sd); sd.SetAsBox(8.5f, 0.5f, new Vec2(0.0f, 0.0f), Box2DX.Common.Settings.Pi * .5f); body.CreateShape(sd); cd.Radius = 7.0f; cd.Friction = 0.9f; body.CreateShape(cd); body.SetMassFromShapes(); jr.Initialize(g, body, body.GetWorldCenter()); jr.MaxMotorTorque = 100000.0f; jr.MotorSpeed = -5.0f; _world.CreateJoint(jr); // big compressor sd.SetAsBox(3.0f, 4.0f); sd.Density = 10.0f; bd.Position.Set(-16.0f, 17.0f); Body hammerleft = _world.CreateBody(bd); hammerleft.CreateShape(sd); hammerleft.SetMassFromShapes(); DistanceJointDef jd = new DistanceJointDef(); jd.Initialize(body, hammerleft, body.GetWorldCenter() + new Vec2(0.0f, 6.0f), hammerleft.GetWorldCenter()); _world.CreateJoint(jd); bd.Position.Set(4.0f, 17.0f); Body hammerright = _world.CreateBody(bd); hammerright.CreateShape(sd); hammerright.SetMassFromShapes(); jd.Initialize(body, hammerright, body.GetWorldCenter() - new Vec2(0.0f, 6.0f), hammerright.GetWorldCenter()); _world.CreateJoint(jd); // pusher sd.SetAsBox(6.0f, 0.75f); bd.Position.Set(-21.0f, 9.0f); Body pusher = _world.CreateBody(bd); pusher.CreateShape(sd); sd.SetAsBox(2.0f, 1.5f, new Vec2(-5.0f, 0.0f), 0.0f); pusher.SetMassFromShapes(); pusher.CreateShape(sd); jd.Initialize(rightmotor, pusher, rightmotor.GetWorldCenter() + new Vec2(-8.0f, 0.0f), pusher.GetWorldCenter() + new Vec2(5.0f, 0.0f)); _world.CreateJoint(jd); } // Static bodies above motors { PolygonDef sd = new PolygonDef(); CircleDef cd = new CircleDef(); sd.SetAsBox(9.0f, 0.5f); sd.Friction = 0.05f; sd.Restitution = 0.01f; BodyDef bd = new BodyDef(); bd.Position.Set(-15.5f, 12.0f); bd.Angle = 0.0f; Body g = _world.CreateBody(bd); g.CreateShape(sd); sd.SetAsBox(8.0f, 0.5f, new Vec2(23.0f, 0.0f), 0.0f); g.CreateShape(sd); // compressor statics sd.SetAsBox(7.0f, 0.5f, new Vec2(-2.0f, 9.0f), 0.0f); g.CreateShape(sd); sd.SetAsBox(9.0f, 0.5f, new Vec2(22.0f, 9.0f), 0.0f); g.CreateShape(sd); sd.SetAsBox(19.0f, 0.5f, new Vec2(-9.0f, 15.0f), -0.05f); g.CreateShape(sd); sd.SetAsBox(4.7f, 0.5f, new Vec2(15.0f, 11.5f), -0.5f); g.CreateShape(sd); // below compressor sd.SetAsBox(26.0f, 0.3f, new Vec2(17.0f, -4.4f), -0.02f); g.CreateShape(sd); cd.Radius = 1.0f; cd.Friction = 1.0f; cd.LocalPosition = new Vec2(29.0f, -6.0f); g.CreateShape(cd); cd.Radius = 0.7f; cd.LocalPosition = new Vec2(-2.0f, -4.5f); g.CreateShape(cd); } // Elevator { BodyDef bd = new BodyDef(); CircleDef cd = new CircleDef(); PolygonDef sd = new PolygonDef(); bd.Position.Set(40.0f, 4.0f); _elev = _world.CreateBody(bd); sd.SetAsBox(0.5f, 2.5f, new Vec2(3.0f, -3.0f), 0.0f); sd.Density = 1.0f; sd.Friction = 0.01f; _elev.CreateShape(sd); sd.SetAsBox(7.0f, 0.5f, new Vec2(-3.5f, -5.5f), 0.0f); _elev.CreateShape(sd); sd.SetAsBox(0.5f, 2.5f, new Vec2(-11.0f, -3.5f), 0.0f); _elev.CreateShape(sd); _elev.SetMassFromShapes(); PrismaticJointDef jp = new PrismaticJointDef(); jp.Initialize(_ground, _elev, bd.Position, new Vec2(0.0f, 1.0f)); jp.LowerTranslation = 0.0f; jp.UpperTranslation = 100.0f; jp.EnableLimit = true; jp.EnableMotor = true; jp.MaxMotorForce = 10000.0f; jp.MotorSpeed = 0.0f; _joint_elev = (PrismaticJoint)_world.CreateJoint(jp); // Korb sd.SetAsBox(2.3f, 0.5f, new Vec2(1.0f, 0.0f), 0.0f); sd.Density = 0.5f; bd.Position.Set(29.0f, 6.5f); Body body = _world.CreateBody(bd); body.CreateShape(sd); sd.SetAsBox(2.5f, 0.5f, new Vec2(3.0f, -2.0f), Box2DX.Common.Settings.Pi / 2.0f); body.CreateShape(sd); sd.SetAsBox(4.6f, 0.5f, new Vec2(7.8f, -4.0f), 0.0f); body.CreateShape(sd); sd.SetAsBox(0.5f, 4.5f, new Vec2(12.0f, 0.0f), 0.0f); body.CreateShape(sd); sd.SetAsBox(0.5f, 0.5f, new Vec2(13.0f, 4.0f), 0.0f); body.CreateShape(sd); cd.Radius = 0.7f; cd.Density = 1.0f; cd.Friction = 0.01f; cd.LocalPosition = new Vec2(0.0f, 0.0f); body.CreateShape(cd); body.SetMassFromShapes(); RevoluteJointDef jr = new RevoluteJointDef(); jr.Initialize(_elev, body, bd.Position); jr.EnableLimit = true; jr.LowerAngle = -0.2f; jr.UpperAngle = Box2DX.Common.Settings.Pi * 1.1f; jr.CollideConnected = true; _world.CreateJoint(jr); // upper body exit sd.SetAsBox(14.0f, 0.5f, new Vec2(-3.5f, -10.0f), 0.0f); bd.Position.Set(17.5f, 96.0f); body = _world.CreateBody(bd); body.CreateShape(sd); } // "Elastic body" 64 bodies - something like a lin. elastic compound // connected via dynamic forces (springs) { PolygonDef sd = new PolygonDef(); sd.SetAsBox(0.55f, 0.55f); sd.Density = 1.5f; sd.Friction = 0.01f; sd.Filter.GroupIndex = -1; Vec2 startpoint = new Vec2(30.0f, 20.0f); BodyDef bd = new BodyDef(); bd.IsBullet = false; bd.AllowSleep = false; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { bd.Position.Set(j * 1.02f, 2.51f + 1.02f * i); bd.Position += startpoint; Body body = _world.CreateBody(bd); bodies[8 * i + j] = body; body.CreateShape(sd); body.SetMassFromShapes(); } } } }
public TheoJansen() { _offset.Set(0.0f, 8.0f); _motorSpeed = 2.0f; _motorOn = true; Vec2 pivot = new Vec2(0.0f, 0.8f); { PolygonDef sd = new PolygonDef(); sd.SetAsBox(50.0f, 10.0f); BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, -10.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(sd); sd.SetAsBox(0.5f, 5.0f, new Vec2(-50.0f, 15.0f), 0.0f); ground.CreateShape(sd); sd.SetAsBox(0.5f, 5.0f, new Vec2(50.0f, 15.0f), 0.0f); ground.CreateShape(sd); } for (int i = 0; i < 40; ++i) { CircleDef sd = new CircleDef(); sd.Density = 1.0f; sd.Radius = 0.25f; BodyDef bd = new BodyDef(); bd.Position.Set(-40.0f + 2.0f * i, 0.5f); Body body = _world.CreateBody(bd); body.CreateShape(sd); body.SetMassFromShapes(); } { PolygonDef sd = new PolygonDef(); sd.Density = 1.0f; sd.SetAsBox(2.5f, 1.0f); sd.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.Position = pivot + _offset; _chassis = _world.CreateBody(bd); _chassis.CreateShape(sd); _chassis.SetMassFromShapes(); } { CircleDef sd = new CircleDef(); sd.Density = 1.0f; sd.Radius = 1.6f; sd.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.Position = pivot + _offset; _wheel = _world.CreateBody(bd); _wheel.CreateShape(sd); _wheel.SetMassFromShapes(); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(_wheel, _chassis, pivot + _offset); jd.CollideConnected = false; jd.MotorSpeed = _motorSpeed; jd.MaxMotorTorque = 400.0f; jd.EnableMotor = _motorOn; _motorJoint = (RevoluteJoint)_world.CreateJoint(jd); } Vec2 wheelAnchor; wheelAnchor = pivot + new Vec2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetXForm(_wheel.GetPosition(), 120.0f * Box2DX.Common.Settings.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetXForm(_wheel.GetPosition(), -120.0f * Box2DX.Common.Settings.Pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
public Car() { { // car body PolygonDef poly1 = new PolygonDef(), poly2 = new PolygonDef(); // bottom half poly1.VertexCount = 5; poly1.Vertices[4].Set(-2.2f, -0.74f); poly1.Vertices[3].Set(-2.2f, 0); poly1.Vertices[2].Set(1.0f, 0); poly1.Vertices[1].Set(2.2f, -0.2f); poly1.Vertices[0].Set(2.2f, -0.74f); poly1.Filter.GroupIndex = -1; poly1.Density = 20.0f; poly1.Friction = 0.68f; poly1.Filter.GroupIndex = -1; // top half poly2.VertexCount = 4; poly2.Vertices[3].Set(-1.7f, 0); poly2.Vertices[2].Set(-1.3f, 0.7f); poly2.Vertices[1].Set(0.5f, 0.74f); poly2.Vertices[0].Set(1.0f, 0); poly2.Filter.GroupIndex = -1; poly2.Density = 5.0f; poly2.Friction = 0.68f; poly2.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.Position.Set(-35.0f, 2.8f); _vehicle = _world.CreateBody(bd); _vehicle.CreateShape(poly1); _vehicle.CreateShape(poly2); _vehicle.SetMassFromShapes(); } { // vehicle wheels CircleDef circ = new CircleDef(); circ.Density = 40.0f; circ.Radius = 0.38608f; circ.Friction = 0.8f; circ.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.AllowSleep = false; bd.Position.Set(-33.8f, 2.0f); _rightWheel = _world.CreateBody(bd); _rightWheel.CreateShape(circ); _rightWheel.SetMassFromShapes(); bd.Position.Set(-36.2f, 2.0f); _leftWheel = _world.CreateBody(bd); _leftWheel.CreateShape(circ); _leftWheel.SetMassFromShapes(); } { // join wheels to chassis Vec2 anchor = new Vec2(); RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(_vehicle, _leftWheel, _leftWheel.GetWorldCenter()); jd.CollideConnected = false; jd.EnableMotor = true; jd.MaxMotorTorque = 10.0f; jd.MotorSpeed = 0.0f; _leftJoint = (RevoluteJoint)_world.CreateJoint(jd); jd.Initialize(_vehicle, _rightWheel, _rightWheel.GetWorldCenter()); jd.CollideConnected = false; _rightJoint = (RevoluteJoint)_world.CreateJoint(jd); } { // ground PolygonDef box = new PolygonDef(); box.SetAsBox(19.5f, 0.5f); box.Friction = 0.62f; BodyDef bd = new BodyDef(); bd.Position.Set(-25.0f, 1.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } { // more ground PolygonDef box = new PolygonDef(); BodyDef bd = new BodyDef(); box.SetAsBox(9.5f, 0.5f, Vec2.Zero, 0.1f * Box2DX.Common.Settings.Pi); box.Friction = 0.62f; bd.Position.Set(27.0f - 30.0f, 3.1f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } { // more ground PolygonDef box = new PolygonDef(); BodyDef bd = new BodyDef(); box.SetAsBox(9.5f, 0.5f, Vec2.Zero, -0.1f * Box2DX.Common.Settings.Pi); box.Friction = 0.62f; bd.Position.Set(55.0f - 30.0f, 3.1f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } { // more ground PolygonDef box = new PolygonDef(); BodyDef bd = new BodyDef(); box.SetAsBox(9.5f, 0.5f, Vec2.Zero, 0.03f * Box2DX.Common.Settings.Pi); box.Friction = 0.62f; bd.Position.Set(41.0f, 2.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } { // more ground PolygonDef box = new PolygonDef(); BodyDef bd = new BodyDef(); box.SetAsBox(5.0f, 0.5f, Vec2.Zero, 0.15f * Box2DX.Common.Settings.Pi); box.Friction = 0.62f; bd.Position.Set(50.0f, 4.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } { // more ground PolygonDef box = new PolygonDef(); BodyDef bd = new BodyDef(); box.SetAsBox(20.0f, 0.5f); box.Friction = 0.62f; bd.Position.Set(85.0f, 2.0f); Body ground = _world.CreateBody(bd); ground.CreateShape(box); } }
public virtual void InitPhysics() { if (_physBody != null) { Log.Instance.Log("WARNING: Call to InitPhysics had no effect. Actor has already had physics initialized."); return; } ShapeType shapeType = ShapeType.Box; Box2D.World physicsWorld = Angel.World.Instance.PhysicsWorld; ShapeDef shape; switch (shapeType) { case ShapeType.Box: PolygonDef box = new PolygonDef(); box.SetAsBox(0.5f * Size.X, 0.5f * Size.Y); shape = box; break; case ShapeType.Circle: CircleDef circle = new CircleDef(); circle.Radius = 0.5f * Size.X; shape = circle; break; default: Log.Instance.Log("InitPhysics(): Invalid shape type given"); return; } shape.Density = _fDensity; shape.Friction = _fFriction; shape.Restitution = _fRestitution; shape.IsSensor = _bIsSensor; if (_iCollisionFlags != -1) { //shape->maskBits = (short)collisionFlags; //shape->categoryBits = (short)collisionFlags; } // InitShape( shape ); BodyDef bodyDef = new BodyDef(); bodyDef.UserData = this; bodyDef.Position = this.Position; bodyDef.Angle = MathHelper.ToRadians(-Rotation); bodyDef.FixedRotation = _bFixedRotation; _physBody = physicsWorld.CreateBody(bodyDef); _physBody.CreateShape(shape); _physBody.SetMassFromShapes(); CustomInitPhysics(); }
public void LaunchBomb() { if (_bomb != null) { _world.DestroyBody(_bomb); _bomb = null; } BodyDef bd = new BodyDef(); bd.AllowSleep = true; bd.Position.Set(Box2DX.Common.Math.Random(-15.0f, 15.0f), 30.0f); bd.IsBullet = true; _bomb = _world.CreateBody(bd); _bomb.SetLinearVelocity(-5.0f * bd.Position); CircleDef sd = new CircleDef(); sd.Radius = 0.3f; sd.Density = 20.0f; sd.Restitution = 0.1f; _bomb.CreateShape(sd); _bomb.SetMassFromShapes(); }
public Biped(World w, Vec2 position) { m_world = w; BipedDef def = new BipedDef(); BodyDef bd; // create body parts bd = def.LFootDef; bd.Position += position; LFoot = w.CreateBody(bd); LFoot.CreateShape(def.LFootPoly); LFoot.SetMassFromShapes(); bd = def.RFootDef; bd.Position += position; RFoot = w.CreateBody(bd); RFoot.CreateShape(def.RFootPoly); RFoot.SetMassFromShapes(); bd = def.LCalfDef; bd.Position += position; LCalf = w.CreateBody(bd); LCalf.CreateShape(def.LCalfPoly); LCalf.SetMassFromShapes(); bd = def.RCalfDef; bd.Position += position; RCalf = w.CreateBody(bd); RCalf.CreateShape(def.RCalfPoly); RCalf.SetMassFromShapes(); bd = def.LThighDef; bd.Position += position; LThigh = w.CreateBody(bd); LThigh.CreateShape(def.LThighPoly); LThigh.SetMassFromShapes(); bd = def.RThighDef; bd.Position += position; RThigh = w.CreateBody(bd); RThigh.CreateShape(def.RThighPoly); RThigh.SetMassFromShapes(); bd = def.PelvisDef; bd.Position += position; Pelvis = w.CreateBody(bd); Pelvis.CreateShape(def.PelvisPoly); Pelvis.SetMassFromShapes(); bd = def.PelvisDef; bd.Position += position; Stomach = w.CreateBody(bd); Stomach.CreateShape(def.StomachPoly); Stomach.SetMassFromShapes(); bd = def.ChestDef; bd.Position += position; Chest = w.CreateBody(bd); Chest.CreateShape(def.ChestPoly); Chest.SetMassFromShapes(); bd = def.NeckDef; bd.Position += position; Neck = w.CreateBody(bd); Neck.CreateShape(def.NeckPoly); Neck.SetMassFromShapes(); bd = def.HeadDef; bd.Position += position; Head = w.CreateBody(bd); Head.CreateShape(def.HeadCirc); Head.SetMassFromShapes(); bd = def.LUpperArmDef; bd.Position += position; LUpperArm = w.CreateBody(bd); LUpperArm.CreateShape(def.LUpperArmPoly); LUpperArm.SetMassFromShapes(); bd = def.RUpperArmDef; bd.Position += position; RUpperArm = w.CreateBody(bd); RUpperArm.CreateShape(def.RUpperArmPoly); RUpperArm.SetMassFromShapes(); bd = def.LForearmDef; bd.Position += position; LForearm = w.CreateBody(bd); LForearm.CreateShape(def.LForearmPoly); LForearm.SetMassFromShapes(); bd = def.RForearmDef; bd.Position += position; RForearm = w.CreateBody(bd); RForearm.CreateShape(def.RForearmPoly); RForearm.SetMassFromShapes(); bd = def.LHandDef; bd.Position += position; LHand = w.CreateBody(bd); LHand.CreateShape(def.LHandPoly); LHand.SetMassFromShapes(); bd = def.RHandDef; bd.Position += position; RHand = w.CreateBody(bd); RHand.CreateShape(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 void AddToWorld() { body = world.CreateBody(bodyDef); foreach (ShapeDef shape in shapes) body.CreateShape(shape); body.SetMassFromShapes(); body.SetUserData(this); foreach (Controller controller in controllers) { controller.AddBody(body); world.AddController(controller); } foreach (PhysicsObject child in children) child.AddToWorld(); }
/// <summary> /// Add objects to the Box2d world. /// </summary> protected override void PopulateWorld() { // ==== Define the ground body ==== BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(_trackLengthHalf, -1f); // Call the body factory which creates the ground box shape. // The body is also added to the world. Body groundBody = _world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); // The extents are the half-widths of the box. groundShapeDef.SetAsBox(_trackLengthHalf + 1f, 1f); groundShapeDef.Friction = _simParams._defaultFriction; groundShapeDef.Restitution = _simParams._defaultRestitution; groundShapeDef.Filter.CategoryBits = 0x3; // Add the ground shape to the ground body. groundBody.CreateShape(groundShapeDef); // ==== Define walker torso. BodyDef torsoBodyDef = new BodyDef(); torsoBodyDef.Position.Set(0f, 1.45f); torsoBodyDef.IsBullet = true; // Create walker torso. _torsoBody = _world.CreateBody(torsoBodyDef); PolygonDef torsoShapeDef = new PolygonDef(); torsoShapeDef.SetAsBox(0.10f, 0.45f); torsoShapeDef.Friction = _simParams._defaultFriction; torsoShapeDef.Restitution = 0f; torsoShapeDef.Density = 2f; torsoShapeDef.Filter.CategoryBits = 0x2; _torsoBody.CreateShape(torsoShapeDef); _torsoBody.SetMassFromShapes(); // ===== Create legs. // Leg joint definition. RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.CollideConnected = false; jointDef.EnableMotor = true; jointDef.MaxMotorTorque = 0f; // Other re-usable stuff . const float legRadius = 0.05f; // Half the thickness of the leg Vec2 upperLegPosBase = new Vec2(0f, 1.05f); Vec2 lowerLegPosBase = new Vec2(0f, 0.55f); // ===== Create left leg. // Upper leg. Body upperLeftLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to torso (hip joint) jointDef.Initialize(_torsoBody, upperLeftLegBody, upperLegPosBase); _leftHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // Lower leg. _leftLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to upper leg (knee joint) jointDef.Initialize(upperLeftLegBody, _leftLowerLegBody, lowerLegPosBase); _leftKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // ===== Create right leg. // Upper leg. Body upperRightLegBody = CreatePole(upperLegPosBase, 0.5f, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to torso (hip joint) jointDef.Initialize(_torsoBody, upperRightLegBody, upperLegPosBase); _rightHipJoint = (RevoluteJoint)_world.CreateJoint(jointDef); // Lower leg. _rightLowerLegBody = CreatePole(lowerLegPosBase, __lowerLegLength, (float)SysMath.PI, legRadius, 2f, 0x1); // Join to upper leg (knee joint) jointDef.Initialize(upperRightLegBody, _rightLowerLegBody, lowerLegPosBase); _rightKneeJoint = (RevoluteJoint)_world.CreateJoint(jointDef); }
public PlayerCharacter(World _world, Vector2 _position) : base(_world, _position) { playerIndex = playerCount++; color = PlayerCharacter.Colors[playerIndex]; accelerate = 0; rotate = 0; isDead = false; this.angVelocity = 0; //build the unicycle CircleDef circleDef = new CircleDef(); circleDef.Radius = 1; circleDef.Density = 1f; circleDef.Friction = 1.0f; circleDef.Restitution = 0.0f; circleDef.LocalPosition.Set(0, 0); wheel = body.CreateShape(circleDef); body.SetMassFromShapes(); body.SetUserData(this); // link body and this to register collisions in this //build the head and connect with the wheel BodyDef bodydef = new BodyDef(); bodydef.Position = _position + new Vector2(0.0f, 3.5f); bodydef.Angle = 0f; chest = _world.CreateBody(bodydef); //add the head circleDef.Density = 0.0001f; circleDef.Radius = 0.75f; circleDef.LocalPosition.Set(0, 3); head = chest.CreateShape(circleDef); rotationHead = _position; PolygonDef Boxdef = new PolygonDef(); Boxdef.SetAsBox(1, 1.5f); Boxdef.Density = 0.25f; Boxdef.Friction = 0.4f; Box2DX.Collision.Shape s2 = chest.CreateShape(Boxdef); chest.SetMassFromShapes(); chest.SetUserData(this); //Jointshit RevoluteJointDef jointDefKW = new RevoluteJointDef(); jointDefKW.Body2 = chest; jointDefKW.Body1 = body; jointDefKW.CollideConnected = false; jointDefKW.LocalAnchor2 = new Vector2(-0.0f, -3.8f); jointDefKW.LocalAnchor1 = new Vector2(0, 0); jointDefKW.EnableLimit = false; _world.CreateJoint(jointDefKW); // add visuals Texture wheelTexture = AssetManager.getTexture(AssetManager.TextureName.ShoopWheel); wheelSprite = new AnimatedSprite(wheelTexture, 1.0f, 1, (Vector2)wheelTexture.Size); wheelSprite.Scale = Constants.windowScaleFactor * new Vector2(0.2f, 0.2f); //(Vector2.One / (Vector2)wheelTexture.Size * 2F * circleDef.Radius).toScreenCoord() - Vector2.Zero.toScreenCoord();//new Vector2(0.08f, 0.08f); wheelSprite.Origin = ((Vector2)wheelSprite.spriteSize) / 2F; sheepSprite = new Sprite(AssetManager.getTexture(AssetManager.TextureName.ShoopInfronUnicycle)); sheepSprite.Origin = ((Vector2)sheepSprite.Texture.Size) / 2F; sheepSprite.Scale = Constants.windowScaleFactor * new Vector2(_position.X > Constants.worldSizeX / 2F ? -0.2f : 0.2f, 0.2f); }
/// <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); }
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(); } }