Skip to content

Commit

Permalink
turn hard-coded 'pixelsPerMeter' into Box2DWorld's property
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Apr 17, 2014
1 parent 71af1a2 commit 8800bda
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 175 deletions.
26 changes: 13 additions & 13 deletions box2dbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Box2DBody::Box2DBody(QQuickItem *parent) :
Box2DBody::~Box2DBody()
{
if (mBody)
mWorld->DestroyBody(mBody);
mWorld->world().DestroyBody(mBody);
}

void Box2DBody::setLinearDamping(float linearDamping)
Expand Down Expand Up @@ -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()) {
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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);
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
8 changes: 4 additions & 4 deletions box2dbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class Box2DBody : public QQuickItem

QQmlListProperty<Box2DFixture> fixtures();

void initialize(b2World *world);
void initialize(Box2DWorld *world);
void synchronize();
void nullifyBody();

Expand All @@ -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);
Expand All @@ -143,7 +143,7 @@ class Box2DBody : public QQuickItem

private:
b2Body *mBody;
b2World *mWorld;
Box2DWorld *mWorld;
b2BodyDef mBodyDef;
bool mSynchronizing;
bool mInitializePending;
Expand Down Expand Up @@ -205,7 +205,7 @@ inline b2Body *Box2DBody::body() const
return mBody;
}

inline b2World *Box2DBody::world() const
inline Box2DWorld *Box2DBody::world() const
{
return mWorld;
}
Expand Down
44 changes: 22 additions & 22 deletions box2ddebugdraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class DebugDraw : public b2Draw
{
public:
DebugDraw(QPainter *painter, b2World &world);
DebugDraw(QPainter *painter, Box2DWorld &world);

void draw();

Expand All @@ -54,21 +54,21 @@ 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)
{
}

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)
Expand All @@ -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;
}
Expand All @@ -95,37 +95,37 @@ 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,
const b2Color &color)
{
mPainter->setPen(Qt::NoPen);
mPainter->setBrush(toQColor(color));
mPainter->drawPolygon(toQPolygonF(vertices, vertexCount));
mPainter->drawPolygon(toQPolygonF(mWorld, vertices, vertexCount));
}

void DebugDraw::DrawCircle(const b2Vec2 &center, float32 radius,
const b2Color &color)
{
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 &center, float32 radius,
const b2Vec2 &axis, const b2Color &color)
{
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());
Expand All @@ -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());

Expand Down Expand Up @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions box2ddistancejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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
Expand Down
Loading

0 comments on commit 8800bda

Please sign in to comment.