-
Notifications
You must be signed in to change notification settings - Fork 4
/
world.cpp
executable file
·156 lines (136 loc) · 3.97 KB
/
world.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
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
#include <qmath.h>
#include <QTime>
#include <QDebug>
#include "world.h"
World::World() {
qsrand(QDateTime::currentDateTime().toTime_t());
algorithm = new GeneticAlgorithm();
connect(algorithm, SIGNAL(freeCallListNumber(uint)),
SIGNAL(freeCallListNumber(uint)));
algorithm->init();
algorithm->nextCar();
uptime = 0;
init();
}
World::~World() {
algorithm->deleteLater();
destroy();
}
//public
void World::addSparksList(const int impulses, const b2Vec2 pos,
const QColor *color) {
if (impulses > 32) {
sparkStuct spark;
spark.count = qMin(impulses/4, 32);
spark.pos = pos;
spark.color = color;
sparkList.push_back(spark);
}
}
GeneticAlgorithm *World::getAlgorithm() {
return algorithm;
}
Car *World::getCar() {
return car;
}
b2Vec2 World::getCenter() {
return car->getPossition();
}
Track *World::getTrack() {
return track;
}
b2Body *World::getSpark(const int index) {
return sparks[index]->GetBody();
}
int World::getSparkCount() {
return sparks.size();
}
float World::getUptime() {
return uptime;
}
void World::step() {
car->update();
updateSparks();
b2world->Step(TIME_STEP, ITERATIONS, ITERATIONS);
uptime += TIME_STEP;
}
//private
void World::destroy() {
while (sparks.size()) {
destroySpark(0);
}
sparks.clear();
car->deletePhisicsBody();
car->deleteLater();
delete track;
delete contactListener;
delete b2world;
}
void World::destroySpark(const int index) {
QColor *color = (QColor *)sparks[index]->GetUserData();
delete color;
b2world->DestroyBody(sparks[index]->GetBody());
sparks.remove(index);
}
void World::init() {
qsrand(QDateTime::currentDateTime().toTime_t());
b2Vec2 gravity(0.0, GRAVITY);
b2world = new b2World(gravity);
b2world->SetContinuousPhysics(true);
b2world->SetAutoClearForces(true);
contactListener = new ContactListener(this);
b2world->SetContactListener(contactListener);
track = new Track(b2world);
car = new Car(algorithm, b2world);
connect(car, SIGNAL(stoped()), SLOT(carStoped()));
emit creteNewCar();
qsrand(car->getBody()->GetMass());
}
//slots
void World::carStoped() {
algorithm->setScoreAndTime(car->getMaxPossition(), car->getTime());
algorithm->nextCar();
destroy();
init();
}
//private
void World::updateSparks() {
while (sparkList.size()) {
sparkStuct sparkEntry = sparkList.at(0);
sparkList.remove(0);
for (int i = 0; i < sparkEntry.count; i++) {
if (sparks.size() >= MAX_SPARK_COUNT)
continue;
b2PolygonShape shape;
shape.SetAsBox(float(qrand())/float(RAND_MAX)/30+0.02,
float(qrand())/float(RAND_MAX)/30+0.02, b2Vec2(0,0), 0);
b2FixtureDef fixtureDef;
fixtureDef.shape = &shape;
fixtureDef.filter.groupIndex = -1;
fixtureDef.filter.maskBits = 2;
fixtureDef.filter.categoryBits = 3;
fixtureDef.density = 0.5;
fixtureDef.restitution = 0.7;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = sparkEntry.pos;
b2Body *spark = b2world->CreateBody(&bodyDef);
b2Fixture *fixture = spark->CreateFixture(&fixtureDef);
fixture->SetUserData(new QColor(*sparkEntry.color));
float speed = qMax(3.0f, car->getSpeed());
float x = (float(qrand())/float(RAND_MAX)*0.75 + 0.25)*speed*2 - speed;
float y = (float(qrand())/float(RAND_MAX)*0.75 + 0.25)*speed;
spark->SetLinearVelocity(b2Vec2(x, y));
sparks.push_back(fixture);
}
}
int i = 0;
while (i < sparks.size()) {
b2Vec2 vel = sparks[i]->GetBody()->GetLinearVelocity();
if (qAbs(vel.x) < 0.01 && qAbs(vel.y) < 0.01) {
destroySpark(i);
continue;
}
i++;
}
}