forked from salarali/UI2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.cpp
51 lines (44 loc) · 1.48 KB
/
simulator.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
#include "simulator.h"
#include <qtimer.h>
#include <qobject.h>
#include <qtimeline.h>
#include <qbasictimer.h>
#include <QTimerEvent>
Simulator::Simulator(QGraphicsScene *scene, QObject *parent): QObject(parent), timerId(0), scene(scene){
// Define the gravity vector.
b2Vec2 gravity(0.0f,-10.0f);
world = new b2World(gravity, true);
b2BodyDef bodyDef;
b2PolygonShape shapeDef;
b2FixtureDef fixtureDef;
bodyDef.position.Set(200.0f,-250);
shapeDef.SetAsBox(100.0f, 10.0f);
fixtureDef.shape = &shapeDef;
groundBody = world->CreateBody(&bodyDef);
groundBody->CreateFixture(&fixtureDef);
// Create lots of little colored triangles, random pos, rotation, color.
for (int i = 0; i < BODYCOUNT; ++i) {
//poly << QPointF(0, -10) << QPointF(-5, 0) << QPointF(5, 0);
Bot* polygon = bodyItems[i] = new Bot(world);
polygon->setPos(200+-20 + qrand() % 40,200+ -75 - qrand() % 150);
polygon->setRotation(qrand() % 360);
polygon->setBrush(QColor(128 + qrand() % 128, 128 + qrand() % 128, 128 + qrand() % 128));
polygon->_init();
scene->addItem(polygon);
}
}
Simulator::~Simulator(){
delete world;
}
void Simulator::timerEvent(QTimerEvent *event){
if (event->timerId() == timerId) {
world->Step(B2_TIMESTEP, B2_ITERATIONS, 0);
for (int i = 0; i < BODYCOUNT; ++i)
bodyItems[i]->_update();
}
QObject::timerEvent(event);
}
void Simulator::start(){
if (!timerId)
timerId = startTimer(1000 / 60.0);
}