public override void Keyboard() { if (KeyboardManager.IsPressed(Key.C)) { if (m_fixture2 == null) { CircleShape shape = new CircleShape(); shape.m_radius = 3.0f; shape.m_p.Set(0.5f, -4.0f); shape.Density = 10; m_fixture2 = m_body.CreateFixture(shape); m_body.SetAwake(true); } } if (KeyboardManager.IsPressed(Key.D)){ if (m_fixture2 != null) { m_body.DestroyFixture(m_fixture2); m_fixture2 = null; m_body.SetAwake(true); } } if (KeyboardManager.IsPressed(Key.S)){ if (m_fixture2 != null) { m_sensor = !m_sensor; m_fixture2.IsSensor = m_sensor; } } }
/// Compute the collision manifold between two circles. public static void CollideCircles(out Manifold manifold, CircleShape circleA, Transform xfA, CircleShape circleB, Transform xfB) { throw new NotImplementedException(); //manifold.pointCount = 0; //Vec2 pA = Utilities.Mul(xfA, circleA.m_p); //Vec2 pB = Utilities.Mul(xfB, circleB.m_p); //Vec2 d = pB - pA; //float distSqr = Utilities.Dot(d, d); //float rA = circleA.m_radius, rB = circleB.m_radius; //float radius = rA + rB; //if (distSqr > radius * radius) //{ // return; //} //manifold.type = Manifold::e_circles; //manifold.localPoint = circleA.m_p; //manifold.localNormal.SetZero(); //manifold.pointCount = 1; //manifold.points[0].localPoint = circleB.m_p; //manifold.points[0].id.key = 0; }
public override void Keyboard() { if (KeyboardManager.IsPressed(Key.Comma)) { if (m_bullet != null) { m_world.DestroyBody(m_bullet); m_bullet = null; } { CircleShape shape = new CircleShape(); shape.m_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._dynamicBody; bd.bullet = true; bd.Position.Set(-31.0f, 5.0f); m_bullet = m_world.CreateBody(bd); m_bullet.CreateFixture(fd); m_bullet.SetLinearVelocity(new Vec2(400.0f, 0.0f)); } } }
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 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); } } }
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)); }
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; } }
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 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 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 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); } }
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 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); } }
/// Compute the collision manifold between a polygon and a circle. public static void CollidePolygonAndCircle(out Manifold manifold, PolygonShape polygonA, Transform xfA, CircleShape circleB, Transform xfB) { manifold = new Manifold(); manifold.points.Clear(); // Compute circle position in the frame of the polygon. Vec2 c = Utilities.Mul(xfB, circleB.m_p); Vec2 cLocal = Utilities.MulT(xfA, c); // Find the min separating edge. int normalIndex = 0; float separation = -Single.MaxValue; float radius = polygonA.m_radius + circleB.m_radius; int vertexCount = polygonA.m_count; List<Vec2> vertices = new List<Vec2>(polygonA.m_vertices); List<Vec2> normals = new List<Vec2>(polygonA.m_normals); for (int i = 0; i < vertexCount; ++i) { float s = Utilities.Dot(normals[i], cLocal - vertices[i]); if (s > radius) { // Early out. return; } if (s > separation) { separation = s; normalIndex = i; } } // Vertices that subtend the incident face. int vertIndex1 = normalIndex; int vertIndex2 = vertIndex1 + 1 < vertexCount ? vertIndex1 + 1 : 0; Vec2 v1 = vertices[vertIndex1]; Vec2 v2 = vertices[vertIndex2]; // If the center is inside the polygon ... if (separation < Single.Epsilon) { manifold.points.Clear(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_faceA; manifold.localNormal = normals[normalIndex]; manifold.localPoint = 0.5f * (v1 + v2); manifold.points[0].localPoint = circleB.m_p; manifold.points[0].id.key = 0; return; } // Compute barycentric coordinates float u1 = Utilities.Dot(cLocal - v1, v2 - v1); float u2 = Utilities.Dot(cLocal - v2, v1 - v2); if (u1 <= 0.0f) { if (Utilities.DistanceSquared(cLocal, v1) > radius * radius) { return; } manifold.points.Clear(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_faceA; manifold.localNormal = cLocal - v1; manifold.localNormal.Normalize(); manifold.localPoint = v1; manifold.points[0].localPoint = circleB.m_p; manifold.points[0].id.key = 0; } else if (u2 <= 0.0f) { if (Utilities.DistanceSquared(cLocal, v2) > radius * radius) { return; } manifold.points = new List<ManifoldPoint>(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_faceA; manifold.localNormal = cLocal - v2; manifold.localNormal.Normalize(); manifold.localPoint = v2; manifold.points[0].localPoint = circleB.m_p; manifold.points[0].id.key = 0; } else { Vec2 faceCenter = 0.5f * (v1 + v2); float separation2 = Utilities.Dot(cLocal - faceCenter, normals[vertIndex1]); if (separation2 > radius) { return; } manifold.points = new List<ManifoldPoint>(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_faceA; manifold.localNormal = normals[vertIndex1]; manifold.localPoint = faceCenter; manifold.points[0].localPoint = circleB.m_p; manifold.points[0].id.key = 0; } }
/// Compute the collision manifold between an edge and a circle. public static void CollideEdgeAndCircle(out Manifold manifold, EdgeShape edgeA, Transform xfA, CircleShape circleB, Transform xfB){ manifold = new Manifold(); // Compute circle in frame of edge Vec2 Q = Utilities.MulT(xfA, Utilities.Mul(xfB, circleB.m_p)); Vec2 A = edgeA.m_vertex1, B = edgeA.m_vertex2; Vec2 e = B - A; // Barycentric coordinates float u = Utilities.Dot(e, B - Q); float v = Utilities.Dot(e, Q - A); float radius = edgeA.m_radius + circleB.m_radius; ContactFeature cf; cf.indexB = 0; cf.typeB = ContactFeature.FeatureType.e_vertex; // Region A if (v <= 0.0f) { Vec2 P = A; Vec2 d = Q - P; float dd = Utilities.Dot(d, d); if (dd > radius * radius) { return; } // Is there an edge connected to A? if (edgeA.m_hasVertex0) { Vec2 A1 = edgeA.m_vertex0; Vec2 B1 = A; Vec2 e1 = B1 - A1; float u1 = Utilities.Dot(e1, B1 - Q); // Is the circle in Region AB of the previous edge? if (u1 > 0.0f) { return; } } cf.indexA = 0; cf.typeA = ContactFeature.FeatureType.e_vertex; manifold.points.Clear(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_circles; manifold.localNormal.SetZero(); manifold.localPoint = P; manifold.points[0].id.key = 0; manifold.points[0].id.cf = cf; manifold.points[0].localPoint = circleB.m_p; return; } // Region B if (u <= 0.0f) { Vec2 P = B; Vec2 d = Q - P; float dd = Utilities.Dot(d, d); if (dd > radius * radius) { return; } // Is there an edge connected to B? if (edgeA.m_hasVertex3) { Vec2 B2 = edgeA.m_vertex3; Vec2 A2 = B; Vec2 e2 = B2 - A2; float v2 = Utilities.Dot(e2, Q - A2); // Is the circle in Region AB of the next edge? if (v2 > 0.0f) { return; } } cf.indexA = 1; cf.typeA = ContactFeature.FeatureType.e_vertex; manifold.points.Clear(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_circles; manifold.localNormal.SetZero(); manifold.localPoint = P; manifold.points[0].id.key = 0; manifold.points[0].id.cf = cf; manifold.points[0].localPoint = circleB.m_p; return; } // Region AB float den = Utilities.Dot(e, e); Utilities.Assert(den > 0.0f); Vec2 Pb = (1.0f / den) * (u * A + v * B); Vec2 db = Q - Pb; float ddb = Utilities.Dot(db, db); if (ddb > radius * radius) { return; } Vec2 n = new Vec2(-e.Y, e.X); if (Utilities.Dot(n, Q - A) < 0.0f) { n.Set(-n.X, -n.Y); } n.Normalize(); cf.indexA = 0; cf.typeA = ContactFeature.FeatureType.e_face; manifold.points.Clear(); manifold.points.Add(new ManifoldPoint()); manifold.type = Manifold.ManifoldType.e_faceA; manifold.localNormal = n; manifold.localPoint = A; manifold.points[0].id.key = 0; manifold.points[0].id.cf = cf; manifold.points[0].localPoint = circleB.m_p; }
public CollisionProcessing() { // Ground body { EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f)); FixtureDef sd = new FixtureDef(); sd.shape = shape;; BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); ground.CreateFixture(sd); } float xLo = -5.0f, xHi = 5.0f; float yLo = 2.0f, yHi = 35.0f; // Small triangle Vec2[] vertices = new Vec2[3]; vertices[0].Set(-1.0f, 0.0f); vertices[1].Set(1.0f, 0.0f); vertices[2].Set(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._dynamicBody; triangleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body1 = m_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.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body2 = m_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._dynamicBody; boxBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body3 = m_world.CreateBody(boxBodyDef); body3.CreateFixture(boxShapeDef); // Large box (recycle definitions) polygon.SetAsBox(2.0f, 1.0f); boxBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body4 = m_world.CreateBody(boxBodyDef); body4.CreateFixture(boxShapeDef); // Small circle CircleShape circle = new CircleShape(); circle.m_radius = 1.0f; FixtureDef circleShapeDef = new FixtureDef(); circleShapeDef.shape = circle; circleShapeDef.Density = 1.0f; BodyDef circleBodyDef = new BodyDef(); circleBodyDef.type = BodyType._dynamicBody; circleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body5 = m_world.CreateBody(circleBodyDef); body5.CreateFixture(circleShapeDef); // Large circle circle.m_radius *= 2.0f; circleBodyDef.Position.Set(RandomFloat(xLo, xHi), RandomFloat(yLo, yHi)); Body body6 = m_world.CreateBody(circleBodyDef); body6.CreateFixture(circleShapeDef); }
public TheoJansen() { m_offset.Set(0.0f, 8.0f); m_motorSpeed = 2.0f; m_motorOn = true; Vec2 pivot = new Vec2(0.0f, 0.8f); // Ground { BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Density = 0; shape.Set(new Vec2(-50.0f, 0.0f), new Vec2(50.0f, 0.0f)); ground.CreateFixture(shape); shape.Set(new Vec2(-50.0f, 0.0f), new Vec2(-50.0f, 10.0f)); ground.CreateFixture(shape); shape.Set(new Vec2(50.0f, 0.0f), new Vec2(50.0f, 10.0f)); ground.CreateFixture(shape); } // Balls for (int i = 0; i < 40; ++i) { CircleShape shape = new CircleShape(); shape.m_radius = 0.25f; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-40.0f + 2.0f * i, 0.5f); Body body = m_world.CreateBody(bd); body.CreateFixture(shape); } // 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._dynamicBody; bd.Position = pivot + m_offset; m_chassis = m_world.CreateBody(bd); m_chassis.CreateFixture(sd); } { CircleShape shape = new CircleShape(); shape.m_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._dynamicBody; bd.Position = pivot + m_offset; m_wheel = m_world.CreateBody(bd); m_wheel.CreateFixture(sd); } { RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(m_wheel, m_chassis, pivot + m_offset); jd.collideConnected = false; jd.motorSpeed = m_motorSpeed; jd.maxMotorTorque = 400.0f; jd.enableMotor = m_motorOn; m_motorJoint = (RevoluteJoint)m_world.CreateJoint(jd); } Vec2 wheelAnchor; wheelAnchor = pivot + new Vec2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); m_wheel.SetTransform(m_wheel.GetPosition(), 120.0f * (float)Math.PI / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); m_wheel.SetTransform(m_wheel.GetPosition(), -120.0f * (float)Math.PI / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }
public Dominos() { Body b1; { EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); shape.Density = 0; BodyDef bd = new BodyDef(); b1 = m_world.CreateBody(bd); b1.CreateFixture(shape); } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.25f); BodyDef bd = new BodyDef(); bd.Position.Set(-1.5f, 10.0f); Body ground = m_world.CreateBody(bd); shape.Density = 0; ground.CreateFixture(shape); } { 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._dynamicBody; bd.Position.Set(-6.0f + 1.0f * i, 11.25f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(7.0f, 0.25f, new Vec2(0, 0), 0.3f); shape.Density = 0; BodyDef bd = new BodyDef(); bd.Position.Set(1.0f, 6.0f); Body ground = m_world.CreateBody(bd); ground.CreateFixture(shape); } Body b2; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 1.5f); shape.Density = 0; BodyDef bd = new BodyDef(); bd.Position.Set(-7.0f, 4.0f); b2 = m_world.CreateBody(bd); b2.CreateFixture(shape); } Body b3; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(6.0f, 0.125f); shape.Density = 10; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-0.9f, 1.0f); bd.angle = -0.15f; b3 = m_world.CreateBody(bd); b3.CreateFixture(shape); } RevoluteJointDef jd = new RevoluteJointDef(); Vec2 anchor = new Vec2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.collideConnected = true; m_world.CreateJoint(jd); Body b4; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.25f, 0.25f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-10.0f, 15.0f); b4 = m_world.CreateBody(bd); shape.Density = 10; shape.Density = 10; b4.CreateFixture(shape); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); m_world.CreateJoint(jd); Body b5; { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(6.5f, 3.0f); b5 = m_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 Vec2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vec2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new Vec2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); m_world.CreateJoint(jd); Body b6; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(6.5f, 4.1f); b6 = m_world.CreateBody(bd); shape.Density = 30; b6.CreateFixture(shape); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); m_world.CreateJoint(jd); Body b7; { PolygonShape shape = new PolygonShape(); shape.SetAsBox(0.1f, 1.0f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(7.4f, 1.0f); b7 = m_world.CreateBody(bd); shape.Density = 10; b7.CreateFixture(shape); } DistanceJointDef djd = new DistanceJointDef(); djd.bodyA = b3; djd.bodyB = b7; djd.localAnchorA.Set(6.0f, 0.0f); djd.localAnchorB.Set(0.0f, -1.0f); Vec2 d = djd.bodyB.GetWorldPoint(djd.localAnchorB) - djd.bodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length(); m_world.CreateJoint(djd); { float radius = 0.2f; CircleShape shape = new CircleShape(); shape.m_radius = radius; for (int i = 0; i < 4; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(5.9f + 2.0f * radius * i, 2.4f); Body body = m_world.CreateBody(bd); shape.Density = 10; body.CreateFixture(shape); } } }
public CollisionFiltering() { // Ground body { EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f)); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.friction = 0.3f; BodyDef bd = new BodyDef(); Body ground = m_world.CreateBody(bd); ground.CreateFixture(sd); } // Small triangle Vec2[] vertices = new Vec2[3]; vertices[0].Set(-1.0f, 0.0f); vertices[1].Set(1.0f, 0.0f); vertices[2].Set(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._dynamicBody; triangleBodyDef.Position.Set(-5.0f, 2.0f); Body body1 = m_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.Set(-5.0f, 6.0f); triangleBodyDef.fixedRotation = true; // look at me! Body body2 = m_world.CreateBody(triangleBodyDef); body2.CreateFixture(triangleShapeDef); { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-5.0f, 10.0f); Body body = m_world.CreateBody(bd); PolygonShape p = new PolygonShape(); p.SetAsBox(0.5f, 1.0f); body.CreateFixture(p); PrismaticJointDef jd = new PrismaticJointDef(); jd.bodyA = body2; jd.bodyB = body; jd.enableLimit = true; jd.localAnchorA.Set(0.0f, 4.0f); jd.localAnchorB.SetZero(); jd.localAxisA.Set(0.0f, 1.0f); jd.lowerTranslation = -1.0f; jd.upperTranslation = 1.0f; m_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._dynamicBody; boxBodyDef.Position.Set(0.0f, 2.0f); Body body3 = m_world.CreateBody(boxBodyDef); body3.CreateFixture(boxShapeDef); // Large box (recycle definitions) polygon.SetAsBox(2.0f, 1.0f); boxShapeDef.Filter.GroupIndex = k_largeGroup; boxBodyDef.Position.Set(0.0f, 6.0f); Body body4 = m_world.CreateBody(boxBodyDef); body4.CreateFixture(boxShapeDef); // Small circle CircleShape circle = new CircleShape(); circle.m_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._dynamicBody; circleBodyDef.Position.Set(5.0f, 2.0f); Body body5 = m_world.CreateBody(circleBodyDef); body5.CreateFixture(circleShapeDef); // Large circle circle.m_radius *= 2.0f; circleShapeDef.Filter.GroupIndex = k_largeGroup; circleBodyDef.Position.Set(5.0f, 6.0f); Body body6 = m_world.CreateBody(circleBodyDef); body6.CreateFixture(circleShapeDef); }
public CompoundShapes() { { BodyDef bd = new BodyDef(); bd.Position.Set(0.0f, 0.0f); Body body = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(50.0f, 0.0f), new Vec2(-50.0f, 0.0f)); shape.Density = 0; body.CreateFixture(shape); } { CircleShape circle1 = new CircleShape(); circle1.m_radius = 0.5f; circle1.m_p.Set(-0.5f, 0.5f); circle1.Density = 2; CircleShape circle2 = new CircleShape(); circle2.m_radius = 0.5f; circle2.m_p.Set(0.5f, 0.5f); circle2.Density = 0; for (int i = 0; i < 10; ++i) { float x = RandomFloat(-0.1f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(x + 5.0f, 1.05f + 2.5f * i); bd.angle = RandomFloat(-(float)Math.PI, (float)Math.PI); Body body = m_world.CreateBody(bd); body.CreateFixture(circle1); body.CreateFixture(circle2); } } { PolygonShape polygon1 = new PolygonShape(); polygon1.SetAsBox(0.25f, 0.5f); polygon1.Density = 2; PolygonShape polygon2 = new PolygonShape(); polygon2.SetAsBox(0.25f, 0.5f, new Vec2(0.0f, -0.5f), 0.5f * (float)Math.PI); polygon2.Density = 2; for (int i = 0; i < 10; ++i) { float x = RandomFloat(-0.1f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(x - 5.0f, 1.05f + 2.5f * i); bd.angle = RandomFloat(-(float)Math.PI, (float)Math.PI); Body body = m_world.CreateBody(bd); body.CreateFixture(polygon1); body.CreateFixture(polygon2); } } { Transform xf1 = new Transform(); xf1.q.Set(0.3524f * (float)Math.PI); xf1.p = xf1.q.GetXAxis(); Vec2[] vertices = new Vec2[3]; PolygonShape triangle1 = new PolygonShape(); 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)); triangle1.Set(vertices, 3); Transform xf2 = new Transform(); xf2.q.Set(-0.3524f * (float)Math.PI); xf2.p = -xf2.q.GetXAxis(); PolygonShape triangle2 = new PolygonShape(); 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)); triangle2.Set(vertices, 3); for (int i = 0; i < 10; ++i) { float x = RandomFloat(-0.1f, 0.1f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(x, 2.05f + 2.5f * i); bd.angle = 0.0f; Body body = m_world.CreateBody(bd); triangle1.Density = 2; triangle2.Density = 2; body.CreateFixture(triangle1); body.CreateFixture(triangle2); } } { PolygonShape bottom = new PolygonShape(); bottom.SetAsBox( 1.5f, 0.15f ); PolygonShape left = new PolygonShape(); left.SetAsBox(0.15f, 2.7f, new Vec2(-1.45f, 2.35f), 0.2f); PolygonShape right = new PolygonShape(); right.SetAsBox(0.15f, 2.7f, new Vec2(1.45f, 2.35f), -0.2f); BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set( 0.0f, 2.0f ); Body body = m_world.CreateBody(bd); bottom.Density = 4; left.Density = 4; right.Density = 4; body.CreateFixture(bottom); body.CreateFixture(left); body.CreateFixture(right); } }
public Car() { m_hz = 4.0f; m_zeta = 0.7f; m_speed = 50.0f; Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 0.0f; fd.friction = 0.6f; shape.Set(new Vec2(-20.0f, 0.0f), new Vec2(20.0f, 0.0f)); ground.CreateFixture(fd); float[] hs = new float[]{0.25f, 1.0f, 4.0f, 0.0f, 0.0f, -1.0f, -2.0f, -2.0f, -1.25f, 0.0f}; float x = 20.0f, y1 = 0.0f, dx = 5.0f; for (int i = 0; i < 10; ++i) { float y2 = hs[i]; shape.Set(new Vec2(x, y1), new Vec2(x + dx, y2)); ground.CreateFixture(fd); y1 = y2; x += dx; } for (int i = 0; i < 10; ++i) { float y2 = hs[i]; shape.Set(new Vec2(x, y1), new Vec2(x + dx, y2)); ground.CreateFixture(fd); y1 = y2; x += dx; } shape.Set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 80.0f; shape.Set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 40.0f; shape.Set(new Vec2(x, 0.0f), new Vec2(x + 10.0f, 5.0f)); ground.CreateFixture(fd); x += 20.0f; shape.Set(new Vec2(x, 0.0f), new Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 40.0f; shape.Set(new Vec2(x, 0.0f), new Vec2(x, 20.0f)); ground.CreateFixture(fd); } // Teeter { BodyDef bd = new BodyDef(); bd.Position.Set(140.0f, 1.0f); bd.type = BodyType._dynamicBody; Body body = m_world.CreateBody(bd); PolygonShape box = new PolygonShape(); box.SetAsBox(10.0f, 0.25f); box.Density = 1; body.CreateFixture(box); RevoluteJointDef jd = new RevoluteJointDef(); jd.Initialize(ground, body, body.GetPosition()); jd.lowerAngle = -8.0f * (float)Math.PI / 180.0f; jd.upperAngle = 8.0f * (float)Math.PI / 180.0f; jd.enableLimit = true; m_world.CreateJoint(jd); body.ApplyAngularImpulse(100.0f, true); } // Bridge { int N = 20; PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 1.0f; fd.friction = 0.6f; RevoluteJointDef jd = new RevoluteJointDef(); Body prevBody = ground; for (int i = 0; i < N; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(161.0f + 2.0f * i, -0.125f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); Vec2 anchor = new Vec2(160.0f + 2.0f * i, -0.125f); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); prevBody = body; } Vec2 anchor2 = new Vec2(160.0f + 2.0f * N, -0.125f); jd.Initialize(prevBody, ground, anchor2); m_world.CreateJoint(jd); } // Boxes { PolygonShape box = new PolygonShape(); box.SetAsBox(0.5f, 0.5f); box.Density = 0.5f; Body body = null; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(230.0f, 0.5f); body = m_world.CreateBody(bd); body.CreateFixture(box); bd.Position.Set(230.0f, 1.5f); body = m_world.CreateBody(bd); body.CreateFixture(box); bd.Position.Set(230.0f, 2.5f); body = m_world.CreateBody(bd); body.CreateFixture(box); bd.Position.Set(230.0f, 3.5f); body = m_world.CreateBody(bd); body.CreateFixture(box); bd.Position.Set(230.0f, 4.5f); body = m_world.CreateBody(bd); body.CreateFixture(box); } // Car { PolygonShape chassis = new PolygonShape(); Vec2[] vertices = new Vec2[8]; vertices[0].Set(-1.5f, -0.5f); vertices[1].Set(1.5f, -0.5f); vertices[2].Set(1.5f, 0.0f); vertices[3].Set(0.0f, 0.9f); vertices[4].Set(-1.15f, 0.9f); vertices[5].Set(-1.5f, 0.2f); chassis.Set(vertices, 6); CircleShape circle = new CircleShape(); circle.m_radius = 0.4f; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 1.0f); m_car = m_world.CreateBody(bd); m_car.CreateFixture(chassis); FixtureDef fd = new FixtureDef(); fd.shape = circle; fd.Density = 1.0f; fd.friction = 0.9f; bd.Position.Set(-1.0f, 0.35f); m_wheel1 = m_world.CreateBody(bd); m_wheel1.CreateFixture(fd); bd.Position.Set(1.0f, 0.4f); m_wheel2 = m_world.CreateBody(bd); m_wheel2.CreateFixture(fd); WheelJointDef jd = new WheelJointDef(); Vec2 axis = new Vec2(0.0f, 1.0f); jd.Initialize(m_car, m_wheel1, m_wheel1.GetPosition(), axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 20.0f; jd.enableMotor = true; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring1 = (WheelJoint)m_world.CreateJoint(jd); jd.Initialize(m_car, m_wheel2, m_wheel2.GetPosition(), axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 10.0f; jd.enableMotor = false; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring2 = (WheelJoint)m_world.CreateJoint(jd); } }
Cantilever() { 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; WeldJointDef jd = new WeldJointDef(); 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); prevBody = body; } } { PolygonShape shape = new PolygonShape(); shape.SetAsBox(1.0f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.Density = 20.0f; WeldJointDef jd = new WeldJointDef(); jd.frequencyHz = 5.0f; jd.dampingRatio = 0.7f; Body prevBody = ground; for (int i = 0; i < 3; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(-14.0f + 2.0f * i, 15.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); Vec2 anchor = new Vec2(-15.0f + 2.0f * i, 15.0f); jd.Initialize(prevBody, body, anchor); m_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._dynamicBody; bd.Position.Set(-4.5f + 1.0f * i, 5.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { Vec2 anchor = new Vec2(-5.0f + 1.0f * i, 5.0f); jd.Initialize(prevBody, body, anchor); m_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(); jd.frequencyHz = 8.0f; jd.dampingRatio = 0.7f; Body prevBody = ground; for (int i = 0; i < e_count; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(5.5f + 1.0f * i, 10.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(fd); if (i > 0) { Vec2 anchor = new Vec2(5.0f + 1.0f * i, 10.0f); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); } prevBody = body; } } 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 < 2; ++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 Gears() { Body ground = null; { BodyDef bd = new BodyDef(); ground = m_world.CreateBody(bd); EdgeShape shape = new EdgeShape(); shape.Set(new Vec2(50.0f, 0.0f), new Vec2(-50.0f, 0.0f)); shape.Density = 0; ground.CreateFixture(shape); } { CircleShape circle1 = new CircleShape(); circle1.m_radius = 1.0f; circle1.Density = 5; PolygonShape box = new PolygonShape(); box.SetAsBox(0.5f, 5.0f); box.Density = 5; CircleShape circle2 = new CircleShape(); circle2.m_radius = 2.0f; circle2.Density = 5; BodyDef bd1 = new BodyDef(); bd1.type = BodyType._staticBody; bd1.Position.Set(10.0f, 9.0f); Body body1 = m_world.CreateBody(bd1); body1.CreateFixture(circle1); BodyDef bd2 = new BodyDef(); bd2.type = BodyType._dynamicBody; bd2.Position.Set(10.0f, 8.0f); Body body2 = m_world.CreateBody(bd2); body2.CreateFixture(box); BodyDef bd3 = new BodyDef(); bd3.type = BodyType._dynamicBody; bd3.Position.Set(10.0f, 6.0f); Body body3 = m_world.CreateBody(bd3); body3.CreateFixture(circle2); RevoluteJointDef jd1 = new RevoluteJointDef(); jd1.Initialize(body2, body1, bd1.Position); Joint joint1 = m_world.CreateJoint(jd1); RevoluteJointDef jd2 = new RevoluteJointDef(); jd2.Initialize(body2, body3, bd3.Position); Joint joint2 = m_world.CreateJoint(jd2); GearJointDef jd4 = new GearJointDef(); jd4.bodyA = body1; jd4.bodyB = body3; jd4.joint1 = joint1; jd4.joint2 = joint2; jd4.ratio = circle2.m_radius / circle1.m_radius; m_world.CreateJoint(jd4); } { CircleShape circle1 = new CircleShape(); circle1.m_radius = 1.0f; circle1.Density = 5; CircleShape circle2 = new CircleShape(); circle2.m_radius = 2.0f; circle2.Density = 5; PolygonShape box = new PolygonShape(); box.SetAsBox(0.5f, 5.0f); BodyDef bd1 = new BodyDef(); bd1.type = BodyType._dynamicBody; bd1.Position.Set(-3.0f, 12.0f); Body body1 = m_world.CreateBody(bd1); body1.CreateFixture(circle1); RevoluteJointDef jd1 = new RevoluteJointDef(); jd1.bodyA = ground; jd1.bodyB = body1; jd1.localAnchorA = ground.GetLocalPoint(bd1.Position); jd1.localAnchorB = body1.GetLocalPoint(bd1.Position); jd1.referenceAngle = body1.GetAngle() - ground.GetAngle(); m_joint1 = (RevoluteJoint)m_world.CreateJoint(jd1); BodyDef bd2 = new BodyDef(); bd2.type = BodyType._dynamicBody; bd2.Position.Set(0.0f, 12.0f); Body body2 = m_world.CreateBody(bd2); body2.CreateFixture(circle2); RevoluteJointDef jd2 = new RevoluteJointDef(); jd2.Initialize(ground, body2, bd2.Position); m_joint2 = (RevoluteJoint)m_world.CreateJoint(jd2); BodyDef bd3 = new BodyDef(); bd3.type = BodyType._dynamicBody; bd3.Position.Set(2.5f, 12.0f); Body body3 = m_world.CreateBody(bd3); box.Density = 5; body3.CreateFixture(box); PrismaticJointDef jd3 = new PrismaticJointDef(); jd3.Initialize(ground, body3, bd3.Position, new Vec2(0.0f, 1.0f)); jd3.lowerTranslation = -5.0f; jd3.upperTranslation = 5.0f; jd3.enableLimit = true; m_joint3 = (PrismaticJoint)m_world.CreateJoint(jd3); GearJointDef jd4 = new GearJointDef(); jd4.bodyA = body1; jd4.bodyB = body2; jd4.joint1 = m_joint1; jd4.joint2 = m_joint2; jd4.ratio = circle2.m_radius / circle1.m_radius; m_joint4 = (GearJoint)m_world.CreateJoint(jd4); GearJointDef jd5 = new GearJointDef(); jd5.bodyA = body2; jd5.bodyB = body3; jd5.joint1 = m_joint2; jd5.joint2 = m_joint3; jd5.ratio = -1.0f / circle2.m_radius; m_joint5 = (GearJoint)m_world.CreateJoint(jd5); } }
public ContinuousTest() { { 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); } #if true { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position.Set(0.0f, 20.0f); //bd.angle = 0.1f; PolygonShape shape = new PolygonShape(); shape.SetAsBox(2.0f, 0.1f); m_body = m_world.CreateBody(bd); m_body.CreateFixture(shape); m_angularVelocity = RandomFloat(-50.0f, 50.0f); //m_angularVelocity = 46.661274f; m_body.SetLinearVelocity(new Vec2(0.0f, -100.0f)); m_body.SetAngularVelocity(m_angularVelocity); } #else { BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.position.Set(0.0f, 2.0f); Body body = m_world.CreateBody(bd); CircleShape shape = new CircleShape(); shape.m_p.SetZero(); shape.m_radius = 0.5f; body.CreateFixture(shape); bd.bullet = true; bd.position.Set(0.0f, 10.0f); body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.SetLinearVelocity(new Vec2(0.0f, -100.0f)); } #endif Utilities._gjkCalls = 0; Utilities._gjkIters = 0; Utilities._gjkMaxIters = 0; Utilities._toiCalls = 0; Utilities._toiIters = 0; Utilities._toiRootIters = 0; Utilities._toiMaxRootIters = 0; Utilities._toiTime = 0.0f; Utilities._toiMaxTime = 0.0f; }
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; }
public Revolute() { 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)); FixtureDef fd = new FixtureDef(); fd.shape = shape; //fd.Filter.CategoryBits = 2; ground.CreateFixture(fd); } { CircleShape shape = new CircleShape(); shape.m_radius = 0.5f; shape.Density = 5; BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; RevoluteJointDef rjd = new RevoluteJointDef(); bd.Position.Set(-10.0f, 20.0f); Body body = m_world.CreateBody(bd); body.CreateFixture(shape); float w = 100.0f; body.SetAngularVelocity(w); body.SetLinearVelocity(new Vec2(-8.0f * w, 0.0f)); rjd.Initialize(ground, body, new Vec2(-10.0f, 12.0f)); rjd.motorSpeed = 1.0f * (float)Math.PI; rjd.maxMotorTorque = 10000.0f; rjd.enableMotor = false; rjd.lowerAngle = -0.25f * (float)Math.PI; rjd.upperAngle = 0.5f * (float)Math.PI; rjd.enableLimit = true; rjd.collideConnected = true; m_joint = (RevoluteJoint)m_world.CreateJoint(rjd); } { CircleShape circle_shape = new CircleShape(); circle_shape.m_radius = 3.0f; BodyDef circle_bd = new BodyDef(); circle_bd.type = BodyType._dynamicBody; circle_bd.Position.Set(5.0f, 30.0f); FixtureDef fd = new FixtureDef(); fd.Density = 5.0f; fd.Filter.MaskBits = 1; fd.shape = circle_shape; m_ball = m_world.CreateBody(circle_bd); m_ball.CreateFixture(fd); PolygonShape polygon_shape = new PolygonShape(); polygon_shape.SetAsBox(10.0f, 0.2f, new Vec2(-10.0f, 0.0f), 0.0f); polygon_shape.Density = 2; BodyDef polygon_bd = new BodyDef(); polygon_bd.Position.Set(20.0f, 10.0f); polygon_bd.type = BodyType._dynamicBody; polygon_bd.bullet = true; Body polygon_body = m_world.CreateBody(polygon_bd); polygon_body.CreateFixture(polygon_shape); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.Initialize(ground, polygon_body, new Vec2(20.0f, 10.0f)); rjd.lowerAngle = -0.25f * (float)Math.PI; rjd.upperAngle = 0.0f * (float)Math.PI; rjd.enableLimit = true; m_world.CreateJoint(rjd); } // Tests mass computation of a small object far from the origin { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType._dynamicBody; Body body = m_world.CreateBody(bodyDef); PolygonShape polyShape = new PolygonShape(); Vec2[] verts = new Vec2[3]; verts[0].Set( 17.63f, 36.31f ); verts[1].Set( 17.52f, 36.69f ); verts[2].Set( 17.19f, 36.36f ); polyShape.Set(verts, 3); FixtureDef polyFixtureDef = new FixtureDef(); polyFixtureDef.shape = polyShape; polyFixtureDef.Density = 1; body.CreateFixture(polyFixtureDef); //assertion hits inside here } }
public void LaunchBomb(Vec2 position, Vec2 velocity){ if (m_bomb != null) { m_world.DestroyBody(m_bomb); m_bomb = null; } BodyDef bd = new BodyDef(); bd.type = BodyType._dynamicBody; bd.Position = position; bd.bullet = true; m_bomb = m_world.CreateBody(bd); m_bomb.SetLinearVelocity(velocity); CircleShape circle = new CircleShape(); circle.m_radius = 0.3f; FixtureDef fd = new FixtureDef(); fd.shape = circle; fd.Density = 20.0f; fd.restitution = 0.0f; Vec2 minV = position - new Vec2(0.3f,0.3f); Vec2 maxV = position + new Vec2(0.3f, 0.3f); AABB aabb; aabb.lowerBound = minV; aabb.upperBound = maxV; m_bomb.CreateFixture(fd); }
private void DrawShape(Fixture fixture, Transform xf, Color color) { switch (fixture.GetShapeType()) { case ShapeType.Circle: { CircleShape circle = (CircleShape)fixture.GetShape(); Vec2 center = Utilities.Mul(xf, circle.m_p); float radius = circle.m_radius; Vec2 axis = Utilities.Mul(xf.q, new Vec2(1.0f, 0.0f)); m_debugDraw.DrawSolidCircle(center, radius, axis, color); } break; case ShapeType.Edge: { EdgeShape edge = (EdgeShape)fixture.GetShape(); Vec2 v1 = Utilities.Mul(xf, edge.m_vertex1); Vec2 v2 = Utilities.Mul(xf, edge.m_vertex2); m_debugDraw.DrawSegment(v1, v2, color); } break; case ShapeType.Chain: { ChainShape chain = (ChainShape)fixture.GetShape(); int count = chain.m_count; List <Vec2> vertices = chain.m_vertices; Vec2 v1 = Utilities.Mul(xf, vertices[0]); for (int i = 1; i < count; ++i) { Vec2 v2 = Utilities.Mul(xf, vertices[i]); m_debugDraw.DrawSegment(v1, v2, color); m_debugDraw.DrawCircle(v1, 0.05f, color); v1 = v2; } } break; case ShapeType.Polygon: { PolygonShape poly = (PolygonShape)fixture.GetShape(); int vertexCount = poly.m_count; Utilities.Assert(vertexCount <= Settings._maxPolygonVertices); Vec2[] vertices = new Vec2[Settings._maxPolygonVertices]; for (int i = 0; i < vertexCount; ++i) { vertices[i] = Utilities.Mul(xf, poly.m_vertices[i]); } m_debugDraw.DrawSolidPolygon(vertices, vertexCount, color); } break; default: break; } }