-
Notifications
You must be signed in to change notification settings - Fork 4
/
car.cpp
executable file
·301 lines (275 loc) · 10.1 KB
/
car.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <qmath.h>
#include "car.h"
Car::Car(GeneticAlgorithm *pAlghorithm, b2World *world):
algorithm (pAlghorithm),
b2world(world) {
b2BodyDef bodyDef;
init(bodyDef);
createCart();
createWheels(bodyDef);
updateTorque();
baseSpringForce = 7.5*carBody->GetMass();
update();
}
//public
void Car::breakFixture(const int index) {
breakCartFixture[index] = false;
int i = index%8;
if (index < 8) {
QColor *color = (QColor *)cartFixture[i]->GetUserData();
carBody->DestroyFixture(cartFixture[i]);
cartFixture[i] = NULL;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = carBody->GetPosition();
bodyDef.angle = carBody->GetAngle();
b2Body *piece = b2world->CreateBody(&bodyDef);
b2Fixture *f = piece->CreateFixture(&cartShapes[i], 2);
f->SetUserData(color);
pieces.push_back(f);
} else {
QColor *color = (QColor *)axleFixture[i]->GetUserData();
carBody->DestroyFixture(axleFixture[i]);
axleFixture[i] = NULL;
b2Fixture *f = axle[i]->CreateFixture(&axleShapes[i], 2);
f->SetUserData(color);
wheelOn[i] = BROKEN_WHEELS;
wheelsCount--;
}
}
void Car::deletePhisicsBody() {
QColor *color;
for (int i = 0; i < 8; i++) {
if (wheelOn[i] != NO_WHEELS || wheelOn[i] == BROKEN_WHEELS) {
if (wheelOn[i] != BROKEN_WHEELS)
b2world->DestroyJoint(spring[i]);
b2world->DestroyJoint(motor[i]);
for (b2Fixture *f = axle[i]->GetFixtureList(); f;
f = f->GetNext()) {
if (f->GetUserData())
delete (QColor *)f->GetUserData();
}
b2world->DestroyBody(axle[i]);
color = (QColor *)wheel[i]->GetUserData();
delete color;
b2world->DestroyBody(wheel[i]->GetBody());
}
}
for (b2Fixture *f = carBody->GetFixtureList(); f; f = f->GetNext()) {
if (f->GetUserData())
delete (QColor *)f->GetUserData();
}
b2world->DestroyBody(carBody);
QVectorIterator<b2Fixture *> piece(pieces);
while (piece.hasNext()) {
b2Fixture *p = piece.next();
color = (QColor *)p->GetUserData();
delete color;
b2world->DestroyBody(p->GetBody());
}
}
b2Body *Car::getAxleBody(const int index) {
return wheelOn[index] == NO_WHEELS? NULL: axle[index];
}
b2Fixture *Car::getAxleFixture(const int index) {
return wheelOn[index] == NO_WHEELS? NULL: axleFixture[index];
}
b2Body *Car::getBody() {
return carBody;
}
b2Fixture *Car::getCartFixture(const int index) {
return cartFixture[index];
}
b2Body *Car::getWheel(const int index) {
return wheelOn[index] == NO_WHEELS? NULL: wheel[index]->GetBody();
}
float Car::getMaxPossition() {
return TRACK_LENGTH < maxPossition? TRACK_LENGTH: maxPossition;
}
b2Body *Car::getPiece(const int index) {
return pieces[index]->GetBody();
}
int Car::getPiecesCount() {
return pieces.size();
}
b2Vec2 Car::getPossition() {
return carBody->GetPosition();
}
int Car::getRemainingTime() {
return (MAX_ITTERATION - itteration)/60;
}
float Car::getSpeed() {
b2Vec2 vel = carBody->GetLinearVelocity();
return qSqrt(qPow(vel.x, 2) + qPow(vel.y, 2));
}
float Car::getTime() {
return itteration/60.0f;
}
float Car::getTorque() {
return torque;
}
void Car::setBreakFixture(const int index, const bool val) {
breakCartFixture[index] = val;
}
void Car::update() {
for (int i = 0; i < 8; i++) {
if (wheelOn[i] >= 0) {
spring[i]->SetMaxMotorForce(baseSpringForce + 40*baseSpringForce*
qPow(spring[i]->GetJointTranslation(),
2));
spring[i]->SetMotorSpeed(-20*spring[i]->GetJointTranslation());
}
if (brokeNum < 7 && breakCartFixture[i]) {
brokeNum++;
breakFixture(i);
for (int j = 0; j < 8; j++) {
if (wheelOn[j] == i) {
b2world->DestroyJoint(spring[j]);
breakFixture(8 + j);
motor[j]->SetMotorSpeed(0);
motor[j]->SetMaxMotorTorque(0);
}
}
updateTorque();
}
if (breakCartFixture[i + 8]) {
b2world->DestroyJoint(spring[i]);
breakFixture(i+8);
motor[i]->SetMotorSpeed(0);
motor[i]->SetMaxMotorTorque(0);
updateTorque();
}
}
itteration++;
float possition = carBody->GetPosition().x;
maxPossition = qMax(maxPossition, possition);
if (maxPossition - prevPossition > 1) {
slow = 0;
prevPossition = maxPossition;
} else {
if (carBody->GetLinearVelocity().x < 1)
slow++;
}
float maxSlow = possition > 10? 300: 180;
if (slow >= maxSlow || possition >= TRACK_LENGTH ||
itteration > MAX_ITTERATION || possition < -10 || brokeNum >= 7)
emit stoped();
}
void Car::updateTorque() {
float totalMass = carBody->GetMass();
torque = wheelsCount? totalMass*MASS_MULT*15/qPow(2,float(wheelsCount) - 1):
0;
for (int i = 0; i < 8; i++) {
if (wheelOn[i] >= 0) {
motor[i]->SetMaxMotorTorque(torque);
}
}
}
void Car::createCart() {
b2Vec2 triangle[3];
triangle[0].Set(0,0);
for (int i = 0; i < 8; i++) {
float angle = algorithm->getCartAngle(i);
float magnitude = algorithm->getMagnitude(i);
float x = magnitude*qCos(angle);
float y = magnitude*qSin(angle);
float nextMagnitude = algorithm->getMagnitude((i + 1)%8);
float nextAngle = algorithm->getCartAngle((i + 1)%8);
float nextX = nextMagnitude*qCos(nextAngle);
float nextY = nextMagnitude*qSin(nextAngle);
triangle[0].Set(0 ,0);
triangle[1].Set(x, y);
triangle[2].Set(nextX, nextY);
cartShapes[i].Set(triangle, 3);
b2FixtureDef fixtureDef;
fixtureDef.shape = &cartShapes[i];
fixtureDef.density = 2;
fixtureDef.friction = 10;
fixtureDef.restitution = 0.05;
fixtureDef.filter.groupIndex = -1;
cartFixture[i] = createFixture(&fixtureDef, carBody,
algorithm->getColorCart(i));
setBreakFixture(i, false);
}
}
b2Fixture *Car::createFixture(const b2FixtureDef* def, b2Body *body,
const QColor color) {
b2Fixture *fixture = body->CreateFixture(def);
QColor *c = new QColor(color);
fixture->SetUserData(c);
return fixture;
}
void Car::createWheels(b2BodyDef &bodyDef) {
b2PrismaticJointDef prismaticJointDef;
prismaticJointDef.lowerTranslation = -0.1;
prismaticJointDef.upperTranslation = 0.25;
prismaticJointDef.enableLimit = true;
prismaticJointDef.enableMotor = true;
for (int i = 0; i < 8; i++) {
wheelOn[i] = algorithm->getWheelOn(i);
setBreakFixture(8 + i, false);
if (wheelOn[i] == NO_WHEELS)
continue;
wheelsCount++;
float angle = algorithm->getCartAngle(wheelOn[i]);
float magnitude = algorithm->getMagnitude(wheelOn[i]);
float x = magnitude*qCos(angle);
float y = magnitude*qSin(angle);
float axleAngle = algorithm->getAxleAngle(i);
axleShapes[i].SetAsBox(0.2, 0.1, b2Vec2(x, y), axleAngle);
b2FixtureDef fixtureDef;
fixtureDef.shape = &axleShapes[i];
fixtureDef.density = 2;
fixtureDef.friction = 10;
fixtureDef.restitution = 0.05;
fixtureDef.filter.groupIndex = -1;
axleFixture[i] = createFixture(&fixtureDef, carBody,
algorithm->getColorAxle(i));
axle[i] = b2world->CreateBody(&bodyDef);
b2PolygonShape polygonShape;
polygonShape.SetAsBox(0.2, 0.05, b2Vec2(x - 0.3*qCos(axleAngle),
y - 0.3*qSin(axleAngle)), axleAngle);
fixtureDef.shape = &polygonShape;
fixtureDef.density = 20;
createFixture(&fixtureDef, axle[i], algorithm->getColorAxle(i));
prismaticJointDef.Initialize(carBody, axle[i],
axle[i]->GetWorldCenter(),
b2Vec2(qCos(axleAngle), qSin(axleAngle)));
spring[i] = (b2PrismaticJoint *)
b2world->CreateJoint(&prismaticJointDef);
b2CircleShape circleShape;
circleShape.m_radius = algorithm->getWheelRadius(i);
b2FixtureDef wheelFixtureDef;
wheelFixtureDef.shape = &circleShape;
wheelFixtureDef.density = 0.5;
wheelFixtureDef.friction = 10;
wheelFixtureDef.restitution = 0.1;
wheelFixtureDef.filter.groupIndex = -1;
b2BodyDef wheelBodyDef;
wheelBodyDef.position.Set(carBody->GetPosition().x + x -
qCos(axleAngle)/2, carBody->GetPosition().y +
y - qSin(axleAngle)/2);
wheelBodyDef.allowSleep = false;
wheelBodyDef.type = b2_dynamicBody;
b2Body *bodyWheel = b2world->CreateBody(&wheelBodyDef);
wheel[i] = createFixture(&wheelFixtureDef, bodyWheel, Qt::black);
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.enableMotor = true;
revoluteJointDef.Initialize(axle[i], bodyWheel,
bodyWheel->GetWorldCenter());
motor[i] = (b2RevoluteJoint *)b2world->CreateJoint(&revoluteJointDef);
motor[i]->SetMotorSpeed(-6*M_PI);
}
}
void Car::init(b2BodyDef &bodyDef) {
bodyDef.position.Set(0, 4);
bodyDef.angle = 0;
bodyDef.type = b2_dynamicBody;
carBody = b2world->CreateBody(&bodyDef);
brokeNum = 0;
wheelsCount = 0;
maxPossition = 0;
prevPossition = 0;
itteration = 0;
slow = 0;
}