Skip to content

Commit

Permalink
Added b2JointDef reference to Box2DJoint base class
Browse files Browse the repository at this point in the history
This allowed introducing a Joint.jointType property and using
b2JointDef::collideConnected directly instead of having
mCollideConnected. The latter also made sure that collideConnected
defaults to true for the PulleyJoint, as it does in Box2D.
  • Loading branch information
bjorn committed Apr 6, 2014
1 parent 073615b commit 5394442
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 35 deletions.
3 changes: 0 additions & 3 deletions box2dbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
class Box2DFixture;
class Box2DWorld;

class b2Body;
class b2World;

/**
* The Box2D body, build up from a list of shapes.
*/
Expand Down
4 changes: 1 addition & 3 deletions box2ddistancejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DDistanceJoint::Box2DDistanceJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mDistanceJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -112,8 +112,6 @@ b2Joint *Box2DDistanceJoint::createJoint()
mDistanceJointDef.bodyB = bodyB()->body();
}

mDistanceJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mDistanceJointDef);
}

Expand Down
3 changes: 1 addition & 2 deletions box2dfrictionjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DFrictionJoint::Box2DFrictionJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mFrictionJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -108,7 +108,6 @@ b2Joint *Box2DFrictionJoint::createJoint()
mFrictionJointDef.bodyA = bodyA()->body();
mFrictionJointDef.bodyB = bodyB()->body();
}
mFrictionJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mFrictionJointDef);
}
Expand Down
2 changes: 1 addition & 1 deletion box2dgearjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "box2dbody.h"

Box2DGearJoint::Box2DGearJoint(QObject *parent) :
Box2DJoint(parent)
Box2DJoint(mGearJointDef, parent)
{
}

Expand Down
8 changes: 4 additions & 4 deletions box2djoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

#include "box2dbody.h"

Box2DJoint::Box2DJoint(QObject *parent) :
Box2DJoint::Box2DJoint(b2JointDef &jointDef, QObject *parent) :
QObject(parent),
mJointDef(jointDef),
mComponentComplete(false),
mInitializePending(false),
mCollideConnected(false),
mBodyA(0),
mBodyB(0),
mWorld(0),
Expand All @@ -47,10 +47,10 @@ Box2DJoint::~Box2DJoint()

void Box2DJoint::setCollideConnected(bool collideConnected)
{
if (mCollideConnected == collideConnected)
if (mJointDef.collideConnected == collideConnected)
return;

mCollideConnected = collideConnected;
mJointDef.collideConnected = collideConnected;

emit collideConnectedChanged();
}
Expand Down
30 changes: 27 additions & 3 deletions box2djoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,33 @@ class Box2DJoint : public QObject, public QQmlParserStatus
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)

Q_ENUMS(JointType)
Q_PROPERTY(JointType jointType READ jointType CONSTANT)
Q_PROPERTY(bool collideConnected READ collideConnected WRITE setCollideConnected NOTIFY collideConnectedChanged)
Q_PROPERTY(Box2DBody *bodyA READ bodyA WRITE setBodyA NOTIFY bodyAChanged)
Q_PROPERTY(Box2DBody *bodyB READ bodyB WRITE setBodyB NOTIFY bodyBChanged)

public:
explicit Box2DJoint(QObject *parent = 0);
enum JointType { // Matches b2JointType
UnknownJoint,
RevoluteJoint,
PrismaticJoint,
DistanceJoint,
PulleyJoint,
MouseJoint,
GearJoint,
WheelJoint,
WeldJoint,
FrictionJoint,
RopeJoint,
MotorJoint
};

Box2DJoint(b2JointDef &jointDef, QObject *parent = 0);
~Box2DJoint();

JointType jointType() const;

bool collideConnected() const;
void setCollideConnected(bool collideConnected);

Expand Down Expand Up @@ -83,18 +102,23 @@ private slots:
void created();

private:
b2JointDef &mJointDef;
bool mComponentComplete;
bool mInitializePending;
bool mCollideConnected;
Box2DBody *mBodyA;
Box2DBody *mBodyB;
b2World *mWorld;
b2Joint *mJoint;
};

inline Box2DJoint::JointType Box2DJoint::jointType() const
{
return static_cast<JointType>(mJointDef.type);
}

inline bool Box2DJoint::collideConnected() const
{
return mCollideConnected;
return mJointDef.collideConnected;
}

inline Box2DBody *Box2DJoint::bodyA() const
Expand Down
3 changes: 1 addition & 2 deletions box2dmotorjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "box2dbody.h"

Box2DMotorJoint::Box2DMotorJoint(QObject *parent) :
Box2DJoint(parent)
Box2DJoint(mMotorJointDef, parent)
{
}

Expand Down Expand Up @@ -101,7 +101,6 @@ void Box2DMotorJoint::setCorrectionFactor(float correctionFactor)
b2Joint *Box2DMotorJoint::createJoint()
{
mMotorJointDef.Initialize(bodyA()->body(), bodyB()->body());
mMotorJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mMotorJointDef);
}
2 changes: 1 addition & 1 deletion box2dmousejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DMouseJoint::Box2DMouseJoint(QObject *parent) :
Box2DJoint(parent)
Box2DJoint(mMouseJointDef, parent)
{
}

Expand Down
3 changes: 1 addition & 2 deletions box2dprismaticjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DPrismaticJoint::Box2DPrismaticJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mPrismaticJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -169,7 +169,6 @@ b2Joint *Box2DPrismaticJoint::createJoint()
mPrismaticJointDef.bodyB = bodyB()->body();
mPrismaticJointDef.referenceAngle = 0.0;
}
mPrismaticJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mPrismaticJointDef);
}
Expand Down
4 changes: 2 additions & 2 deletions box2dpulleyjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DPulleyJoint::Box2DPulleyJoint(QObject *parent) :
Box2DJoint(parent)
Box2DJoint(mPulleyJointDef, parent)
{
}

Expand Down Expand Up @@ -136,7 +136,7 @@ b2Joint *Box2DPulleyJoint::createJoint()
}
mPulleyJointDef.bodyA = bodyA()->body();
mPulleyJointDef.bodyB = bodyB()->body();
mPulleyJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mPulleyJointDef);
}

Expand Down
3 changes: 1 addition & 2 deletions box2drevolutejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "box2dbody.h"

Box2DRevoluteJoint::Box2DRevoluteJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mRevoluteJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -144,7 +144,6 @@ b2Joint *Box2DRevoluteJoint::createJoint()
mRevoluteJointDef.bodyA = bodyA()->body();
mRevoluteJointDef.bodyB = bodyB()->body();
}
mRevoluteJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mRevoluteJointDef);
}
Expand Down
3 changes: 1 addition & 2 deletions box2dropejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "box2dbody.h"

Box2DRopeJoint::Box2DRopeJoint(QObject *parent) :
Box2DJoint(parent)
Box2DJoint(mRopeJointDef, parent)
{
}

Expand Down Expand Up @@ -81,7 +81,6 @@ b2Joint *Box2DRopeJoint::createJoint()
{
mRopeJointDef.bodyA = bodyA()->body();
mRopeJointDef.bodyB = bodyB()->body();
mRopeJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mRopeJointDef);
}
Expand Down
3 changes: 1 addition & 2 deletions box2dweldjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "box2dbody.h"

Box2DWeldJoint::Box2DWeldJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mWeldJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -104,7 +104,6 @@ b2Joint *Box2DWeldJoint::createJoint()
mWeldJointDef.bodyA = bodyA()->body();
mWeldJointDef.bodyB = bodyB()->body();
}
mWeldJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mWeldJointDef);
}
3 changes: 1 addition & 2 deletions box2dwheeljoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "box2dbody.h"

Box2DWheelJoint::Box2DWheelJoint(QObject *parent) :
Box2DJoint(parent),
Box2DJoint(mWheelJointDef, parent),
mAnchorsAuto(true)
{
}
Expand Down Expand Up @@ -145,7 +145,6 @@ b2Joint *Box2DWheelJoint::createJoint()
mWheelJointDef.bodyA = bodyA()->body();
mWheelJointDef.bodyB = bodyB()->body();
}
mWheelJointDef.collideConnected = collideConnected();

return world()->CreateJoint(&mWheelJointDef);
}
Expand Down
7 changes: 3 additions & 4 deletions examples/friction/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ Rectangle {
height: 600
color: "#EFEFFF"

Component
{
Component {
id: frictionJoint
FrictionJoint {
bodyA: anchor
maxForce: 0.5
maxTorque: 0.5
localAnchorA: Qt.point(10,10)
localAnchorB: Qt.point(10,10)
localAnchorA: Qt.point(10, 10)
localAnchorB: Qt.point(10, 10)
}
}

Expand Down

0 comments on commit 5394442

Please sign in to comment.