Skip to content

Commit

Permalink
Fixed up the MotorJoint
Browse files Browse the repository at this point in the history
Don't crash when setting linearOffset before joint creation.

Also, when linearOffset and angularOffset are set before joint creation,
their values now actually take effect whereas before they were ignored.
  • Loading branch information
bjorn committed Apr 27, 2014
1 parent 4006d82 commit 37d4786
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 35 deletions.
78 changes: 47 additions & 31 deletions box2dmotorjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,83 +29,99 @@
#include "box2dworld.h"
#include "box2dbody.h"

Box2DMotorJoint::Box2DMotorJoint(QObject *parent) :
Box2DJoint(MotorJoint, parent)
Box2DMotorJoint::Box2DMotorJoint(QObject *parent)
: Box2DJoint(MotorJoint, parent)
, m_angularOffset(0.0f)
, m_maxForce(1.0f)
, m_maxTorque(1.0f)
, m_correctionFactor(0.3f)
, m_defaultLinearOffset(true)
, m_defaultAngularOffset(true)
{
}

QPointF Box2DMotorJoint::linearOffset() const
{
return world()->toPixels(mMotorJointDef.linearOffset);
}

void Box2DMotorJoint::setLinearOffset(const QPointF &linearOffset)
{
const b2Vec2 linearOffsetMeters = world()->toMeters(linearOffset);
if (mMotorJointDef.linearOffset == linearOffsetMeters)
m_defaultLinearOffset = false;

if (m_linearOffset == linearOffset)
return;

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

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

void Box2DMotorJoint::setAngularOffset(float angularOffset)
{
const float angularOffsetRad = toRadians(angularOffset);
if (mMotorJointDef.angularOffset == angularOffsetRad)
m_defaultAngularOffset = false;

if (m_angularOffset == angularOffset)
return;

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

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

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

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

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

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

mMotorJointDef.correctionFactor = correctionFactor;
m_correctionFactor = correctionFactor;
if (motorJoint())
motorJoint()->SetCorrectionFactor(correctionFactor);
emit correctionFactorChanged();
}

b2Joint *Box2DMotorJoint::createJoint()
{
initializeJointDef(mMotorJointDef);

mMotorJointDef.Initialize(mMotorJointDef.bodyA, mMotorJointDef.bodyB);

return world()->world().CreateJoint(&mMotorJointDef);
b2MotorJointDef jointDef;
initializeJointDef(jointDef);

if (m_defaultLinearOffset) {
const b2Vec2 &positionB = jointDef.bodyB->GetPosition();
jointDef.linearOffset = jointDef.bodyA->GetLocalPoint(positionB);
} else {
jointDef.linearOffset = world()->toMeters(m_linearOffset);
}

if (m_defaultAngularOffset) {
float32 angleA = jointDef.bodyA->GetAngle();
float32 angleB = jointDef.bodyB->GetAngle();
jointDef.angularOffset = angleB - angleA;
} else {
jointDef.angularOffset = toRadians(m_angularOffset);
}

jointDef.maxForce = m_maxForce;
jointDef.maxTorque = m_maxTorque;
jointDef.correctionFactor = m_correctionFactor;

return world()->world().CreateJoint(&jointDef);
}
24 changes: 20 additions & 4 deletions box2dmotorjoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,38 @@ class Box2DMotorJoint : public Box2DJoint
b2Joint *createJoint();

private:
b2MotorJointDef mMotorJointDef;
QPointF m_linearOffset;
float m_angularOffset;
float m_maxForce;
float m_maxTorque;
float m_correctionFactor;
bool m_defaultLinearOffset;
bool m_defaultAngularOffset;
};

inline QPointF Box2DMotorJoint::linearOffset() const
{
return m_linearOffset;
}

inline float Box2DMotorJoint::angularOffset() const
{
return m_angularOffset;
}

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

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

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

inline b2MotorJoint *Box2DMotorJoint::motorJoint() const
Expand Down

0 comments on commit 37d4786

Please sign in to comment.