Skip to content

Commit

Permalink
Optimized and fixed joint properties
Browse files Browse the repository at this point in the history
* Simplified and inlined getters when the properties are never modified
  by Box2D
* Added missing scaling in DistanceJoint.length getter
* Fixed FrictionJoint.maxTorque setting the MaxForce when the joint is
  already created
* Check FrictionJoint.maxForce and FrictionJoint.maxTorque for validity
* When the PulleyJoint is not yet created, return the starting lengths
  from getCurrentLengthA and getCurrentLengthB instead of 0
* Check GearJoint.ratio for validity
* Fixed MotorJoint.angularOffset to be properly inverted
* Fixed change notification signals of MouseJoint properties
* Fixed conversion of PrismaticJoint.motorSpeed
* Fixed conversion of WelfJoint.referenceAngle in getter
  • Loading branch information
bjorn committed Apr 5, 2014
1 parent 3e9713f commit b837497
Show file tree
Hide file tree
Showing 23 changed files with 294 additions and 302 deletions.
42 changes: 14 additions & 28 deletions box2ddistancejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,53 +36,39 @@ Box2DDistanceJoint::Box2DDistanceJoint(QObject *parent) :

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

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

mDistanceJointDef.length = length / scaleRatio;
if (distanceJoint())
distanceJoint()->SetLength(mDistanceJointDef.length);
emit lengthChanged();
}

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

void Box2DDistanceJoint::setFrequencyHz(float _frequencyHz)
void Box2DDistanceJoint::setFrequencyHz(float frequencyHz)
{
if (frequencyHz() == _frequencyHz)
if (mDistanceJointDef.frequencyHz == frequencyHz)
return;
mDistanceJointDef.frequencyHz = _frequencyHz;
if (distanceJoint())
distanceJoint()->SetFrequency(_frequencyHz);
emit frequencyHzChanged();
}

float Box2DDistanceJoint::dampingRatio() const
{
mDistanceJointDef.frequencyHz = frequencyHz;
if (distanceJoint())
return distanceJoint()->GetDampingRatio();
return mDistanceJointDef.dampingRatio;
distanceJoint()->SetFrequency(frequencyHz);
emit frequencyHzChanged();
}

void Box2DDistanceJoint::setDampingRatio(float _dampingRatio)
void Box2DDistanceJoint::setDampingRatio(float dampingRatio)
{
if (dampingRatio() == _dampingRatio)
if (mDistanceJointDef.dampingRatio == dampingRatio)
return;

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

Expand Down
10 changes: 10 additions & 0 deletions box2ddistancejoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class Box2DDistanceJoint : public Box2DJoint
bool anchorsAuto;
};

inline float Box2DDistanceJoint::frequencyHz() const
{
return mDistanceJointDef.frequencyHz;
}

inline float Box2DDistanceJoint::dampingRatio() const
{
return mDistanceJointDef.dampingRatio;
}

inline b2DistanceJoint *Box2DDistanceJoint::distanceJoint() const
{
return static_cast<b2DistanceJoint*>(joint());
Expand Down
36 changes: 15 additions & 21 deletions box2dfrictionjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,33 @@ Box2DFrictionJoint::Box2DFrictionJoint(QObject *parent) :
{
}

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

void Box2DFrictionJoint::setMaxForce(float maxForce)
{
if (frictionJoint() && frictionJoint()->GetMaxForce() == maxForce)
if (!(b2IsValid(maxForce) && maxForce >= 0.0f)) {
qWarning() << "FrictionJoint: Invalid maxForce:" << maxForce;
return;
}
if (mFrictionJointDef.maxForce == maxForce)
return;

mFrictionJointDef.maxForce = maxForce;
if (frictionJoint())
frictionJoint()->SetMaxForce(maxForce);
else
mFrictionJointDef.maxForce = maxForce;
emit maxForceChanged();
}

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

void Box2DFrictionJoint::setMaxTorque(float maxTorque)
{
if (frictionJoint() && frictionJoint()->GetMaxTorque() == maxTorque)
if (!(b2IsValid(maxTorque) && maxTorque >= 0.0f)) {
qWarning() << "FrictionJoint: Invalid maxTorque:" << maxTorque;
return;
}
if (mFrictionJointDef.maxTorque == maxTorque)
return;

mFrictionJointDef.maxTorque = maxTorque;
if (frictionJoint())
frictionJoint()->SetMaxForce(maxTorque);
else
mFrictionJointDef.maxTorque = maxTorque;
frictionJoint()->SetMaxTorque(maxTorque);
emit maxTorqueChanged();
}

Expand Down
10 changes: 10 additions & 0 deletions box2dfrictionjoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class Box2DFrictionJoint : public Box2DJoint
bool anchorsAuto;
};

inline float Box2DFrictionJoint::maxForce() const
{
return mFrictionJointDef.maxForce;
}

inline float Box2DFrictionJoint::maxTorque() const
{
return mFrictionJointDef.maxTorque;
}

inline b2FrictionJoint *Box2DFrictionJoint::frictionJoint() const
{
return static_cast<b2FrictionJoint*>(joint());
Expand Down
32 changes: 13 additions & 19 deletions box2dgearjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,24 @@ Box2DGearJoint::Box2DGearJoint(QObject *parent) :
{
}

float Box2DGearJoint::ratio() const
void Box2DGearJoint::setRatio(float ratio)
{
if (gearJoint())
gearJoint()->GetRatio();
return mGearJointDef.ratio;
}

void Box2DGearJoint::setRatio(float _ratio)
{
if (qFuzzyCompare(_ratio, ratio()))
if (!b2IsValid(ratio)) {
qWarning() << "GearJoint: Invalid ratio:" << ratio;
return;
mGearJointDef.ratio = _ratio;
}
if (mGearJointDef.ratio == ratio)
return;

mGearJointDef.ratio = ratio;
if (gearJoint())
gearJoint()->SetRatio(_ratio);
gearJoint()->SetRatio(ratio);
emit ratioChanged();
}

Box2DJoint *Box2DGearJoint::joint1() const
{
if (gearJoint())
return toBox2DJoint(gearJoint()->GetJoint1());
return toBox2DJoint(mGearJointDef.joint1);
return mGearJointDef.joint1 ? toBox2DJoint(mGearJointDef.joint1) : 0;
}

void Box2DGearJoint::setJoint1(Box2DJoint *_joint1)
Expand All @@ -65,14 +61,12 @@ void Box2DGearJoint::setJoint1(Box2DJoint *_joint1)
initialize();
emit joint1Changed();
}
else connect(_joint1,SIGNAL(created()),this,SLOT(joint1Created()));
else connect(_joint1, SIGNAL(created()), this, SLOT(joint1Created()));
}

Box2DJoint *Box2DGearJoint::joint2() const
{
if (gearJoint())
return toBox2DJoint(gearJoint()->GetJoint2());
return toBox2DJoint(mGearJointDef.joint2);
return mGearJointDef.joint2 ? toBox2DJoint(mGearJointDef.joint2) : 0;
}

void Box2DGearJoint::setJoint2(Box2DJoint *_joint2)
Expand All @@ -84,7 +78,7 @@ void Box2DGearJoint::setJoint2(Box2DJoint *_joint2)
initialize();
emit joint2Changed();
}
else connect(_joint2,SIGNAL(created()),this,SLOT(joint2Created()));
else connect(_joint2, SIGNAL(created()), this, SLOT(joint2Created()));
}

b2Joint *Box2DGearJoint::createJoint()
Expand Down
7 changes: 6 additions & 1 deletion box2dgearjoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Box2DGearJoint : public Box2DJoint
explicit Box2DGearJoint(QObject *parent = 0);

float ratio() const;
void setRatio(float _ratio);
void setRatio(float ratio);

Box2DJoint *joint1() const;
void setJoint1(Box2DJoint *_joint1);
Expand All @@ -67,6 +67,11 @@ private slots:
b2GearJointDef mGearJointDef;
};

inline float Box2DGearJoint::ratio() const
{
return mGearJointDef.ratio;
}

inline b2GearJoint *Box2DGearJoint::gearJoint() const
{
return static_cast<b2GearJoint*>(joint());
Expand Down
2 changes: 0 additions & 2 deletions box2djoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ inline b2Joint *Box2DJoint::joint() const
*/
inline Box2DJoint *toBox2DJoint(b2Joint *joint)
{
if (!joint)
return 0;
return static_cast<Box2DJoint*>(joint->GetUserData());
}

Expand Down
36 changes: 11 additions & 25 deletions box2dmotorjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,70 +42,56 @@ QPointF Box2DMotorJoint::linearOffset() const

void Box2DMotorJoint::setLinearOffset(const QPointF &linearOffset)
{
if (this->linearOffset() == linearOffset)
const b2Vec2 linearOffsetMeters(linearOffset.x() / scaleRatio,
-linearOffset.y() / scaleRatio);
if (mMotorJointDef.linearOffset == linearOffsetMeters)
return;
mMotorJointDef.linearOffset = b2Vec2(linearOffset.x() / scaleRatio,
-linearOffset.y() / scaleRatio);

mMotorJointDef.linearOffset = linearOffsetMeters;
if (motorJoint())
motorJoint()->SetLinearOffset(mMotorJointDef.linearOffset);
motorJoint()->SetLinearOffset(linearOffsetMeters);
emit linearOffsetChanged();
}

float Box2DMotorJoint::angularOffset() const
{
return mMotorJointDef.angularOffset * 180 / b2_pi;
}

void Box2DMotorJoint::setAngularOffset(float angularOffset)
{
float angularOffsetRad = angularOffset * ( b2_pi / 180);
const float angularOffsetRad = angularOffset * -b2_pi / 180;
if (mMotorJointDef.angularOffset == angularOffsetRad)
return;

mMotorJointDef.angularOffset = angularOffsetRad;
if (motorJoint())
motorJoint()->SetAngularOffset(angularOffsetRad);
emit angularOffsetChanged();
}

float Box2DMotorJoint::maxForce() const
{
return mMotorJointDef.maxForce;
}

void Box2DMotorJoint::setMaxForce(float maxForce)
{
if (mMotorJointDef.maxForce == maxForce)
return;

mMotorJointDef.maxForce = maxForce;
if (motorJoint())
motorJoint()->SetMaxForce(maxForce);
emit maxForceChanged();
}

float Box2DMotorJoint::maxTorque() const
{
return mMotorJointDef.maxTorque;
}

void Box2DMotorJoint::setMaxTorque(float maxTorque)
{
if (mMotorJointDef.maxTorque == maxTorque)
return;

mMotorJointDef.maxTorque = maxTorque;
if (motorJoint())
motorJoint()->SetMaxTorque(maxTorque);
emit maxTorqueChanged();
}

float Box2DMotorJoint::correctionFactor() const
{
return mMotorJointDef.correctionFactor;
}

void Box2DMotorJoint::setCorrectionFactor(float correctionFactor)
{
if (mMotorJointDef.correctionFactor == correctionFactor)
return;

mMotorJointDef.correctionFactor = correctionFactor;
if (motorJoint())
motorJoint()->SetCorrectionFactor(correctionFactor);
Expand Down
20 changes: 20 additions & 0 deletions box2dmotorjoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ class Box2DMotorJoint : public Box2DJoint
b2MotorJointDef mMotorJointDef;
};

inline float Box2DMotorJoint::angularOffset() const
{
return mMotorJointDef.angularOffset * -180 / b2_pi;
}

inline float Box2DMotorJoint::maxForce() const
{
return mMotorJointDef.maxForce;
}

inline float Box2DMotorJoint::maxTorque() const
{
return mMotorJointDef.maxTorque;
}

inline float Box2DMotorJoint::correctionFactor() const
{
return mMotorJointDef.correctionFactor;
}

inline b2MotorJoint *Box2DMotorJoint::motorJoint() const
{
return static_cast<b2MotorJoint*>(joint());
Expand Down
Loading

0 comments on commit b837497

Please sign in to comment.