forked from sudnikand/QBox2D
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqscene.cpp
49 lines (35 loc) · 1.28 KB
/
qscene.cpp
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
#include "qscene.h"
QScene::QScene(QObject * parent) :
QGraphicsScene(parent)
{
}
void QScene::removeItem(QBox2DItem *item){
QGraphicsScene::removeItem(item->graphics());
}
void QScene::addItem(QBox2DItem *item){
qDebug()<<"Add an Item";
const b2Shape *s = item->body()->GetFixtureList()->GetShape();
if (!s) return;
b2Shape::Type shapeType = s->GetType();
QAbstractGraphicsShapeItem *graphics;
if (shapeType == b2Shape::e_polygon){
const b2PolygonShape* shape = static_cast<const b2PolygonShape*>(s);
QPolygonF polygon;
for (int32 i = 0; i< shape->GetVertexCount(); ++i){
b2Vec2 v = shape->GetVertex(i);
polygon.append(QPointF(v.x,v.y));
}
graphics = new QGraphicsPolygonItem(polygon);
}
else if (shapeType == b2Shape::e_circle){
const b2CircleShape* shape = static_cast<const b2CircleShape*>(s);
float32 radius = shape->m_radius;
QRectF rect(-radius, -radius, radius*2, radius*2);
graphics = new QGraphicsEllipseItem(rect);
}
graphics->setPos( item->position().x, item->position().y);
graphics->setRotation(RAD2ANG(item->rotation()));
graphics->setBrush(item->color());
item->setGraphics(graphics);
QGraphicsScene::addItem(graphics);
}