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 Breakable() { // Ground body { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0); } // Breakable dynamic body { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 40.0f); bd.Angle = 0.25f * Box2DX.Common.Settings.PI; _body1 = _world.CreateBody(bd); _shape1.SetAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f); _piece1 = _body1.CreateFixture(_shape1, 1.0f); _shape2.SetAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f); _piece2 = _body1.CreateFixture(_shape2, 1.0f); } _break = false; _broke = false; }
public TheoJansen() { _offset.Set(0.0f, 8.0f); _motorSpeed = 2.0f; _motorOn = true; Vec2 pivot = new Vec2(0.0f, 0.8f); // Ground { BodyDef bd = new BodyDef(); Body ground = _world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f)); ground.CreateFixture(shape, 0); shape.SetAsEdge(new Vec2(-50.0f, 0.0f), new Vec2(-50.0f, 10.0f)); ground.CreateFixture(shape, 0); shape.SetAsEdge(new Vec2(50.0f, 0.0f), new Vec2(50.0f, 10.0f)); ground.CreateFixture(shape, 0); } // Balls for (int i = 0; i < 40; ++i) { CircleShape shape = new CircleShape(); shape._radius = 0.25f; BodyDef bd = new BodyDef(); bd.Position.Set(-40.0f + 2.0f * i, 0.5f); Body body = _world.CreateBody(bd); body.CreateFixture(shape, 1.0f); } // Chassis { PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.5f, 1.0f); FixtureDef sd = new FixtureDef(); sd.Density = 1.0f; sd.Shape = shape; sd.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.Position = pivot + _offset; _chassis = _world.CreateBody(bd); _chassis.CreateFixture(sd); } { CircleShape shape = new CircleShape(); shape._radius = 1.6f; FixtureDef sd = new FixtureDef(); sd.Density = 1.0f; sd.Shape = shape; sd.Filter.GroupIndex = -1; BodyDef bd = new BodyDef(); bd.Position = pivot + _offset; _wheel = _world.CreateBody(bd); _wheel.CreateFixture(sd); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(_wheel, _chassis, pivot + _offset); jd.CollideConnected = false; jd.MotorSpeed = _motorSpeed; jd.MaxMotorTorque = 400.0f; jd.EnableMotor = _motorOn; _motorJoint = (RevoluteJoint)_world.CreateJoint(jd); } Vec2 wheelAnchor; wheelAnchor = pivot + new Vec2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.GetPosition(), 120.0f * Box2DX.Common.Settings.PI / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); _wheel.SetTransform(_wheel.GetPosition(), -120.0f * Box2DX.Common.Settings.PI / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
private void AddBoundaryBlock(Body groundBody, float x, float y, float width, float height) { // Define the ground box shape. PolygonDef groundShapeDef = new PolygonDef(); groundShapeDef.SetAsBox(width/2, height/2, new Vec2(x, y), 0); // Add the ground shape to the ground body. Fixture fix = groundBody.CreateFixture(groundShapeDef); fix.UserData = this; fix.Filter.CategoryBits = 0x0001; }
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; } }
private void InitPhysics(BattleArena arena) { // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.Position.Set(0.0f, 4.0f); body = arena.world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox(Pokemon.Width / 2, Pokemon.Height / 2); // Set the box density to be non-zero, so it will be dynamic. shapeDef.Density = 1.0f; // Override the default friction. //shapeDef.Friction = 2f; // Add the shape to the body. Fixture fix = body.CreateFixture(shapeDef); fix.Density = 1; fix.Friction = 0.3f; fix.Restitution = 0.1f; fix.UserData = this; fix.Filter.GroupIndex = this.ID; fix.Filter.CategoryBits = 0x0002; if (Pokemon.Types.Contains(PokemonType.Ghost)){ fix.Filter.MaskBits = 0xFFFF - 0x0002; } else { fix.Filter.MaskBits = 0xFFFF; } // Now tell the dynamic body to compute it's mass properties base // on its shape. var mass = body.GetMassData(); mass.Mass = Pokemon.Weight; body.SetMass(mass); body.SetFixedRotation(true); }
public void LaunchBomb(Vec2 position, Vec2 velocity) { if (_bomb != null) { _world.DestroyBody(_bomb); _bomb = null; } BodyDef bd = new BodyDef(); bd.AllowSleep = true; bd.Position = position; bd.IsBullet = 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.1f; Vec2 minV = position - new Vec2(0.3f, 0.3f); Vec2 maxV = position + new Vec2(0.3f, 0.3f); AABB aabb = new AABB(); aabb.LowerBound = minV; aabb.UpperBound = maxV; _bomb.CreateFixture(fd); }
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); PolygonShape shape = new PolygonShape(); FixtureDef sd = new FixtureDef(); sd.Shape = shape; sd.Density = 0.0f; sd.Restitution = k_restitution; // Left vertical shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.SetAsEdge(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.SetAsEdge(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.SetAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Transform xf1 = new Transform(); xf1.R.Set(0.3524f * Box2DX.Common.Settings.PI); xf1.Position = Math.Mul(xf1.R, new Vec2(1.0f, 0.0f)); Vec2[] vertices = new Vec2[3]; vertices[0] = Math.Mul(xf1, new Vec2(-1.0f, 0.0f)); vertices[1] = Math.Mul(xf1, new Vec2(1.0f, 0.0f)); vertices[2] = Math.Mul(xf1, new Vec2(0.0f, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.Set(vertices, 3); FixtureDef sd1 = new FixtureDef(); sd1.Shape = poly1; sd1.Density = 2.0f; Transform xf2 = new Transform(); xf2.R.Set(-0.3524f * Box2DX.Common.Settings.PI); xf2.Position = Math.Mul(xf2.R, new Vec2(-1.0f, 0.0f)); vertices[0] = Math.Mul(xf2, new Vec2(-1.0f, 0.0f)); vertices[1] = Math.Mul(xf2, new Vec2(1.0f, 0.0f)); vertices[2] = Math.Mul(xf2, new Vec2(0.0f, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.Set(vertices, 3); FixtureDef sd2 = new FixtureDef(); sd2.Shape = poly2; sd2.Density = 2.0f; BodyDef bd = new BodyDef(); bd.AngularDamping = 2.0f; bd.LinearDamping = 0.1f; bd.Position.Set(0.0f, 2.0f); bd.Angle = Box2DX.Common.Settings.PI; _body = _world.CreateBody(bd); _body.CreateFixture(sd1); _body.CreateFixture(sd2); } }