forked from qml-box2d/qml-box2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box2dfixture.h
273 lines (222 loc) · 7.27 KB
/
box2dfixture.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* box2dfixture.h
* Copyright (c) 2010-2011 Thorbjørn Lindeijer <[email protected]>
* Copyright (c) 2011 Daker Fernandes Pinheiro <[email protected]>
* Copyright (c) 2011 Alessandro Portale <[email protected]>
*
* This file is part of the Box2D QML plugin.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef BOX2DFIXTURE_H
#define BOX2DFIXTURE_H
#include <QQuickItem>
#include <QFlags>
#include <Box2D.h>
class Box2DBody;
class Box2DFixture : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(float density READ density WRITE setDensity NOTIFY densityChanged)
Q_PROPERTY(float friction READ friction WRITE setFriction NOTIFY frictionChanged)
Q_PROPERTY(float restitution READ restitution WRITE setRestitution NOTIFY restitutionChanged)
Q_PROPERTY(bool sensor READ isSensor WRITE setSensor NOTIFY sensorChanged)
Q_PROPERTY(CategoryFlags categories READ categories WRITE setCategories NOTIFY categoriesChanged)
Q_PROPERTY(CategoryFlags collidesWith READ collidesWith WRITE setCollidesWith NOTIFY collidesWithChanged)
Q_PROPERTY(int groupIndex READ groupIndex WRITE setGroupIndex NOTIFY groupIndexChanged)
Q_ENUMS(CategoryFlag)
Q_FLAGS(CategoryFlags)
public:
explicit Box2DFixture(QQuickItem *parent = 0);
enum CategoryFlag {Category1 = 0x0001, Category2 = 0x0002, Category3 = 0x0004, Category4 = 0x0008,
Category5 = 0x0010, Category6 = 0x0020, Category7 = 0x0040, Category8 = 0x0080,
Category9 = 0x0100, Category10 = 0x0200, Category11 = 0x0400, Category12 = 0x0800,
Category13 = 0x1000, Category14 = 0x2000, Category15 = 0x4000, Category16 = 0x8000,
All = 0xFFFF, None=0x0000};
Q_DECLARE_FLAGS(CategoryFlags, CategoryFlag)
float density() const;
void setDensity(float density);
float friction() const;
void setFriction(float friction);
float restitution() const;
void setRestitution(float restitution);
bool isSensor() const;
void setSensor(bool sensor);
CategoryFlags categories() const;
void setCategories(CategoryFlags layers);
CategoryFlags collidesWith() const;
void setCollidesWith(CategoryFlags layers);
int groupIndex() const;
void setGroupIndex(int groupIndex);
void createFixture(b2Body *body);
virtual void scale() {}
Q_INVOKABLE Box2DBody *getBody() const;
protected:
b2Fixture *mFixture;
b2FixtureDef mFixtureDef;
b2Body *mBody;
float factorWidth;
float factorHeight;
virtual b2Shape *createShape() = 0;
void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry);
void applyShape(b2Shape * shape);
signals:
void densityChanged();
void frictionChanged();
void restitutionChanged();
void sensorChanged();
void categoriesChanged();
void collidesWithChanged();
void groupIndexChanged();
void beginContact(Box2DFixture *other);
void contactChanged(Box2DFixture *other);
void endContact(Box2DFixture *other);
private:
friend class Box2DWorld;
void emitBeginContact(Box2DFixture *other);
void emitContactChanged(Box2DFixture *other);
void emitEndContact(Box2DFixture *other);
};
Q_DECLARE_OPERATORS_FOR_FLAGS(Box2DFixture::CategoryFlags)
class Box2DBox : public Box2DFixture
{
Q_OBJECT
public:
explicit Box2DBox(QQuickItem *parent = 0) :
Box2DFixture(parent)
{}
void scale();
protected:
b2Shape *createShape();
b2Vec2 vertices[4];
};
class Box2DCircle : public Box2DFixture
{
Q_OBJECT
Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)
public:
explicit Box2DCircle(QQuickItem *parent = 0) :
Box2DFixture(parent)
{ }
float radius() const { return mRadius; }
void setRadius(float radius) {
if (mRadius == radius)
return;
mRadius = radius;
scale();
emit radiusChanged();
}
void scale();
signals:
void radiusChanged();
protected:
b2Shape *createShape();
private:
float mRadius;
};
class Box2DVerticesShape : public Box2DFixture
{
Q_OBJECT
Q_PROPERTY(QVariantList vertices READ vertices WRITE setVertices NOTIFY verticesChanged)
public:
explicit Box2DVerticesShape(QQuickItem *parent = 0) :
Box2DFixture(parent)
{ }
QVariantList vertices() const { return mVertices; }
void setVertices(const QVariantList &vertices) {
if (vertices == mVertices)
return;
mVertices = vertices;
emit verticesChanged();
}
signals:
void verticesChanged();
protected:
QVariantList mVertices;
b2Vec2 *scaleVertices();
virtual b2Shape *createShape(){ return NULL; }
};
class Box2DPolygon : public Box2DVerticesShape
{
public:
explicit Box2DPolygon(QQuickItem *parent = 0) :
Box2DVerticesShape(parent)
{ }
void scale();
protected:
b2Shape *createShape();
};
class Box2DChain : public Box2DVerticesShape
{
Q_OBJECT
Q_PROPERTY(bool loop READ loop WRITE setLoop NOTIFY loopChanged)
Q_PROPERTY(QPointF prevVertex READ prevVertex WRITE setPrevVertex NOTIFY prevVertexChanged)
Q_PROPERTY(QPointF nextVertex READ nextVertex WRITE setNextVertex NOTIFY nextVertexChanged)
public:
explicit Box2DChain(QQuickItem *parent = 0) :
Box2DVerticesShape(parent),
mLoop(false)
{ }
void scale();
bool loop() const { return mLoop; }
void setLoop(bool loop) {
mLoop = loop;
emit loopChanged();
}
QPointF prevVertex() const { return mPrevVertex; }
void setPrevVertex(QPointF &prevVertex) {
mPrevVertex = prevVertex;
prevVertexFlag = true;
}
QPointF nextVertex() const { return mNextVertex; }
void setNextVertex(QPointF &nextVertex) {
mNextVertex = nextVertex;
nextVertexFlag = true;
}
protected:
b2Shape *createShape();
bool mLoop;
bool prevVertexFlag;
bool nextVertexFlag;
QPointF mPrevVertex;
QPointF mNextVertex;
signals:
void loopChanged();
void prevVertexChanged();
void nextVertexChanged();
};
class Box2DEdge : public Box2DVerticesShape
{
Q_OBJECT
public:
explicit Box2DEdge(QQuickItem *parent = 0) :
Box2DVerticesShape(parent)
{ }
void scale();
protected:
b2Shape *createShape();
};
/**
* Convenience function to get the Box2DFixture wrapping a b2Fixture.
*/
inline Box2DFixture *toBox2DFixture(b2Fixture *fixture)
{
return static_cast<Box2DFixture*>(fixture->GetUserData());
}
#endif // BOX2DFIXTURE_H