public BulletTest() { { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 0.0f); Body body = m_world.CreateBody(bd); EdgeShape edge = new EdgeShape(); edge.Set(new Vec2(-10.0f, 0.0f), new Vec2(10.0f, 0.0f)); edge.Density = 0; body.CreateFixture(edge); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.2f, 1.0f, new Vec2(0.5f, 1.0f), 0.0f); shape.Density = 0; body.CreateFixture(shape); } { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 4.0f); PolygonShape box = new PolygonShape(); box.SetAsBox(2.0f, 0.1f); box.Density = 100; m_body = m_world.CreateBody(bd); m_body.CreateFixture(box); box.SetAsBox(0.25f, 0.25f); //m_x = RandomFloat(-1.0f, 1.0f); m_x = 0.20352793f; bd.Position.Set(m_x, 10.0f); bd.bullet = true; m_bullet = m_world.CreateBody(bd); m_bullet.CreateFixture(box); m_bullet.SetLinearVelocity(new Vec2(0.0f, -50.0f)); } }
public ApplyForce() { m_world.SetGravity(new Vec2(0.0f, 0.0f)); const float k_restitution = 0.4f; Body ground; { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 20.0f); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.Density = 0.0f; sd.restitution = k_restitution; // Left vertical shape.Set(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f)); ground.CreateFixture(sd); // Right vertical shape.Set(new Vec2(20.0f, -20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Top horizontal shape.Set(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f)); ground.CreateFixture(sd); // Bottom horizontal shape.Set(new Vec2(-20.0f, -20.0f), new Vec2(20.0f, -20.0f)); ground.CreateFixture(sd); } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * (float)Math.PI); xf1.p = xf1.q.GetXAxis(); Vec2[] vertices = new Vec2[3]; vertices[0] = Utilities.Mul(xf1, new Vec2(-1.0f, 0.0f)); vertices[1] = Utilities.Mul(xf1, new Vec2(1.0f, 0.0f)); vertices[2] = Utilities.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 = 4.0f; Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * (float)Math.PI); xf2.p = -xf2.q.GetXAxis(); vertices[0] = Utilities.Mul(xf2, new Vec2(-1.0f, 0.0f)); vertices[1] = Utilities.Mul(xf2, new Vec2(1.0f, 0.0f)); vertices[2] = Utilities.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.type = BodyType._dynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.Position.Set(0.0f, 2.0f); bd.angle = (float)Math.PI; bd.allowSleep = false; m_body = m_world.CreateBody(bd); m_body.CreateFixture(sd1); m_body.CreateFixture(sd2); } { 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 < 10; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 5.0f + 1.54f * i); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); float gravity = 10.0f; float I = body.GetInertia(); float mass = body.GetMass(); // For a circle: I = 0.5 * m * r * r ==> r = sqrt(2 * I / m) float radius = (float)Math.Sqrt(2.0f * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.SetZero(); jd.localAnchorB.SetZero(); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; m_world.CreateJoint(jd); } } }
static void Main(string[] args) { // Define the size of the world. Simulation will still work // if bodies reach the end of the world, but it will be slower. AABB worldAABB = new AABB { UpperBound = new Vector2(100, 100), LowerBound = new Vector2(-100, -100) }; // Define the gravity vector. Vector2 gravity = new Vector2(0.0f, -10.0f); // Do we want to let bodies sleep? bool doSleep = true; // Construct a world object, which will hold and simulate the rigid bodies. World world = new World(worldAABB, gravity, doSleep); // Define the ground body. BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position = new Vector2(0.0f, -10.0f); // 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(50.0f, 10.0f); // Add the ground shape to the ground body. groundBody.CreateFixture(groundShapeDef); // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.Position = new Vector2(0.0f, 4.0f); Body body = world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonDef shapeDef = new PolygonDef(); shapeDef.SetAsBox(1.0f, 1.0f); // Set the box density to be non-zero, so it will be dynamic. shapeDef.Density = 1.0f; // Override the default friction. shapeDef.Friction = 0.3f; // Add the shape to the body. body.CreateFixture(shapeDef); // Now tell the dynamic body to compute it's mass properties base // on its shape. body.SetMassFromShapes(); // Prepare for simulation. Typically we use a time step of 1/60 of a // second (60Hz) and 10 iterations. This provides a high quality simulation // in most game scenarios. float timeStep = 1.0f / 60.0f; int velocityIterations = 8; int positionIterations = 1; // This is our little game loop. for (int i = 0; i < 100; ++i) { // Instruct the world to perform a single step of simulation. It is // generally best to keep the time step and iterations fixed. world.Step(timeStep, velocityIterations, positionIterations); // Now print the position and angle of the body. Vector2 position = body.GetPosition(); float angle = body.GetAngle(); Console.WriteLine("Step: {3} - X: {0}, Y: {1}, Angle: {2}", new object[] { position.X.ToString(), position.Y.ToString(), angle.ToString(), i.ToString() }); } // When the world destructor is called, all bodies and joints are freed. This can // create orphaned pointers, so be careful about your world management. Console.ReadLine(); }