Skip to content

Commit e397930

Browse files
committed
move jolt debug to lib
1 parent 8cda177 commit e397930

File tree

3 files changed

+50
-89
lines changed

3 files changed

+50
-89
lines changed

src/ai/can-chase/physics.ts

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { DNAComponent } from '@lib/dna/dna_component';
33
import { DNASystem } from '@lib/dna/dna_system';
44
import { Gfx3Jolt } from '@lib/gfx3_physics/gfx3_physics_jolt';
55
import { gfx3DebugRenderer } from '@lib/gfx3/gfx3_debug_renderer';
6+
import { drawShape } from '@lib/gfx3_physics/gfx3_physics_jolt_debug';
67
// ---------------------------------------------------------------------------------------
78
import { EntityComponent } from './entity';
89
import { InputComponent } from './input';
@@ -74,8 +75,6 @@ export class PhysicsSystem extends DNASystem {
7475
}
7576
}
7677

77-
78-
7978
onAfterUpdate(ts: number): void {
8079
const clampedDeltaMs = Math.min(ts, 16.67);
8180
console.log(`Physics update: ${clampedDeltaMs}ms`);
@@ -85,7 +84,7 @@ export class PhysicsSystem extends DNASystem {
8584
onAfterDraw(): void {
8685
console.log(`Drawing ${this.debugBodies.length} debug bodies`);
8786
for (const body of this.debugBodies) {
88-
DRAW_SHAPE(body.GetShape(), body.GetWorldTransform());
87+
drawShape(body.GetShape(), body.GetWorldTransform());
8988
}
9089
}
9190

@@ -271,46 +270,4 @@ export class PhysicsSystem extends DNASystem {
271270
settings.mBroadPhaseLayerInterface, 2, settings.mObjectLayerPairFilter, 2
272271
);
273272
}
274-
}
275-
276-
function DRAW_SHAPE(shape: any, matrix: any) {
277-
let vertexCount = 0;
278-
const finalVertices = [];
279-
280-
// Get triangle data
281-
const scale = new Gfx3Jolt.Vec3(1, 1, 1);
282-
const triContext = new Gfx3Jolt.ShapeGetTriangles(shape, Gfx3Jolt.AABox.prototype.sBiggest(), shape.GetCenterOfMass(), Gfx3Jolt.Quat.prototype.sIdentity(), scale);
283-
Gfx3Jolt.destroy(scale);
284-
285-
// Get a view on the triangle data (does not make a copy)
286-
const vertices = new Float32Array(Gfx3Jolt.HEAPF32.buffer, triContext.GetVerticesData(), triContext.GetVerticesSize() / Float32Array.BYTES_PER_ELEMENT);
287-
288-
// Now move the triangle data to a buffer and clone it so that we can free the memory from the C++ heap (which could be limited in size)
289-
for (let i = 0; i < vertices.length / 3; i += 3) {
290-
const v0 = [vertices[i * 3 + 0], vertices[i * 3 + 1], vertices[i * 3 + 2]];
291-
const v1 = [vertices[i * 3 + 3], vertices[i * 3 + 4], vertices[i * 3 + 5]];
292-
const v2 = [vertices[i * 3 + 6], vertices[i * 3 + 7], vertices[i * 3 + 8]];
293-
294-
finalVertices.push(...v0, 0, 1, 0);
295-
finalVertices.push(...v1, 0, 1, 0);
296-
297-
finalVertices.push(...v1, 0, 1, 0);
298-
finalVertices.push(...v2, 0, 1, 0);
299-
300-
finalVertices.push(...v2, 0, 1, 0);
301-
finalVertices.push(...v0, 0, 1, 0);
302-
vertexCount += 6;
303-
}
304-
305-
Gfx3Jolt.destroy(triContext);
306-
307-
gfx3DebugRenderer.drawVertices(finalVertices, vertexCount, [
308-
matrix.GetColumn4(0).GetX(), matrix.GetColumn4(0).GetY(), matrix.GetColumn4(0).GetZ(), matrix.GetColumn4(0).GetW(),
309-
matrix.GetColumn4(1).GetX(), matrix.GetColumn4(1).GetY(), matrix.GetColumn4(1).GetZ(), matrix.GetColumn4(1).GetW(),
310-
matrix.GetColumn4(2).GetX(), matrix.GetColumn4(2).GetY(), matrix.GetColumn4(2).GetZ(), matrix.GetColumn4(2).GetW(),
311-
matrix.GetColumn4(3).GetX(), matrix.GetColumn4(3).GetY(), matrix.GetColumn4(3).GetZ(), matrix.GetColumn4(3).GetW()
312-
]);
313-
}
314-
315-
316-
273+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Jolt from 'jolt-physics';
2+
// ---------------------------------------------------------------------------------------
3+
import { gfx3DebugRenderer } from '@lib/gfx3/gfx3_debug_renderer';
4+
import { Gfx3Jolt } from '@lib/gfx3_physics/gfx3_physics_jolt';
5+
6+
export const drawShape = (shape: Jolt.Shape, matrix: Jolt.RMat44) => {
7+
let vertexCount = 0;
8+
const finalVertices = [];
9+
10+
// Get triangle data
11+
const scale = new Gfx3Jolt.Vec3(1, 1, 1);
12+
const triContext = new Gfx3Jolt.ShapeGetTriangles(shape, Gfx3Jolt.AABox.prototype.sBiggest(), shape.GetCenterOfMass(), Gfx3Jolt.Quat.prototype.sIdentity(), scale);
13+
Gfx3Jolt.destroy(scale);
14+
15+
// Get a view on the triangle data (does not make a copy)
16+
const vertices = new Float32Array(Gfx3Jolt.HEAPF32.buffer, triContext.GetVerticesData(), triContext.GetVerticesSize() / Float32Array.BYTES_PER_ELEMENT);
17+
18+
// Now move the triangle data to a buffer and clone it so that we can free the memory from the C++ heap (which could be limited in size)
19+
for (let i = 0; i < vertices.length / 3; i += 3) {
20+
const v0 = [vertices[i * 3 + 0], vertices[i * 3 + 1], vertices[i * 3 + 2]];
21+
const v1 = [vertices[i * 3 + 3], vertices[i * 3 + 4], vertices[i * 3 + 5]];
22+
const v2 = [vertices[i * 3 + 6], vertices[i * 3 + 7], vertices[i * 3 + 8]];
23+
24+
finalVertices.push(...v0, 0, 1, 0);
25+
finalVertices.push(...v1, 0, 1, 0);
26+
27+
finalVertices.push(...v1, 0, 1, 0);
28+
finalVertices.push(...v2, 0, 1, 0);
29+
30+
finalVertices.push(...v2, 0, 1, 0);
31+
finalVertices.push(...v0, 0, 1, 0);
32+
vertexCount += 6;
33+
}
34+
35+
Gfx3Jolt.destroy(triContext);
36+
37+
gfx3DebugRenderer.drawVertices(finalVertices, vertexCount, [
38+
matrix.GetColumn4(0).GetX(), matrix.GetColumn4(0).GetY(), matrix.GetColumn4(0).GetZ(), matrix.GetColumn4(0).GetW(),
39+
matrix.GetColumn4(1).GetX(), matrix.GetColumn4(1).GetY(), matrix.GetColumn4(1).GetZ(), matrix.GetColumn4(1).GetW(),
40+
matrix.GetColumn4(2).GetX(), matrix.GetColumn4(2).GetY(), matrix.GetColumn4(2).GetZ(), matrix.GetColumn4(2).GetW(),
41+
matrix.GetColumn4(3).GetX(), matrix.GetColumn4(3).GetY(), matrix.GetColumn4(3).GetZ(), matrix.GetColumn4(3).GetW()
42+
]);
43+
}

src/templates/physics-jolt/physics.ts

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Jolt from 'jolt-physics';
22
// ---------------------------------------------------------------------------------------
33
import { dnaManager } from '@lib/dna/dna_manager';
4-
import { gfx3DebugRenderer } from '@lib/gfx3/gfx3_debug_renderer';
54
import { Gfx3Jolt } from '@lib/gfx3_physics/gfx3_physics_jolt';
65
import { DNASystem } from '@lib/dna/dna_system';
76
import { DNAComponent } from '@lib/dna/dna_component';
87
import { UT } from '@lib/core/utils';
8+
import { drawShape } from '@lib/gfx3_physics/gfx3_physics_jolt_debug';
99
// ---------------------------------------------------------------------------------------
1010
import { EntityComponent } from './entity';
1111
// ---------------------------------------------------------------------------------------
@@ -246,12 +246,12 @@ export class PhysicsSystem extends DNASystem {
246246
return;
247247
}
248248

249-
DRAW_SHAPE(physics.standingShape, physics.vCharacter.GetWorldTransform());
249+
drawShape(physics.standingShape, physics.vCharacter.GetWorldTransform());
250250
}
251251

252252
onAfterDraw(): void {
253253
for (const body of this.debugBodies) {
254-
DRAW_SHAPE(body.GetShape(), body.GetWorldTransform());
254+
drawShape (body.GetShape(), body.GetWorldTransform());
255255
}
256256
}
257257

@@ -354,43 +354,4 @@ function UNWRAP_VEC3(v: vec3): Jolt.Vec3 {
354354

355355
// function UNWRAP_QUAT(q: vec4): Jolt.Quat {
356356
// return new Jolt.Quat(q[0], q[1], q[2], q[3])
357-
// }
358-
359-
function DRAW_SHAPE(shape: Jolt.Shape, matrix: Jolt.RMat44) {
360-
let vertexCount = 0;
361-
const finalVertices = [];
362-
363-
// Get triangle data
364-
const scale = new Gfx3Jolt.Vec3(1, 1, 1);
365-
const triContext = new Gfx3Jolt.ShapeGetTriangles(shape, Gfx3Jolt.AABox.prototype.sBiggest(), shape.GetCenterOfMass(), Gfx3Jolt.Quat.prototype.sIdentity(), scale);
366-
Gfx3Jolt.destroy(scale);
367-
368-
// Get a view on the triangle data (does not make a copy)
369-
const vertices = new Float32Array(Gfx3Jolt.HEAPF32.buffer, triContext.GetVerticesData(), triContext.GetVerticesSize() / Float32Array.BYTES_PER_ELEMENT);
370-
371-
// Now move the triangle data to a buffer and clone it so that we can free the memory from the C++ heap (which could be limited in size)
372-
for (let i = 0; i < vertices.length / 3; i += 3) {
373-
const v0 = [vertices[i * 3 + 0], vertices[i * 3 + 1], vertices[i * 3 + 2]];
374-
const v1 = [vertices[i * 3 + 3], vertices[i * 3 + 4], vertices[i * 3 + 5]];
375-
const v2 = [vertices[i * 3 + 6], vertices[i * 3 + 7], vertices[i * 3 + 8]];
376-
377-
finalVertices.push(...v0, 0, 1, 0);
378-
finalVertices.push(...v1, 0, 1, 0);
379-
380-
finalVertices.push(...v1, 0, 1, 0);
381-
finalVertices.push(...v2, 0, 1, 0);
382-
383-
finalVertices.push(...v2, 0, 1, 0);
384-
finalVertices.push(...v0, 0, 1, 0);
385-
vertexCount += 6;
386-
}
387-
388-
Gfx3Jolt.destroy(triContext);
389-
390-
gfx3DebugRenderer.drawVertices(finalVertices, vertexCount, [
391-
matrix.GetColumn4(0).GetX(), matrix.GetColumn4(0).GetY(), matrix.GetColumn4(0).GetZ(), matrix.GetColumn4(0).GetW(),
392-
matrix.GetColumn4(1).GetX(), matrix.GetColumn4(1).GetY(), matrix.GetColumn4(1).GetZ(), matrix.GetColumn4(1).GetW(),
393-
matrix.GetColumn4(2).GetX(), matrix.GetColumn4(2).GetY(), matrix.GetColumn4(2).GetZ(), matrix.GetColumn4(2).GetW(),
394-
matrix.GetColumn4(3).GetX(), matrix.GetColumn4(3).GetY(), matrix.GetColumn4(3).GetZ(), matrix.GetColumn4(3).GetW()
395-
]);
396-
}
357+
// }

0 commit comments

Comments
 (0)