Skip to content

Commit d30fa14

Browse files
author
Roger Zanoni
committed
Don't force joints to use the bodies world center as anchors
Signed-off-by: Roger Zanoni <roger.zanoni@openbossa.org>
1 parent db0a270 commit d30fa14

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/box2ddistancejointitem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void Box2DDistanceJointItem::initialize()
5454

5555
jointDef.Initialize(m_entityA->body(),
5656
m_entityB->body(),
57-
m_entityA->body()->GetWorldCenter(),
58-
m_entityB->body()->GetWorldCenter());
57+
m_anchorA.isNull() ? m_entityA->body()->GetWorldCenter() : b2Util::b2Vec(m_anchorA, m_scaleRatio),
58+
m_anchorB.isNull() ? m_entityB->body()->GetWorldCenter() : b2Util::b2Vec(m_anchorB, m_scaleRatio));
5959

6060

6161
m_joint = static_cast<b2DistanceJoint *>(m_worldPtr->CreateJoint(&jointDef));

src/box2djointitem.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,23 @@ void Box2DJointItem::setCollideConnected(const bool &collideConnected)
7171
emit collideConnectedChanged();
7272
}
7373
}
74+
75+
void Box2DJointItem::setAnchorA(const QPointF &anchorA)
76+
{
77+
if (m_anchorA == anchorA)
78+
return;
79+
80+
m_anchorA = anchorA;
81+
82+
emit anchorAChanged();
83+
}
84+
85+
void Box2DJointItem::setAnchorB(const QPointF &anchorB)
86+
{
87+
if (m_anchorB == anchorB)
88+
return;
89+
90+
m_anchorB = anchorB;
91+
92+
emit anchorBChanged();
93+
}

src/box2djointitem.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class Box2DJointItem : public Box2DBaseItem
3535
Q_PROPERTY(Entity *entityA READ entityA WRITE setEntityA NOTIFY entityAChanged)
3636
Q_PROPERTY(Entity *entityB READ entityB WRITE setEntityB NOTIFY entityBChanged)
3737
Q_PROPERTY(bool collideConnected READ collideConnected WRITE setCollideConnected NOTIFY collideConnectedChanged)
38+
Q_PROPERTY(QPointF anchorA READ anchorA WRITE setAnchorA NOTIFY anchorAChanged)
39+
Q_PROPERTY(QPointF anchorB READ anchorB WRITE setAnchorB NOTIFY anchorBChanged)
3840

3941
public:
4042
Box2DJointItem(Scene *parent = 0);
@@ -49,16 +51,26 @@ class Box2DJointItem : public Box2DBaseItem
4951
bool collideConnected() const;
5052
void setCollideConnected(const bool &collideConnected);
5153

54+
QPointF anchorA() const { return m_anchorA; }
55+
void setAnchorA(const QPointF &anchorA);
56+
57+
QPointF anchorB() const { return m_anchorB; }
58+
void setAnchorB(const QPointF &anchorB);
59+
5260
signals:
5361
void entityAChanged();
5462
void entityBChanged();
5563
void collideConnectedChanged();
64+
void anchorAChanged();
65+
void anchorBChanged();
5666

5767
protected:
5868
Entity *m_entityA;
5969
Entity *m_entityB;
6070
bool m_collideConnected;
6171
b2Joint *m_joint;
72+
QPointF m_anchorA;
73+
QPointF m_anchorB;
6274
};
6375

6476
#endif /* _BOX2DJOINTITEM_H_ */

0 commit comments

Comments
 (0)