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
| #include "mainwidget.h"
#include "ui_mainwidget.h"
#inclide <QGraphicsSceneMouseEvent>
#include <QDebug>
//Box2D units -> scene units
qreal fromB2(qreal value){
return value*SCALE;
}
//scene units -> Box2D units
qreal toB2 (qreal value) {
return value/SCALE;
}
MainWidget::MainWidget (QWidget *parent) :
QWidget (parent),
ui (new Ui::MainWidget)
{
ui -> setupUi(this);
world = new b2World(b2Vec2(0.00f, 10.00f));
scene = new Scene (0, 0, 8, 6, world);
ui -> graphicsView -> setScene (scene);
scene -> addRect (scene -> sceneRect());
scene->addRect(new GroundRect(world, QSize(4, 0.1), QPointF(2, 1), 15));
scene->addRect(new GroundRect(world, QSize(4, 0.1), QPointF(6, 2), -10));
scene->addRect(new GroundRect(world, QSize(4, 0.1), QPointF(2, 3), 15));
scene->addRect(new GroundRect(world, QSize(4, 0.1), QPointF(6, 4), -10));
scene->addRect(new GroundRect(world, QSize(8, 0.1), QPointF(4, 5.95), 0));
scene->addRect(new GroundRect(world, QSize(0.1, 1), QPointF(0.05, 5.5), 0));
frameTimer = new QTimer (this);
connect(frameTimer, SIGNAL(timeout()),
scene,, SLOT(advance()));
frameTimer->start(1000/60);
}
MainWidget::~MainWidget()
{
delete ui;
delete scene;
delete world;
}
Circle::Circle(b2World *world, qreal radius, QPointF initPos) :
QGraphicsEllipseItem(0)
{
setRect(-fromB2(radius), -fromB2(radius), fromB2(radius*2), fromB2(radius*2));
setBrush(QBrush(Qt::green));
setPos(fromB2(initPos.x()), fromB2(initPos.y()));
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(initPos.x(), initPos.y());
bodyDef.linearDamping = 0.2;
body = world->CreatBody(&bodyDef);
b2CircleShape shape;
shape.m_radius = radius;
b2Fixture* fixture = body->CreateFixture(&shape, 1.0f);
fixture->SetRestitution(0.7);
}
Circle::~Circle()
{
body->GetWorld()->DestroyBody(body);
}
void Circle::advance(int phase)
{
if (phase) {
setPos(fromB2(body->GetPosition().x),
fromB2(body->GetPosition().y));
}
}
GroundRect::GroundRect(b2World *world, QSizeF size, QPointF initPos, qreal angle):
QGraphicsRestItem(0)
{
setRect(-fromB2(size.width()/2), -fromB2(size.height()/2),
fromB2(size.width()), fromB2(size.height()));
setBrush(QBrush(Qt::gray));
setPos(fromB2(initPos.x()), fromB2(initPos.y()));
setRotation(angle);
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
bodyDef.position.Set(initPos.x(), initPos.y());
bodyDef.angle = 3.14*angle/180;
body = world->CreatBody(&bodyDef);
b2PolygonShape shape;
shape.SetAsBox(size.width()/2, size.height()/2);
body->CreateFixture(&shape, 0.0f);
}
GroundRect::~GroundRect()
{
body->GetWorld()->DestroyBody(body);
}
Scene::Scene(qreal x, qreal y, qreal width, qreal height, b2World *world)
: QGraphicsScene(fromB2(x), fromB2(y), fromB2(width), fromB2(height))
{
this->world = world;
}
void Scene::advance()
{
world->Step(1.00f/60.00, 6, 2);
QGraphicsScene::advance();
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
addItem(new Circle(world, o.2,
QPointF(toB2 (event->scenePos().x()),
toB2 (event->scenePos().y()))));
} |