forked from roniemartinez/qbox2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqbox2dbody.h
112 lines (100 loc) · 3.38 KB
/
qbox2dbody.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef QBOX2DBODY_H
#define QBOX2DBODY_H
#include <QGraphicsItemGroup>
#include <QPainter>
#include <QHash>
#include <QGraphicsScene>
#include <Box2D/Collision/Shapes/b2Shape.h>
#include <Box2D/Collision/Shapes/b2ChainShape.h>
#include <Box2D/Collision/Shapes/b2CircleShape.h>
#include <Box2D/Collision/Shapes/b2EdgeShape.h>
#include <Box2D/Collision/Shapes/b2PolygonShape.h>
#include <Box2D/Dynamics/b2Body.h>
#include <Box2D/Dynamics/b2Fixture.h>
#include <Box2D/Dynamics/b2World.h>
#include <QDebug>
#include "qbox2dcommon.h"
class QBox2DBody : public QGraphicsItemGroup
{
public:
explicit QBox2DBody(b2Body* qb2Body, QGraphicsItem *parent = 0);
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
Q_UNUSED(painter);
/*add this if you want to show a trace of the boundingRect*/
//painter->drawRect(childrenBoundingRect());
b2Vec2 position = getPosition();
float32 angle = getAngle();
qreal newRotation = -(angle * 360.0) / (2 * b2_pi);
setPos(position.x*sizeMultiplier, position.y*-sizeMultiplier);
if (!qFuzzyCompare(rotation(), newRotation))
setRotation(newRotation);
if (isSelected()) {
painter->setBrush(Qt::transparent);
painter->setPen(Qt::DashLine);
painter->drawRect(childrenBoundingRect());
}
}
b2Body* body() {
return q_b2Body;
}
b2Fixture* createFixture(const b2Shape* shape, float32 density, const QBrush& brush = QBrush(Qt::green));
b2Fixture* createFixture(const b2FixtureDef* def, const QBrush &brush = QBrush(Qt::green));
void destroyFixture (b2Fixture *fixture) {
removeFromGroup(q_b2FixtureManager[fixture]);
scene()->removeItem(q_b2FixtureManager[fixture]);
q_b2FixtureManager.remove(fixture);
q_b2Body->DestroyFixture(fixture);
}
void setTransform(const b2Vec2 &position, float32 angle) {
q_b2Body->SetTransform(position,angle);
}
const b2Transform& getTransform() const {
return q_b2Body->GetTransform();
}
const b2Vec2& getPosition() const {
return q_b2Body->GetPosition();
}
float32 getAngle() const {
return q_b2Body->GetAngle();
}
const b2Vec2& getWorldCenter() const {
return q_b2Body->GetWorldCenter();
}
const b2Vec2& getLocalCenter() const {
return q_b2Body->GetLocalCenter();
}
void setLinearVelocity (const b2Vec2 &v) {
q_b2Body->SetLinearVelocity(v);
}
b2Vec2 getLinearVelocity() const {
return q_b2Body->GetLinearVelocity();
}
b2Vec2 getLocalPoint (const b2Vec2 &worldPoint) const {
return q_b2Body->GetLocalPoint(worldPoint);
}
void setAngularVelocity(float32 omega) {
q_b2Body->SetAngularVelocity(omega);
}
float32 getAngularVelocity() const {
return q_b2Body->GetAngularVelocity();
}
void applyAngularImpulse(float32 impulse) {
q_b2Body->ApplyAngularImpulse(impulse);
}
void setAwake(bool flag) {
q_b2Body->SetAwake(flag);
}
bool isAwake() const {
return q_b2Body->IsAwake();
}
signals:
public slots:
private:
b2Body* q_b2Body;
QHash<b2Fixture*, QGraphicsItem*> q_b2FixtureManager;
void processFixture(b2Fixture *qb2Fixture, const QBrush &brush);
};
#endif // QBOX2DBODY_H