Skip to content

Commit

Permalink
Fixed RevoluteJoint (added localAnchorB, angular units changed to use…
Browse files Browse the repository at this point in the history
… degree)
  • Loading branch information
folibis committed Jan 1, 2014
1 parent f36fe24 commit 1550e96
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 41 deletions.
1 change: 1 addition & 0 deletions box2dfixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ b2Shape *Box2DBox::createShape()
int32 count = 4;

b2PolygonShape *shape = new b2PolygonShape;
//shape->SetAsBox(width()/ scaleRatio,height()/ scaleRatio );
shape->Set(vertices, count);
return shape;
}
Expand Down
14 changes: 8 additions & 6 deletions box2dmotorjoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,32 @@ Box2DMotorJoint::~Box2DMotorJoint()

QPointF Box2DMotorJoint::linearOffset() const
{
return QPointF(mMotorJointDef.linearOffset.x,mMotorJointDef.linearOffset.y);
return QPointF(mMotorJointDef.linearOffset.x * scaleRatio,mMotorJointDef.linearOffset.y * scaleRatio);
}

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

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

void Box2DMotorJoint::setAngularOffset(const float angularOffset)
{
if(mMotorJointDef.angularOffset == angularOffset)
float angularOffsetRad = angularOffset * ( b2_pi / 180);
if(mMotorJointDef.angularOffset == angularOffsetRad)
return;
mMotorJointDef.angularOffset = angularOffset;
mMotorJointDef.angularOffset = angularOffsetRad;
if(mMotorJoint)
mMotorJoint->SetAngularOffset(angularOffset);
mMotorJoint->SetAngularOffset(angularOffsetRad);
emit angularOffsetChanged();
}

Expand Down Expand Up @@ -123,6 +124,7 @@ void Box2DMotorJoint::nullifyJoint()

void Box2DMotorJoint::createJoint()
{

mMotorJointDef.Initialize(bodyA()->body(),bodyB()->body());
mMotorJointDef.collideConnected = collideConnected();
mMotorJoint = static_cast<b2MotorJoint*>(
Expand Down
2 changes: 2 additions & 0 deletions box2dplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "box2ddistancejoint.h"
#include "box2dprismaticjoint.h"
#include "box2drevolutejoint.h"
#include "box2dmotorjoint.h"

Box2DPlugin::Box2DPlugin(QObject *parent) :
QQmlExtensionPlugin(parent)
Expand All @@ -58,4 +59,5 @@ void Box2DPlugin::registerTypes(const char *uri)
qmlRegisterType<Box2DDistanceJoint>(uri, 1, 0, "DistanceJoint");
qmlRegisterType<Box2DPrismaticJoint>(uri, 1, 0, "PrismaticJoint");
qmlRegisterType<Box2DRevoluteJoint>(uri, 1, 0, "RevoluteJoint");
qmlRegisterType<Box2DMotorJoint>(uri, 1, 0, "MotorJoint");
}
66 changes: 33 additions & 33 deletions box2drevolutejoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
Box2DRevoluteJoint::Box2DRevoluteJoint(QObject *parent) :
Box2DJoint(parent),
mRevoluteJointDef(),
mRevoluteJoint(0),
mOverrideLocalAnchorA(false)
mRevoluteJoint(0)
{
}

Expand All @@ -44,35 +43,36 @@ Box2DRevoluteJoint::~Box2DRevoluteJoint()

float Box2DRevoluteJoint::lowerAngle() const
{
return mRevoluteJointDef.lowerAngle;
return mRevoluteJointDef.lowerAngle * 180 / b2_pi;
}

void Box2DRevoluteJoint::setLowerAngle(float lowerAngle)
{
if (mRevoluteJointDef.lowerAngle == lowerAngle * b2_pi)
float lowerAngleRad = lowerAngle * b2_pi / 180;
if (mRevoluteJointDef.lowerAngle == lowerAngleRad)
return;

mRevoluteJointDef.lowerAngle = lowerAngle * b2_pi;
mRevoluteJointDef.lowerAngle = lowerAngleRad;
if (mRevoluteJoint)
mRevoluteJoint->SetLimits(lowerAngle * b2_pi,
mRevoluteJointDef.upperAngle);
mRevoluteJoint->SetLimits(lowerAngleRad,mRevoluteJointDef.upperAngle);
emit lowerAngleChanged();
}

float Box2DRevoluteJoint::upperAngle() const
{
return mRevoluteJointDef.upperAngle;
return mRevoluteJointDef.upperAngle * 180 / b2_pi;
}

void Box2DRevoluteJoint::setUpperAngle(float upperAngle)
{
if (mRevoluteJointDef.upperAngle == upperAngle * b2_pi)
float upperAngleRad = upperAngle * b2_pi / 180;
if (mRevoluteJointDef.upperAngle == upperAngleRad)
return;

mRevoluteJointDef.upperAngle = upperAngle * b2_pi;
mRevoluteJointDef.upperAngle = upperAngleRad;
if (mRevoluteJoint)
mRevoluteJoint->SetLimits(mRevoluteJointDef.lowerAngle,
upperAngle * b2_pi);
upperAngleRad);
emit upperAngleChanged();
}

Expand All @@ -94,17 +94,18 @@ void Box2DRevoluteJoint::setMaxMotorTorque(float maxMotorTorque)

float Box2DRevoluteJoint::motorSpeed() const
{
return mRevoluteJointDef.motorSpeed;
return mRevoluteJointDef.motorSpeed * b2_pi / 180;
}

void Box2DRevoluteJoint::setMotorSpeed(float motorSpeed)
{
if (mRevoluteJointDef.motorSpeed == motorSpeed)
float motorSpeedRad = motorSpeed * b2_pi / 180;
if (mRevoluteJointDef.motorSpeed == motorSpeedRad)
return;

mRevoluteJointDef.motorSpeed = motorSpeed;
mRevoluteJointDef.motorSpeed = motorSpeedRad;
if (mRevoluteJoint)
mRevoluteJoint->SetMotorSpeed(motorSpeed);
mRevoluteJoint->SetMotorSpeed(motorSpeedRad);
emit motorSpeedChanged();
}

Expand Down Expand Up @@ -142,39 +143,38 @@ void Box2DRevoluteJoint::setEnableMotor(bool enableMotor)

QPointF Box2DRevoluteJoint::localAnchorA() const
{
if (mOverrideLocalAnchorA)
return mLocalAnchorA;
else
return QPointF(mRevoluteJointDef.localAnchorA.x * scaleRatio,
-mRevoluteJointDef.localAnchorA.y * scaleRatio);
return QPointF(mRevoluteJointDef.localAnchorA.x * scaleRatio, mRevoluteJointDef.localAnchorA.y * scaleRatio);
}

void Box2DRevoluteJoint::setLocalAnchorA(const QPointF &localAnchorA)
QPointF Box2DRevoluteJoint::localAnchorB() const
{
if (mOverrideLocalAnchorA && mLocalAnchorA == localAnchorA)
return;
return QPointF(mRevoluteJointDef.localAnchorB.x * scaleRatio, mRevoluteJointDef.localAnchorB.y * scaleRatio);
}

mOverrideLocalAnchorA = true;
mLocalAnchorA = localAnchorA;
void Box2DRevoluteJoint::setLocalAnchorA(const QPointF &localAnchorA)
{

mRevoluteJointDef.localAnchorA = b2Vec2(localAnchorA.x() / scaleRatio,-localAnchorA.y() / scaleRatio);
emit localAnchorAChanged();
}

void Box2DRevoluteJoint::setLocalAnchorB(const QPointF &localAnchorB)
{

mRevoluteJointDef.localAnchorB = b2Vec2(localAnchorB.x() / scaleRatio,-localAnchorB.y() / scaleRatio);
emit localAnchorBChanged();
}

void Box2DRevoluteJoint::nullifyJoint()
{
mRevoluteJoint = 0;
}

void Box2DRevoluteJoint::createJoint()
{
b2Vec2 anchor = mOverrideLocalAnchorA ?
b2Vec2(mLocalAnchorA.x() / scaleRatio,
-mLocalAnchorA.y() / scaleRatio) +
bodyA()->body()->GetPosition() :
bodyA()->body()->GetWorldCenter();

mRevoluteJointDef.Initialize(bodyA()->body(), bodyB()->body(),
anchor);
mRevoluteJointDef.bodyA = bodyA()->body();
mRevoluteJointDef.bodyB = bodyB()->body();
mRevoluteJointDef.referenceAngle = 0;
mRevoluteJointDef.collideConnected = collideConnected();

mRevoluteJoint = static_cast<b2RevoluteJoint*>(
Expand Down
7 changes: 5 additions & 2 deletions box2drevolutejoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Box2DRevoluteJoint : public Box2DJoint
Q_PROPERTY(bool enableLimit READ enableLimit WRITE setEnableLimit NOTIFY enableLimitChanged)
Q_PROPERTY(bool enableMotor READ enableMotor WRITE setEnableMotor NOTIFY enableMotorChanged)
Q_PROPERTY(QPointF localAnchorA READ localAnchorA WRITE setLocalAnchorA NOTIFY localAnchorAChanged)
Q_PROPERTY(QPointF localAnchorB READ localAnchorB WRITE setLocalAnchorB NOTIFY localAnchorBChanged)

public:
explicit Box2DRevoluteJoint(QObject *parent = 0);
Expand All @@ -71,6 +72,9 @@ class Box2DRevoluteJoint : public Box2DJoint
QPointF localAnchorA() const;
void setLocalAnchorA(const QPointF &localAnchorA);

QPointF localAnchorB() const;
void setLocalAnchorB(const QPointF &localAnchorB);

void nullifyJoint();
void createJoint();
void cleanup(b2World *world);
Expand All @@ -83,12 +87,11 @@ class Box2DRevoluteJoint : public Box2DJoint
void enableLimitChanged();
void enableMotorChanged();
void localAnchorAChanged();
void localAnchorBChanged();

private:
b2RevoluteJointDef mRevoluteJointDef;
b2RevoluteJoint *mRevoluteJoint;
bool mOverrideLocalAnchorA;
QPointF mLocalAnchorA;
};

#endif // BOX2DREVOLUTEJOINT_H

0 comments on commit 1550e96

Please sign in to comment.