Skip to content

Commit

Permalink
Added conversion functions and used them everywhere
Browse files Browse the repository at this point in the history
For converting between pixels/meters and radians/degrees. They also do
the inversion of the y-axis and the rotation.

I tried not to change any existing conversion in this commit, but it
still fixes missing y-inversion in Box2DPulleyJoint::localAnchorB and in
all getReactionForce members.
  • Loading branch information
bjorn committed Apr 6, 2014
1 parent 5b94388 commit 7ef291f
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 284 deletions.
82 changes: 29 additions & 53 deletions box2dbody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,17 @@ void Box2DBody::setAwake(bool awake)

QPointF Box2DBody::linearVelocity() const
{
b2Vec2 point;
if (mBody)
point = mBody->GetLinearVelocity();
else
point = mBodyDef.linearVelocity;
return QPointF(point.x * scaleRatio, -point.y * scaleRatio);
return toPixels(mBody->GetLinearVelocity());
return toPixels(mBodyDef.linearVelocity);
}

void Box2DBody::setLinearVelocity(const QPointF &velocity)
{
if (linearVelocity() == velocity)
return;

mBodyDef.linearVelocity = b2Vec2(velocity.x() / scaleRatio,
-velocity.y() / scaleRatio);
mBodyDef.linearVelocity = toMeters(velocity);
if (mBody)
mBody->SetLinearVelocity(mBodyDef.linearVelocity);

Expand All @@ -172,20 +168,16 @@ void Box2DBody::setLinearVelocity(const QPointF &velocity)

float Box2DBody::angularVelocity() const
{
float velocity;
if (mBody)
velocity = mBody->GetAngularVelocity();
else
velocity = mBodyDef.angularVelocity;
return -velocity * 180 / b2_pi;
return toDegrees(mBody->GetAngularVelocity());
return toDegrees(mBodyDef.angularVelocity);
}

void Box2DBody::setAngularVelocity(float velocity)
{
if (angularVelocity() == velocity)
return;

mBodyDef.angularVelocity = -velocity * (b2_pi / 180);
mBodyDef.angularVelocity = toRadians(velocity);
if (mBody)
mBody->SetAngularVelocity(mBodyDef.angularVelocity);

Expand Down Expand Up @@ -243,8 +235,8 @@ void Box2DBody::initialize(b2World *world)
mInitializePending = true;
return;
}
mBodyDef.position.Set(x() / scaleRatio, -y() / scaleRatio);
mBodyDef.angle = rotation() * b2_pi / -180;
mBodyDef.position = toMeters(position());
mBodyDef.angle = toRadians(rotation());
mBody = world->CreateBody(&mBodyDef);
mInitializePending = false;
foreach (Box2DFixture *fixture, mFixtures)
Expand All @@ -263,25 +255,24 @@ void Box2DBody::synchronize()
const b2Vec2 position = mBody->GetPosition();
const float32 angle = mBody->GetAngle();

const qreal newX = position.x * scaleRatio;
const qreal newY = -position.y * scaleRatio;
const qreal newRotation = -(angle * 180.0) / b2_pi;
const QPointF newPosition = toPixels(position);
const qreal newRotation = toDegrees(angle);

bool xChanged = false;
bool yChanged = false;

if (!qFuzzyCompare(x(), newX)) {
setX(newX);
if (!qFuzzyCompare(x(), newPosition.x())) {
setX(newPosition.x());
xChanged = true;
}

if (!qFuzzyCompare(y(), newY)) {
setY(newY);
if (!qFuzzyCompare(y(), newPosition.y())) {
setY(newPosition.y());
yChanged = true;
}

if (xChanged || yChanged)
emit positionChanged(QPointF(newX, newY));
emit positionChanged(newPosition);

if (!qFuzzyCompare(rotation(), newRotation))
setRotation(newRotation);
Expand All @@ -301,9 +292,8 @@ void Box2DBody::geometryChanged(const QRectF &newGeometry,
const QRectF &oldGeometry)
{
if (!mSynchronizing && mBody) {
if (newGeometry.x() != oldGeometry.x() || newGeometry.y() != oldGeometry.y()) {
mBody->SetTransform(b2Vec2(newGeometry.x() / scaleRatio,
-newGeometry.y() / scaleRatio),
if (newGeometry.topLeft() != oldGeometry.topLeft()) {
mBody->SetTransform(toMeters(newGeometry.topLeft()),
mBody->GetAngle());
}
}
Expand All @@ -314,51 +304,43 @@ void Box2DBody::onRotationChanged()
{
if (!mSynchronizing && mBody) {
mBody->SetTransform(mBody->GetPosition(),
rotation() * b2_pi / -180.0);
toRadians(rotation()));
}
}

void Box2DBody::applyLinearImpulse(const QPointF &impulse,
const QPointF &point)
{
if (mBody) {
mBody->ApplyLinearImpulse(b2Vec2(impulse.x() / scaleRatio,
-impulse.y() / scaleRatio),
b2Vec2(point.x() / scaleRatio,
-point.y() / scaleRatio),true);
mBody->ApplyLinearImpulse(toMeters(impulse),
toMeters(point), true);
}
}

void Box2DBody::applyTorque(qreal torque)
{
if (mBody)
mBody->ApplyTorque(torque,true);
mBody->ApplyTorque(torque, true);
}

QPointF Box2DBody::getWorldCenter() const
{
QPointF worldCenter;
if (mBody) {
const b2Vec2 &center = mBody->GetWorldCenter();
worldCenter.setX(center.x * scaleRatio);
worldCenter.setY(-center.y * scaleRatio);
}
return worldCenter;
if (mBody)
return toPixels(mBody->GetWorldCenter());
return QPointF();
}

void Box2DBody::applyForce(const QPointF &force, const QPointF &point)
{
if (mBody) {
mBody->ApplyForce(b2Vec2(force.x() / scaleRatio,
-force.y() / scaleRatio),
b2Vec2(point.x() / scaleRatio,
-point.y() / scaleRatio), true);
mBody->ApplyForce(toMeters(force),
toMeters(point), true);
}
}

float Box2DBody::getMass() const
{
return mBody ? mBody->GetMass() * scaleRatio * scaleRatio : 0.0;
return mBody ? toPixels(toPixels(mBody->GetMass())) : 0.0;
}

void Box2DBody::resetMassData()
Expand All @@ -374,16 +356,10 @@ float Box2DBody::getInertia() const

QPointF Box2DBody::getLinearVelocityFromWorldPoint(const QPointF &point) const
{
const b2Vec2 b2Point = mBody->GetLinearVelocityFromWorldPoint(b2Vec2(point.x() / scaleRatio,
-point.y() / scaleRatio));
return QPointF(b2Point.x * scaleRatio,
-b2Point.y * scaleRatio);
return toPixels(mBody->GetLinearVelocityFromWorldPoint(toMeters(point)));
}

QPointF Box2DBody::getLinearVelocityFromLocalPoint(const QPointF &point) const
{
const b2Vec2 b2Point = mBody->GetLinearVelocityFromLocalPoint(b2Vec2(point.x() / scaleRatio,
-point.y() / scaleRatio));
return QPointF(b2Point.x * scaleRatio,
-b2Point.y * scaleRatio);
return toPixels(mBody->GetLinearVelocityFromLocalPoint(toMeters(point)));
}
4 changes: 2 additions & 2 deletions box2dcontact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ void Box2DContact::resetRestitution()

qreal Box2DContact::getTangentSpeed() const
{
return mContact->GetTangentSpeed() * scaleRatio;
return toPixels(mContact->GetTangentSpeed());
}

void Box2DContact::setTangentSpeed(qreal speed)
{
mContact->SetTangentSpeed(speed / scaleRatio);
mContact->SetTangentSpeed(toMeters(speed));
}
32 changes: 13 additions & 19 deletions box2ddebugdraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ void DebugDraw::draw()
mWorld->SetDebugDraw(0);
}

static QPointF toQPointF(const b2Vec2 &vec)
{
return QPointF(vec.x * scaleRatio,
-vec.y * scaleRatio);
}

static QColor toQColor(const b2Color &color)
{
return QColor(color.r * 255,
Expand All @@ -90,7 +84,7 @@ static QPolygonF toQPolygonF(const b2Vec2 *vertices, int32 vertexCount)
polygon.reserve(vertexCount);

for (int i = 0; i < vertexCount; ++i)
polygon.append(toQPointF(vertices[i]));
polygon.append(toPixels(vertices[i]));

return polygon;
}
Expand All @@ -116,22 +110,22 @@ void DebugDraw::DrawCircle(const b2Vec2 &center, float32 radius,
{
mPainter->setPen(toQColor(color));
mPainter->setBrush(Qt::NoBrush);
mPainter->drawEllipse(toQPointF(center),
radius * scaleRatio,
radius * scaleRatio);
mPainter->drawEllipse(toPixels(center),
toPixels(radius),
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 = toQPointF(center);
QPointF p2 = toQPointF(axis);
QPointF p1 = toPixels(center);
QPointF p2 = toPixels(axis);
mPainter->drawEllipse(p1,
radius * scaleRatio,
radius * scaleRatio);
mPainter->setPen(qRgb(200,64,0));
toPixels(radius),
toPixels(radius));
mPainter->setPen(qRgb(200, 64, 0));
p2.setX(p1.x() + radius * p2.x());
p2.setY(p1.y() + radius * p2.y());
mPainter->drawLine(p1,p2);
Expand All @@ -141,20 +135,20 @@ void DebugDraw::DrawSegment(const b2Vec2 &p1, const b2Vec2 &p2,
const b2Color &color)
{
mPainter->setPen(toQColor(color));
mPainter->drawLine(toQPointF(p1), toQPointF(p2));
mPainter->drawLine(toPixels(p1), toPixels(p2));
}

void DebugDraw::DrawTransform(const b2Transform &xf)
{
QPointF p1 = toQPointF(xf.p);
QPointF p2 = toQPointF(xf.q.GetXAxis());
QPointF p1 = toPixels(xf.p);
QPointF p2 = 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 = toQPointF(xf.q.GetYAxis());
p2 = toPixels(xf.q.GetYAxis());
p2 = QPointF(p1.x() + mAxisScale * p2.x(),
p1.y() + mAxisScale * p2.y());

Expand Down
24 changes: 9 additions & 15 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 mDistanceJointDef.length * scaleRatio;
return toPixels(mDistanceJointDef.length);
}

void Box2DDistanceJoint::setLength(float length)
{
if (mDistanceJointDef.length == length / scaleRatio)
if (mDistanceJointDef.length == toMeters(length))
return;

mDistanceJointDef.length = length / scaleRatio;
mDistanceJointDef.length = toMeters(length);
if (distanceJoint())
distanceJoint()->SetLength(mDistanceJointDef.length);
emit lengthChanged();
Expand Down Expand Up @@ -74,28 +74,24 @@ void Box2DDistanceJoint::setDampingRatio(float dampingRatio)

QPointF Box2DDistanceJoint::localAnchorA() const
{
return QPointF(mDistanceJointDef.localAnchorA.x * scaleRatio,
-mDistanceJointDef.localAnchorA.y * scaleRatio);
return toPixels(mDistanceJointDef.localAnchorA);
}

void Box2DDistanceJoint::setLocalAnchorA(const QPointF &localAnchorA)
{
mDistanceJointDef.localAnchorA = b2Vec2(localAnchorA.x() / scaleRatio,
-localAnchorA.y() / scaleRatio);
mDistanceJointDef.localAnchorA = toMeters(localAnchorA);
mAnchorsAuto = false;
emit localAnchorBChanged();
}

QPointF Box2DDistanceJoint::localAnchorB() const
{
return QPointF(mDistanceJointDef.localAnchorB.x * scaleRatio,
-mDistanceJointDef.localAnchorB.y * scaleRatio);
return toPixels(mDistanceJointDef.localAnchorB);
}

void Box2DDistanceJoint::setLocalAnchorB(const QPointF &localAnchorB)
{
mDistanceJointDef.localAnchorB = b2Vec2(localAnchorB.x() / scaleRatio,
-localAnchorB.y() / scaleRatio);
mDistanceJointDef.localAnchorB = toMeters(localAnchorB);
mAnchorsAuto = false;
emit localAnchorBChanged();
}
Expand All @@ -117,10 +113,8 @@ b2Joint *Box2DDistanceJoint::createJoint()

QPointF Box2DDistanceJoint::getReactionForce(float32 inv_dt) const
{
if (distanceJoint()) {
b2Vec2 point = distanceJoint()->GetReactionForce(inv_dt);
return QPointF(point.x * scaleRatio, point.y * scaleRatio);
}
if (distanceJoint())
return toPixels(distanceJoint()->GetReactionForce(inv_dt));
return QPointF();
}

Expand Down
Loading

0 comments on commit 7ef291f

Please sign in to comment.