diff --git a/box2dbody.cpp b/box2dbody.cpp index bc13e9a0..76c0a841 100644 --- a/box2dbody.cpp +++ b/box2dbody.cpp @@ -61,7 +61,7 @@ Box2DBody::Box2DBody(QQuickItem *parent) : Box2DBody::~Box2DBody() { if (mBody) - mWorld->DestroyBody(mBody); + mWorld->world().DestroyBody(mBody); } void Box2DBody::setLinearDamping(float linearDamping) @@ -242,10 +242,10 @@ void Box2DBody::addFixture(Box2DFixture *fixture) fixture->setParentItem(this); mFixtures.append(fixture); if (mBody) - fixture->initialize(mBody); + fixture->initialize(this); } -void Box2DBody::initialize(b2World *world) +void Box2DBody::initialize(Box2DWorld *world) { mWorld = world; if (!isComponentComplete()) { @@ -255,12 +255,12 @@ void Box2DBody::initialize(b2World *world) mInitializePending = true; return; } - mBodyDef.position = toMeters(position()); + mBodyDef.position = mWorld->toMeters(position()); mBodyDef.angle = toRadians(rotation()); - mBody = world->CreateBody(&mBodyDef); + mBody = mWorld->world().CreateBody(&mBodyDef); mInitializePending = false; foreach (Box2DFixture *fixture, mFixtures) - fixture->initialize(mBody); + fixture->initialize(this); emit bodyCreated(); } @@ -273,7 +273,7 @@ void Box2DBody::synchronize() mSynchronizing = true; if (sync(mBodyDef.position, mBody->GetPosition())) { - setPosition(toPixels(mBodyDef.position)); + setPosition(mWorld->toPixels(mBodyDef.position)); emit positionChanged(); } @@ -296,7 +296,7 @@ void Box2DBody::geometryChanged(const QRectF &newGeometry, { if (!mSynchronizing && mBody) { if (newGeometry.topLeft() != oldGeometry.topLeft()) { - mBodyDef.position = toMeters(newGeometry.topLeft()); + mBodyDef.position = mWorld->toMeters(newGeometry.topLeft()); mBody->SetTransform(mBodyDef.position, mBodyDef.angle); } } @@ -316,7 +316,7 @@ void Box2DBody::applyLinearImpulse(const QPointF &impulse, const QPointF &point) { if (mBody) - mBody->ApplyLinearImpulse(invertY(impulse), toMeters(point), true); + mBody->ApplyLinearImpulse(invertY(impulse), mWorld->toMeters(point), true); } void Box2DBody::applyAngularImpulse(qreal impulse) @@ -334,14 +334,14 @@ void Box2DBody::applyTorque(qreal torque) QPointF Box2DBody::getWorldCenter() const { if (mBody) - return toPixels(mBody->GetWorldCenter()); + return mWorld->toPixels(mBody->GetWorldCenter()); return QPointF(); } void Box2DBody::applyForce(const QPointF &force, const QPointF &point) { if (mBody) - mBody->ApplyForce(invertY(force), toMeters(point), true); + mBody->ApplyForce(invertY(force), mWorld->toMeters(point), true); } void Box2DBody::applyForceToCenter(const QPointF &force) @@ -369,13 +369,13 @@ float Box2DBody::getInertia() const QPointF Box2DBody::getLinearVelocityFromWorldPoint(const QPointF &point) const { if (mBody) - return invertY(mBody->GetLinearVelocityFromWorldPoint(toMeters(point))); + return invertY(mBody->GetLinearVelocityFromWorldPoint(mWorld->toMeters(point))); return QPointF(); } QPointF Box2DBody::getLinearVelocityFromLocalPoint(const QPointF &point) const { if (mBody) - return invertY(mBody->GetLinearVelocityFromLocalPoint(toMeters(point))); + return invertY(mBody->GetLinearVelocityFromLocalPoint(mWorld->toMeters(point))); return QPointF(); } diff --git a/box2dbody.h b/box2dbody.h index 9e52db3e..26a27fe1 100644 --- a/box2dbody.h +++ b/box2dbody.h @@ -103,7 +103,7 @@ class Box2DBody : public QQuickItem QQmlListProperty fixtures(); - void initialize(b2World *world); + void initialize(Box2DWorld *world); void synchronize(); void nullifyBody(); @@ -122,7 +122,7 @@ class Box2DBody : public QQuickItem void componentComplete(); b2Body *body() const; - b2World *world() const; + Box2DWorld *world() const; protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry); @@ -143,7 +143,7 @@ class Box2DBody : public QQuickItem private: b2Body *mBody; - b2World *mWorld; + Box2DWorld *mWorld; b2BodyDef mBodyDef; bool mSynchronizing; bool mInitializePending; @@ -205,7 +205,7 @@ inline b2Body *Box2DBody::body() const return mBody; } -inline b2World *Box2DBody::world() const +inline Box2DWorld *Box2DBody::world() const { return mWorld; } diff --git a/box2ddebugdraw.cpp b/box2ddebugdraw.cpp index b1b0c733..9bc27929 100644 --- a/box2ddebugdraw.cpp +++ b/box2ddebugdraw.cpp @@ -34,7 +34,7 @@ class DebugDraw : public b2Draw { public: - DebugDraw(QPainter *painter, b2World &world); + DebugDraw(QPainter *painter, Box2DWorld &world); void draw(); @@ -54,11 +54,11 @@ class DebugDraw : public b2Draw private: QPainter *mPainter; - b2World &mWorld; + Box2DWorld &mWorld; qreal mAxisScale; }; -DebugDraw::DebugDraw(QPainter *painter, b2World &world) +DebugDraw::DebugDraw(QPainter *painter, Box2DWorld &world) : mPainter(painter) , mWorld(world) { @@ -66,9 +66,9 @@ DebugDraw::DebugDraw(QPainter *painter, b2World &world) void DebugDraw::draw() { - mWorld.SetDebugDraw(this); - mWorld.DrawDebugData(); - mWorld.SetDebugDraw(0); + mWorld.world().SetDebugDraw(this); + mWorld.world().DrawDebugData(); + mWorld.world().SetDebugDraw(0); } static QColor toQColor(const b2Color &color) @@ -79,13 +79,13 @@ static QColor toQColor(const b2Color &color) color.a * 255); } -static QPolygonF toQPolygonF(const b2Vec2 *vertices, int32 vertexCount) +static QPolygonF toQPolygonF(const Box2DWorld &world, const b2Vec2 *vertices, int32 vertexCount) { QPolygonF polygon; polygon.reserve(vertexCount); for (int i = 0; i < vertexCount; ++i) - polygon.append(toPixels(vertices[i])); + polygon.append(world.toPixels(vertices[i])); return polygon; } @@ -95,7 +95,7 @@ void DebugDraw::DrawPolygon(const b2Vec2 *vertices, int32 vertexCount, { mPainter->setPen(toQColor(color)); mPainter->setBrush(Qt::NoBrush); - mPainter->drawPolygon(toQPolygonF(vertices, vertexCount)); + mPainter->drawPolygon(toQPolygonF(mWorld, vertices, vertexCount)); } void DebugDraw::DrawSolidPolygon(const b2Vec2 *vertices, int32 vertexCount, @@ -103,7 +103,7 @@ void DebugDraw::DrawSolidPolygon(const b2Vec2 *vertices, int32 vertexCount, { mPainter->setPen(Qt::NoPen); mPainter->setBrush(toQColor(color)); - mPainter->drawPolygon(toQPolygonF(vertices, vertexCount)); + mPainter->drawPolygon(toQPolygonF(mWorld, vertices, vertexCount)); } void DebugDraw::DrawCircle(const b2Vec2 ¢er, float32 radius, @@ -111,9 +111,9 @@ void DebugDraw::DrawCircle(const b2Vec2 ¢er, float32 radius, { mPainter->setPen(toQColor(color)); mPainter->setBrush(Qt::NoBrush); - mPainter->drawEllipse(toPixels(center), - toPixels(radius), - toPixels(radius)); + mPainter->drawEllipse(mWorld.toPixels(center), + mWorld.toPixels(radius), + mWorld.toPixels(radius)); } void DebugDraw::DrawSolidCircle(const b2Vec2 ¢er, float32 radius, @@ -121,11 +121,11 @@ void DebugDraw::DrawSolidCircle(const b2Vec2 ¢er, float32 radius, { mPainter->setPen(Qt::NoPen); mPainter->setBrush(toQColor(color)); - QPointF p1 = toPixels(center); - QPointF p2 = toPixels(axis); + QPointF p1 = mWorld.toPixels(center); + QPointF p2 = mWorld.toPixels(axis); mPainter->drawEllipse(p1, - toPixels(radius), - toPixels(radius)); + mWorld.toPixels(radius), + mWorld.toPixels(radius)); mPainter->setPen(qRgb(200, 64, 0)); p2.setX(p1.x() + radius * p2.x()); p2.setY(p1.y() + radius * p2.y()); @@ -136,20 +136,20 @@ void DebugDraw::DrawSegment(const b2Vec2 &p1, const b2Vec2 &p2, const b2Color &color) { mPainter->setPen(toQColor(color)); - mPainter->drawLine(toPixels(p1), toPixels(p2)); + mPainter->drawLine(mWorld.toPixels(p1), mWorld.toPixels(p2)); } void DebugDraw::DrawTransform(const b2Transform &xf) { - QPointF p1 = toPixels(xf.p); - QPointF p2 = toPixels(xf.q.GetXAxis()); + QPointF p1 = mWorld.toPixels(xf.p); + QPointF p2 = mWorld.toPixels(xf.q.GetXAxis()); p2 = QPointF(p1.x() + mAxisScale * p2.x(), p1.y() + mAxisScale * p2.y()); mPainter->setPen(Qt::blue); // X axis mPainter->drawLine(p1,p2); - p2 = toPixels(xf.q.GetYAxis()); + p2 = mWorld.toPixels(xf.q.GetYAxis()); p2 = QPointF(p1.x() + mAxisScale * p2.x(), p1.y() + mAxisScale * p2.y()); @@ -210,7 +210,7 @@ void Box2DDebugDraw::paint(QPainter *p) if (!mWorld) return; - DebugDraw debugDraw(p, mWorld->world()); + DebugDraw debugDraw(p, *mWorld); debugDraw.SetFlags(mFlags); debugDraw.setAxisScale(mAxisScale); debugDraw.draw(); diff --git a/box2ddistancejoint.cpp b/box2ddistancejoint.cpp index 825632fd..f4ba2710 100644 --- a/box2ddistancejoint.cpp +++ b/box2ddistancejoint.cpp @@ -36,15 +36,15 @@ Box2DDistanceJoint::Box2DDistanceJoint(QObject *parent) : float Box2DDistanceJoint::length() const { - return toPixels(mDistanceJointDef.length); + return world()->toPixels(mDistanceJointDef.length); } void Box2DDistanceJoint::setLength(float length) { - if (mDistanceJointDef.length == toMeters(length)) + if (mDistanceJointDef.length == world()->toMeters(length)) return; - mDistanceJointDef.length = toMeters(length); + mDistanceJointDef.length = world()->toMeters(length); if (distanceJoint()) distanceJoint()->SetLength(mDistanceJointDef.length); emit lengthChanged(); @@ -74,24 +74,24 @@ void Box2DDistanceJoint::setDampingRatio(float dampingRatio) QPointF Box2DDistanceJoint::localAnchorA() const { - return toPixels(mDistanceJointDef.localAnchorA); + return world()->toPixels(mDistanceJointDef.localAnchorA); } void Box2DDistanceJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mDistanceJointDef.localAnchorA = toMeters(localAnchorA); + mDistanceJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorBChanged(); } QPointF Box2DDistanceJoint::localAnchorB() const { - return toPixels(mDistanceJointDef.localAnchorB); + return world()->toPixels(mDistanceJointDef.localAnchorB); } void Box2DDistanceJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mDistanceJointDef.localAnchorB = toMeters(localAnchorB); + mDistanceJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } @@ -105,7 +105,7 @@ b2Joint *Box2DDistanceJoint::createJoint() mDistanceJointDef.bodyB->GetWorldCenter()); } - return world()->CreateJoint(&mDistanceJointDef); + return world()->world().CreateJoint(&mDistanceJointDef); } QPointF Box2DDistanceJoint::getReactionForce(float32 inv_dt) const diff --git a/box2dfixture.cpp b/box2dfixture.cpp index 4c09abbf..aacfed0f 100644 --- a/box2dfixture.cpp +++ b/box2dfixture.cpp @@ -157,21 +157,21 @@ void Box2DFixture::setGroupIndex(int groupIndex) emit groupIndexChanged(); } -void Box2DFixture::initialize(b2Body *body) +void Box2DFixture::initialize(Box2DBody *body) { + mBody = body; b2Shape *shape = createShape(); if (!shape) return; mFixtureDef.shape = shape; - mFixture = body->CreateFixture(&mFixtureDef); - mBody = body; + mFixture = body->body()->CreateFixture(&mFixtureDef); delete shape; } Box2DBody *Box2DFixture::getBody() const { - return mBody ? toBox2DBody(mBody) : 0; + return mBody; } void Box2DFixture::recreateFixture() @@ -179,7 +179,7 @@ void Box2DFixture::recreateFixture() if (!mBody) return; if (mFixture) - mBody->DestroyFixture(mFixture); + mBody->body()->DestroyFixture(mFixture); initialize(mBody); } @@ -200,9 +200,9 @@ b2Shape *Box2DBox::createShape() y() + halfHeight); b2PolygonShape *shape = new b2PolygonShape; - shape->SetAsBox(toMeters(halfWidth), - toMeters(halfHeight), - toMeters(center), + shape->SetAsBox(mBody->world()->toMeters(halfWidth), + mBody->world()->toMeters(halfHeight), + mBody->world()->toMeters(center), toRadians(rotation())); return shape; @@ -231,8 +231,8 @@ b2Shape *Box2DCircle::createShape() { b2CircleShape *shape = new b2CircleShape; - shape->m_radius = toMeters(mRadius); - shape->m_p = toMeters(position() + QPointF(mRadius, mRadius)); + shape->m_radius = mBody->world()->toMeters(mRadius); + shape->m_p = mBody->world()->toMeters(position() + QPointF(mRadius, mRadius)); return shape; } @@ -260,7 +260,7 @@ b2Shape *Box2DPolygon::createShape() QScopedArrayPointer vertices(new b2Vec2[count]); for (int i = 0; i < count; ++i) { - vertices[i] = toMeters(mVertices.at(i).toPointF()); + vertices[i] = mBody->world()->toMeters(mVertices.at(i).toPointF()); if (i > 0) { if (b2DistanceSquared(vertices[i - 1], vertices[i]) <= b2_linearSlop * b2_linearSlop) { @@ -340,7 +340,7 @@ b2Shape *Box2DChain::createShape() QScopedArrayPointer vertices(new b2Vec2[count]); for (int i = 0; i < count; ++i) { - vertices[i] = toMeters(mVertices.at(i).toPointF()); + vertices[i] = mBody->world()->toMeters(mVertices.at(i).toPointF()); if (i > 0) { if (b2DistanceSquared(vertices[i - 1], vertices[i]) <= b2_linearSlop * b2_linearSlop) { @@ -357,9 +357,9 @@ b2Shape *Box2DChain::createShape() shape->CreateChain(vertices.data(), count); if (mPrevVertexFlag) - shape->SetPrevVertex(toMeters(mPrevVertex)); + shape->SetPrevVertex(mBody->world()->toMeters(mPrevVertex)); if (mNextVertexFlag) - shape->SetNextVertex(toMeters(mNextVertex)); + shape->SetNextVertex(mBody->world()->toMeters(mNextVertex)); } return shape; @@ -384,8 +384,8 @@ b2Shape *Box2DEdge::createShape() qWarning() << "Edge: Invalid number of vertices:" << count; return 0; } - const b2Vec2 vertex1 = toMeters(mVertices.at(0).toPointF()); - const b2Vec2 vertex2 = toMeters(mVertices.at(1).toPointF()); + const b2Vec2 vertex1 = mBody->world()->toMeters(mVertices.at(0).toPointF()); + const b2Vec2 vertex2 = mBody->world()->toMeters(mVertices.at(1).toPointF()); if (b2DistanceSquared(vertex1, vertex2) <= b2_linearSlop * b2_linearSlop) { qWarning() << "Edge: vertices are too close together"; return 0; diff --git a/box2dfixture.h b/box2dfixture.h index 4728ef62..9122dcd6 100644 --- a/box2dfixture.h +++ b/box2dfixture.h @@ -82,7 +82,7 @@ class Box2DFixture : public QQuickItem int groupIndex() const; void setGroupIndex(int groupIndex); - void initialize(b2Body *body); + void initialize(Box2DBody *body); Q_INVOKABLE Box2DBody *getBody() const; @@ -105,7 +105,7 @@ class Box2DFixture : public QQuickItem b2Fixture *mFixture; b2FixtureDef mFixtureDef; - b2Body *mBody; + Box2DBody *mBody; }; Q_DECLARE_OPERATORS_FOR_FLAGS(Box2DFixture::CategoryFlags) diff --git a/box2dfrictionjoint.cpp b/box2dfrictionjoint.cpp index 41dfc61f..d2222c77 100644 --- a/box2dfrictionjoint.cpp +++ b/box2dfrictionjoint.cpp @@ -67,13 +67,13 @@ void Box2DFrictionJoint::setMaxTorque(float maxTorque) QPointF Box2DFrictionJoint::localAnchorA() const { if (frictionJoint()) - return toPixels(frictionJoint()->GetAnchorA()); - return toPixels(mFrictionJointDef.localAnchorA); + return world()->toPixels(frictionJoint()->GetAnchorA()); + return world()->toPixels(mFrictionJointDef.localAnchorA); } void Box2DFrictionJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mFrictionJointDef.localAnchorA = toMeters(localAnchorA); + mFrictionJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorAChanged(); } @@ -81,13 +81,13 @@ void Box2DFrictionJoint::setLocalAnchorA(const QPointF &localAnchorA) QPointF Box2DFrictionJoint::localAnchorB() const { if (frictionJoint()) - return toPixels(frictionJoint()->GetAnchorB()); - return toPixels(mFrictionJointDef.localAnchorB); + return world()->toPixels(frictionJoint()->GetAnchorB()); + return world()->toPixels(mFrictionJointDef.localAnchorB); } void Box2DFrictionJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mFrictionJointDef.localAnchorB = toMeters(localAnchorB); + mFrictionJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } @@ -100,7 +100,7 @@ b2Joint *Box2DFrictionJoint::createJoint() mFrictionJointDef.bodyA->GetWorldCenter()); } - return world()->CreateJoint(&mFrictionJointDef); + return world()->world().CreateJoint(&mFrictionJointDef); } QPointF Box2DFrictionJoint::getReactionForce(float32 inv_dt) const diff --git a/box2dgearjoint.cpp b/box2dgearjoint.cpp index 8f4ac614..29310244 100644 --- a/box2dgearjoint.cpp +++ b/box2dgearjoint.cpp @@ -109,7 +109,7 @@ b2Joint *Box2DGearJoint::createJoint() mGearJointDef.joint1 = mJoint1->joint(); mGearJointDef.joint2 = mJoint2->joint(); - return world()->CreateJoint(&mGearJointDef); + return world()->world().CreateJoint(&mGearJointDef); } void Box2DGearJoint::joint1Created() diff --git a/box2djoint.cpp b/box2djoint.cpp index 2e0fddab..6f9f8ad2 100644 --- a/box2djoint.cpp +++ b/box2djoint.cpp @@ -24,7 +24,7 @@ */ #include "box2djoint.h" - +#include "box2dworld.h" #include "box2dbody.h" Box2DJoint::Box2DJoint(b2JointDef &jointDef, QObject *parent) : @@ -42,7 +42,7 @@ Box2DJoint::Box2DJoint(b2JointDef &jointDef, QObject *parent) : Box2DJoint::~Box2DJoint() { if (mJoint) - mWorld->DestroyJoint(mJoint); + mWorld->world().DestroyJoint(mJoint); } void Box2DJoint::setCollideConnected(bool collideConnected) @@ -96,7 +96,7 @@ void Box2DJoint::initialize() // Destroy any previously created joint if (mJoint) { - mWorld->DestroyJoint(mJoint); + mWorld->world().DestroyJoint(mJoint); mJoint = 0; mWorld = 0; } diff --git a/box2djoint.h b/box2djoint.h index fffea5a7..6fe1ec10 100644 --- a/box2djoint.h +++ b/box2djoint.h @@ -81,7 +81,7 @@ class Box2DJoint : public QObject, public QQmlParserStatus void initialize(); void nullifyJoint(); - b2World *world() const; + Box2DWorld *world() const; b2Joint *joint() const; // QQmlParserStatus interface @@ -107,7 +107,7 @@ private slots: bool mInitializePending; Box2DBody *mBodyA; Box2DBody *mBodyB; - b2World *mWorld; + Box2DWorld *mWorld; b2Joint *mJoint; }; @@ -136,7 +136,7 @@ inline void Box2DJoint::nullifyJoint() mJoint = 0; } -inline b2World *Box2DJoint::world() const +inline Box2DWorld *Box2DJoint::world() const { return mWorld; } diff --git a/box2dmotorjoint.cpp b/box2dmotorjoint.cpp index 276d9ae1..079570ce 100644 --- a/box2dmotorjoint.cpp +++ b/box2dmotorjoint.cpp @@ -36,12 +36,12 @@ Box2DMotorJoint::Box2DMotorJoint(QObject *parent) : QPointF Box2DMotorJoint::linearOffset() const { - return toPixels(mMotorJointDef.linearOffset); + return world()->toPixels(mMotorJointDef.linearOffset); } void Box2DMotorJoint::setLinearOffset(const QPointF &linearOffset) { - const b2Vec2 linearOffsetMeters = toMeters(linearOffset); + const b2Vec2 linearOffsetMeters = world()->toMeters(linearOffset); if (mMotorJointDef.linearOffset == linearOffsetMeters) return; @@ -105,5 +105,5 @@ b2Joint *Box2DMotorJoint::createJoint() { mMotorJointDef.Initialize(bodyA()->body(), bodyB()->body()); - return world()->CreateJoint(&mMotorJointDef); + return world()->world().CreateJoint(&mMotorJointDef); } diff --git a/box2dmousejoint.cpp b/box2dmousejoint.cpp index 19619597..5cd4bc4e 100644 --- a/box2dmousejoint.cpp +++ b/box2dmousejoint.cpp @@ -68,12 +68,12 @@ void Box2DMouseJoint::setMaxForce(float maxForce) QPointF Box2DMouseJoint::target() const { - return toPixels(mMouseJointDef.target); + return world()->toPixels(mMouseJointDef.target); } void Box2DMouseJoint::setTarget(const QPointF &target) { - const b2Vec2 targetMeters = toMeters(target); + const b2Vec2 targetMeters = world()->toMeters(target); if (mMouseJointDef.target == targetMeters) return; @@ -85,7 +85,7 @@ void Box2DMouseJoint::setTarget(const QPointF &target) b2Joint *Box2DMouseJoint::createJoint() { - return world()->CreateJoint(&mMouseJointDef); + return world()->world().CreateJoint(&mMouseJointDef); } QPointF Box2DMouseJoint::getReactionForce(float32 inv_dt) const diff --git a/box2dprismaticjoint.cpp b/box2dprismaticjoint.cpp index dbed1f9d..87ca0a73 100644 --- a/box2dprismaticjoint.cpp +++ b/box2dprismaticjoint.cpp @@ -36,12 +36,12 @@ Box2DPrismaticJoint::Box2DPrismaticJoint(QObject *parent) : float Box2DPrismaticJoint::lowerTranslation() const { - return toPixels(mPrismaticJointDef.lowerTranslation); + return world()->toPixels(mPrismaticJointDef.lowerTranslation); } void Box2DPrismaticJoint::setLowerTranslation(float lowerTranslation) { - const float lowerTranslationMeters = toMeters(lowerTranslation); + const float lowerTranslationMeters = world()->toMeters(lowerTranslation); if (mPrismaticJointDef.lowerTranslation == lowerTranslationMeters) return; @@ -54,12 +54,12 @@ void Box2DPrismaticJoint::setLowerTranslation(float lowerTranslation) float Box2DPrismaticJoint::upperTranslation() const { - return toPixels(mPrismaticJointDef.upperTranslation); + return world()->toPixels(mPrismaticJointDef.upperTranslation); } void Box2DPrismaticJoint::setUpperTranslation(float upperTranslation) { - const float upperTranslationMeters = toMeters(upperTranslation); + const float upperTranslationMeters = world()->toMeters(upperTranslation); if (mPrismaticJointDef.upperTranslation == upperTranslationMeters) return; @@ -122,7 +122,7 @@ void Box2DPrismaticJoint::setEnableMotor(bool enableMotor) QPointF Box2DPrismaticJoint::axis() const { - return toPixels(mPrismaticJointDef.localAxisA); + return world()->toPixels(mPrismaticJointDef.localAxisA); } void Box2DPrismaticJoint::setAxis(const QPointF &axis) @@ -134,24 +134,24 @@ void Box2DPrismaticJoint::setAxis(const QPointF &axis) QPointF Box2DPrismaticJoint::localAnchorA() const { - return toPixels(mPrismaticJointDef.localAnchorA); + return world()->toPixels(mPrismaticJointDef.localAnchorA); } QPointF Box2DPrismaticJoint::localAnchorB() const { - return toPixels(mPrismaticJointDef.localAnchorB); + return world()->toPixels(mPrismaticJointDef.localAnchorB); } void Box2DPrismaticJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mPrismaticJointDef.localAnchorA = toMeters(localAnchorA); + mPrismaticJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorAChanged(); } void Box2DPrismaticJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mPrismaticJointDef.localAnchorB = toMeters(localAnchorB); + mPrismaticJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } @@ -167,13 +167,13 @@ b2Joint *Box2DPrismaticJoint::createJoint() mPrismaticJointDef.referenceAngle = 0.0; } - return world()->CreateJoint(&mPrismaticJointDef); + return world()->world().CreateJoint(&mPrismaticJointDef); } float Box2DPrismaticJoint::getJointTranslation() const { if (prismaticJoint()) - return toPixels(prismaticJoint()->GetJointTranslation()); + return world()->toPixels(prismaticJoint()->GetJointTranslation()); return 0.0; } diff --git a/box2dpulleyjoint.cpp b/box2dpulleyjoint.cpp index 33944f2d..abee4d47 100644 --- a/box2dpulleyjoint.cpp +++ b/box2dpulleyjoint.cpp @@ -35,29 +35,29 @@ Box2DPulleyJoint::Box2DPulleyJoint(QObject *parent) : float Box2DPulleyJoint::lengthA() const { - return toPixels(mPulleyJointDef.lengthA); + return world()->toPixels(mPulleyJointDef.lengthA); } void Box2DPulleyJoint::setLengthA(float lengthA) { - if (mPulleyJointDef.lengthA == toMeters(lengthA)) + if (mPulleyJointDef.lengthA == world()->toMeters(lengthA)) return; - mPulleyJointDef.lengthA = toMeters(lengthA); + mPulleyJointDef.lengthA = world()->toMeters(lengthA); emit lengthAChanged(); } float Box2DPulleyJoint::lengthB() const { - return toPixels(mPulleyJointDef.lengthB); + return world()->toPixels(mPulleyJointDef.lengthB); } void Box2DPulleyJoint::setLengthB(float lengthB) { - if (mPulleyJointDef.lengthB == toMeters(lengthB)) + if (mPulleyJointDef.lengthB == world()->toMeters(lengthB)) return; - mPulleyJointDef.lengthB = toMeters(lengthB); + mPulleyJointDef.lengthB = world()->toMeters(lengthB); emit lengthBChanged(); } @@ -72,49 +72,49 @@ void Box2DPulleyJoint::setRatio(float ratio) QPointF Box2DPulleyJoint::groundAnchorA() const { - return toPixels(mPulleyJointDef.groundAnchorA); + return world()->toPixels(mPulleyJointDef.groundAnchorA); } void Box2DPulleyJoint::setGroundAnchorA(const QPointF &groundAnchorA) { - mPulleyJointDef.groundAnchorA = toMeters(groundAnchorA); + mPulleyJointDef.groundAnchorA = world()->toMeters(groundAnchorA); emit groundAnchorAChanged(); } QPointF Box2DPulleyJoint::groundAnchorB() const { - return toPixels(mPulleyJointDef.groundAnchorB); + return world()->toPixels(mPulleyJointDef.groundAnchorB); } void Box2DPulleyJoint::setGroundAnchorB(const QPointF &groundAnchorB) { - mPulleyJointDef.groundAnchorB = toMeters(groundAnchorB); + mPulleyJointDef.groundAnchorB = world()->toMeters(groundAnchorB); emit groundAnchorBChanged(); } QPointF Box2DPulleyJoint::localAnchorA() const { if (pulleyJoint()) - return toPixels(pulleyJoint()->GetAnchorA()); - return toPixels(mPulleyJointDef.localAnchorA); + return world()->toPixels(pulleyJoint()->GetAnchorA()); + return world()->toPixels(mPulleyJointDef.localAnchorA); } void Box2DPulleyJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mPulleyJointDef.localAnchorA = toMeters(localAnchorA); + mPulleyJointDef.localAnchorA = world()->toMeters(localAnchorA); emit localAnchorAChanged(); } QPointF Box2DPulleyJoint::localAnchorB() const { if (pulleyJoint()) - return toPixels(pulleyJoint()->GetAnchorB()); - return toPixels(mPulleyJointDef.localAnchorB); + return world()->toPixels(pulleyJoint()->GetAnchorB()); + return world()->toPixels(mPulleyJointDef.localAnchorB); } void Box2DPulleyJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mPulleyJointDef.localAnchorB = toMeters(localAnchorB); + mPulleyJointDef.localAnchorB = world()->toMeters(localAnchorB); emit localAnchorBChanged(); } @@ -125,20 +125,20 @@ b2Joint *Box2DPulleyJoint::createJoint() return 0; } - return world()->CreateJoint(&mPulleyJointDef); + return world()->world().CreateJoint(&mPulleyJointDef); } float Box2DPulleyJoint::getCurrentLengthA() const { if (pulleyJoint()) - return toPixels(pulleyJoint()->GetCurrentLengthA()); + return world()->toPixels(pulleyJoint()->GetCurrentLengthA()); return lengthA(); } float Box2DPulleyJoint::getCurrentLengthB() const { if (pulleyJoint()) - return toPixels(pulleyJoint()->GetCurrentLengthB()); + return world()->toPixels(pulleyJoint()->GetCurrentLengthB()); return lengthB(); } diff --git a/box2drevolutejoint.cpp b/box2drevolutejoint.cpp index 36f63c9c..7f843982 100644 --- a/box2drevolutejoint.cpp +++ b/box2drevolutejoint.cpp @@ -123,24 +123,24 @@ void Box2DRevoluteJoint::setEnableMotor(bool enableMotor) QPointF Box2DRevoluteJoint::localAnchorA() const { - return toPixels(mRevoluteJointDef.localAnchorA); + return world()->toPixels(mRevoluteJointDef.localAnchorA); } QPointF Box2DRevoluteJoint::localAnchorB() const { - return toPixels(mRevoluteJointDef.localAnchorB); + return world()->toPixels(mRevoluteJointDef.localAnchorB); } void Box2DRevoluteJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mRevoluteJointDef.localAnchorA = toMeters(localAnchorA); + mRevoluteJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorAChanged(); } void Box2DRevoluteJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mRevoluteJointDef.localAnchorB = toMeters(localAnchorB); + mRevoluteJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } @@ -153,7 +153,7 @@ b2Joint *Box2DRevoluteJoint::createJoint() mRevoluteJointDef.bodyA->GetWorldCenter()); } - return world()->CreateJoint(&mRevoluteJointDef); + return world()->world().CreateJoint(&mRevoluteJointDef); } float Box2DRevoluteJoint::getJointAngle() const diff --git a/box2dropejoint.cpp b/box2dropejoint.cpp index b39c01e8..1489866d 100644 --- a/box2dropejoint.cpp +++ b/box2dropejoint.cpp @@ -36,12 +36,12 @@ Box2DRopeJoint::Box2DRopeJoint(QObject *parent) : float Box2DRopeJoint::maxLength() const { - return toPixels(mRopeJointDef.maxLength); + return world()->toPixels(mRopeJointDef.maxLength); } void Box2DRopeJoint::setMaxLength(float maxLength) { - const float maxLengthMeters = toMeters(maxLength); + const float maxLengthMeters = world()->toMeters(maxLength); if (mRopeJointDef.maxLength == maxLengthMeters) return; @@ -53,29 +53,29 @@ void Box2DRopeJoint::setMaxLength(float maxLength) QPointF Box2DRopeJoint::localAnchorA() const { - return toPixels(mRopeJointDef.localAnchorA); + return world()->toPixels(mRopeJointDef.localAnchorA); } void Box2DRopeJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mRopeJointDef.localAnchorA = toMeters(localAnchorA); + mRopeJointDef.localAnchorA = world()->toMeters(localAnchorA); emit localAnchorAChanged(); } QPointF Box2DRopeJoint::localAnchorB() const { - return toPixels(mRopeJointDef.localAnchorB); + return world()->toPixels(mRopeJointDef.localAnchorB); } void Box2DRopeJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mRopeJointDef.localAnchorB = toMeters(localAnchorB); + mRopeJointDef.localAnchorB = world()->toMeters(localAnchorB); emit localAnchorBChanged(); } b2Joint *Box2DRopeJoint::createJoint() { - return world()->CreateJoint(&mRopeJointDef); + return world()->world().CreateJoint(&mRopeJointDef); } QPointF Box2DRopeJoint::getReactionForce(float32 inv_dt) const diff --git a/box2dweldjoint.cpp b/box2dweldjoint.cpp index 38c83ab3..960d455d 100644 --- a/box2dweldjoint.cpp +++ b/box2dweldjoint.cpp @@ -73,24 +73,24 @@ void Box2DWeldJoint::setDampingRatio(float dampingRatio) QPointF Box2DWeldJoint::localAnchorA() const { - return toPixels(mWeldJointDef.localAnchorA); + return world()->toPixels(mWeldJointDef.localAnchorA); } void Box2DWeldJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mWeldJointDef.localAnchorA = toMeters(localAnchorA); + mWeldJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorAChanged(); } QPointF Box2DWeldJoint::localAnchorB() const { - return toPixels(mWeldJointDef.localAnchorB); + return world()->toPixels(mWeldJointDef.localAnchorB); } void Box2DWeldJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mWeldJointDef.localAnchorB = toMeters(localAnchorB); + mWeldJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } @@ -103,5 +103,5 @@ b2Joint *Box2DWeldJoint::createJoint() mWeldJointDef.bodyA->GetWorldCenter()); } - return world()->CreateJoint(&mWeldJointDef); + return world()->world().CreateJoint(&mWeldJointDef); } diff --git a/box2dwheeljoint.cpp b/box2dwheeljoint.cpp index ef5d708e..3c222aaa 100644 --- a/box2dwheeljoint.cpp +++ b/box2dwheeljoint.cpp @@ -98,36 +98,36 @@ void Box2DWheelJoint::setEnableMotor(bool enableMotor) QPointF Box2DWheelJoint::localAnchorA() const { - return toPixels(mWheelJointDef.localAnchorA); + return world()->toPixels(mWheelJointDef.localAnchorA); } void Box2DWheelJoint::setLocalAnchorA(const QPointF &localAnchorA) { - mWheelJointDef.localAnchorA = toMeters(localAnchorA); + mWheelJointDef.localAnchorA = world()->toMeters(localAnchorA); mAnchorsAuto = false; emit localAnchorAChanged(); } QPointF Box2DWheelJoint::localAnchorB() const { - return toPixels(mWheelJointDef.localAnchorB); + return world()->toPixels(mWheelJointDef.localAnchorB); } void Box2DWheelJoint::setLocalAnchorB(const QPointF &localAnchorB) { - mWheelJointDef.localAnchorB = toMeters(localAnchorB); + mWheelJointDef.localAnchorB = world()->toMeters(localAnchorB); mAnchorsAuto = false; emit localAnchorBChanged(); } QPointF Box2DWheelJoint::localAxisA() const { - return toPixels(mWheelJointDef.localAxisA); + return world()->toPixels(mWheelJointDef.localAxisA); } void Box2DWheelJoint::setLocalAxisA(const QPointF &localAxisA) { - mWheelJointDef.localAxisA = toMeters(localAxisA); + mWheelJointDef.localAxisA = world()->toMeters(localAxisA); mAnchorsAuto = false; emit localAxisAChanged(); } @@ -141,13 +141,13 @@ b2Joint *Box2DWheelJoint::createJoint() mWheelJointDef.localAxisA); } - return world()->CreateJoint(&mWheelJointDef); + return world()->world().CreateJoint(&mWheelJointDef); } float Box2DWheelJoint::getJointTranslation() const { if (wheelJoint()) - return toPixels(wheelJoint()->GetJointTranslation()); + return world()->toPixels(wheelJoint()->GetJointTranslation()); return 0; } diff --git a/box2dworld.cpp b/box2dworld.cpp index ae00e707..b659413b 100644 --- a/box2dworld.cpp +++ b/box2dworld.cpp @@ -119,7 +119,6 @@ void ContactListener::PostSolve(b2Contact *contact, const b2ContactImpulse *impu emit mWorld->postSolve(&mContact); } - Box2DWorld::Box2DWorld(QQuickItem *parent) : QQuickItem(parent), mWorld(b2Vec2(0.0f, -10.0f)), @@ -129,7 +128,8 @@ Box2DWorld::Box2DWorld(QQuickItem *parent) : mPositionIterations(3), mIsRunning(true), mStepDriver(new StepDriver(this)), - mProfile(new Box2DProfile(&mWorld, this)) + mProfile(new Box2DProfile(&mWorld, this)), + mPixelsPerMeter(32.0f) { mWorld.SetContactListener(mContactListener); mWorld.SetDestructionListener(this); @@ -213,6 +213,19 @@ void Box2DWorld::setAutoClearForces(bool autoClearForces) emit autoClearForcesChanged(); } +void Box2DWorld::setPixelsPerMeter(float pixelsPerMeter) +{ + if (pixelsPerMeter <= 0.0f) { + qWarning("World: pixelsPerMeter must be > 0.0f"); + return; + } + + if (mPixelsPerMeter != pixelsPerMeter) { + mPixelsPerMeter = pixelsPerMeter; + pixelsPerMeterChanged(); + } +} + void Box2DWorld::componentComplete() { QQuickItem::componentComplete(); @@ -286,7 +299,7 @@ void Box2DWorld::itemChange(ItemChange change, const ItemChangeData &value) if (change == ItemChildAddedChange) { QObject *child = value.item; if (Box2DBody *body = dynamic_cast(child)) - body->initialize(&mWorld); + body->initialize(this); } } @@ -297,7 +310,7 @@ void Box2DWorld::initializeBodies(QQuickItem *parent) { foreach (QQuickItem *item, parent->childItems()) { if (Box2DBody *body = dynamic_cast(item)) - body->initialize(&mWorld); + body->initialize(this); initializeBodies(item); } diff --git a/box2dworld.h b/box2dworld.h index 5d1cd611..a03a1026 100644 --- a/box2dworld.h +++ b/box2dworld.h @@ -40,13 +40,6 @@ class Box2DWorld; class ContactListener; class StepDriver; -// TODO: Maybe turn this into a property of the world, though it can't be -// changed dynamically. -static const float pixelsPerMeter = 32.0f; // 32 pixels in one meter -static const float metersPerPixel = 1.0f / pixelsPerMeter; -static const float pixelsPerMeterY = -pixelsPerMeter; // Y-axis inverted -static const float metersPerPixelY = -metersPerPixel; - /** * Small utility class to synchronize the stepping with the framerate. */ @@ -117,6 +110,7 @@ class Box2DWorld : public QQuickItem, b2DestructionListener Q_PROPERTY(QPointF gravity READ gravity WRITE setGravity NOTIFY gravityChanged) Q_PROPERTY(bool autoClearForces READ autoClearForces WRITE setAutoClearForces NOTIFY autoClearForcesChanged) Q_PROPERTY(Box2DProfile *profile READ profile NOTIFY stepped) + Q_PROPERTY(float pixelsPerMeter READ pixelsPerMeter WRITE setPixelsPerMeter NOTIFY pixelsPerMeterChanged) public: explicit Box2DWorld(QQuickItem *parent = 0); @@ -142,6 +136,19 @@ class Box2DWorld : public QQuickItem, b2DestructionListener Box2DProfile *profile() const; + float pixelsPerMeter() const; + void setPixelsPerMeter(float pixelsPerMeter); + + float metersPerPixel() const; + float pixelsPerMeterY() const; + float metersPerPixelY() const; + + float toPixels(float length) const; + float toMeters(float length) const; + + QPointF toPixels(const b2Vec2 &vec) const; + b2Vec2 toMeters(const QPointF &point) const; + void componentComplete(); b2World &world(); @@ -165,6 +172,7 @@ class Box2DWorld : public QQuickItem, b2DestructionListener void autoClearForcesChanged(); void runningChanged(); void stepped(); + void pixelsPerMeterChanged(); protected: void itemChange(ItemChange, const ItemChangeData &); @@ -179,6 +187,7 @@ class Box2DWorld : public QQuickItem, b2DestructionListener bool mIsRunning; StepDriver *mStepDriver; Box2DProfile *mProfile; + float mPixelsPerMeter; }; @@ -265,65 +274,85 @@ inline Box2DProfile *Box2DWorld::profile() const return mProfile; } -inline b2World &Box2DWorld::world() +inline float Box2DWorld::pixelsPerMeter() const { - return mWorld; + return mPixelsPerMeter; } -inline void Box2DWorld::clearForces() +inline float Box2DWorld::metersPerPixel() const { - mWorld.ClearForces(); + return 1.0f / pixelsPerMeter(); } +inline float Box2DWorld::pixelsPerMeterY() const +{ + return -pixelsPerMeter(); // Y-axis inverted +} + +inline float Box2DWorld::metersPerPixelY() const +{ + return -metersPerPixel(); +} /** - * Inverts the y-axis as required for forces and velocities. + * Converts lengths from Box2D to QML units. */ -inline QPointF invertY(const b2Vec2 &vec) +inline float Box2DWorld::toPixels(float length) const { - return QPointF(vec.x, -vec.y); + return length * pixelsPerMeter(); } /** - * Inverts the y-axis as required for forces and velocities. + * Converts lengths from QML to Box2D units. */ -inline b2Vec2 invertY(const QPointF &vec) +inline float Box2DWorld::toMeters(float length) const { - return b2Vec2(vec.x(), -vec.y()); + return length * metersPerPixel(); } /** - * Converts lengths from Box2D to QML units. + * Converts positions and sizes from Box2D to QML coordinates. */ -inline float toPixels(float length) +inline QPointF Box2DWorld::toPixels(const b2Vec2 &vec) const { - return length * pixelsPerMeter; + return QPointF(vec.x * pixelsPerMeter(), + vec.y * pixelsPerMeterY()); } /** - * Converts lengths from QML to Box2D units. + * Converts positions and sizes from QML to Box2D coordinates. */ -inline float toMeters(float length) +inline b2Vec2 Box2DWorld::toMeters(const QPointF &point) const +{ + return b2Vec2(point.x() * metersPerPixel(), + point.y() * metersPerPixelY()); +} + +inline b2World &Box2DWorld::world() +{ + return mWorld; +} + +inline void Box2DWorld::clearForces() { - return length * metersPerPixel; + mWorld.ClearForces(); } + /** - * Converts positions and sizes from Box2D to QML coordinates. + * Inverts the y-axis as required for forces and velocities. */ -inline QPointF toPixels(const b2Vec2 &vec) +inline QPointF invertY(const b2Vec2 &vec) { - return QPointF(vec.x * pixelsPerMeter, - vec.y * pixelsPerMeterY); + return QPointF(vec.x, -vec.y); } /** - * Converts positions and sizes from QML to Box2D coordinates. + * Inverts the y-axis as required for forces and velocities. */ -inline b2Vec2 toMeters(const QPointF &point) +inline b2Vec2 invertY(const QPointF &vec) { - return b2Vec2(point.x() * metersPerPixel, - point.y() * metersPerPixelY); + return b2Vec2(vec.x(), -vec.y()); } /**