public ShapeEditing() { { BodyDef bd1 = new BodyDef(); Body ground = m_world.CreateBody(bd1); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 10.0f); m_body = m_world.CreateBody(bd); PolygonShape shape2 = new PolygonShape(); shape2.SetAsBox(4.0f, 4.0f, new Vec2(0.0f, 0.0f), 0.0f); shape2.Density = 10; m_fixture1 = m_body.CreateFixture(shape2); m_fixture2 = null; m_sensor = false; }
Breakable() { // Ground body { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } // Breakable dynamic body { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 40.0f); bd.angle = 0.25f * (float)Math.PI; m_body1 = m_world.CreateBody(bd); m_shape1.SetAsBox(0.5f, 0.5f, new Vec2(-0.5f, 0.0f), 0.0f); m_shape1.Density = 1; m_piece1 = m_body1.CreateFixture(m_shape1); m_shape2.SetAsBox(0.5f, 0.5f, new Vec2(0.5f, 0.0f), 0.0f); m_shape2.Density = 1; m_piece2 = m_body1.CreateFixture(m_shape2); } m_break = false; m_broke = false; }
void Break() { // Create two bodies from one. Body body1 = m_piece1.GetBody(); Vec2 center = body1.GetWorldCenter(); body1.DestroyFixture(m_piece2); m_piece2 = null; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position = body1.GetPosition(); bd.angle = body1.GetAngle(); Body body2 = m_world.CreateBody(bd); m_shape2.Density = 1; m_piece2 = body2.CreateFixture(m_shape2); // Compute consistent velocities for new bodies based on // cached velocity. Vec2 center1 = body1.GetWorldCenter(); Vec2 center2 = body2.GetWorldCenter(); Vec2 velocity1 = m_velocity + Utilities.Cross(m_angularVelocity, center1 - center); Vec2 velocity2 = m_velocity + Utilities.Cross(m_angularVelocity, center2 - center); body1.SetAngularVelocity(m_angularVelocity); body1.SetLinearVelocity(velocity1); body2.SetAngularVelocity(m_angularVelocity); body2.SetLinearVelocity(velocity2); }
public SphereStack() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { CircleShape shape = new CircleShape(); shape.m_radius = 1.0f; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 4.0f + 3.0f * i); m_bodies[i] = m_world.CreateBody(bd); m_bodies[i].CreateFixture(shape); m_bodies[i].SetLinearVelocity(new Vec2(0.0f, -50.0f)); } } }
public Test(){ Vec2 gravity = new Vec2(); gravity.Set(0.0f, -10.0f); m_world = new World(gravity); m_bomb = null; m_textLine = 30; m_mouseJoint = null; m_pointCount = 0; m_debugDraw = new DebugDraw(); m_destructionListener = new TestDestructionListener(); m_destructionListener.test = this; m_world.SetDestructionListener(m_destructionListener); m_world.SetContactListener(this); m_world.SetDebugDraw(m_debugDraw); m_bombSpawning = false; m_stepCount = 0; BodyDef bodyDef = new BodyDef(); m_groundBody = m_world.CreateBody(bodyDef); m_maxProfile = new Profile(); m_totalProfile = new Profile(); }
public SensorTest() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); { EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } #if ZERO { FixtureDef sd; sd.SetAsBox(10.0f, 2.0f, new Vec2(0.0f, 20.0f), 0.0f); sd.IsSensor = true; m_sensor = ground.CreateFixture(sd); } #else { CircleShape shape = new CircleShape(); shape.m_radius = 5.0f; shape.m_p.Set(0.0f, 10.0f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.IsSensor = true; m_sensor = ground.CreateFixture(fd); } #endif } { CircleShape shape = new CircleShape(); shape.m_radius = 1.0f; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-10.0f + 3.0f * i, 20.0f); bd.UserData = m_touching[i]; m_touching[i] = false; m_bodies[i] = m_world.CreateBody(bd); m_bodies[i].CreateFixture(shape); } } }
//e_columnCount = 1, //e_rowCount = 1 public VerticalStack() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); shape.Set(new Vec2(20.0f, 0.0f), new Vec2(20.0f, 20.0f)); shape.Density = 0; ground.CreateFixture(shape); } float[] xs = {0.0f, -10.0f, -5.0f, 5.0f, 10.0f}; for (int j = 0; j < 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 < e_rowCount; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; int n = j * e_rowCount + i; Utilities.Assert(n < e_rowCount * e_columnCount); m_indices[n] = n; bd.UserData = m_indices[n]; float x = 0.0f; //float x = RandomFloat(-0.02f, 0.02f); //float x = i % 2 == 0 ? -0.025f : 0.025f; bd.Position.Set(xs[j] + x, 0.752f + 1.54f * i); Body body = m_world.CreateBody(bd); m_bodies[n] = body; body.CreateFixture(fd); } } m_bullet = null; }
public Confined() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape2 = new EdgeShape(); // Floor shape2.Set(new Vec2(-10.0f, 0.0f), new Vec2(10.0f, 0.0f)); shape2.Density = 0; ground.CreateFixture(shape2); // Left wall shape2.Set(new Vec2(-10.0f, 0.0f), new Vec2(-10.0f, 20.0f)); ground.CreateFixture(shape2); // Right wall shape2.Set(new Vec2(10.0f, 0.0f), new Vec2(10.0f, 20.0f)); ground.CreateFixture(shape2); // Roof shape2.Set(new Vec2(-10.0f, 20.0f), new Vec2(10.0f, 20.0f)); ground.CreateFixture(shape2); } float radius = 0.5f; CircleShape shape = new CircleShape(); shape.m_p.SetZero(); shape.m_radius = radius; FixtureDef fd = new FixtureDef(); fd.shape = shape; 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._dynamicBody; bd.Position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } } m_world.SetGravity(new Vec2(0.0f, 0.0f)); }
/// Create a rigid body given a definition. No reference to the definition /// is retained. /// @warning This function is locked during callbacks. public Body CreateBody(BodyDef def){ Utilities.Assert(IsLocked() == false); if (IsLocked()) { return null; } Body b = new Body(def, this); // Add to world doubly linked list. m_bodyList.Add(b); return b; }
public GravityTest() { // Define the gravity vector. Vec2 gravity = new Vec2(0.0f, 10.0f); // Construct a world object, which will hold and simulate the rigid bodies. m_world.SetGravity(gravity); // Define the ground body. BodyDef groundBodyDef = new BodyDef(); groundBodyDef.Position.Set(0.0f, 20.0f); // Call the body factory which allocates memory for the ground body // from a pool and creates the ground box shape (also from a pool). // The body is also added to the world. Body groundBody = m_world.CreateBody(groundBodyDef); // Define the ground box shape. PolygonShape groundBox = new PolygonShape(); // The extents are the half-widths of the box. groundBox.SetAsBox(50.0f, 10.0f); groundBox.Density = 0; // Add the ground fixture to the ground body. groundBody.CreateFixture(groundBox); // Define the dynamic body. We set its position and call the body factory. BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType._dynamicBody; bodyDef.Position = new Vec2(0.0f, 4.0f); Body body = m_world.CreateBody(bodyDef); // Define another box shape for our dynamic body. PolygonShape dynamicBox = new PolygonShape(); dynamicBox.SetAsBox(1.0f, 1.0f); // Define the dynamic body fixture. FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = dynamicBox; // Set the box Density to be non-zero, so it will be dynamic. fixtureDef.Density = 1.0f; // Override the default friction. fixtureDef.friction = 0.3f; // Add the shape to the body. body.CreateFixture(fixtureDef); }
public OneSidedPlatform() { // Ground { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } // Platform { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 10.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(3.0f, 0.5f); shape.Density = 0; m_platform = body.CreateFixture(shape); m_bottom = 10.0f - 0.5f; m_top = 10.0f + 0.5f; } // Actor { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 12.0f); Body body = m_world.CreateBody(bd); m_radius = 0.5f; CircleShape shape = new CircleShape(); shape.m_radius = m_radius; shape.Density = 20; m_character = body.CreateFixture(shape); body.SetLinearVelocity(new Vec2(0.0f, -50.0f)); m_state = State.e_unknown; } }
public Prismatic() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.0f, 0.5f); shape.Density = 5; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-10.0f, 10.0f); bd.angle = 0.5f * (float)Math.PI; bd.allowSleep = false; Body body = m_world.CreateBody(bd); body.CreateFixture(shape); PrismaticJointDef pjd = new PrismaticJointDef(); // Bouncy limit Vec2 axis = new Vec2(2.0f, 1.0f); axis.Normalize(); pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), axis); // Non-bouncy limit //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(1.0f, 0.0f)); pjd.motorSpeed = 10.0f; pjd.maxMotorForce = 10000.0f; pjd.enableMotor = true; pjd.lowerTranslation = 0.0f; pjd.upperTranslation = 20.0f; pjd.enableLimit = true; m_joint = (PrismaticJoint)m_world.CreateJoint(pjd); } }
public override void Step(TestSettings settings) { base.Step(settings); if (m_count < e_count) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 10.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.125f, 0.125f); body.CreateFixture(shape); ++m_count; } }
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 Chain() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(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; const float y = 25.0f; Body prevBody = ground; for (int i = 0; i < 30; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.5f + i, y); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); Vec2 anchor = new Vec2((float)(i), y); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); prevBody = body; } } }
public Body AddNode(Body parent, Vec2 localAnchor, int depth, float offset, float a) { Vec2 h = new Vec2(0.0f, a); Vec2 p = parent.GetPosition() + localAnchor - h; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType._dynamicBody; bodyDef.Position = p; Body body = m_world.CreateBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f * a, a); shape.Density = 20; body.CreateFixture(shape); if (depth == e_depth) { return body; } shape.SetAsBox(offset, 0.25f * a, new Vec2(0, -a), 0.0f); body.CreateFixture(shape); Vec2 a1 = new Vec2(offset, -a); Vec2 a2 = new Vec2(-offset, -a); Body body1 = AddNode(body, a1, depth + 1, 0.5f * offset, a); Body body2 = AddNode(body, a2, depth + 1, 0.5f * offset, a); RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.bodyA = body; jointDef.localAnchorB = h; jointDef.localAnchorA = a1; jointDef.bodyB = body1; m_world.CreateJoint(jointDef); jointDef.localAnchorA = a2; jointDef.bodyB = body2; m_world.CreateJoint(jointDef); return body; }
public Pyramid() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { float a = 0.5f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(a, a); shape.Density = 5; Vec2 x = new Vec2(-7.0f, 0.75f); Vec2 y; Vec2 deltaX = new Vec2(0.5625f, 1.25f); Vec2 deltaY = new Vec2(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._dynamicBody; bd.Position = y; Body body = m_world.CreateBody(bd); body.CreateFixture(shape); y += deltaY; } x += deltaX; } } }
public MotorJointTest() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f)); FixtureDef fd = new FixtureDef(); fd.shape = shape; ground.CreateFixture(fd); } // Define motorized body { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 8.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.0f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.6f; fd.Density = 2.0f; body.CreateFixture(fd); MotorJointDef mjd = new MotorJointDef(); mjd.Initialize(ground, body); mjd.maxForce = 1000.0f; mjd.maxTorque = 1000.0f; m_joint = (MotorJoint)m_world.CreateJoint(mjd); } m_go = false; m_time = 0.0f; }
public ConveyorBelt() { // Ground { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } // Platform { BodyDef bd = new BodyDef(); bd.Position.Set(-5.0f, 5.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(10.0f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.8f; m_platform = body.CreateFixture(fd); } // Boxes for (int i = 0; i < 5; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-10.0f + 2.0f * i, 7.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); shape.Density = 20; body.CreateFixture(shape); } }
void CreateCircle() { float radius = 2.0f; CircleShape shape = new CircleShape(); shape.m_p.SetZero(); shape.m_radius = radius; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 1.0f; fd.friction = 0.0f; Vec2 p = new Vec2(RandomFloat(), 3.0f + RandomFloat()); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position = p; //bd.allowSleep = false; Body body = m_world.CreateBody(bd); body.CreateFixture(fd); }
public Tumbler() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); } { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.allowSleep = false; bd.Position.Set(0.0f, 10.0f); Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.Density = 5; shape.SetAsBox(0.5f, 10.0f, new Vec2( 10.0f, 0.0f), 0.0f); body.CreateFixture(shape); shape.SetAsBox(0.5f, 10.0f, new Vec2(-10.0f, 0.0f), 0.0f); body.CreateFixture(shape); shape.SetAsBox(10.0f, 0.5f, new Vec2(0.0f, 10.0f), 0.0f); body.CreateFixture(shape); shape.SetAsBox(10.0f, 0.5f, new Vec2(0.0f, -10.0f), 0.0f); body.CreateFixture(shape); RevoluteJointDef jd = new RevoluteJointDef(); jd.bodyA = ground; jd.bodyB = body; jd.localAnchorA.Set(0.0f, 10.0f); jd.localAnchorB.Set(0.0f, 0.0f); jd.referenceAngle = 0.0f; jd.motorSpeed = 0.05f * (float)Math.PI; jd.maxMotorTorque = 1e8f; jd.enableMotor = true; m_joint = (RevoluteJoint)m_world.CreateJoint(jd); } m_count = 0; }
public MobileBalanced() { Body ground; // Create ground body. { BodyDef bodyDef = new BodyDef(); bodyDef.Position.Set(0.0f, 20.0f); ground = m_world.CreateBody(bodyDef); } float a = 0.5f; Vec2 h = new Vec2(0.0f, a); Body root = AddNode(ground, new Vec2(0, 0), 0, 3.0f, a); RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.bodyA = ground; jointDef.bodyB = root; jointDef.localAnchorA.SetZero(); jointDef.localAnchorB = h; m_world.CreateJoint(jointDef); }
public AddPair() { m_world.SetGravity(new Vec2(0.0f,0.0f)); { CircleShape shape = new CircleShape(); shape.m_p.SetZero(); shape.m_radius = 0.1f; float minX = -6.0f; float maxX = 0.0f; float minY = 4.0f; float maxY = 6.0f; for (int i = 0; i < 400; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position = new Vec2(RandomFloat(minX,maxX),RandomFloat(minY,maxY)); Body body = m_world.CreateBody(bd); shape.Density = 0.01f; body.CreateFixture(shape); } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.5f, 1.5f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-40.0f,5.0f); bd.bullet = true; Body body = m_world.CreateBody(bd); body.CreateFixture(shape); body.SetLinearVelocity(new Vec2(150.0f, 0.0f)); } }
public VaryingRestitution() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { CircleShape shape = new CircleShape(); shape.m_radius = 1.0f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 1.0f; float[] restitution = {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._dynamicBody; bd.Position.Set(-10.0f + 3.0f * i, 20.0f); Body body = m_world.CreateBody(bd); fd.restitution = restitution[i]; body.CreateFixture(fd); } } }
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; }
internal Body(BodyDef bd, World world){ Utilities.Assert(bd.Position.IsValid()); Utilities.Assert(bd.linearVelocity.IsValid()); Utilities.Assert(Utilities.IsValid(bd.angle)); Utilities.Assert(Utilities.IsValid(bd.angularVelocity)); Utilities.Assert(Utilities.IsValid(bd.angularDamping) && bd.angularDamping >= 0.0f); Utilities.Assert(Utilities.IsValid(bd.linearDamping) && bd.linearDamping >= 0.0f); m_flags = 0; if (bd.bullet) { m_flags |= BodyFlags.e_bulletFlag; } if (bd.fixedRotation) { m_flags |= BodyFlags.e_fixedRotationFlag; } if (bd.allowSleep) { m_flags |= BodyFlags.e_autoSleepFlag; } if (bd.awake) { m_flags |= BodyFlags.e_awakeFlag; } if (bd.active) { m_flags |= BodyFlags.e_activeFlag; } m_world = world; m_xf.p = bd.Position; m_xf.q.Set(bd.angle); m_sweep.localCenter.SetZero(); m_sweep.c0 = m_xf.p; m_sweep.c = m_xf.p; m_sweep.a0 = bd.angle; m_sweep.a = bd.angle; m_sweep.alpha0 = 0.0f; m_jointList = new List<JointEdge>(); m_contactList = new List<ContactEdge>(); m_prev = null; m_next = null; m_linearVelocity = bd.linearVelocity; m_angularVelocity = bd.angularVelocity; m_linearDamping = bd.linearDamping; m_angularDamping = bd.angularDamping; m_gravityScale = bd.gravityScale; m_force.SetZero(); m_torque = 0.0f; m_sleepTime = 0.0f; m_type = bd.type; if (m_type == BodyType._dynamicBody) { m_mass = 1.0f; m_invMass = 1.0f; } else { m_mass = 0.0f; m_invMass = 0.0f; } m_I = 0.0f; m_invI = 0.0f; m_userData = bd.UserData; m_fixtureList = new List<Fixture>(); }
public CharacterCollision() { // Ground body { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } // Collinear edges with no adjacency information. // This shows the problematic case where a box shape can hit // an internal vertex. { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Density = 0; shape.Set(new Vec2(-8.0f, 1.0f), new Vec2(-6.0f, 1.0f)); ground.CreateFixture(shape); shape.Set(new Vec2(-6.0f, 1.0f), new Vec2(-4.0f, 1.0f)); ground.CreateFixture(shape); shape.Set(new Vec2(-4.0f, 1.0f), new Vec2(-2.0f, 1.0f)); ground.CreateFixture(shape); } // Chain shape { BodyDef bd = new BodyDef(); bd.angle = 0.25f * (float)Math.PI; Body ground = m_world.CreateBody(bd); Vec2[] vs = new Vec2[4]; vs[0].Set(5.0f, 7.0f); vs[1].Set(6.0f, 8.0f); vs[2].Set(7.0f, 8.0f); vs[3].Set(8.0f, 7.0f); ChainShape shape = new ChainShape(); shape.CreateChain(vs, 4); shape.Density = 0; ground.CreateFixture(shape); } // Square tiles. This shows that adjacency shapes may // have non-smooth collision. There is no solution // to this problem. { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.Density = 0; shape.SetAsBox(1.0f, 1.0f, new Vec2(4.0f, 3.0f), 0.0f); ground.CreateFixture(shape); shape.SetAsBox(1.0f, 1.0f, new Vec2(6.0f, 3.0f), 0.0f); ground.CreateFixture(shape); shape.SetAsBox(1.0f, 1.0f, new Vec2(8.0f, 3.0f), 0.0f); ground.CreateFixture(shape); } // Square made from an edge loop. Collision should be smooth. { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); Vec2[] vs = new Vec2[4]; vs[0].Set(-1.0f, 3.0f); vs[1].Set(1.0f, 3.0f); vs[2].Set(1.0f, 5.0f); vs[3].Set(-1.0f, 5.0f); ChainShape shape = new ChainShape(); shape.CreateLoop(vs, 4); shape.Density = 0; ground.CreateFixture(shape); } // Edge loop. Collision should be smooth. { BodyDef bd = new BodyDef(); bd.Position.Set(-10.0f, 4.0f); Body ground = m_world.CreateBody(bd); Vec2[] vs = new Vec2[10]; vs[0].Set(0.0f, 0.0f); vs[1].Set(6.0f, 0.0f); vs[2].Set(6.0f, 2.0f); vs[3].Set(4.0f, 1.0f); vs[4].Set(2.0f, 2.0f); vs[5].Set(0.0f, 2.0f); vs[6].Set(-2.0f, 2.0f); vs[7].Set(-4.0f, 3.0f); vs[8].Set(-6.0f, 2.0f); vs[9].Set(-6.0f, 0.0f); ChainShape shape = new ChainShape(); shape.CreateLoop(vs, 10); shape.Density = 0; ground.CreateFixture(shape); } // Square character 1 { BodyDef bd = new BodyDef(); bd.Position.Set(-3.0f, 8.0f); bd.type = BodyType._dynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; body.CreateFixture(fd); } // Square character 2 { BodyDef bd = new BodyDef(); bd.Position.Set(-5.0f, 5.0f); bd.type = BodyType._dynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; body.CreateFixture(fd); } // Hexagon character { BodyDef bd = new BodyDef(); bd.Position.Set(-5.0f, 8.0f); bd.type = BodyType._dynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = m_world.CreateBody(bd); float angle = 0.0f; float delta = (float)Math.PI / 3.0f; Vec2[] vertices = new Vec2[6]; for (int i = 0; i < 6; ++i) { vertices[i].Set(0.5f * (float)Math.Cos(angle), 0.5f * (float)Math.Sin(angle)); angle += delta; } PolygonShape shape = new PolygonShape(); shape.Set(vertices, 6); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; body.CreateFixture(fd); } // Circle character { BodyDef bd = new BodyDef(); bd.Position.Set(3.0f, 5.0f); bd.type = BodyType._dynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = m_world.CreateBody(bd); CircleShape shape = new CircleShape(); shape.m_radius = 0.5f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; body.CreateFixture(fd); } // Circle character { BodyDef bd = new BodyDef(); bd.Position.Set(-7.0f, 6.0f); bd.type = BodyType._dynamicBody; bd.allowSleep = false; m_character = m_world.CreateBody(bd); CircleShape shape = new CircleShape(); shape.m_radius = 0.25f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; fd.friction = 1.0f; m_character.CreateFixture(fd); } }
public EdgeTest() { { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); Vec2 v1 = new Vec2(-10.0f, 0.0f), v2 = new Vec2(-7.0f, -2.0f), v3 = new Vec2(-4.0f, 0.0f); Vec2 v4 = new Vec2(0.0f, 0.0f), v5 = new Vec2(4.0f, 0.0f), v6 = new Vec2(7.0f, 2.0f), v7 = new Vec2(10.0f, 0.0f); EdgeShape shape = new EdgeShape(); shape.Set(v1, v2); shape.m_hasVertex3 = true; shape.m_vertex3 = v3; shape.Density = 0; ground.CreateFixture(shape); shape.Set(v2, v3); shape.m_hasVertex0 = true; shape.m_hasVertex3 = true; shape.m_vertex0 = v1; shape.m_vertex3 = v4; ground.CreateFixture(shape); shape.Set(v3, v4); shape.m_hasVertex0 = true; shape.m_hasVertex3 = true; shape.m_vertex0 = v2; shape.m_vertex3 = v5; ground.CreateFixture(shape); shape.Set(v4, v5); shape.m_hasVertex0 = true; shape.m_hasVertex3 = true; shape.m_vertex0 = v3; shape.m_vertex3 = v6; ground.CreateFixture(shape); shape.Set(v5, v6); shape.m_hasVertex0 = true; shape.m_hasVertex3 = true; shape.m_vertex0 = v4; shape.m_vertex3 = v7; ground.CreateFixture(shape); shape.Set(v6, v7); shape.m_hasVertex0 = true; shape.m_vertex0 = v5; ground.CreateFixture(shape); } { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-0.5f, 0.6f); bd.allowSleep = false; Body body = m_world.CreateBody(bd); CircleShape shape = new CircleShape(); shape.m_radius = 0.5f; body.CreateFixture(shape); } { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(1.0f, 0.6f); bd.allowSleep = false; Body body = m_world.CreateBody(bd); PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.5f); body.CreateFixture(shape); } }
Bridge() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; fd.friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-14.5f + 1.0f * i, 5.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); Vec2 anchor = new Vec2(-15.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); if (i == (e_count >> 1)) { m_middle = body; } prevBody = body; } Vec2 anchor2 = new Vec2(-15.0f + 1.0f * e_count, 5.0f); jd.Initialize(prevBody, ground, anchor2); m_world.CreateJoint(jd); } for (int i = 0; i < 2; ++i) { Vec2[] vertices = new Vec2[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(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._dynamicBody; bd.Position.Set(-8.0f + 8.0f * i, 12.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } for (int i = 0; i < 3; ++i) { CircleShape shape = new CircleShape(); shape.m_radius = 0.5f; FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-6.0f + 6.0f * i, 10.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } }
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); } } }
internal Body(BodyDef bd, World world) { Utilities.Assert(bd.Position.IsValid()); Utilities.Assert(bd.linearVelocity.IsValid()); Utilities.Assert(Utilities.IsValid(bd.angle)); Utilities.Assert(Utilities.IsValid(bd.angularVelocity)); Utilities.Assert(Utilities.IsValid(bd.angularDamping) && bd.angularDamping >= 0.0f); Utilities.Assert(Utilities.IsValid(bd.linearDamping) && bd.linearDamping >= 0.0f); m_flags = 0; if (bd.bullet) { m_flags |= BodyFlags.e_bulletFlag; } if (bd.fixedRotation) { m_flags |= BodyFlags.e_fixedRotationFlag; } if (bd.allowSleep) { m_flags |= BodyFlags.e_autoSleepFlag; } if (bd.awake) { m_flags |= BodyFlags.e_awakeFlag; } if (bd.active) { m_flags |= BodyFlags.e_activeFlag; } m_world = world; m_xf.p = bd.Position; m_xf.q.Set(bd.angle); m_sweep.localCenter.SetZero(); m_sweep.c0 = m_xf.p; m_sweep.c = m_xf.p; m_sweep.a0 = bd.angle; m_sweep.a = bd.angle; m_sweep.alpha0 = 0.0f; m_jointList = new List <JointEdge>(); m_contactList = new List <ContactEdge>(); m_prev = null; m_next = null; m_linearVelocity = bd.linearVelocity; m_angularVelocity = bd.angularVelocity; m_linearDamping = bd.linearDamping; m_angularDamping = bd.angularDamping; m_gravityScale = bd.gravityScale; m_force.SetZero(); m_torque = 0.0f; m_sleepTime = 0.0f; m_type = bd.type; if (m_type == BodyType._dynamicBody) { m_mass = 1.0f; m_invMass = 1.0f; } else { m_mass = 0.0f; m_invMass = 0.0f; } m_I = 0.0f; m_invI = 0.0f; m_userData = bd.UserData; m_fixtureList = new List <Fixture>(); }