-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTest.cpp
More file actions
304 lines (265 loc) · 9.15 KB
/
Test.cpp
File metadata and controls
304 lines (265 loc) · 9.15 KB
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
302
303
304
#include "Test.h"
#include "Settings.h"
#include "CPhysics/Circle.h"
#include "CPhysics/Polygon.h"
#include <memory>
Test::Test()
{
world = World(Vectors2D(0.0f, -9.81f));
}
Test::~Test()
{
}
std::unique_ptr<Body> Test::createRandomObject(const Vectors2D& lowerBound, const Vectors2D& upperBound, const int maxRadius) const
{
std::unique_ptr<Body> b;
real radius = static_cast<float>(generateRandomNoInRange(5, maxRadius));
real x = generateRandomNoInRange(lowerBound.x + radius, upperBound.x - radius);
real y = generateRandomNoInRange(lowerBound.y + radius, upperBound.y - radius);
real rotation = generateRandomNoInRange(0.0f, 7.0f);
switch (coinFlip()) {
case 0:
b = std::make_unique<Body>(std::make_unique<Circle>(radius), x, y);
b->setOrientation(rotation);
break;
case 1:
const unsigned int sides = static_cast<unsigned int>(generateRandomNoInRange(3, 10));
b = std::make_unique<Body>(std::make_unique<Polygon>(radius, sides), x, y);
b->setOrientation(rotation);
break;
}
return b;
}
bool Test::overlap(const Body* b) const
{
for (Body* a : world.getBodies()) {
if (AABBOverLap(a, b)) {
return false;
}
}
return true;
}
void Test::generateRandomObjects(const Vectors2D& lowerBound, const Vectors2D& upperBound, int totalObjects, const int maxRadius) {
while (totalObjects > 0) {
std::unique_ptr<Body> b = createRandomObject(lowerBound, upperBound, maxRadius);
if (overlap(b.get())) {
world.addBody(std::move(b));
totalObjects--;
}
}
}
void Test::generateBoxOfObjects()
{
Body* top = world.createBody<Polygon>(-20.0f, 500.0f, 900.0f, 20.0f);
top->setDensity(0.0f);
Body* right = world.createBody<Polygon>(900.0f, 20.0f, 500.0f, 20.0f);
right->setOrientation(1.5708f);
right->setDensity(0.0f);
Body* bottom = world.createBody<Polygon>(20.0f, -500.0f, 900.0f, 20.0f);
bottom->setDensity(0.0f);
Body* left = world.createBody<Polygon>(-900.0f, -20.0f, 500.0f, 20.0f);
left->setOrientation(1.5708f);
left->setDensity(0.0f);
{
generateRandomObjects(Vectors2D(-880.0f, -480.0f), Vectors2D(880.0f, 480.0f), 25, 100);
world.setStaticWorld();
}
}
void Test::step(real dt, unsigned int solver_iterations)
{
world.step(dt, solver_iterations, settings.PENETRATION_ALLOWANCE, settings.PENETRATION_CORRECTION);
}
void Test::render()
{
for (ShadowCasting& p : shadowcasts) {
p.updateProjections(world.getBodies());
std::vector<RayAngleInformation> raydata = p.getRaydata();
for (unsigned int i = 0; i < raydata.size(); i++) {
Ray ray1 = raydata[i].getRAY();
if (ray1.getRayInformation().getB() == nullptr) continue;
Ray ray2 = raydata[i + 1 == raydata.size() ? 0 : i + 1].getRAY();
if (ray2.getRayInformation().getB() == nullptr) continue;
debugDraw.drawShadowPolygon(ray1, ray2, raydata[i].getRAY().getStartPoint(), settings.shadow);
}
debugDraw.flush();
}
if (settings.drawShapes) {
for (Body* b : world.getBodies()) {
switch (b->shape->getType()) {
case Shape::eCircle: {
Circle* circle = static_cast<Circle*>(b->shape.get());
const float radius = circle->getRadius();
Vectors2D line = circle->rotation * Vectors2D(1, 0);
if (b->mass == 0.0f)
debugDraw.drawSolidCircle(b->position, radius, line, settings.staticOutLine, settings.staticFill);
else
debugDraw.drawSolidCircle(b->position, radius, line, settings.shapeOutLine, settings.shapeFill);
}
break;
case Shape::ePolygon: {
Polygon* polygon = static_cast<Polygon*>(b->shape.get());
if (b->mass == 0.0f)
debugDraw.drawSolidPolygon(polygon->getVertices(), b->position, b->shape->rotation, polygon->getVertexCount(), settings.staticOutLine, settings.staticFill);
else
debugDraw.drawSolidPolygon(polygon->getVertices(), b->position, b->shape->rotation, polygon->getVertexCount(), settings.shapeOutLine, settings.shapeFill);
}
break;
}
}
}
if (settings.drawAABBs) {
for (Body* b : world.getBodies())
{
debugDraw.drawAABB(b->position, b->aabb.get(), settings.aabb);
}
}
if (settings.drawJoints) {
for (Joint* j : world.getJoints())
{
debugDraw.drawJoint(j, settings.joints);
}
}
for (Ray& r : rays)
{
debugDraw.drawRay(r, settings.rayToBody);
}
for (Rayscatter& r : rayscatters)
{
debugDraw.drawRayscatter(r, settings.rayToBody);
}
for (ProximityExplosion& p : proximityExplosions)
p.update(world.getBodies());
for (ProximityExplosion& p : proximityExplosions) {
debugDraw.drawCircle(p.getEpicentre(), static_cast<float>(p.getProximity()), settings.proximity);
p.updateLinesToBody();
for (const Vectors2D& v : p.getLinesToBodies())
{
debugDraw.drawLine(v, p.getEpicentre(), settings.linesToObjects);
}
}
for (RaycastExplosion& p : raycastExplosions) {
p.update(world.getBodies());
debugDraw.drawRayscatter(p.getRayscatter(), settings.scatterRays);
}
for (Slice& s : slices) {
Vectors2D epicenter = s.getStartpoint();
Vectors2D endPoint = (s.getDirection() * s.getDistance()) + s.getStartpoint();
debugDraw.drawLine(epicenter, endPoint, settings.projectedRay);
for (unsigned int i = 0; i < s.getIntersectingBodiesInfo().size(); i++) {
if ((i + 1) % 2 == 0) {
Vectors2D intersection1 = s.getIntersectingBodiesInfo()[i - 1].getCoord();
Vectors2D intersection2 = s.getIntersectingBodiesInfo()[i].getCoord();
debugDraw.drawLine(intersection1, intersection2, settings.rayToBody);
}
}
}
if (settings.drawContacts)
{
for (const Arbiter& contact : world.getContactsVector()) {
Vectors2D point = contact.getContacts(0);
Vectors2D line = (contact.getNormal().normal()) * settings.CROSS_LINE_SCALAR;
debugDraw.drawCross(point, line, settings.contactPoint);
line = line.normal();
debugDraw.drawCross(point, line, settings.contactPoint);
}
}
if (settings.drawCOMs) {
for (const Body* b : world.getBodies()) {
Vectors2D centre = b->position;
Vectors2D line = Vectors2D(static_cast<float>(settings.COM_RADIUS), 0.0f);
mul(b->shape->rotation, line);
debugDraw.drawCross(centre, line, settings.joints);
}
}
}
void Test::updateProximity(const Vectors2D& pw)
{
proximityExplosions[0].changeEpicentre(pw);
}
void Test::updateRaycast(const Vectors2D& pw)
{
raycastExplosions[0].getRayscatter().changeEpicentre(pw);
}
Body* Test::addPillar(std::unique_ptr<Body> b, real density)
{
b->restitution = 0.2f;
b->setDensity(density);
return world.addBody(std::move(b));
}
void Test::createTower(unsigned int floors, real x, real y)
{
const real height = 30.0f;
const real width = 8.0f;
x += width;
const real heightOfPillar = height + height;
const real widthOfPillar = width + width;
for (unsigned int k = 0; k < floors; k++) {
addPillar(std::make_unique<Body>(std::make_unique<Polygon>(width, height), x, y + height), 1.0f);
addPillar(std::make_unique<Body>(std::make_unique<Polygon>(width, height), x + heightOfPillar - widthOfPillar, y + height), 1.0f);
addPillar(std::make_unique<Body>(std::make_unique<Polygon>(height, width), x + height - width, y + heightOfPillar + width), 1.0f);
y += heightOfPillar + width + width;
}
}
void Test::slice(const int i)
{
slices[i].sliceObjects(world);
}
void Test::breakJoint(unsigned int i)
{
world.removeJoint(world.getJoints()[i]);
}
void Test::drawInstructions()
{
}
bool Test::isPointInside(const Body* b, const Vectors2D& startPoint)
{
if (b->shape->getType() == Shape::ePolygon) {
Polygon* poly = static_cast<Polygon*>(b->shape.get());
for (unsigned int i = 0; i < poly->getVertices().size(); i++) {
Vectors2D localPoint = startPoint - poly->body->position + (poly->body->shape->rotation * poly->getVertices()[i]);
if (dotProduct(localPoint, poly->body->shape->rotation * poly->getNormals()[i]) > 0.0f) {
return false;
}
}
}
else if (b->shape->getType() == Shape::eCircle) {
Circle* circle = static_cast<Circle*>(b->shape.get());
Vectors2D d = b->position - startPoint;
return !(d.len() > circle->getRadius());
}
return true;
}
void Test::generateRandomPolygon(const Vectors2D& location, const real minRadius, const real maxRadius) {
std::vector<Vectors2D> verts;
for (int i = 0; i < 30; i++) {
real scalar = generateRandomNoInRange(-minRadius, maxRadius);
Vectors2D direction = Vectors2D(generateRandomNoInRange(0.0f, 4.0f));
direction *= scalar;
verts.push_back(direction);
}
Body* b = world.createBody<Polygon>(location.x, location.y, verts);
b->setDensity(generateRandomNoInRange(1.0f, 4.0f));
b->restitution = generateRandomNoInRange(0.0f, 1.0f);
}
void Test::generateRandomCircle(const Vectors2D& location, const real minRadius, const real maxRadius)
{
Body* b = world.createBody<Circle>(location.x, location.y, generateRandomNoInRange(minRadius, maxRadius));
b->setDensity(generateRandomNoInRange(1.0f, 4.0f));
b->restitution = generateRandomNoInRange(0.0f, 1.0f);
}
void Test::buildShelf(real x, real y)
{
Body* shelf = world.createBody<Polygon>(x, y, 100.0f, 10.0f);
shelf->setDensity(0.0f);
const int boxes = 4;
for (unsigned int i = 0; i < boxes; i++) {
world.createBody<Polygon>(x, y + 30.0f + static_cast<float>(i * 40), 10.0f, 20.0f);
}
}
void Test::createParticleExplosion(const Vectors2D& location)
{
ParticleExplosion p = ParticleExplosion(location, 100, 10.0f, &world);
p.createParticles(0.5f, 100.0f, 5.0f);
p.applyBlastImpulse(100.0f);
particles.push_back(p);
};