-
Notifications
You must be signed in to change notification settings - Fork 1
/
Barrier.cpp
71 lines (52 loc) · 1.55 KB
/
Barrier.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <QDebug>
#include "Barrier.h"
#include "main.h"
Barrier::Barrier(QGraphicsItem *parent)
{
QGraphicsItem::setParentItem(parent);
polygon.clear();
}
void Barrier::setPolygon(const QPolygonF &iPolygon)
{
polygon=iPolygon;
}
void Barrier::putToPhysicsWorld()
{
b2BodyDef bodyDef;
bodyDef.type=b2_staticBody;
bodyDef.position.Set(0.0, 0.0);
b2Body *body=physicsWorld->CreateBody(&bodyDef);
QPolygonF scenePolygon=mapToScene(polygon);
b2Vec2 vertices[10];
for(int i=0; i<scenePolygon.size(); i++) { // // for(int i=scenePolygon.size()-1; i>=0; i--)
vertices[i].Set(scenePolygon.at(i).x(), scenePolygon.at(i).y());
qDebug() << "Item coordinats:" << polygon.at(i).x() << polygon.at(i).y() << "Scene coordinats:" << scenePolygon.at(i).x() << scenePolygon.at(i).y() ;
}
b2PolygonShape polygonShape;
polygonShape.Set(vertices, scenePolygon.size());
// body->CreateShape(&polygonShape);
body->CreateFixture(&polygonShape, 0.0);
// Запоминается настроенное тело
physicsBody=body;
}
QRectF Barrier::boundingRect() const
{
return polygon.boundingRect();
}
QPainterPath Barrier::shape() const
{
QPainterPath path;
path.addPolygon( polygon );
return path;
}
void Barrier::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
// Filling
painter->setBrush(QColor(87, 113, 140));
// Edges
QPen pen;
pen.setWidth(0.1);
pen.setBrush(Qt::white);
painter->setPen(pen);
painter->drawPolygon( polygon );
}