b2Shape* Kolf::LineShape::createShape() { b2EdgeShape* shape = new b2EdgeShape; shape->Set( toB2Vec2(m_line.p1() * Kolf::Box2DScaleFactor), toB2Vec2(m_line.p2() * Kolf::Box2DScaleFactor) ); return shape; }
void TiledMap::setPosition(osg::Vec3 position) { transformNode->setPosition(position); for(int i = 0; i < physicsBodies.size(); ++i) physicsBodies[i]->SetTransform(toB2Vec2(position), physicsBodies[i]->GetAngle()); }
b2Shape* Kolf::EllipseShape::createShape() { const b2Vec2 c = toB2Vec2(m_rect.center() * Kolf::Box2DScaleFactor); //ensure some minimum size because b2PolygonShape gets confused when its //area is smaller than FLT_EPSILON (TODO: handle rx*ry == 0 differently?) const qreal rx = qMax(qreal(m_rect.width() * Kolf::Box2DScaleFactor / 2), qreal(1e-5)); const qreal ry = qMax(qreal(m_rect.height() * Kolf::Box2DScaleFactor / 2), qreal(1e-5)); if (rx == ry) { //use circle shape when possible because it's cheaper and exact b2CircleShape* shape = new b2CircleShape; shape->m_p = c; shape->m_radius = rx; return shape; } else { //elliptical shape is not pre-made in Box2D, so create a polygon instead b2PolygonShape* shape = new b2PolygonShape; static const int N = qMin(20, b2_maxPolygonVertices); //increase N if the approximation turns out to be too bad //TODO: calculate the (cos, sin) pairs only once QVarLengthArray<b2Vec2, 20> vertices(N); static const qreal angleStep = 2 * M_PI / N; for (int i = 0; i < N; ++i) { const qreal angle = -i * angleStep; //CCW order as required by Box2D vertices[i].x = c.x + rx * cos(angle); vertices[i].y = c.y + ry * sin(angle); } shape->Set(vertices.data(), N); return shape; } }
b2Shape* Kolf::RectShape::createShape() { b2PolygonShape* shape = new b2PolygonShape; shape->SetAsBox( m_rect.width() * Kolf::Box2DScaleFactor / 2, m_rect.height() * Kolf::Box2DScaleFactor / 2, toB2Vec2(m_rect.center() * Kolf::Box2DScaleFactor), 0 //intrinsic rotation angle ); return shape; }