-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathDebugRenderer.cpp
More file actions
565 lines (426 loc) · 24.3 KB
/
DebugRenderer.cpp
File metadata and controls
565 lines (426 loc) · 24.3 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2024 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include <reactphysics3d/utils/DebugRenderer.h>
#include <reactphysics3d/memory/MemoryManager.h>
#include <cassert>
#include <reactphysics3d/collision/shapes/ConvexMeshShape.h>
#include <reactphysics3d/collision/shapes/ConcaveMeshShape.h>
#include <reactphysics3d/collision/shapes/HeightFieldShape.h>
#include <reactphysics3d/collision/shapes/BoxShape.h>
#include <reactphysics3d/collision/shapes/SphereShape.h>
#include <reactphysics3d/collision/shapes/CapsuleShape.h>
#include <reactphysics3d/collision/Collider.h>
#include <reactphysics3d/engine/PhysicsWorld.h>
#include <reactphysics3d/containers/Pair.h>
using namespace reactphysics3d;
// Constructor
DebugRenderer::DebugRenderer(MemoryAllocator& allocator)
:mAllocator(allocator), mLines(allocator), mTriangles(allocator), mDisplayedDebugItems(0), mMapDebugItemWithColor(allocator),
mContactPointSphereRadius(DEFAULT_CONTACT_POINT_SPHERE_RADIUS), mContactNormalLength(DEFAULT_CONTACT_NORMAL_LENGTH),
mCollisionShapeNormalLength(DEFAULT_COLLISION_SHAPE_NORMAL_LENGTH) {
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLIDER_AABB, static_cast<uint32>(DebugColor::MAGENTA)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLIDER_BROADPHASE_AABB, static_cast<uint32>(DebugColor::YELLOW)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLISION_SHAPE, static_cast<uint32>(DebugColor::GREEN)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::CONTACT_POINT, static_cast<uint32>(DebugColor::RED)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::CONTACT_NORMAL, static_cast<uint32>(DebugColor::WHITE)));
mMapDebugItemWithColor.add(Pair<DebugItem, uint32>(DebugItem::COLLISION_SHAPE_NORMAL, static_cast<uint32>(DebugColor::BLUE)));
}
// Destructor
DebugRenderer::~DebugRenderer() {
}
// Clear all the debugging primitives (points, lines, triangles, ...)
void DebugRenderer::reset() {
mLines.clear();
mTriangles.clear();
}
// Draw an AABB
void DebugRenderer::drawAABB(const AABB& aabb, uint32 color) {
const Vector3& min = aabb.getMin();
const Vector3& max = aabb.getMax();
// Bottom edges
mLines.add(DebugLine(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color));
mLines.add(DebugLine(Vector3(max.x, min.y, max.z), Vector3(max.x, min.y, min.z), color));
mLines.add(DebugLine(Vector3(max.x, min.y, min.z), Vector3(min.x, min.y, min.z), color));
mLines.add(DebugLine(Vector3(min.x, min.y, min.z), Vector3(min.x, min.y, max.z), color));
// Top edges
mLines.add(DebugLine(Vector3(min.x, max.y, max.z), Vector3(max.x, max.y, max.z), color));
mLines.add(DebugLine(Vector3(max.x, max.y, max.z), Vector3(max.x, max.y, min.z), color));
mLines.add(DebugLine(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color));
mLines.add(DebugLine(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color));
// Side edges
mLines.add(DebugLine(Vector3(min.x, min.y, max.z), Vector3(min.x, max.y, max.z), color));
mLines.add(DebugLine(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color));
mLines.add(DebugLine(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color));
mLines.add(DebugLine(Vector3(min.x, min.y, min.z), Vector3(min.x, max.y, min.z), color));
}
// Draw a box
void DebugRenderer::drawBox(const Transform& transform, const BoxShape* boxShape, uint32 colorShape, uint32 colorShapeNormals) {
const bool drawShape = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE);
const bool drawNormal = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE_NORMAL);
Vector3 halfExtents = boxShape->getHalfExtents();
Vector3 vertices[8];
// Vertices
vertices[0] = transform * Vector3(-halfExtents.x, -halfExtents.y, halfExtents.z);
vertices[1] = transform * Vector3(halfExtents.x, -halfExtents.y, halfExtents.z);
vertices[2] = transform * Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z);
vertices[3] = transform * Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z);
vertices[4] = transform * Vector3(-halfExtents.x, halfExtents.y, halfExtents.z);
vertices[5] = transform * Vector3(halfExtents.x, halfExtents.y, halfExtents.z);
vertices[6] = transform * Vector3(halfExtents.x, halfExtents.y, -halfExtents.z);
vertices[7] = transform * Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z);
if (drawShape) {
// Triangle faces
mTriangles.add(DebugTriangle(vertices[0], vertices[1], vertices[5], colorShape));
mTriangles.add(DebugTriangle(vertices[0], vertices[5], vertices[4], colorShape));
mTriangles.add(DebugTriangle(vertices[1], vertices[2], vertices[6], colorShape));
mTriangles.add(DebugTriangle(vertices[1], vertices[6], vertices[5], colorShape));
mTriangles.add(DebugTriangle(vertices[2], vertices[3], vertices[6], colorShape));
mTriangles.add(DebugTriangle(vertices[3], vertices[7], vertices[6], colorShape));
mTriangles.add(DebugTriangle(vertices[0], vertices[7], vertices[3], colorShape));
mTriangles.add(DebugTriangle(vertices[0], vertices[4], vertices[7], colorShape));
mTriangles.add(DebugTriangle(vertices[0], vertices[2], vertices[1], colorShape));
mTriangles.add(DebugTriangle(vertices[0], vertices[3], vertices[2], colorShape));
mTriangles.add(DebugTriangle(vertices[5], vertices[6], vertices[4], colorShape));
mTriangles.add(DebugTriangle(vertices[4], vertices[6], vertices[7], colorShape));
}
if (drawNormal) {
// Face normals
for (int f=0; f < 6; f++) {
const HalfEdgeStructure::Face& face = boxShape->getFace(f);
const Vector3 facePoint = 0.25 * (boxShape->getVertexPosition(face.faceVertices[0]) + boxShape->getVertexPosition(face.faceVertices[1]) +
boxShape->getVertexPosition(face.faceVertices[2]) + boxShape->getVertexPosition(face.faceVertices[3]));
const Vector3 normalPoint = facePoint + mCollisionShapeNormalLength * boxShape->getFaceNormal(f);
mLines.add(DebugLine(transform * facePoint, transform * normalPoint, colorShapeNormals));
}
}
}
/// Draw a sphere
void DebugRenderer::drawSphere(const Vector3& position, decimal radius, uint32 color) {
Vector3 vertices[(NB_SECTORS_SPHERE + 1) * (NB_STACKS_SPHERE + 1) + (NB_SECTORS_SPHERE + 1)];
// Vertices
const decimal sectorStep = 2 * PI_RP3D / NB_SECTORS_SPHERE;
const decimal stackStep = PI_RP3D / NB_STACKS_SPHERE;
for (uint32 i = 0; i <= NB_STACKS_SPHERE; i++) {
const decimal stackAngle = PI_RP3D / 2 - i * stackStep;
const decimal radiusCosStackAngle = radius * std::cos(stackAngle);
const decimal z = radius * std::sin(stackAngle);
for (uint32 j = 0; j <= NB_SECTORS_SPHERE; j++) {
const decimal sectorAngle = j * sectorStep;
const decimal x = radiusCosStackAngle * std::cos(sectorAngle);
const decimal y = radiusCosStackAngle * std::sin(sectorAngle);
vertices[i * (NB_SECTORS_SPHERE + 1) + j] = position + Vector3(x, y, z);
}
}
// Faces
for (uint32 i = 0; i < NB_STACKS_SPHERE; i++) {
uint32 a1 = i * (NB_SECTORS_SPHERE + 1);
uint32 a2 = a1 + NB_SECTORS_SPHERE + 1;
for (uint32 j = 0; j < NB_SECTORS_SPHERE; j++, a1++, a2++) {
// 2 triangles per sector except for the first and last stacks
if (i != 0) {
mTriangles.add(DebugTriangle(vertices[a1], vertices[a2], vertices[a1 + 1], color));
}
if (i != (NB_STACKS_SPHERE - 1)) {
mTriangles.add(DebugTriangle(vertices[a1 + 1], vertices[a2], vertices[a2 + 1], color));
}
}
}
}
// Draw a capsule
void DebugRenderer::drawCapsule(const Transform& transform, decimal radius, decimal height, uint32 color) {
Vector3 vertices[(NB_SECTORS_SPHERE + 1) * (NB_STACKS_SPHERE + 1) + (NB_SECTORS_SPHERE + 1)];
const decimal halfHeight = decimal(0.5) * height;
// Use an even number of stacks
const uint32 nbStacks = NB_STACKS_SPHERE % 2 == 0 ? NB_STACKS_SPHERE : NB_STACKS_SPHERE - 1;
const uint32 nbHalfStacks = nbStacks / 2;
// Vertices
const decimal sectorStep = 2 * PI_RP3D / NB_SECTORS_SPHERE;
const decimal stackStep = PI_RP3D / nbStacks;
uint32 vertexIndex = 0;
// Top cap sphere vertices
for (uint32 i = 0; i <= nbHalfStacks; i++) {
const decimal stackAngle = PI_RP3D / 2 - i * stackStep;
const decimal radiusCosStackAngle = radius * std::cos(stackAngle);
const decimal y = radius * std::sin(stackAngle);
for (uint32 j = 0; j <= NB_SECTORS_SPHERE; j++) {
const decimal sectorAngle = j * sectorStep;
const decimal x = radiusCosStackAngle * std::sin(sectorAngle);
const decimal z = radiusCosStackAngle * std::cos(sectorAngle);
assert(vertexIndex < (NB_SECTORS_SPHERE + 1) * (nbStacks + 1) + (NB_SECTORS_SPHERE + 1));
vertices[vertexIndex] = transform * Vector3(x, y + halfHeight, z);
vertexIndex++;
}
}
// Bottom cap sphere vertices
for (uint32 i = 0; i <= nbHalfStacks; i++) {
const decimal stackAngle = PI_RP3D / 2 - (nbHalfStacks + i) * stackStep;
const decimal radiusCosStackAngle = radius * std::cos(stackAngle);
const decimal y = radius * std::sin(stackAngle);
for (uint32 j = 0; j <= NB_SECTORS_SPHERE; j++) {
const decimal sectorAngle = j * sectorStep;
const decimal x = radiusCosStackAngle * std::sin(sectorAngle);
const decimal z = radiusCosStackAngle * std::cos(sectorAngle);
assert(vertexIndex < (NB_SECTORS_SPHERE + 1) * (nbStacks + 1) + (NB_SECTORS_SPHERE + 1));
vertices[vertexIndex] = transform * Vector3(x, y - halfHeight, z);
vertexIndex++;
}
}
// Faces of the top cap sphere
for (uint32 i = 0; i < nbHalfStacks; i++) {
uint32 a1 = i * (NB_SECTORS_SPHERE + 1);
uint32 a2 = a1 + NB_SECTORS_SPHERE + 1;
for (uint32 j = 0; j < NB_SECTORS_SPHERE; j++, a1++, a2++) {
// 2 triangles per sector except for the first stack
if (i != 0) {
mTriangles.add(DebugTriangle(vertices[a1], vertices[a2], vertices[a1 + 1], color));
}
mTriangles.add(DebugTriangle(vertices[a1 + 1], vertices[a2], vertices[a2 + 1], color));
}
}
// Faces of the bottom cap sphere
for (uint32 i = 0; i < nbHalfStacks; i++) {
uint32 a1 = (nbHalfStacks + 1) * (NB_SECTORS_SPHERE + 1) + i * (NB_SECTORS_SPHERE + 1);
uint32 a2 = a1 + NB_SECTORS_SPHERE + 1;
for (uint32 j = 0; j < NB_SECTORS_SPHERE; j++, a1++, a2++) {
// 2 triangles per sector except for the last stack
mTriangles.add(DebugTriangle(vertices[a1], vertices[a2], vertices[a1 + 1], color));
if (i != (nbHalfStacks - 1)) {
mTriangles.add(DebugTriangle(vertices[a1 + 1], vertices[a2], vertices[a2 + 1], color));
}
}
}
// Faces of the cylinder between the two spheres
uint32 a1 = nbHalfStacks * (NB_SECTORS_SPHERE + 1);
uint32 a2 = a1 + NB_SECTORS_SPHERE + 1;
for (uint32 i = 0; i < NB_SECTORS_SPHERE; i++, a1++, a2++) {
mTriangles.add(DebugTriangle(vertices[a1 + 1], vertices[a2], vertices[a2 + 1], color));
mTriangles.add(DebugTriangle(vertices[a1], vertices[a2], vertices[a1 + 1], color));
}
}
// Draw a convex mesh
void DebugRenderer::drawConvexMesh(const Transform& transform, const ConvexMeshShape* convexMesh,
uint32 colorShape, uint32 colorShapeNormals) {
const bool drawShape = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE);
const bool drawNormal = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE_NORMAL);
// For each face of the convex mesh
for (uint32 f = 0; f < convexMesh->getNbFaces(); f++) {
const HalfEdgeStructure::Face& face = convexMesh->getFace(f);
assert(face.faceVertices.size() >= 3);
Vector3 centroid;
uint32 v1Index = face.faceVertices[0];
Vector3 v1 = convexMesh->getVertexPosition(v1Index);
v1 = transform * v1;
// Perform a fan triangulation of the convex polygon face
const uint32 nbFaceVertices = static_cast<uint32>(face.faceVertices.size());
for (uint32 v = 2; v < nbFaceVertices; v++) {
uint32 v2Index = face.faceVertices[v - 1];
uint32 v3Index = face.faceVertices[v];
Vector3 v2 = convexMesh->getVertexPosition(v2Index);
Vector3 v3 = convexMesh->getVertexPosition(v3Index);
v2 = transform * v2;
v3 = transform * v3;
if (v == 2) {
centroid += v1 + v2;
}
centroid += v3;
if (drawShape) {
mTriangles.add(DebugTriangle(v1, v2, v3, colorShape));
}
}
if (drawNormal) {
centroid /= nbFaceVertices;
const Vector3 normalPoint = centroid + transform.getOrientation() * convexMesh->getFaceNormal(f) * mCollisionShapeNormalLength;
mLines.add(DebugLine(centroid, normalPoint, colorShapeNormals));
}
}
}
// Draw a concave mesh shape
void DebugRenderer::drawConcaveMeshShape(const Transform& transform, const ConcaveMeshShape* concaveMeshShape,
uint32 colorShape, uint32 colorShapeNormals) {
const bool drawShape = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE);
const bool drawNormal = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE_NORMAL);
if (drawShape) {
// For each triangle of the mesh
for (uint32 t = 0; t < concaveMeshShape->getNbTriangles(); t++) {
Vector3 v1, v2, v3;
concaveMeshShape->getTriangleVertices(t, v1, v2, v3);
v1 = transform * v1;
v2 = transform * v2;
v3 = transform * v3;
mTriangles.add(DebugTriangle(v1, v2, v3, colorShape));
}
}
if (drawNormal) {
// For each vertex of the mesh
for (uint32 v = 0; v < concaveMeshShape->getNbVertices(); v++) {
const Vector3& vertex = transform * concaveMeshShape->getVertex(v);
const Vector3 normalPoint = vertex + transform.getOrientation() * concaveMeshShape->getVertexNormal(v) * mCollisionShapeNormalLength;
mLines.add(DebugLine(vertex, normalPoint, colorShapeNormals));
}
}
}
// Draw a height field shape
void DebugRenderer::drawHeightFieldShape(const Transform& transform, const HeightFieldShape* heightFieldShape,
uint32 colorShape, uint32 colorShapeNormals) {
const bool drawShape = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE);
const bool drawNormal = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE_NORMAL);
// For each sub-grid points (except the last ones one each dimension)
for (uint32 i = 0; i < heightFieldShape->getHeightField()->getNbColumns() - 1; i++) {
for (uint32 j = 0; j < heightFieldShape->getHeightField()->getNbRows() - 1; j++) {
// Compute the four point of the current quad
Vector3 p1 = heightFieldShape->getVertexAt(i, j);
Vector3 p2 = heightFieldShape->getVertexAt(i, j + 1);
Vector3 p3 = heightFieldShape->getVertexAt(i + 1, j);
Vector3 p4 = heightFieldShape->getVertexAt(i + 1, j + 1);
p1 = transform * p1;
p2 = transform * p2;
p3 = transform * p3;
p4 = transform * p4;
if (drawShape) {
mTriangles.add(DebugTriangle(p1, p2, p3, colorShape));
mTriangles.add(DebugTriangle(p3, p2, p4, colorShape));
}
if (drawNormal) {
// Compute the triangle normal
Vector3 triangle1Normal = (p2 - p1).cross(p3 - p1).getUnit();
const Vector3 normalPoint1 = p1 + triangle1Normal * mCollisionShapeNormalLength;
const Vector3 normalPoint2 = p2 + triangle1Normal * mCollisionShapeNormalLength;
const Vector3 normalPoint3 = p3 + triangle1Normal * mCollisionShapeNormalLength;
const Vector3 normalPoint4 = p4 + triangle1Normal * mCollisionShapeNormalLength;
mLines.add(DebugLine(p1, normalPoint1, colorShapeNormals));
mLines.add(DebugLine(p2, normalPoint2, colorShapeNormals));
mLines.add(DebugLine(p3, normalPoint3, colorShapeNormals));
mLines.add(DebugLine(p4, normalPoint4, colorShapeNormals));
}
}
}
}
// Draw the collision shape of a collider
void DebugRenderer::drawCollisionShapeOfCollider(const Collider* collider) {
uint32 colorShape = mMapDebugItemWithColor[DebugItem::COLLISION_SHAPE];
uint32 colorShapeNormals = mMapDebugItemWithColor[DebugItem::COLLISION_SHAPE_NORMAL];
switch (collider->getCollisionShape()->getName()) {
case CollisionShapeName::BOX:
{
const BoxShape* boxShape = static_cast<const BoxShape*>(collider->getCollisionShape());
drawBox(collider->getLocalToWorldTransform(), boxShape, colorShape, colorShapeNormals);
break;
}
case CollisionShapeName::SPHERE:
{
const SphereShape* sphereShape = static_cast<const SphereShape*>(collider->getCollisionShape());
drawSphere(collider->getLocalToWorldTransform().getPosition(), sphereShape->getRadius(), colorShape);
break;
}
case CollisionShapeName::CAPSULE:
{
const CapsuleShape* capsuleShape = static_cast<const CapsuleShape*>(collider->getCollisionShape());
drawCapsule(collider->getLocalToWorldTransform(), capsuleShape->getRadius(), capsuleShape->getHeight(), colorShape);
break;
}
case CollisionShapeName::CONVEX_MESH:
{
const ConvexMeshShape* convexMeshShape = static_cast<const ConvexMeshShape*>(collider->getCollisionShape());
drawConvexMesh(collider->getLocalToWorldTransform(), convexMeshShape, colorShape, colorShapeNormals);
break;
}
case CollisionShapeName::TRIANGLE_MESH:
{
const ConcaveMeshShape* concaveMeshShape = static_cast<const ConcaveMeshShape*>(collider->getCollisionShape());
drawConcaveMeshShape(collider->getLocalToWorldTransform(), concaveMeshShape, colorShape, colorShapeNormals);
break;
}
case CollisionShapeName::HEIGHTFIELD:
{
const HeightFieldShape* heighFieldShape = static_cast<const HeightFieldShape*>(collider->getCollisionShape());
drawHeightFieldShape(collider->getLocalToWorldTransform(), heighFieldShape, colorShape, colorShapeNormals);
break;
}
default:
{
assert(false);
}
}
}
// Generate the rendering primitives (triangles, lines, ...) of a physics world
void DebugRenderer::computeDebugRenderingPrimitives(const PhysicsWorld& world) {
const bool drawColliderAABB = getIsDebugItemDisplayed(DebugItem::COLLIDER_AABB);
const bool drawColliderBroadphaseAABB = getIsDebugItemDisplayed(DebugItem::COLLIDER_BROADPHASE_AABB);
const bool drawCollisionShape = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE);
const bool drawCollisionShapeNormals = getIsDebugItemDisplayed(DebugItem::COLLISION_SHAPE_NORMAL);
const uint32 nbRigidBodies = world.getNbRigidBodies();
// For each body of the world
for (uint32 b = 0; b < nbRigidBodies; b++) {
// Get a body
const Body* body = world.getRigidBody(b);
if (body->isActive() && body->isDebugEnabled()) {
// For each collider of the body
for (uint32 c = 0; c < body->getNbColliders(); c++) {
// Get a collider
const Collider* collider = body->getCollider(c);
// If we need to draw the collider AABB
if (drawColliderAABB) {
drawAABB(collider->getWorldAABB(), mMapDebugItemWithColor[DebugItem::COLLIDER_AABB]);
}
// If we need to draw the collider broad-phase AABB
if (drawColliderBroadphaseAABB) {
if (collider->getBroadPhaseId() != -1) {
drawAABB(world.mCollisionDetection.mBroadPhaseSystem.getFatAABB(collider->getBroadPhaseId()), mMapDebugItemWithColor[DebugItem::COLLIDER_BROADPHASE_AABB]);
}
}
// If we need to draw the collision shape
if (drawCollisionShape || drawCollisionShapeNormals) {
drawCollisionShapeOfCollider(collider);
}
}
}
}
}
// Called when some contacts occur
void DebugRenderer::onContact(const CollisionCallback::CallbackData& callbackData) {
// If we need to draw contact points
if (getIsDebugItemDisplayed(DebugItem::CONTACT_POINT) || getIsDebugItemDisplayed(DebugItem::CONTACT_NORMAL)) {
// For each contact pair
for (uint32 p = 0; p < callbackData.getNbContactPairs(); p++) {
CollisionCallback::ContactPair contactPair = callbackData.getContactPair(p);
if (contactPair.getEventType() != CollisionCallback::ContactPair::EventType::ContactExit) {
// For each contact point of the contact pair
for (uint32 c = 0; c < contactPair.getNbContactPoints(); c++) {
CollisionCallback::ContactPoint contactPoint = contactPair.getContactPoint(c);
Vector3 point = contactPair.getCollider1()->getLocalToWorldTransform() * contactPoint.getLocalPointOnCollider1();
if (getIsDebugItemDisplayed(DebugItem::CONTACT_POINT)) {
// Contact point
drawSphere(point, mContactPointSphereRadius, mMapDebugItemWithColor[DebugItem::CONTACT_POINT]);
}
if (getIsDebugItemDisplayed(DebugItem::CONTACT_NORMAL)) {
// Contact normal
mLines.add(DebugLine(point, point + contactPoint.getWorldNormal() * mContactNormalLength, mMapDebugItemWithColor[DebugItem::CONTACT_NORMAL]));
}
}
}
}
}
}