void b2PolygonDef::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle) { SetAsBox(hx, hy); b2XForm xf; xf.position = center; xf.R.Set(angle); for (int32 i = 0; i < vertexCount; ++i) { vertices[i] = b2Mul(xf, vertices[i]); } }
inline virtual void registerWithWorld(std::shared_ptr<gecom::WorldProxy> world) { B2PhysicsComponent::registerWithWorld(world); b2BodyDef def; def.type = b2_dynamicBody; def.fixedRotation = true; def.position.Set(getParent()->getPosition().x(), getParent()->getPosition().y()); def.linearDamping = 0.6f; setB2Body(world->createBody(def, shared_from_this())); auto bbb = std::make_shared<b2PolygonShape>(); bbb->SetAsBox(m_half_width, m_half_height); auto fix = std::make_shared<b2FixtureDef>(); fix->shape = bbb.get(); fix->density = 130; fix->friction = 0.99f; world->createFixture(getBodyID(), fix, bbb); }
void init( const Vec2& p1, const Vec2& p2, int attr ) { b2Vec2 barOrigin = p1; b2Vec2 bar = p2 - p1; bar *= 1.0f/PIXELS_PER_METREf; barOrigin *= 1.0f/PIXELS_PER_METREf;; SetAsBox( bar.Length()/2.0f, 0.1f, 0.5f*bar + barOrigin, vec2Angle( bar )); // SetAsBox( bar.Length()/2.0f+b2_toiSlop, b2_toiSlop*2.0f, // 0.5f*bar + barOrigin, vec2Angle( bar )); friction = 0.3f; if ( attr & ATTRIB_GROUND ) { density = 0.0f; } else if ( attr & ATTRIB_GOAL ) { density = 100.0f; } else if ( attr & ATTRIB_TOKEN ) { density = 3.0f; friction = 0.1f; } else { density = 5.0f; } restitution = 0.2f; }
void b2PolygonShape::Set(const b2Vec2* vertices, int32 count) { b2Assert(3 <= count && count <= b2_maxPolygonVertices); if (count < 3) { SetAsBox(1.0f, 1.0f); return; } int32 n = b2Min(count, b2_maxPolygonVertices); // Perform welding and copy vertices into local buffer. b2Vec2 ps[b2_maxPolygonVertices]; int32 tempCount = 0; for (int32 i = 0; i < n; ++i) { b2Vec2 v = vertices[i]; bool unique = true; for (int32 j = 0; j < tempCount; ++j) { if (b2DistanceSquared(v, ps[j]) < 0.5f * b2_linearSlop) { unique = false; break; } } if (unique) { ps[tempCount++] = v; } } n = tempCount; if (n < 3) { // Polygon is degenerate. b2Assert(false); SetAsBox(1.0f, 1.0f); return; } // Create the convex hull using the Gift wrapping algorithm // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm // Find the right most point on the hull int32 i0 = 0; float32 x0 = ps[0].x; for (int32 i = 1; i < n; ++i) { float32 x = ps[i].x; if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) { i0 = i; x0 = x; } } int32 hull[b2_maxPolygonVertices]; int32 m = 0; int32 ih = i0; for (;;) { hull[m] = ih; int32 ie = 0; for (int32 j = 1; j < n; ++j) { if (ie == ih) { ie = j; continue; } b2Vec2 r = ps[ie] - ps[hull[m]]; b2Vec2 v = ps[j] - ps[hull[m]]; float32 c = b2Cross(r, v); if (c < 0.0f) { ie = j; } // Collinearity check if (c == 0.0f && v.LengthSquared() > r.LengthSquared()) { ie = j; } } ++m; ih = ie; if (ie == i0) { break; } } if (m < 3) { // Polygon is degenerate. b2Assert(false); SetAsBox(1.0f, 1.0f); return; } m_count = m; // Copy vertices. for (int32 i = 0; i < m; ++i) { m_vertices[i] = ps[hull[i]]; } // Compute normals. Ensure the edges have non-zero length. for (int32 i = 0; i < m; ++i) { int32 i1 = i; int32 i2 = i + 1 < m ? i + 1 : 0; b2Vec2 edge = m_vertices[i2] - m_vertices[i1]; b2Assert(edge.LengthSquared() > b2_epsilon * b2_epsilon); m_normals[i] = b2Cross(edge, 1.0f); m_normals[i].Normalize(); } // Compute the polygon centroid. m_centroid = ComputeCentroid(m_vertices, m); }
void b2PolygonShape::Set(const b2Vec2* vertices, int32 count) { if (count < 3) { SetAsBox(0.01f, 0.01f); return; } count = b2Min(count, b2_maxPolygonVertices); // Copy vertices into local buffer b2Vec2 ps[b2_maxPolygonVertices]; for (int32 i = 0; i < count; ++i) { ps[i] = vertices[i]; } // Create the convex hull using the Gift wrapping algorithm // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm // Find the right most point on the hull int32 i0 = 0; float32 x0 = ps[0].x; for (int32 i = 1; i < count; ++i) { float32 x = ps[i].x; if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) { i0 = i; x0 = x; } } int32 hull[b2_maxPolygonVertices]; int32 m = 0; int32 ih = i0; for (;;) { hull[m] = ih; int32 ie = 0; for (int32 j = 1; j < count; ++j) { if (ie == ih) { ie = j; continue; } b2Vec2 r = ps[ie] - ps[hull[m]]; b2Vec2 v = ps[j] - ps[hull[m]]; float32 c = b2Cross(r, v); if (c < 0.0f) { ie = j; } // Collinearity check if (c == 0.0f && v.LengthSquared() > r.LengthSquared()) { ie = j; } } ++m; ih = ie; if (ie == i0) { break; } } if (m < 3) { SetAsBox(0.01f, 0.01f); return; } m_count = m; // Copy vertices. for (int32 i = 0; i < m; ++i) { m_vertices[i] = ps[hull[i]]; } // Compute normals. Ensure the edges have non-zero length. for (int32 i = 0; i < m; ++i) { int32 i1 = i; int32 i2 = i + 1 < m ? i + 1 : 0; b2Vec2 edge = m_vertices[i2] - m_vertices[i1]; b2Assert(edge.LengthSquared() > b2_epsilon * b2_epsilon); m_normals[i] = b2Cross(edge, 1.0f); m_normals[i].Normalize(); } // Compute the polygon centroid. m_centroid = ComputeCentroid(m_vertices, m); }
App::App() : world(b2Vec2(0, -9.8)), playerBodiesPoints(playerBodiesCount), playerBodies(playerBodiesCount), playerBodiesShapes(playerBodiesCount), playerEyesBodiesPoints(playerEyesCount), playerEyesBodies(playerEyesCount), playerEyesBodiesShapes(playerEyesCount), borderShapes(4), borderBodies(4) { world.SetDebugDraw(new graphics::DebugDraw); for (auto i = 0; i < 4; ++i) { const auto shape = new b2PolygonShape; shape->SetAsBox(100, 3); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = borderCategory; const auto fixt = body->CreateFixture(&fdef); borderShapes[i].reset(shape); borderBodies[i] = body; } for (auto i = 0; i < playerBodiesCount; ++i) { const auto shape = new b2PolygonShape; shape->SetAsBox(playerBodySize, playerBodySize); b2BodyDef bdef; bdef.position.x = 0 + rand() % 50; bdef.position.y = 15 + rand() % 30; bdef.type = b2_dynamicBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.density = playerBodyDencity; fdef.friction = playerBodyFriction; fdef.restitution = playerBodyRestitution; fdef.filter.categoryBits = playerBodyCategory; const auto fixt = body->CreateFixture(&fdef); playerBodiesShapes[i].reset(shape); playerBodies[i] = body; playerBodiesPoints[i] = bdef.position; playerBodiesFallen[body] = false; } for (auto i = 0; i < playerEyesCount; ++i) { const auto shape = new b2CircleShape; shape->m_radius = playerEyeSize; b2BodyDef bdef; bdef.position.x = 0 + rand() % 50; bdef.position.y = 15 + rand() % 30; bdef.type = b2_dynamicBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.density = playerEyeDencity; fdef.friction = playerEyeFriction; fdef.restitution = playerEyeRestitution; fdef.filter.categoryBits = playerEyeCategory; const auto fixt = body->CreateFixture(&fdef); playerEyesBodiesShapes[i].reset(shape); playerEyesBodies[i] = body; playerEyesBodiesPoints[i] = bdef.position; } { const auto shape = new b2PolygonShape; shape->SetAsBox(2, 13); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = borderCategory; const auto fixt = body->CreateFixture(&fdef); topObstacleShape.reset(shape); topObstacle = body; } { const auto shape = new b2PolygonShape; shape->SetAsBox(2, 5); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = borderCategory; const auto fixt = body->CreateFixture(&fdef); bottomObstacleShape.reset(shape); bottomObstacle = body; } { const auto shape = new b2PolygonShape; shape->SetAsBox(2, 7); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = borderCategory; const auto fixt = body->CreateFixture(&fdef); bottomHighObstacleShape.reset(shape); bottomHighObstacle = body; } { const auto shape = new b2PolygonShape; shape->SetAsBox(12.5, 2); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; bdef.position.x = -100.0f; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = holeCategory; const auto fixt = body->CreateFixture(&fdef); holeTopShape.reset(shape); holeTopBody = body; } { const auto shape = new b2PolygonShape; shape->SetAsBox(12.5, 2); b2BodyDef bdef; bdef.awake = false; bdef.allowSleep = true; bdef.active = true; bdef.type = b2_staticBody; bdef.position.x = -100.0f; const auto body = world.CreateBody(&bdef); b2FixtureDef fdef; fdef.shape = shape; fdef.friction = borderFriction; fdef.restitution = borderRestitution; fdef.filter.categoryBits = holeCategory; const auto fixt = body->CreateFixture(&fdef); holeBottomShape.reset(shape); holeBottomBody = body; } ComputePoints(); RespawnBorders(true); }