Skip to content

Commit

Permalink
Cleaned up a lot of duplicate code in the joints
Browse files Browse the repository at this point in the history
All joints stored a pointer to the Box2D joint, which has now been moved
up to the Box2DJoint base class. As a result the cleanup code and part of
the creation code could be moved up as well.

Some common bugs were fixed in the process:

* Missing 'return' in Box2DDistanceJoint::dampingRatio
* Missing 'return' in Box2DFrictionJoint::localAnchorA
* Missing 'return' in Box2DFrictionJoint::localAnchorB
* Missing 'return' in Box2DPulleyJoint::groundAnchorA
* Missing 'return' in Box2DPulleyJoint::groundAnchorB
* Missing 'return' in Box2DPulleyJoint::localAnchorA
* Missing 'return' in Box2DPulleyJoint::localAnchorB
* Missing y-inversion in Box2DFrictionJoint::localAnchorA
* Missing y-inversion in Box2DFrictionJoint::localAnchorB
* Missing y-inversion in Box2DMotorJoint::linearOffset
* Missing y-inversion in Box2DPrismaticJoint::localAnchorA
* Missing y-inversion in Box2DPrismaticJoint::localAnchorB
* Missing y-inversion in Box2DPulleyJoint::groundAnchorA
* Missing y-inversion in Box2DPulleyJoint::groundAnchorB
* Missing y-inversion in Box2DPulleyJoint::localAnchorA
* Missing y-inversion in Box2DPulleyJoint::localAnchorB
* Missing y-inversion in Box2DRevoluteJoint::localAnchorA
* Missing y-inversion in Box2DRevoluteJoint::localAnchorB
* Missing y-inversion in Box2DRopeJoint::localAnchorA
* Missing y-inversion in Box2DRopeJoint::localAnchorB
* Missing y-inversion in Box2DWheelJoint::localAnchorA
* Missing y-inversion in Box2DWheelJoint::localAnchorB
* Missing y-inversion in Box2DWheelJoint::localAxisA
  • Loading branch information
bjorn committed Apr 5, 2014
1 parent a861f31 commit 075ad33
Show file tree
Hide file tree
Showing 24 changed files with 454 additions and 721 deletions.
86 changes: 29 additions & 57 deletions box2ddistancejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,31 @@

Box2DDistanceJoint::Box2DDistanceJoint(QObject *parent) :
Box2DJoint(parent),
mDistanceJoint(0),
anchorsAuto(true)
{
}

Box2DDistanceJoint::~Box2DDistanceJoint()
{
cleanup(world());
}

float Box2DDistanceJoint::length() const
{
if (mDistanceJoint) return mDistanceJoint->GetLength();
if (distanceJoint())
return distanceJoint()->GetLength();
return mDistanceJointDef.length;
}

void Box2DDistanceJoint::setLength(float _length)
{
if (qFuzzyCompare(length(),_length / scaleRatio))
if (qFuzzyCompare(length(), _length / scaleRatio))
return;
mDistanceJointDef.length = _length / scaleRatio;
if (mDistanceJoint)
mDistanceJoint->SetLength(mDistanceJointDef.length);
if (distanceJoint())
distanceJoint()->SetLength(mDistanceJointDef.length);
emit lengthChanged();
}

float Box2DDistanceJoint::frequencyHz() const
{
if (mDistanceJoint) return mDistanceJoint->GetFrequency();
if (distanceJoint())
return distanceJoint()->GetFrequency();
return mDistanceJointDef.frequencyHz;
}

Expand All @@ -67,14 +63,15 @@ void Box2DDistanceJoint::setFrequencyHz(float _frequencyHz)
if (frequencyHz() == _frequencyHz)
return;
mDistanceJointDef.frequencyHz = _frequencyHz;
if (mDistanceJoint)
mDistanceJoint->SetFrequency(_frequencyHz);
if (distanceJoint())
distanceJoint()->SetFrequency(_frequencyHz);
emit frequencyHzChanged();
}

float Box2DDistanceJoint::dampingRatio() const
{
if (mDistanceJoint) mDistanceJoint->GetDampingRatio();
if (distanceJoint())
return distanceJoint()->GetDampingRatio();
return mDistanceJointDef.dampingRatio;
}

Expand All @@ -84,93 +81,68 @@ void Box2DDistanceJoint::setDampingRatio(float _dampingRatio)
return;

mDistanceJointDef.dampingRatio = _dampingRatio;
if (mDistanceJoint)
mDistanceJoint->SetDampingRatio(_dampingRatio);
if (distanceJoint())
distanceJoint()->SetDampingRatio(_dampingRatio);
emit dampingRatioChanged();
}

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

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

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

void Box2DDistanceJoint::setLocalAnchorB(const QPointF &localAnchorB)
{
mDistanceJointDef.localAnchorB = b2Vec2(localAnchorB.x() / scaleRatio,
-localAnchorB.y() / scaleRatio);
-localAnchorB.y() / scaleRatio);
anchorsAuto = false;
emit localAnchorBChanged();
}

void Box2DDistanceJoint::nullifyJoint()
b2Joint *Box2DDistanceJoint::createJoint()
{
mDistanceJoint = 0;
}

void Box2DDistanceJoint::createJoint()
{
if (anchorsAuto) mDistanceJointDef.Initialize(bodyA()->body(),
bodyB()->body(),
bodyA()->body()->GetWorldCenter(),
bodyB()->body()->GetWorldCenter());
else {
if (anchorsAuto) {
mDistanceJointDef.Initialize(bodyA()->body(),
bodyB()->body(),
bodyA()->body()->GetWorldCenter(),
bodyB()->body()->GetWorldCenter());
} else {
mDistanceJointDef.bodyA = bodyA()->body();
mDistanceJointDef.bodyB = bodyB()->body();
}

mDistanceJointDef.collideConnected = collideConnected();
mDistanceJoint = static_cast<b2DistanceJoint*>
(world()->CreateJoint(&mDistanceJointDef));
mDistanceJoint->SetUserData(this);
mInitializePending = false;
emit created();
}

void Box2DDistanceJoint::cleanup(b2World *world)
{
if (!world) {
qWarning() << "DistanceJoint: There is no world connected";
return;
}
if (mDistanceJoint && bodyA() && bodyB()) {
mDistanceJoint->SetUserData(0);
world->DestroyJoint(mDistanceJoint);
mDistanceJoint = 0;
}
}

b2Joint *Box2DDistanceJoint::joint() const
{
return mDistanceJoint;
return world()->CreateJoint(&mDistanceJointDef);
}

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

float Box2DDistanceJoint::getReactionTorque(float32 inv_dt) const
{
if (mDistanceJoint)
return mDistanceJoint->GetReactionTorque(inv_dt);
if (distanceJoint())
return distanceJoint()->GetReactionTorque(inv_dt);
return 0.0f;
}
18 changes: 9 additions & 9 deletions box2ddistancejoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include "box2djoint.h"
#include <Box2D.h>

class b2World;
class b2DistanceJoint;

class Box2DDistanceJoint : public Box2DJoint
{
Q_OBJECT
Expand All @@ -44,7 +41,6 @@ class Box2DDistanceJoint : public Box2DJoint

public:
explicit Box2DDistanceJoint(QObject *parent = 0);
~Box2DDistanceJoint();

float length() const;
void setLength(float length);
Expand All @@ -61,10 +57,7 @@ class Box2DDistanceJoint : public Box2DJoint
QPointF localAnchorB() const;
void setLocalAnchorB(const QPointF &localAnchorB);

void nullifyJoint();
void createJoint();
void cleanup(b2World *world);
b2Joint *joint() const;
b2DistanceJoint *distanceJoint() const;

Q_INVOKABLE QPointF getReactionForce(float32 inv_dt) const;
Q_INVOKABLE float getReactionTorque(float32 inv_dt) const;
Expand All @@ -76,10 +69,17 @@ class Box2DDistanceJoint : public Box2DJoint
void localAnchorAChanged();
void localAnchorBChanged();

protected:
b2Joint *createJoint();

private:
b2DistanceJointDef mDistanceJointDef;
b2DistanceJoint *mDistanceJoint;
bool anchorsAuto;
};

inline b2DistanceJoint *Box2DDistanceJoint::distanceJoint() const
{
return static_cast<b2DistanceJoint*>(joint());
}

#endif // BOX2DDISTANCEJOINT_H
90 changes: 34 additions & 56 deletions box2dfrictionjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,53 @@

Box2DFrictionJoint::Box2DFrictionJoint(QObject *parent) :
Box2DJoint(parent),
mFrictionJoint(0),
anchorsAuto(true)
{
}

Box2DFrictionJoint::~Box2DFrictionJoint()
{
cleanup(world());
}

float Box2DFrictionJoint::maxForce() const
{
if (mFrictionJoint) return mFrictionJoint->GetMaxForce();
if (frictionJoint())
return frictionJoint()->GetMaxForce();
return mFrictionJointDef.maxForce;
}

void Box2DFrictionJoint::setMaxForce(float maxForce)
{
if (mFrictionJoint && mFrictionJoint->GetMaxForce() == maxForce) return;
if (mFrictionJoint) mFrictionJoint->SetMaxForce(maxForce);
else mFrictionJointDef.maxForce = maxForce;
if (frictionJoint() && frictionJoint()->GetMaxForce() == maxForce)
return;
if (frictionJoint())
frictionJoint()->SetMaxForce(maxForce);
else
mFrictionJointDef.maxForce = maxForce;
emit maxForceChanged();
}

float Box2DFrictionJoint::maxTorque() const
{
if (mFrictionJoint) return mFrictionJoint->GetMaxTorque();
if (frictionJoint())
return frictionJoint()->GetMaxTorque();
return mFrictionJointDef.maxTorque;
}

void Box2DFrictionJoint::setMaxTorque(float maxTorque)
{
if (mFrictionJoint && mFrictionJoint->GetMaxTorque() == maxTorque) return;
if (mFrictionJoint) mFrictionJoint->SetMaxForce(maxTorque);
else mFrictionJointDef.maxTorque = maxTorque;
if (frictionJoint() && frictionJoint()->GetMaxTorque() == maxTorque)
return;
if (frictionJoint())
frictionJoint()->SetMaxForce(maxTorque);
else
mFrictionJointDef.maxTorque = maxTorque;
emit maxTorqueChanged();
}

QPointF Box2DFrictionJoint::localAnchorA() const
{
if (mFrictionJoint) QPointF(mFrictionJoint->GetAnchorA().x * scaleRatio,
-mFrictionJoint->GetAnchorA().y * scaleRatio);
if (frictionJoint())
return QPointF(frictionJoint()->GetAnchorA().x * scaleRatio,
-frictionJoint()->GetAnchorA().y * scaleRatio);
return QPointF(mFrictionJointDef.localAnchorA.x * scaleRatio,
mFrictionJointDef.localAnchorA.y * scaleRatio);
-mFrictionJointDef.localAnchorA.y * scaleRatio);
}

void Box2DFrictionJoint::setLocalAnchorA(const QPointF &localAnchorA)
Expand All @@ -86,10 +89,11 @@ void Box2DFrictionJoint::setLocalAnchorA(const QPointF &localAnchorA)

QPointF Box2DFrictionJoint::localAnchorB() const
{
if (mFrictionJoint) QPointF(mFrictionJoint->GetAnchorB().x * scaleRatio,
-mFrictionJoint->GetAnchorB().y * scaleRatio);
if (frictionJoint())
return QPointF(frictionJoint()->GetAnchorB().x * scaleRatio,
-frictionJoint()->GetAnchorB().y * scaleRatio);
return QPointF(mFrictionJointDef.localAnchorB.x * scaleRatio,
mFrictionJointDef.localAnchorB.y * scaleRatio);
-mFrictionJointDef.localAnchorB.y * scaleRatio);
}

void Box2DFrictionJoint::setLocalAnchorB(const QPointF &localAnchorB)
Expand All @@ -100,60 +104,34 @@ void Box2DFrictionJoint::setLocalAnchorB(const QPointF &localAnchorB)
emit localAnchorBChanged();
}

void Box2DFrictionJoint::nullifyJoint()
b2Joint *Box2DFrictionJoint::createJoint()
{
mFrictionJoint = 0;
}

void Box2DFrictionJoint::createJoint()
{
if (anchorsAuto)
{
if (anchorsAuto) {
mFrictionJointDef.bodyA = bodyA()->body();
mFrictionJointDef.bodyB = bodyB()->body();
}
else
} else {
mFrictionJointDef.Initialize(bodyA()->body(),
bodyB()->body(),
bodyA()->body()->GetWorldCenter());
mFrictionJointDef.collideConnected = collideConnected();
mFrictionJoint = static_cast<b2FrictionJoint *>(world()->CreateJoint(&mFrictionJointDef));
mFrictionJoint->SetUserData(this);
mInitializePending = false;
emit created();
}

void Box2DFrictionJoint::cleanup(b2World *world)
{
if (!world) {
qWarning() << "FrictionJoint: There is no world connected";
return;
}
if (mFrictionJoint && bodyA() && bodyB()) {
mFrictionJoint->SetUserData(0);
world->DestroyJoint(mFrictionJoint);
mFrictionJoint = 0;
}
}
mFrictionJointDef.collideConnected = collideConnected();

b2Joint *Box2DFrictionJoint::joint() const
{
return mFrictionJoint;
return world()->CreateJoint(&mFrictionJointDef);
}

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

float Box2DFrictionJoint::getReactionTorque(float32 inv_dt) const
{
if (mFrictionJoint)
return mFrictionJoint->GetReactionTorque(inv_dt);
if (frictionJoint())
return frictionJoint()->GetReactionTorque(inv_dt);
return 0.0f;
}

Loading

0 comments on commit 075ad33

Please sign in to comment.