public VaryingRestitution() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 1.0f; b2FixtureDef fd = new b2FixtureDef(); 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) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f + 3.0f * i, 20.0f); b2Body body = m_world.CreateBody(bd); fd.restitution = restitution[i]; body.CreateFixture(fd); } } }
public SphereStack() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 1.0f; for (int i = 0; i < e_count; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 4.0f + 3.0f * i); m_bodies[i] = m_world.CreateBody(bd); m_bodies[i].CreateFixture(shape, 1.0f); m_bodies[i].LinearVelocity = new b2Vec2(0.0f, -50.0f); } } }
void CreateNinja() { spriteImageName = String.Format("{0}_standing", baseImageName); onGround = false; // Define the dynamic body. var bodyDef = new b2BodyDef(); bodyDef.type = b2BodyType.b2_staticBody; //or you could use b2DynamicBody to start the ninja as dynamic bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO); var shape = new b2CircleShape(); var radiusInMeters = (40 / Constants.PTM_RATIO) * 0.5f; //increase or decrease 40 for a different circle size definition shape.Radius = radiusInMeters; // Define the dynamic body fixture. var fixtureDef = new b2FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 1.0f; fixtureDef.friction = 1.0f; fixtureDef.restitution = 0.1f; CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName); }
public override void Keyboard(char key) { switch (key) { case 'c': if (m_fixture2 == null) { b2CircleShape shape = new b2CircleShape(); shape.Radius = 3.0f; shape.Position = new b2Vec2(0.5f, -4.0f); m_fixture2 = m_body.CreateFixture(shape, 10.0f); m_body.SetAwake(true); } break; case 'd': if (m_fixture2 != null) { m_body.DestroyFixture(m_fixture2); m_fixture2 = null; m_body.SetAwake(true); } break; case 's': if (m_fixture2 != null) { m_sensor = !m_sensor; m_fixture2.IsSensor = m_sensor; } break; } }
override public b2Shape Copy() { b2Shape s = new b2CircleShape(); s.Set(this); return(s); }
public override void Keyboard(char key) { switch (key) { case ',': if (m_bullet != null) { m_world.DestroyBody(m_bullet); m_bullet = null; } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 0.25f; b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.bullet = true; bd.position.Set(-31.0f, 5.0f); m_bullet = m_world.CreateBody(bd); m_bullet.CreateFixture(fd); m_bullet.LinearVelocity = new b2Vec2(400.0f, 0.0f); } break; } }
override public void Set(b2Shape other) { base.Set(other); if (other is b2CircleShape) { b2CircleShape other2 = other as b2CircleShape; m_p.SetV(other2.m_p); } }
public ContinuousTest() { { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 0.0f); b2Body body = m_world.CreateBody(bd); b2EdgeShape edge = new b2EdgeShape(); edge.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f)); body.CreateFixture(edge, 0.0f); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.2f, 1.0f, new b2Vec2(0.5f, 1.0f), 0.0f); body.CreateFixture(shape, 0.0f); } #if true { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 20.0f); //bd.angle = 0.1f; b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(2.0f, 0.1f); m_body = m_world.CreateBody(bd); m_body.CreateFixture(shape, 1.0f); m_angularVelocity = Rand.RandomFloat(-50.0f, 50.0f); //m_angularVelocity = 46.661274f; m_body.LinearVelocity = new b2Vec2(0.0f, -100.0f); m_body.AngularVelocity = m_angularVelocity; } #else { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 2.0f); b2Body body = m_world.CreateBody(bd); b2CircleShape shape = new b2CircleShape(); shape.Position = b2Vec2.Zero; shape.Radius = 0.5f; body.CreateFixture(shape, 1.0f); bd.bullet = true; bd.position.Set(0.0f, 10.0f); body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.LinearVelocity = new b2Vec2(0.0f, -100.0f); } #endif }
public Pulleys() { float y = 16.0f; float L = 12.0f; float a = 1.0f; float b = 2.0f; b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape edge = new b2EdgeShape(); edge.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); //ground->CreateFixture(&shape, 0.0f); b2CircleShape circle = new b2CircleShape(); circle.Radius = 2.0f; circle.Position = new b2Vec2(-10.0f, y + b + L); ground.CreateFixture(circle, 0.0f); circle.Position = new b2Vec2(10.0f, y + b + L); ground.CreateFixture(circle, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(a, b); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; //bd.fixedRotation = true; bd.position.Set(-10.0f, y); b2Body body1 = m_world.CreateBody(bd); body1.CreateFixture(shape, 5.0f); bd.position.Set(10.0f, y); b2Body body2 = m_world.CreateBody(bd); body2.CreateFixture(shape, 5.0f); b2PulleyJointDef pulleyDef = new b2PulleyJointDef(); b2Vec2 anchor1 = new b2Vec2(-10.0f, y + b); b2Vec2 anchor2 = new b2Vec2(10.0f, y + b); b2Vec2 groundAnchor1 = new b2Vec2(-10.0f, y + b + L); b2Vec2 groundAnchor2 = new b2Vec2(10.0f, y + b + L); pulleyDef.Initialize(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, 1.5f); m_joint1 = (b2PulleyJoint) m_world.CreateJoint(pulleyDef); } }
public SensorTest() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); { b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } #if false { b2FixtureDef sd = new b2FixtureDef(); sd.SetAsBox(10.0f, 2.0f, new b2Vec2(0.0f, 20.0f), 0.0f); sd.isSensor = true; m_sensor = ground.CreateFixture(sd); } #else { b2CircleShape shape = new b2CircleShape(); shape.Radius = 5.0f; shape.Position = new b2Vec2(0.0f, 10.0f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.isSensor = true; m_sensor = ground.CreateFixture(fd); } #endif } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 1.0f; for (int i = 0; i < e_count; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f + 3.0f * i, 20.0f); bd.userData = i; // m_touching[i]; m_touching[i] = false; m_bodies[i] = m_world.CreateBody(bd); m_bodies[i].CreateFixture(shape, 1.0f); } } }
public Confined() { { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); // Floor shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(10.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); // Left wall shape.Set(new b2Vec2(-10.0f, 0.0f), new b2Vec2(-10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); // Right wall shape.Set(new b2Vec2(10.0f, 0.0f), new b2Vec2(10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); // Roof shape.Set(new b2Vec2(-10.0f, 20.0f), new b2Vec2(10.0f, 20.0f)); ground.CreateFixture(shape, 0.0f); } float radius = 0.5f; b2CircleShape shape1 = new b2CircleShape(); shape1.Position = b2Vec2.Zero; shape1.Radius = radius; b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape1; fd.density = 1.0f; fd.friction = 0.1f; for (int j = 0; j < e_columnCount; ++j) { for (int i = 0; i < e_rowCount; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f + (2.1f * j + 1.0f + 0.01f * i) * radius, (2.0f * i + 1.0f) * radius); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } } m_world.Gravity = new b2Vec2(0.0f, 0.0f); }
public AddPair() { m_world.Gravity = new b2Vec2(0.0f, 0.0f); { b2CircleShape shape = new b2CircleShape(); shape.Position = b2Vec2.Zero; shape.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) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(Rand.RandomFloat(minX, maxX), Rand.RandomFloat(minY, maxY)); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 0.01f); } } { b2Shape shape = null; if (_DoCircle) { shape = new b2CircleShape(); ((b2CircleShape)shape).Radius = 1.5f; } else { shape = new b2PolygonShape(); ((b2PolygonShape)shape).SetAsBox(1.6f, 1.6f); } b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(-40.0f, 5.0f); bd.bullet = true; b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); body.LinearVelocity = new b2Vec2(150.0f, 0.0f); } }
public OneSidedPlatform() { // Ground { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } // Platform { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 10.0f); b2Body body = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(3.0f, 0.5f); m_platform = body.CreateFixture(shape, 0.0f); m_bottom = 10.0f - 0.5f; m_top = 10.0f + 0.5f; } // Actor { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 12.0f); b2Body body = m_world.CreateBody(bd); m_radius = 0.5f; b2CircleShape shape = new b2CircleShape(); shape.Radius = m_radius; m_character = body.CreateFixture(shape, 20.0f); body.LinearVelocity = new b2Vec2(0.0f, -50.0f); m_state = State.e_unknown; } }
public CircleBenchmarkTest() { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); // Floor b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); // Left wall shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(-40.0f, 45.0f)); ground.CreateFixture(shape, 0.0f); // Right wall shape = new b2EdgeShape(); shape.Set(new b2Vec2(40.0f, 0.0f), new b2Vec2(40.0f, 45.0f)); ground.CreateFixture(shape, 0.0f); // Roof shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 45.0f), new b2Vec2(40.0f, 45.0f)); ground.CreateFixture(shape, 0.0f); var sphere = new b2CircleShape(); sphere.Radius = 1.0f; for (int i = 0; i < XCount; i++) { for (int j = 0; j < YCount; ++j) { bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-38f + 2.1f * i, 2.0f + 2.0f * j); var body = m_world.CreateBody(bd); body.CreateFixture(sphere, 1.0f); } } }
public void CreateCircle() { float radius = 2.0f; b2CircleShape shape = new b2CircleShape(); shape.Position = b2Vec2.Zero; shape.Radius = radius; b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.0f; b2Vec2 p = new b2Vec2(Rand.RandomFloat(), 3.0f + Rand.RandomFloat()); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = p; //bd.allowSleep = false; b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); }
public Dominos() { b2Body b1; { b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); b2BodyDef bd = new b2BodyDef(); b1 = m_world.CreateBody(bd); b1.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(6.0f, 0.25f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(-1.5f, 10.0f); b2Body ground = m_world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.1f, 1.0f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.1f; for (int i = 0; i < 10; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-6.0f + 1.0f * i, 11.25f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(7.0f, 0.25f, b2Vec2.Zero, 0.3f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(1.0f, 6.0f); b2Body ground = m_world.CreateBody(bd); ground.CreateFixture(shape, 0.0f); } b2Body b2; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.25f, 1.5f); b2BodyDef bd = new b2BodyDef(); bd.position.Set(-7.0f, 4.0f); b2 = m_world.CreateBody(bd); b2.CreateFixture(shape, 0.0f); } b2Body b3; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(6.0f, 0.125f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-0.9f, 1.0f); bd.angle = -0.15f; b3 = m_world.CreateBody(bd); b3.CreateFixture(shape, 10.0f); } b2RevoluteJointDef jd = new b2RevoluteJointDef(); b2Vec2 anchor = new b2Vec2(); anchor.Set(-2.0f, 1.0f); jd.Initialize(b1, b3, anchor); jd.CollideConnected = true; m_world.CreateJoint(jd); b2Body b4; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.25f, 0.25f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-10.0f, 15.0f); b4 = m_world.CreateBody(bd); b4.CreateFixture(shape, 10.0f); } anchor.Set(-7.0f, 15.0f); jd.Initialize(b2, b4, anchor); m_world.CreateJoint(jd); b2Body b5; { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(6.5f, 3.0f); b5 = m_world.CreateBody(bd); b2PolygonShape shape = new b2PolygonShape(); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 10.0f; fd.friction = 0.1f; shape.SetAsBox(1.0f, 0.1f, new b2Vec2(0.0f, -0.9f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new b2Vec2(-0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); shape.SetAsBox(0.1f, 1.0f, new b2Vec2(0.9f, 0.0f), 0.0f); b5.CreateFixture(fd); } anchor.Set(6.0f, 2.0f); jd.Initialize(b1, b5, anchor); m_world.CreateJoint(jd); b2Body b6; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(1.0f, 0.1f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(6.5f, 4.1f); b6 = m_world.CreateBody(bd); b6.CreateFixture(shape, 30.0f); } anchor.Set(7.5f, 4.0f); jd.Initialize(b5, b6, anchor); m_world.CreateJoint(jd); b2Body b7; { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.1f, 1.0f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(7.4f, 1.0f); b7 = m_world.CreateBody(bd); b7.CreateFixture(shape, 10.0f); } b2DistanceJointDef djd = new b2DistanceJointDef(); djd.BodyA = b3; djd.BodyB = b7; djd.localAnchorA.Set(6.0f, 0.0f); djd.localAnchorB.Set(0.0f, -1.0f); b2Vec2 d = djd.BodyB.GetWorldPoint(djd.localAnchorB) - djd.BodyA.GetWorldPoint(djd.localAnchorA); djd.length = d.Length; m_world.CreateJoint(djd); { float radius = 0.2f; b2CircleShape shape = new b2CircleShape(); shape.Radius = radius; for (int i = 0; i < 4; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(5.9f + 2.0f * radius * i, 2.4f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 10.0f); } } }
/// Compute the collision manifold between a polygon and a circle. public static void b2CollidePolygonAndCircle(b2Manifold manifold, b2PolygonShape polygonA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; // Compute circle position in the frame of the polygon. b2Vec2 c; c.x = (xfB.q.c * circleB.Position.x - xfB.q.s * circleB.Position.y) + xfB.p.x; c.y = (xfB.q.s * circleB.Position.x + xfB.q.c * circleB.Position.y) + xfB.p.y; b2Vec2 cLocal; float px = c.x - xfA.p.x; float py = c.y - xfA.p.y; cLocal.x = (xfA.q.c * px + xfA.q.s * py); cLocal.y = (-xfA.q.s * px + xfA.q.c * py); // Find the min separating edge. int normalIndex = 0; float separation = -b2Settings.b2_maxFloat; float radius = polygonA.Radius + circleB.Radius; int vertexCount = polygonA.m_vertexCount; b2Vec2[] vertices = polygonA.Vertices; b2Vec2[] normals = polygonA.Normals; for (int i = 0; i < vertexCount; ++i) { b2Vec2 tmp; tmp.x = cLocal.x - vertices[i].x; tmp.y = cLocal.y - vertices[i].y; float s = normals[i].x * tmp.x + normals[i].y * tmp.y; // b2Math.b2Dot(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; b2Vec2 v1 = vertices[vertIndex1]; b2Vec2 v2 = vertices[vertIndex2]; // If the center is inside the polygon ... if (separation < b2Settings.b2_epsilon) { manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[normalIndex]; manifold.localPoint.x = 0.5f * (v1.x + v2.x); manifold.localPoint.y = 0.5f * (v1.y + v2.y); manifold.points[0].localPoint = circleB.Position; manifold.points[0].id.key = 0; return; } // Compute barycentric coordinates float ax = cLocal.x - v1.x; float ay = cLocal.y - v1.y; float bx = v2.x - v1.x; float by = v2.y - v1.y; float u1 = ax * bx + ay * by; ax = cLocal.x - v2.x; ay = cLocal.y - v2.y; bx = v1.x - v2.x; by = v1.y - v2.y; float u2 = ax * bx + ay * by; if (u1 <= 0.0f) { if (b2Math.b2DistanceSquared(ref cLocal, ref v1) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = cLocal - v1; manifold.localNormal.Normalize(); manifold.localPoint = v1; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id.key = 0; } else if (u2 <= 0.0f) { if (b2Math.b2DistanceSquared(ref cLocal, ref v2) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal.x = cLocal.x - v2.x; manifold.localNormal.y = cLocal.y - v2.y; manifold.localNormal.Normalize(); manifold.localPoint = v2; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id.key = 0; } else { b2Vec2 faceCenter; faceCenter.x = 0.5f * (v1.x + v2.x); faceCenter.y = 0.5f * (v1.y + v2.y); b2Vec2 a; a.x = cLocal.x - faceCenter.x; a.y = cLocal.y - faceCenter.y; separation = b2Math.b2Dot(ref a, ref normals[vertIndex1]); if (separation > radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[vertIndex1]; manifold.localPoint = faceCenter; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id.key = 0; } }
void CreateEnemy() { // Define the dynamic body. var bodyDef = b2BodyDef.Create(); bodyDef.type = b2BodyType.b2_dynamicBody; //or you could use b2_staticBody bodyDef.fixedRotation = isRotationFixed; bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO); b2PolygonShape shape = new b2PolygonShape(); b2CircleShape shapeCircle = new b2CircleShape(); if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle) { var tempSprite = new CCSprite(spriteImageName); float radiusInMeters = (tempSprite.ContentSize.Width / Constants.PTM_RATIO) * 0.5f; shapeCircle.Radius = radiusInMeters; } else if ( shapeCreationMethod == CreationMethod.ShapeOfSourceImage) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top left corner new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 )/ Constants.PTM_RATIO), //bottom right corner new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO) //top right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.ShapeOfSourceImageButSlightlySmaller ) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -2 ) *.8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 )*.8f / Constants.PTM_RATIO), //top left corner new b2Vec2( (tempSprite.ContentSize.Width / -2 )*.8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 )*.8f / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.ContentSize.Width / 2 )*.8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 )*.8f / Constants.PTM_RATIO), //bottom right corner new b2Vec2( (tempSprite.ContentSize.Width / 2 )*.8f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 )*.8f / Constants.PTM_RATIO) //top right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Triangle) { var tempSprite = new CCSprite(spriteImageName); var num = 3; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom right corner new b2Vec2( 0.0f / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 )/ Constants.PTM_RATIO) // top center of image }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.TriangleRightAngle) { var tempSprite = new CCSprite(spriteImageName); var num = 3; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top right corner new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top left corner new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 )/ Constants.PTM_RATIO) //bottom left corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Trapezoid) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top of image, 3/4's across new b2Vec2( (tempSprite.ContentSize.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4's across new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Hexagon) { var tempSprite = new CCSprite(spriteImageName); var num = 6; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4 across new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // left, center new b2Vec2( (tempSprite.ContentSize.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 1/4 across new b2Vec2( (tempSprite.ContentSize.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // right, center new b2Vec2( (tempSprite.ContentSize.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO) //top of image, 3/4's across }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Pentagon) { var tempSprite = new CCSprite(spriteImageName); var num = 5; b2Vec2[] vertices = { new b2Vec2( 0 / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top of image, center new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // left, center new b2Vec2( (tempSprite.ContentSize.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 1/4 across new b2Vec2( (tempSprite.ContentSize.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // right, center }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Octagon) { var tempSprite = new CCSprite(spriteImageName); var num = 8; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -6 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //use the source image octogonShape.png for reference new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / -6 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / 6 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.ContentSize.Width / 6 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO) }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Parallelogram) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.ContentSize.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4 across new b2Vec2( (tempSprite.ContentSize.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.ContentSize.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.ContentSize.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.ContentSize.Height / 2 ) / Constants.PTM_RATIO) //top right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.CustomCoordinates1) { //use your own custom coordinates from a program like Vertex Helper Pro var num = 4; b2Vec2[] vertices = { new b2Vec2(-64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO), new b2Vec2(-64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO), new b2Vec2(64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO), new b2Vec2(64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO) }; shape.Set(vertices, num); } // Define the dynamic body fixture. var fixtureDef = b2FixtureDef.Create(); if ( shapeCreationMethod == CreationMethod.DiameterOfImageForCircle) { fixtureDef.shape = shapeCircle; } else { fixtureDef.shape = shape; } fixtureDef.density = theDensity; fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.1f; //how bouncy basically CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName); var blinkInterval = cocos2d.Random.Next(3,8); // range 3 to 8 Schedule(Blink, blinkInterval); //comment this out if you never want to show the blink }
public Car() { m_hz = 4.0f; m_zeta = 0.7f; m_speed = 50.0f; b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 0.0f; fd.friction = 0.6f; shape.Set(new b2Vec2(-20.0f, 0.0f), new b2Vec2(20.0f, 0.0f)); ground.CreateFixture(fd); float[] hs = {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 b2Vec2(x, y1), new b2Vec2(x + dx, y2)); ground.CreateFixture(fd); y1 = y2; x += dx; } for (int i = 0; i < 10; ++i) { float y2 = hs[i]; shape.Set(new b2Vec2(x, y1), new b2Vec2(x + dx, y2)); ground.CreateFixture(fd); y1 = y2; x += dx; } shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 80.0f; shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 40.0f; shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 10.0f, 5.0f)); ground.CreateFixture(fd); x += 20.0f; shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x + 40.0f, 0.0f)); ground.CreateFixture(fd); x += 40.0f; shape.Set(new b2Vec2(x, 0.0f), new b2Vec2(x, 20.0f)); ground.CreateFixture(fd); } // Teeter { b2BodyDef bd = new b2BodyDef(); bd.position.Set(140.0f, 1.0f); bd.type = b2BodyType.b2_dynamicBody; b2Body body = m_world.CreateBody(bd); b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(10.0f, 0.25f); body.CreateFixture(box, 1.0f); b2RevoluteJointDef jd = new b2RevoluteJointDef(); jd.Initialize(ground, body, body.Position); jd.lowerAngle = -8.0f * b2Settings.b2_pi / 180.0f; jd.upperAngle = 8.0f * b2Settings.b2_pi / 180.0f; jd.enableLimit = true; m_world.CreateJoint(jd); body.ApplyAngularImpulse(100.0f); } // Bridge { int N = 20; b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(1.0f, 0.125f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.6f; b2RevoluteJointDef jd = new b2RevoluteJointDef(); b2Body prevBody = ground; for (int i = 0; i < N; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(161.0f + 2.0f * i, -0.125f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); b2Vec2 anchor = new b2Vec2(160.0f + 2.0f * i, -0.125f); jd.Initialize(prevBody, body, anchor); m_world.CreateJoint(jd); prevBody = body; } b2Vec2 anchor1 = new b2Vec2(160.0f + 2.0f * N, -0.125f); jd.Initialize(prevBody, ground, anchor1); m_world.CreateJoint(jd); } // Boxes { b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(0.5f, 0.5f); b2Body body = null; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(230.0f, 0.5f); body = m_world.CreateBody(bd); body.CreateFixture(box, 0.5f); bd.position.Set(230.0f, 1.5f); body = m_world.CreateBody(bd); body.CreateFixture(box, 0.5f); bd.position.Set(230.0f, 2.5f); body = m_world.CreateBody(bd); body.CreateFixture(box, 0.5f); bd.position.Set(230.0f, 3.5f); body = m_world.CreateBody(bd); body.CreateFixture(box, 0.5f); bd.position.Set(230.0f, 4.5f); body = m_world.CreateBody(bd); body.CreateFixture(box, 0.5f); } // Car { b2PolygonShape chassis = new b2PolygonShape(); b2Vec2[] vertices = new b2Vec2[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); b2CircleShape circle = new b2CircleShape(); circle.Radius = 0.4f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 1.0f); m_car = m_world.CreateBody(bd); m_car.CreateFixture(chassis, 1.0f); b2FixtureDef fd = new b2FixtureDef(); 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); b2WheelJointDef jd = new b2WheelJointDef(); b2Vec2 axis = new b2Vec2(0.0f, 1.0f); jd.Initialize(m_car, m_wheel1, m_wheel1.Position, axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 20.0f; jd.enableMotor = true; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring1 = (b2WheelJoint) m_world.CreateJoint(jd); jd.Initialize(m_car, m_wheel2, m_wheel2.Position, axis); jd.motorSpeed = 0.0f; jd.maxMotorTorque = 10.0f; jd.enableMotor = false; jd.frequencyHz = m_hz; jd.dampingRatio = m_zeta; m_spring2 = (b2WheelJoint) m_world.CreateJoint(jd); } }
public Pinball() { // Ground body b2Body ground; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2Vec2[] vs = new b2Vec2[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); b2ChainShape loop = new b2ChainShape(); loop.CreateLoop(vs, 5); b2FixtureDef fd = new b2FixtureDef(); fd.shape = loop; fd.density = 0.0f; ground.CreateFixture(fd); } // Flippers { b2Vec2 p1 = new b2Vec2(-2.0f, 0.0f); b2Vec2 p2 = new b2Vec2(2.0f, 0.0f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = p1; b2Body leftFlipper = m_world.CreateBody(bd); bd.position = p2; b2Body rightFlipper = m_world.CreateBody(bd); b2PolygonShape box = new b2PolygonShape(); box.SetAsBox(1.75f, 0.1f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = box; fd.density = 1.0f; leftFlipper.CreateFixture(fd); rightFlipper.CreateFixture(fd); b2RevoluteJointDef jd = new b2RevoluteJointDef(); 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 * b2Settings.b2_pi / 180.0f; jd.upperAngle = 5.0f * b2Settings.b2_pi / 180.0f; m_leftJoint = (b2RevoluteJoint) m_world.CreateJoint(jd); jd.motorSpeed = 0.0f; jd.localAnchorA = p2; jd.BodyB = rightFlipper; jd.lowerAngle = -5.0f * b2Settings.b2_pi / 180.0f; jd.upperAngle = 30.0f * b2Settings.b2_pi / 180.0f; m_rightJoint = (b2RevoluteJoint) m_world.CreateJoint(jd); } // Circle character { b2BodyDef bd = new b2BodyDef(); bd.position.Set(1.0f, 15.0f); bd.type = b2BodyType.b2_dynamicBody; bd.bullet = true; m_ball = m_world.CreateBody(bd); b2CircleShape shape = new b2CircleShape(); shape.Radius = 0.2f; b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; m_ball.CreateFixture(fd); } m_button = false; }
public CompoundShapes() { { b2BodyDef bd = new b2BodyDef(); bd.position.Set(0.0f, 0.0f); b2Body body = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(-50.0f, 0.0f)); body.CreateFixture(shape, 0.0f); } { b2CircleShape circle1 = new b2CircleShape(); circle1.Radius = 0.5f; circle1.Position = new b2Vec2(-0.5f, 0.5f); b2CircleShape circle2 = new b2CircleShape(); circle2.Radius = 0.5f; circle2.Position = new b2Vec2(0.5f, 0.5f); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(x + 5.0f, 1.05f + 2.5f * i); bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi); b2Body body = m_world.CreateBody(bd); body.CreateFixture(circle1, 2.0f); body.CreateFixture(circle2, 0.0f); } } { b2PolygonShape polygon1 = new b2PolygonShape(); polygon1.SetAsBox(0.25f, 0.5f); b2PolygonShape polygon2 = new b2PolygonShape(); polygon2.SetAsBox(0.25f, 0.5f, new b2Vec2(0.0f, -0.5f), 0.5f * b2Settings.b2_pi); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(x - 5.0f, 1.05f + 2.5f * i); bd.angle = Rand.RandomFloat(-b2Settings.b2_pi, b2Settings.b2_pi); b2Body body = m_world.CreateBody(bd); body.CreateFixture(polygon1, 2.0f); body.CreateFixture(polygon2, 2.0f); } } { b2Transform xf1 = new b2Transform(); xf1.q.Set(0.3524f * b2Settings.b2_pi); xf1.p = xf1.q.GetXAxis(); b2Vec2[] vertices = new b2Vec2[3]; b2PolygonShape triangle1 = new b2PolygonShape(); vertices[0] = b2Math.b2Mul(xf1, new b2Vec2(-1.0f, 0.0f)); vertices[1] = b2Math.b2Mul(xf1, new b2Vec2(1.0f, 0.0f)); vertices[2] = b2Math.b2Mul(xf1, new b2Vec2(0.0f, 0.5f)); triangle1.Set(vertices, 3); b2Transform xf2 = new b2Transform(); xf2.q.Set(-0.3524f * b2Settings.b2_pi); xf2.p = -xf2.q.GetXAxis(); b2PolygonShape triangle2 = new b2PolygonShape(); vertices[0] = b2Math.b2Mul(xf2, new b2Vec2(-1.0f, 0.0f)); vertices[1] = b2Math.b2Mul(xf2, new b2Vec2(1.0f, 0.0f)); vertices[2] = b2Math.b2Mul(xf2, new b2Vec2(0.0f, 0.5f)); triangle2.Set(vertices, 3); for (int i = 0; i < 10; ++i) { float x = Rand.RandomFloat(-0.1f, 0.1f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(x, 2.05f + 2.5f * i); bd.angle = 0.0f; b2Body body = m_world.CreateBody(bd); body.CreateFixture(triangle1, 2.0f); body.CreateFixture(triangle2, 2.0f); } } { b2PolygonShape bottom = new b2PolygonShape(); bottom.SetAsBox(1.5f, 0.15f); b2PolygonShape left = new b2PolygonShape(); left.SetAsBox(0.15f, 2.7f, new b2Vec2(-1.45f, 2.35f), 0.2f); b2PolygonShape right = new b2PolygonShape(); right.SetAsBox(0.15f, 2.7f, new b2Vec2(1.45f, 2.35f), -0.2f); b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(0.0f, 2.0f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(bottom, 4.0f); body.CreateFixture(left, 4.0f); body.CreateFixture(right, 4.0f); } }
public void LaunchBomb(b2Vec2 position, b2Vec2 velocity) { if (m_bomb != null) { m_world.DestroyBody(m_bomb); m_bomb = null; } b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = position; bd.bullet = true; m_bomb = m_world.CreateBody(bd); m_bomb.LinearVelocity = velocity; b2CircleShape circle = new b2CircleShape(); circle.Radius = 0.3f; b2FixtureDef fd = new b2FixtureDef(); fd.shape = circle; fd.density = 20.0f; fd.restitution = 0.0f; b2Vec2 minV = position - new b2Vec2(0.3f, 0.3f); b2Vec2 maxV = position + new b2Vec2(0.3f, 0.3f); b2AABB aabb = new b2AABB(); aabb.LowerBound = minV; aabb.UpperBound = maxV; m_bomb.CreateFixture(fd); }
/// Compute the collision manifold between a polygon and a circle. public static void b2CollidePolygonAndCircle(ref b2Manifold manifold, b2PolygonShape polygonA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; // Compute circle position in the frame of the polygon. b2Vec2 c = b2Math.b2Mul(xfB, circleB.Position); b2Vec2 cLocal = b2Math.b2MulT(xfA, c); // Find the min separating edge. int normalIndex = 0; float separation = -b2Settings.b2_maxFloat; float radius = polygonA.Radius + circleB.Radius; int vertexCount = polygonA.VertexCount; b2Vec2[] vertices = polygonA.Vertices; b2Vec2[] normals = polygonA.Normals; for (int i = 0; i < vertexCount; ++i) { float s = b2Math.b2Dot(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; b2Vec2 v1 = vertices[vertIndex1]; b2Vec2 v2 = vertices[vertIndex2]; // If the center is inside the polygon ... if (separation < b2Settings.b2_epsilon) { manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[normalIndex]; manifold.localPoint = 0.5f * (v1 + v2); manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; return; } // Compute barycentric coordinates float u1 = b2Math.b2Dot(cLocal - v1, v2 - v1); float u2 = b2Math.b2Dot(cLocal - v2, v1 - v2); if (u1 <= 0.0f) { if (b2Math.b2DistanceSquared(cLocal, v1) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = cLocal - v1; manifold.localNormal.Normalize(); manifold.localPoint = v1; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } else if (u2 <= 0.0f) { if (b2Math.b2DistanceSquared(cLocal, v2) > radius * radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = cLocal - v2; manifold.localNormal.Normalize(); manifold.localPoint = v2; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } else { b2Vec2 faceCenter = 0.5f * (v1 + v2); separation = b2Math.b2Dot(cLocal - faceCenter, normals[vertIndex1]); if (separation > radius) { return; } manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = normals[vertIndex1]; manifold.localPoint = faceCenter; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; } }
public b2CircleShape(b2CircleShape copy) : base((b2Shape)copy) { m_p = copy.m_p; }
public virtual b2Shape Clone() { b2CircleShape clone = new b2CircleShape(this); return(clone); }
void AddBall() { if (ballsBatch.ChildrenCount < MAX_NUM_BALLS) { int idx = (CCRandom.Float_0_1 () > .5 ? 0 : 1); int idy = (CCRandom.Float_0_1 () > .5 ? 0 : 1); var sprite = new CCPhysicsSprite (ballTexture, new CCRect (32 * idx, 32 * idy, 32, 32), PTM_RATIO); ballsBatch.AddChild (sprite); CCPoint p = GetRandomPosition (sprite.ContentSize); sprite.Position = new CCPoint (p.X, p.Y); var def = new b2BodyDef (); def.position = new b2Vec2 (p.X / PTM_RATIO, p.Y / PTM_RATIO); def.linearVelocity = new b2Vec2 (0.0f, -1.0f); def.type = b2BodyType.b2_dynamicBody; b2Body body = world.CreateBody (def); var circle = new b2CircleShape (); circle.Radius = 0.5f; var fd = new b2FixtureDef (); fd.shape = circle; fd.density = 1f; fd.restitution = 0.85f; fd.friction = 0f; body.CreateFixture (fd); sprite.PhysicsBody = body; } }
public Bridge() { b2Body ground = null; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); } { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(0.5f, 0.125f); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; b2RevoluteJointDef jd = new b2RevoluteJointDef(); b2Body prevBody = ground; for (int i = 0; i < e_count; ++i) { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(-14.5f + 1.0f * i, 5.0f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); b2Vec2 anchor = new b2Vec2(-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; } b2Vec2 anchor1 = new b2Vec2(-15.0f + 1.0f * e_count, 5.0f); jd.Initialize(prevBody, ground, anchor1); m_world.CreateJoint(jd); } for (int i = 0; i < 2; ++i) { b2Vec2[] vertices = new b2Vec2[3]; vertices[0].Set(-0.5f, 0.0f); vertices[1].Set(0.5f, 0.0f); vertices[2].Set(0.0f, 1.5f); b2PolygonShape shape = new b2PolygonShape(); shape.Set(vertices, 3); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = new b2Vec2(-8.0f + 8.0f * i, 12.0f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } for (int i = 0; i < 3; ++i) { b2CircleShape shape = new b2CircleShape(); shape.Radius = 0.5f; b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; fd.density = 1.0f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-6.0f + 6.0f * i, 10.0f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(fd); } }
void CreateObject() { // Define the dynamic body. var bodyDef = new b2BodyDef(); bodyDef.type = b2BodyType.b2_dynamicBody; //or you could use b2_staticBody bodyDef.position.Set(initialLocation.X/Constants.PTM_RATIO, initialLocation.Y/Constants.PTM_RATIO); var shape = new b2PolygonShape(); var shapeCircle = new b2CircleShape(); if (shapeCreationMethod == CreationMethod.DiameterOfImageForCircle) { var tempSprite = new CCSprite(spriteImageName); var radiusInMeters = (tempSprite.Texture.ContentSizeInPixels.Width / Constants.PTM_RATIO) * 0.5f; shapeCircle.Radius = radiusInMeters; } else if ( shapeCreationMethod == CreationMethod.ShapeOfSourceImage) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 )/ Constants.PTM_RATIO), //bottom right corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO) //top right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Triangle) { var tempSprite = new CCSprite(spriteImageName); var num = 3; b2Vec2[] vertices = { new b2Vec2((tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom right corner new b2Vec2( 0.0f / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 )/ Constants.PTM_RATIO) // top center of image }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.TriangleRightAngle) { var tempSprite = new CCSprite(spriteImageName); var num = 3; b2Vec2[] vertices = { new b2Vec2((tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top right corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 )/ Constants.PTM_RATIO) //bottom left corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Trapezoid) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2((tempSprite.Texture.ContentSizeInPixels.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top of image, 3/4's across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4's across new b2Vec2((tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Hexagon) { var tempSprite = new CCSprite(spriteImageName); var num = 6; b2Vec2[] vertices = { new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4 across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // left, center new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 1/4 across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // right, center new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO) //top of image, 3/4's across }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Pentagon) { var tempSprite = new CCSprite(spriteImageName); var num = 5; b2Vec2[] vertices = { new b2Vec2( 0 / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top of image, center new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // left, center new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 1/4 across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, 0.0f / Constants.PTM_RATIO), // right, center }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Octagon) { var tempSprite = new CCSprite(spriteImageName); var num = 8; b2Vec2[] vertices = { new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -6 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //use the source image octogonShape.png for reference new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -6 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 6 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 6 ) / Constants.PTM_RATIO), new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 6 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO) }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.Parallelogram) { var tempSprite = new CCSprite(spriteImageName); var num = 4; b2Vec2[] vertices = { new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO), //top of image, 1/4 across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / -2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom left corner new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 4 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / -2 ) / Constants.PTM_RATIO), //bottom of image, 3/4's across new b2Vec2( (tempSprite.Texture.ContentSizeInPixels.Width / 2 ) / Constants.PTM_RATIO, (tempSprite.Texture.ContentSizeInPixels.Height / 2 ) / Constants.PTM_RATIO) //top right corner }; shape.Set(vertices, num); } else if ( shapeCreationMethod == CreationMethod.CustomCoordinates1) { //use your own custom coordinates from a program like Vertex Helper Pro var num = 4; b2Vec2[] vertices = { new b2Vec2(-64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO), new b2Vec2(-64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO), new b2Vec2(64.0f / Constants.PTM_RATIO, -16.0f / Constants.PTM_RATIO), new b2Vec2(64.0f / Constants.PTM_RATIO, 16.0f / Constants.PTM_RATIO) }; shape.Set(vertices, num); } // Define the dynamic body fixture. var fixtureDef = new b2FixtureDef(); if ( shapeCreationMethod == CreationMethod.DiameterOfImageForCircle) { fixtureDef.shape = shapeCircle; } else { fixtureDef.shape = shape; } fixtureDef.density = theDensity; fixtureDef.friction = 0.3f; fixtureDef.restitution = 0.1f; CreateBodyWithSpriteAndFixture(theWorld, bodyDef, fixtureDef, spriteImageName); if ( angle != 0 ) { int currentAngle = (int)body.Angle ; b2Vec2 locationInMeters = body.Position; body.SetTransform( locationInMeters , CCMacros.CCDegreesToRadians( currentAngle + angle ) ); } if (IsStatic) { MakeBodyStatic(); } }
/// Compute the collision manifold between two circles. public static void b2CollideCircles(b2Manifold manifold, b2CircleShape circleA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; float pAx = (xfA.q.c * circleA.Position.x - xfA.q.s * circleA.Position.y) + xfA.p.x; float pAy = (xfA.q.s * circleA.Position.x + xfA.q.c * circleA.Position.y) + xfA.p.y; float pBx = (xfB.q.c * circleB.Position.x - xfB.q.s * circleB.Position.y) + xfB.p.x; float pBy = (xfB.q.s * circleB.Position.x + xfB.q.c * circleB.Position.y) + xfB.p.y; float dx = pBx - pAx; float dy = pBy - pAy; float distSqr = dx * dx + dy * dy; float rA = circleA.Radius, rB = circleB.Radius; float radius = rA + rB; if (distSqr > radius * radius) { return; } manifold.type = b2ManifoldType.e_circles; manifold.localPoint = circleA.Position; manifold.localNormal = b2Vec2.Zero; manifold.pointCount = 1; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id.key = 0; }
public b2CircleShape(b2CircleShape copy) : base(copy) { Position = copy.Position; }
/// Compute the collision manifold between two circles. public static void b2CollideCircles(ref b2Manifold manifold, b2CircleShape circleA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; b2Vec2 pA = b2Math.b2Mul(xfA, circleA.Position); b2Vec2 pB = b2Math.b2Mul(xfB, circleB.Position); b2Vec2 d = pB - pA; float distSqr = b2Math.b2Dot(d, d); float rA = circleA.Radius, rB = circleB.Radius; float radius = rA + rB; if (distSqr > radius * radius) { return; } manifold.type = b2ManifoldType.e_circles; manifold.localPoint = circleA.Position; manifold.localNormal.SetZero(); manifold.pointCount = 1; manifold.points[0].localPoint = circleB.Position; manifold.points[0].id = b2ContactFeature.Zero; }
public override b2Shape Clone() { b2CircleShape clone = new b2CircleShape(this); return(clone); }
/// Compute the collision manifold between an edge and a circle. public static void b2CollideEdgeAndCircle(ref b2Manifold manifold, b2EdgeShape edgeA, ref b2Transform xfA, b2CircleShape circleB, ref b2Transform xfB) { manifold.pointCount = 0; // Compute circle in frame of edge b2Vec2 Q = b2Math.b2MulT(xfA, b2Math.b2Mul(xfB, circleB.Position)); b2Vec2 A = edgeA.Vertex1, B = edgeA.Vertex2; b2Vec2 e = B - A; b2Vec2 diff; // Barycentric coordinates diff = B - Q; float u = b2Math.b2Dot(ref e, ref diff); // B - Q); diff = Q - A; float v = b2Math.b2Dot(ref e, ref diff); // Q - A); float radius = edgeA.Radius + circleB.Radius; b2ContactFeature cf = b2ContactFeature.Zero; cf.indexB = 0; cf.typeB = b2ContactFeatureType.e_vertex; // Region A if (v <= 0.0f) { b2Vec2 P = A; b2Vec2 d = Q - P; float dd = d.LengthSquared; // b2Math.b2Dot(d, d); if (dd > radius * radius) { return; } // Is there an edge connected to A? if (edgeA.HasVertex0) { b2Vec2 A1 = edgeA.Vertex0; b2Vec2 B1 = A; b2Vec2 e1 = B1 - A1; diff = B1 - Q; float u1 = b2Math.b2Dot(ref e1, ref diff); // Is the circle in Region AB of the previous edge? if (u1 > 0.0f) { return; } } cf.indexA = 0; cf.typeA = b2ContactFeatureType.e_vertex; manifold.pointCount = 1; manifold.type = b2ManifoldType.e_circles; manifold.localNormal.SetZero(); manifold.localPoint = P; manifold.points[0].id.key = 0; manifold.points[0].id.Set(cf); manifold.points[0].localPoint = circleB.Position; return; } // Region B if (u <= 0.0f) { b2Vec2 P = B; b2Vec2 d = Q - P; float dd = d.LengthSquared; // b2Math.b2Dot(d, d); if (dd > radius * radius) { return; } // Is there an edge connected to B? if (edgeA.HasVertex3) { b2Vec2 B2 = edgeA.Vertex3; b2Vec2 A2 = B; b2Vec2 e2 = B2 - A2; diff = Q - A2; float v2 = b2Math.b2Dot(ref e2, ref diff); // Is the circle in Region AB of the next edge? if (v2 > 0.0f) { return; } } cf.indexA = 1; cf.typeA = b2ContactFeatureType.e_vertex; manifold.pointCount = 1; manifold.type = b2ManifoldType.e_circles; manifold.localNormal.SetZero(); manifold.localPoint = P; manifold.points[0].id.key = 0; manifold.points[0].id.Set(cf); manifold.points[0].localPoint = circleB.Position; return; } // Region AB float den = e.Length; // b2Math.b2Dot(e, e); System.Diagnostics.Debug.Assert(den > 0.0f); b2Vec2 xP = (1.0f / den) * (u * A + v * B); b2Vec2 xd = Q - xP; float xdd = xd.LengthSquared; // b2Math.b2Dot(xd, xd); if (xdd > radius * radius) { return; } b2Vec2 n = b2Vec2.Zero; // new b2Vec2(-e.y, e.x); n.m_x = -e.y; n.m_y = e.x; diff = Q - A; if (b2Math.b2Dot(ref n, ref diff) < 0.0f) { // n.Set(-n.x, -n.y); n.Set(-n.m_x, -n.m_y); } n.Normalize(); cf.indexA = 0; cf.typeA = b2ContactFeatureType.e_face; manifold.pointCount = 1; manifold.type = b2ManifoldType.e_faceA; manifold.localNormal = n; manifold.localPoint = A; manifold.points[0].id.key = 0; manifold.points[0].id.Set(cf); manifold.points[0].localPoint = circleB.Position; }
public CollisionFiltering() { // Ground body { b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); b2FixtureDef sd = new b2FixtureDef(); sd.shape = shape; sd.friction = 0.3f; b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); ground.CreateFixture(sd); } // Small triangle b2Vec2[] vertices = new b2Vec2[3]; vertices[0].Set(-1.0f, 0.0f); vertices[1].Set(1.0f, 0.0f); vertices[2].Set(0.0f, 2.0f); b2PolygonShape polygon = new b2PolygonShape(); polygon.Set(vertices, 3); b2FixtureDef triangleShapeDef = new b2FixtureDef(); triangleShapeDef.shape = polygon; triangleShapeDef.density = 1.0f; triangleShapeDef.filter.groupIndex = k_smallGroup; triangleShapeDef.filter.categoryBits = k_triangleCategory; triangleShapeDef.filter.maskBits = k_triangleMask; b2BodyDef triangleBodyDef = new b2BodyDef(); triangleBodyDef.type = b2BodyType.b2_dynamicBody; triangleBodyDef.position.Set(-5.0f, 2.0f); b2Body 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! b2Body body2 = m_world.CreateBody(triangleBodyDef); body2.CreateFixture(triangleShapeDef); { b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-5.0f, 10.0f); b2Body body = m_world.CreateBody(bd); b2PolygonShape p = new b2PolygonShape(); p.SetAsBox(0.5f, 1.0f); body.CreateFixture(p, 1.0f); b2PrismaticJointDef jd = new b2PrismaticJointDef(); 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); b2FixtureDef boxShapeDef = new b2FixtureDef(); 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; b2BodyDef boxBodyDef = new b2BodyDef(); boxBodyDef.type = b2BodyType.b2_dynamicBody; boxBodyDef.position.Set(0.0f, 2.0f); b2Body 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); b2Body body4 = m_world.CreateBody(boxBodyDef); body4.CreateFixture(boxShapeDef); // Small circle b2CircleShape circle = new b2CircleShape(); circle.Radius = 1.0f; b2FixtureDef circleShapeDef = new b2FixtureDef(); circleShapeDef.shape = circle; circleShapeDef.density = 1.0f; circleShapeDef.filter.groupIndex = k_smallGroup; circleShapeDef.filter.categoryBits = k_circleCategory; circleShapeDef.filter.maskBits = k_circleMask; b2BodyDef circleBodyDef = new b2BodyDef(); circleBodyDef.type = b2BodyType.b2_dynamicBody; circleBodyDef.position.Set(5.0f, 2.0f); b2Body body5 = m_world.CreateBody(circleBodyDef); body5.CreateFixture(circleShapeDef); // Large circle circle.Radius = circle.Radius * 2.0f; circleShapeDef.filter.groupIndex = k_largeGroup; circleBodyDef.position.Set(5.0f, 6.0f); b2Body body6 = m_world.CreateBody(circleBodyDef); body6.CreateFixture(circleShapeDef); }
public Revolute() { b2Body ground; { b2BodyDef bd = new b2BodyDef(); ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-40.0f, 0.0f), new b2Vec2(40.0f, 0.0f)); b2FixtureDef fd = new b2FixtureDef(); fd.shape = shape; //fd.filter.categoryBits = 2; ground.CreateFixture(fd); } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 0.5f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; b2RevoluteJointDef rjd = new b2RevoluteJointDef(); bd.position.Set(-10.0f, 20.0f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 5.0f); float w = 100.0f; body.AngularVelocity = w; body.LinearVelocity = new b2Vec2(-8.0f * w, 0.0f); rjd.Initialize(ground, body, new b2Vec2(-10.0f, 12.0f)); rjd.motorSpeed = 1.0f * b2Settings.b2_pi; rjd.maxMotorTorque = 10000.0f; rjd.enableMotor = false; rjd.lowerAngle = -0.25f * b2Settings.b2_pi; rjd.upperAngle = 0.5f * b2Settings.b2_pi; rjd.enableLimit = true; rjd.CollideConnected = true; m_joint = (b2RevoluteJoint) m_world.CreateJoint(rjd); } { b2CircleShape circle_shape = new b2CircleShape(); circle_shape.Radius = 3.0f; b2BodyDef circle_bd = new b2BodyDef(); circle_bd.type = b2BodyType.b2_dynamicBody; circle_bd.position.Set(5.0f, 30.0f); b2FixtureDef fd = new b2FixtureDef(); fd.density = 5.0f; fd.filter.maskBits = 1; fd.shape = circle_shape; m_ball = m_world.CreateBody(circle_bd); m_ball.CreateFixture(fd); b2PolygonShape polygon_shape = new b2PolygonShape(); polygon_shape.SetAsBox(10.0f, 0.2f, new b2Vec2(-10.0f, 0.0f), 0.0f); b2BodyDef polygon_bd = new b2BodyDef(); polygon_bd.position.Set(20.0f, 10.0f); polygon_bd.type = b2BodyType.b2_dynamicBody; polygon_bd.bullet = true; b2Body polygon_body = m_world.CreateBody(polygon_bd); polygon_body.CreateFixture(polygon_shape, 2.0f); b2RevoluteJointDef rjd = new b2RevoluteJointDef(); rjd.Initialize(ground, polygon_body, new b2Vec2(20.0f, 10.0f)); rjd.lowerAngle = -0.25f * b2Settings.b2_pi; rjd.upperAngle = 0.0f * b2Settings.b2_pi; rjd.enableLimit = true; m_world.CreateJoint(rjd); } // Tests mass computation of a small object far from the origin { b2BodyDef bodyDef = new b2BodyDef(); bodyDef.type = b2BodyType.b2_dynamicBody; b2Body body = m_world.CreateBody(bodyDef); b2PolygonShape polyShape = new b2PolygonShape(); b2Vec2[] verts = new b2Vec2[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); b2FixtureDef polyFixtureDef = new b2FixtureDef(); polyFixtureDef.shape = polyShape; polyFixtureDef.density = 1; body.CreateFixture(polyFixtureDef); //assertion hits inside here } }
public TheoJansen() { m_offset.Set(0.0f, 8.0f); m_motorSpeed = 2.0f; m_motorOn = true; b2Vec2 pivot = new b2Vec2(0.0f, 0.8f); // Ground { b2BodyDef bd = new b2BodyDef(); b2Body ground = m_world.CreateBody(bd); b2EdgeShape shape = new b2EdgeShape(); shape.Set(new b2Vec2(-50.0f, 0.0f), new b2Vec2(50.0f, 0.0f)); ground.CreateFixture(shape, 0.0f); shape.Set(new b2Vec2(-50.0f, 0.0f), new b2Vec2(-50.0f, 10.0f)); ground.CreateFixture(shape, 0.0f); shape.Set(new b2Vec2(50.0f, 0.0f), new b2Vec2(50.0f, 10.0f)); ground.CreateFixture(shape, 0.0f); } // Balls for (int i = 0; i < 40; ++i) { b2CircleShape shape = new b2CircleShape(); shape.Radius = 0.25f; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position.Set(-40.0f + 2.0f * i, 0.5f); b2Body body = m_world.CreateBody(bd); body.CreateFixture(shape, 1.0f); } // Chassis { b2PolygonShape shape = new b2PolygonShape(); shape.SetAsBox(2.5f, 1.0f); b2FixtureDef sd = new b2FixtureDef(); sd.density = 1.0f; sd.shape = shape; sd.filter.groupIndex = -1; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = pivot + m_offset; m_chassis = m_world.CreateBody(bd); m_chassis.CreateFixture(sd); } { b2CircleShape shape = new b2CircleShape(); shape.Radius = 1.6f; b2FixtureDef sd = new b2FixtureDef(); sd.density = 1.0f; sd.shape = shape; sd.filter.groupIndex = -1; b2BodyDef bd = new b2BodyDef(); bd.type = b2BodyType.b2_dynamicBody; bd.position = pivot + m_offset; m_wheel = m_world.CreateBody(bd); m_wheel.CreateFixture(sd); } { b2RevoluteJointDef jd = new b2RevoluteJointDef(); 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 = (b2RevoluteJoint) m_world.CreateJoint(jd); } b2Vec2 wheelAnchor = new b2Vec2(); wheelAnchor = pivot + new b2Vec2(0.0f, -0.8f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); m_wheel.SetTransform(m_wheel.Position, 120.0f * b2Settings.b2_pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); m_wheel.SetTransform(m_wheel.Position, -120.0f * b2Settings.b2_pi / 180.0f); CreateLeg(-1.0f, wheelAnchor); CreateLeg(1.0f, wheelAnchor); }