@threenative/physics 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/dist/{Area3D-CvwDbfU6.d.ts → Area3D-BQDiAwQb.d.ts} +211 -69
- package/dist/index.d.ts +73 -4
- package/dist/index.js +886 -175
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +832 -136
- package/dist/navigation/index.d.ts +1 -1
- package/dist/navigation/index.js +201 -4678
- package/package.json +26 -5
package/dist/native/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Matrix4, Vector3, Quaternion, Box3, Object3D, Mesh, InstancedMesh } from 'three';
|
|
2
|
+
|
|
1
3
|
// src/handles.ts
|
|
2
4
|
function physicsHandle(raw) {
|
|
3
5
|
return { raw };
|
|
@@ -26,10 +28,58 @@ var PHYSICS_TRANSFORM_STRIDE = 8;
|
|
|
26
28
|
var PHYSICS_COLLISION_EVENT_STRIDE = 4;
|
|
27
29
|
var PHYSICS_SLEEP_STATE_STRIDE = 2;
|
|
28
30
|
var MAX_PHYSICS_QUERY_RESULTS = 1024;
|
|
31
|
+
var F32_EPSILON = Math.fround(2 ** -23);
|
|
32
|
+
var F32_EPSILON_SQUARED = Math.fround(F32_EPSILON * F32_EPSILON);
|
|
33
|
+
function f32LengthSquared(values) {
|
|
34
|
+
let lengthSquared = 0;
|
|
35
|
+
for (const value of values) {
|
|
36
|
+
const component = Math.fround(value);
|
|
37
|
+
lengthSquared = Math.fround(lengthSquared + Math.fround(component * component));
|
|
38
|
+
}
|
|
39
|
+
return lengthSquared;
|
|
40
|
+
}
|
|
41
|
+
function effectiveContinuousCollision(type, requested) {
|
|
42
|
+
return type === "dynamic" && (requested ?? true);
|
|
43
|
+
}
|
|
29
44
|
function requireFiniteVector(vector, label) {
|
|
30
45
|
if (typeof vector?.x !== "number" || typeof vector.y !== "number" || typeof vector.z !== "number" || !Number.isFinite(vector.x) || !Number.isFinite(vector.y) || !Number.isFinite(vector.z))
|
|
31
46
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
32
47
|
}
|
|
48
|
+
function requireFiniteRotation(rotation, label) {
|
|
49
|
+
if (typeof rotation?.x !== "number" || typeof rotation.y !== "number" || typeof rotation.z !== "number" || typeof rotation.w !== "number" || !Number.isFinite(rotation.x) || !Number.isFinite(rotation.y) || !Number.isFinite(rotation.z) || !Number.isFinite(rotation.w))
|
|
50
|
+
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z, w }.`);
|
|
51
|
+
if (f32LengthSquared([rotation.x, rotation.y, rotation.z, rotation.w]) <= F32_EPSILON)
|
|
52
|
+
throw new Error(`TN_PHYSICS_INVALID: ${label} must not have zero length.`);
|
|
53
|
+
}
|
|
54
|
+
function requirePhysicsJointCreateOptions(options) {
|
|
55
|
+
if (typeof options !== "object" || options === null)
|
|
56
|
+
throw new Error("IPhysicsSimulation joint options must be an object.");
|
|
57
|
+
if (options.type !== "pin" && options.type !== "hinge" && options.type !== "fixed")
|
|
58
|
+
throw new Error("IPhysicsSimulation joint options have an unknown type.");
|
|
59
|
+
if (!Number.isSafeInteger(options.bodyA) || options.bodyA < 0 || !Number.isSafeInteger(options.bodyB) || options.bodyB < 0 || options.bodyA === options.bodyB)
|
|
60
|
+
throw new Error("IPhysicsSimulation joint options must reference two distinct body ids.");
|
|
61
|
+
requireFiniteVector(options.anchorA, "joint anchorA");
|
|
62
|
+
requireFiniteVector(options.anchorB, "joint anchorB");
|
|
63
|
+
if (options.type === "hinge") {
|
|
64
|
+
if (options.axis === void 0) throw new Error("IPhysicsSimulation hinge requires an axis.");
|
|
65
|
+
requireFiniteVector(options.axis, "joint axis");
|
|
66
|
+
if (f32LengthSquared([options.axis.x, options.axis.y, options.axis.z]) <= F32_EPSILON_SQUARED)
|
|
67
|
+
throw new Error("IPhysicsSimulation hinge axis must not have zero length.");
|
|
68
|
+
if (options.limit !== void 0) {
|
|
69
|
+
if (typeof options.limit !== "object" || options.limit === null)
|
|
70
|
+
throw new Error("IPhysicsSimulation hinge limit must contain ordered finite bounds.");
|
|
71
|
+
if (!Number.isFinite(options.limit.lower) || !Number.isFinite(options.limit.upper) || options.limit.lower > options.limit.upper)
|
|
72
|
+
throw new Error("IPhysicsSimulation hinge limit must contain ordered finite bounds.");
|
|
73
|
+
}
|
|
74
|
+
} else if (options.axis !== void 0 || options.limit !== void 0) {
|
|
75
|
+
throw new Error("IPhysicsSimulation joint limits and axes are only valid for a hinge.");
|
|
76
|
+
}
|
|
77
|
+
if (options.type !== "fixed" && (options.frameA !== void 0 || options.frameB !== void 0))
|
|
78
|
+
throw new Error("IPhysicsSimulation joint frames are only valid for a fixed joint.");
|
|
79
|
+
if (options.frameA !== void 0) requireFiniteRotation(options.frameA, "joint frameA");
|
|
80
|
+
if (options.frameB !== void 0) requireFiniteRotation(options.frameB, "joint frameB");
|
|
81
|
+
return options;
|
|
82
|
+
}
|
|
33
83
|
var selectedBackend;
|
|
34
84
|
function installPhysicsSimulationBackend(backend) {
|
|
35
85
|
selectedBackend = backend;
|
|
@@ -221,18 +271,24 @@ function growUint32(buffer, minimum) {
|
|
|
221
271
|
while (length < minimum) length *= 2;
|
|
222
272
|
return length === buffer.length ? buffer : new Uint32Array(length);
|
|
223
273
|
}
|
|
274
|
+
var NATIVE_CHARACTER_STATE_STRIDE = 6;
|
|
224
275
|
function createNativePhysicsSimulation(raw, version) {
|
|
225
276
|
const bodyIds = /* @__PURE__ */ new Set();
|
|
226
277
|
const bodyHandles = /* @__PURE__ */ new Map();
|
|
227
|
-
const
|
|
278
|
+
const jointBodies = /* @__PURE__ */ new Map();
|
|
228
279
|
const characterIds = /* @__PURE__ */ new Set();
|
|
229
280
|
const characterState = /* @__PURE__ */ new Map();
|
|
281
|
+
const characterStatePresent = /* @__PURE__ */ new Set();
|
|
230
282
|
let characterStates = new Float32Array(48);
|
|
231
283
|
const rayOutput = new Float32Array(8);
|
|
232
284
|
let areaPairs = new Uint32Array(32);
|
|
233
285
|
const areaIntersections = /* @__PURE__ */ new Map();
|
|
286
|
+
const emptyAreaIntersections = /* @__PURE__ */ new Set();
|
|
287
|
+
let bodyTransforms = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 8);
|
|
288
|
+
const bodyTransformRows = /* @__PURE__ */ new Map();
|
|
234
289
|
let characterStateDirty = true;
|
|
235
290
|
let areaIntersectionsDirty = true;
|
|
291
|
+
let bodyTransformsDirty = true;
|
|
236
292
|
let disposed = false;
|
|
237
293
|
const requireLive = () => {
|
|
238
294
|
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
@@ -240,6 +296,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
240
296
|
const invalidateObservations = () => {
|
|
241
297
|
characterStateDirty = true;
|
|
242
298
|
areaIntersectionsDirty = true;
|
|
299
|
+
bodyTransformsDirty = true;
|
|
243
300
|
};
|
|
244
301
|
const bodyHandle = (id) => {
|
|
245
302
|
const handle = bodyHandles.get(id);
|
|
@@ -262,23 +319,56 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
262
319
|
position: finiteQueryVector(hit.position, "query hit position")
|
|
263
320
|
};
|
|
264
321
|
};
|
|
322
|
+
const refreshBodyTransforms = () => {
|
|
323
|
+
if (!bodyTransformsDirty) return;
|
|
324
|
+
const required = Math.max(1, bodyIds.size) * PHYSICS_TRANSFORM_STRIDE;
|
|
325
|
+
if (bodyTransforms.length < required) bodyTransforms = new Float32Array(required);
|
|
326
|
+
const count = raw.readVisibleTransforms(bodyTransforms);
|
|
327
|
+
bodyTransformRows.clear();
|
|
328
|
+
for (let index = 0; index < count; index += 1) {
|
|
329
|
+
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
330
|
+
const id = bodyTransforms[offset];
|
|
331
|
+
if (id === void 0 || !Number.isInteger(id) || id < 0)
|
|
332
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed visible transform record");
|
|
333
|
+
bodyTransformRows.set(id, offset);
|
|
334
|
+
}
|
|
335
|
+
bodyTransformsDirty = false;
|
|
336
|
+
};
|
|
337
|
+
const scalarAt = (offset) => {
|
|
338
|
+
const value = bodyTransforms[offset];
|
|
339
|
+
if (value === void 0 || !Number.isFinite(value))
|
|
340
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: visible transform contains a non-finite scalar");
|
|
341
|
+
return value;
|
|
342
|
+
};
|
|
265
343
|
const refreshCharacterState = () => {
|
|
266
344
|
if (!characterStateDirty) return;
|
|
267
|
-
const required = Math.max(1, characterIds.size) *
|
|
345
|
+
const required = Math.max(1, characterIds.size) * NATIVE_CHARACTER_STATE_STRIDE;
|
|
268
346
|
if (characterStates.length < required) characterStates = new Float32Array(required);
|
|
347
|
+
characterStates.fill(Number.NaN, 0, required);
|
|
269
348
|
const stateCount = raw.readCharacterStates(characterStates);
|
|
270
|
-
|
|
349
|
+
characterStatePresent.clear();
|
|
271
350
|
for (let index = 0; index < stateCount; index += 1) {
|
|
272
|
-
const offset = index *
|
|
351
|
+
const offset = index * NATIVE_CHARACTER_STATE_STRIDE;
|
|
273
352
|
const id = characterStates[offset];
|
|
274
353
|
const grounded = characterStates[offset + 1];
|
|
275
354
|
const groundCollider = characterStates[offset + 2];
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
355
|
+
const normalX = characterStates[offset + 3];
|
|
356
|
+
const normalY = characterStates[offset + 4];
|
|
357
|
+
const normalZ = characterStates[offset + 5];
|
|
358
|
+
if (id === void 0 || grounded === void 0 || groundCollider === void 0 || normalX === void 0 || normalY === void 0 || normalZ === void 0 || !Number.isInteger(id) || id < 0 || grounded !== 0 && grounded !== 1 || !Number.isFinite(groundCollider) || !Number.isFinite(normalX) || !Number.isFinite(normalY) || !Number.isFinite(normalZ))
|
|
359
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed six-float character state");
|
|
360
|
+
let state = characterState.get(id);
|
|
361
|
+
if (state === void 0) {
|
|
362
|
+
state = { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } };
|
|
363
|
+
characterState.set(id, state);
|
|
364
|
+
}
|
|
365
|
+
state.grounded = grounded === 1;
|
|
366
|
+
state.groundCollider = groundCollider < 0 ? void 0 : groundCollider;
|
|
367
|
+
state.groundBody = groundCollider < 0 ? void 0 : bodyHandles.get(groundCollider);
|
|
368
|
+
state.groundNormal.x = normalX;
|
|
369
|
+
state.groundNormal.y = normalY;
|
|
370
|
+
state.groundNormal.z = normalZ;
|
|
371
|
+
characterStatePresent.add(id);
|
|
282
372
|
}
|
|
283
373
|
characterStateDirty = false;
|
|
284
374
|
};
|
|
@@ -296,15 +386,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
296
386
|
areaPairs = growUint32(areaPairs, Math.min(maximum, areaPairs.length * 2));
|
|
297
387
|
}
|
|
298
388
|
}
|
|
299
|
-
areaIntersections.clear();
|
|
300
|
-
for (const areaId of areaIds) areaIntersections.set(areaId, /* @__PURE__ */ new Set());
|
|
389
|
+
for (const members of areaIntersections.values()) members.clear();
|
|
301
390
|
for (let index = 0; index < pairCount; index += 1) {
|
|
302
391
|
const offset = index * 2;
|
|
303
392
|
const areaId = areaPairs[offset];
|
|
304
|
-
const
|
|
305
|
-
if (areaId === void 0 ||
|
|
393
|
+
const bodyId2 = areaPairs[offset + 1];
|
|
394
|
+
if (areaId === void 0 || bodyId2 === void 0)
|
|
306
395
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed area intersection");
|
|
307
|
-
areaIntersections.get(areaId)?.add(
|
|
396
|
+
areaIntersections.get(areaId)?.add(bodyId2);
|
|
308
397
|
}
|
|
309
398
|
areaIntersectionsDirty = false;
|
|
310
399
|
};
|
|
@@ -313,6 +402,13 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
313
402
|
rawEventQueue: raw,
|
|
314
403
|
rawWorld: raw,
|
|
315
404
|
createBody: (options) => {
|
|
405
|
+
requireLive();
|
|
406
|
+
if (!Number.isFinite(options.mass) || options.mass < 0)
|
|
407
|
+
throw new Error("Physics body mass must be a finite non-negative number.");
|
|
408
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
409
|
+
throw new Error("Physics body continuousCollision must be a boolean.");
|
|
410
|
+
requireFiniteVector(options.position, "body position");
|
|
411
|
+
requireFiniteRotation(options.rotation, "body rotation");
|
|
316
412
|
const shape = primitiveShape(options.shape);
|
|
317
413
|
const sensor = requirePhysicsBodySensor(options);
|
|
318
414
|
const id = raw.createBody({
|
|
@@ -323,7 +419,11 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
323
419
|
rotation: options.rotation,
|
|
324
420
|
sensor,
|
|
325
421
|
shape,
|
|
326
|
-
type: options.type
|
|
422
|
+
type: options.type,
|
|
423
|
+
continuousCollision: effectiveContinuousCollision(
|
|
424
|
+
options.type,
|
|
425
|
+
options.continuousCollision
|
|
426
|
+
)
|
|
327
427
|
});
|
|
328
428
|
if (!Number.isInteger(id) || id < 0)
|
|
329
429
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid body id");
|
|
@@ -331,8 +431,13 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
331
431
|
const bodyHandleValue = physicsBodyHandle(id, rawHandle, options.entity);
|
|
332
432
|
bodyIds.add(id);
|
|
333
433
|
bodyHandles.set(id, bodyHandleValue);
|
|
334
|
-
if (sensor)
|
|
335
|
-
|
|
434
|
+
if (sensor) {
|
|
435
|
+
areaIntersections.set(id, /* @__PURE__ */ new Set());
|
|
436
|
+
}
|
|
437
|
+
if (options.type === "character") {
|
|
438
|
+
characterIds.add(id);
|
|
439
|
+
characterState.set(id, { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } });
|
|
440
|
+
}
|
|
336
441
|
invalidateObservations();
|
|
337
442
|
return {
|
|
338
443
|
body: bodyHandleValue,
|
|
@@ -341,20 +446,49 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
341
446
|
rawShape: opaqueNativeShape(shape)
|
|
342
447
|
};
|
|
343
448
|
},
|
|
449
|
+
createJoint: (jointOptions) => {
|
|
450
|
+
requireLive();
|
|
451
|
+
const normalized = requirePhysicsJointCreateOptions(jointOptions);
|
|
452
|
+
if (!bodyIds.has(normalized.bodyA) || !bodyIds.has(normalized.bodyB))
|
|
453
|
+
throw new Error("TN_PHYSICS_UNKNOWN_BODY: joint references an unknown body.");
|
|
454
|
+
if (raw.createJoint === void 0)
|
|
455
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
456
|
+
const id = raw.createJoint(normalized);
|
|
457
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
458
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid joint id");
|
|
459
|
+
if (jointBodies.has(id))
|
|
460
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned a duplicate joint id");
|
|
461
|
+
jointBodies.set(id, [normalized.bodyA, normalized.bodyB]);
|
|
462
|
+
return id;
|
|
463
|
+
},
|
|
344
464
|
configureCharacter: (id, options) => {
|
|
465
|
+
requireLive();
|
|
345
466
|
raw.configureCharacter(id, options);
|
|
346
467
|
invalidateObservations();
|
|
347
468
|
},
|
|
348
469
|
removeBody: (id) => {
|
|
470
|
+
requireLive();
|
|
349
471
|
raw.removeBody(id);
|
|
350
472
|
bodyIds.delete(id);
|
|
351
473
|
bodyHandles.delete(id);
|
|
352
|
-
|
|
474
|
+
for (const [jointId, bodies] of jointBodies) {
|
|
475
|
+
if (bodies[0] === id || bodies[1] === id) jointBodies.delete(jointId);
|
|
476
|
+
}
|
|
477
|
+
areaIntersections.delete(id);
|
|
353
478
|
characterIds.delete(id);
|
|
354
479
|
characterState.delete(id);
|
|
480
|
+
characterStatePresent.delete(id);
|
|
355
481
|
invalidateObservations();
|
|
356
482
|
},
|
|
483
|
+
removeJoint: (id) => {
|
|
484
|
+
if (disposed || !jointBodies.has(id)) return;
|
|
485
|
+
if (raw.removeJoint === void 0)
|
|
486
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
487
|
+
raw.removeJoint(id);
|
|
488
|
+
jointBodies.delete(id);
|
|
489
|
+
},
|
|
357
490
|
setBodyTransform: (id, position) => {
|
|
491
|
+
requireLive();
|
|
358
492
|
if (raw.setBodyTransform === void 0)
|
|
359
493
|
throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
|
|
360
494
|
raw.setBodyTransform(id, position);
|
|
@@ -375,6 +509,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
375
509
|
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
376
510
|
raw.applyBodyForce(id, force);
|
|
377
511
|
},
|
|
512
|
+
applyBodyForceAtPoint: (id, force, point) => {
|
|
513
|
+
requireLive();
|
|
514
|
+
requireFiniteVector(force, "force");
|
|
515
|
+
requireFiniteVector(point, "force point");
|
|
516
|
+
if (raw.applyBodyForceAtPoint === void 0)
|
|
517
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
518
|
+
raw.applyBodyForceAtPoint(id, force, point);
|
|
519
|
+
},
|
|
378
520
|
setBodyLinearVelocity: (id, velocity) => {
|
|
379
521
|
requireLive();
|
|
380
522
|
requireFiniteVector(velocity, "linearVelocity");
|
|
@@ -391,11 +533,13 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
391
533
|
return velocity;
|
|
392
534
|
},
|
|
393
535
|
step: (deltaTime, inputSnapshot) => {
|
|
536
|
+
requireLive();
|
|
394
537
|
requirePhysicsStepInput(deltaTime, inputSnapshot, (id) => bodyIds.has(id));
|
|
395
538
|
raw.step(deltaTime, inputSnapshot);
|
|
396
539
|
invalidateObservations();
|
|
397
540
|
},
|
|
398
541
|
readVisibleTransforms: (buffer) => {
|
|
542
|
+
requireLive();
|
|
399
543
|
requirePhysicsRenderBuffer(buffer, bodyIds.size);
|
|
400
544
|
return raw.readVisibleTransforms(buffer);
|
|
401
545
|
},
|
|
@@ -405,13 +549,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
405
549
|
return raw.readBodySleepStates(buffer);
|
|
406
550
|
},
|
|
407
551
|
intersectRay: (value) => {
|
|
552
|
+
requireLive();
|
|
408
553
|
const query = requirePhysicsRayQuery(value);
|
|
409
554
|
const hit = raw.intersectRay(query, rayOutput);
|
|
410
555
|
if (typeof hit === "number") {
|
|
411
556
|
if (hit === 0) return void 0;
|
|
412
557
|
if (hit !== 1)
|
|
413
558
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
|
|
414
|
-
const
|
|
559
|
+
const bodyId2 = rayOutput[0];
|
|
415
560
|
const position = {
|
|
416
561
|
x: rayOutput[1],
|
|
417
562
|
y: rayOutput[2],
|
|
@@ -423,9 +568,9 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
423
568
|
z: rayOutput[6]
|
|
424
569
|
};
|
|
425
570
|
const distance = rayOutput[7];
|
|
426
|
-
if (
|
|
571
|
+
if (bodyId2 === void 0 || !Number.isSafeInteger(bodyId2) || bodyId2 < 0 || distance === void 0)
|
|
427
572
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
|
|
428
|
-
const base2 = nativeHit({ bodyId, position });
|
|
573
|
+
const base2 = nativeHit({ bodyId: bodyId2, position });
|
|
429
574
|
if (!Number.isFinite(distance) || distance < 0)
|
|
430
575
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
|
|
431
576
|
return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
|
|
@@ -441,6 +586,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
441
586
|
};
|
|
442
587
|
},
|
|
443
588
|
intersectShape: (value) => {
|
|
589
|
+
requireLive();
|
|
444
590
|
const query = requirePhysicsShapeQuery(value);
|
|
445
591
|
const hits = raw.intersectShape({
|
|
446
592
|
collisionMask: query.collisionMask,
|
|
@@ -454,21 +600,44 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
454
600
|
return hits.map(nativeHit);
|
|
455
601
|
},
|
|
456
602
|
intersectPoint: (value) => {
|
|
603
|
+
requireLive();
|
|
457
604
|
const query = requirePhysicsPointQuery(value);
|
|
458
605
|
const hits = raw.intersectPoint(query);
|
|
459
606
|
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
460
607
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed point query result");
|
|
461
608
|
return hits.map(nativeHit);
|
|
462
609
|
},
|
|
610
|
+
readBodyTransform: (id) => {
|
|
611
|
+
requireLive();
|
|
612
|
+
refreshBodyTransforms();
|
|
613
|
+
const offset = bodyTransformRows.get(id);
|
|
614
|
+
if (offset === void 0) return void 0;
|
|
615
|
+
return {
|
|
616
|
+
position: {
|
|
617
|
+
x: scalarAt(offset + 1),
|
|
618
|
+
y: scalarAt(offset + 2),
|
|
619
|
+
z: scalarAt(offset + 3)
|
|
620
|
+
},
|
|
621
|
+
rotation: {
|
|
622
|
+
x: scalarAt(offset + 4),
|
|
623
|
+
y: scalarAt(offset + 5),
|
|
624
|
+
z: scalarAt(offset + 6),
|
|
625
|
+
w: scalarAt(offset + 7)
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
},
|
|
463
629
|
readCharacterState: (id) => {
|
|
630
|
+
requireLive();
|
|
464
631
|
refreshCharacterState();
|
|
465
|
-
return characterState.get(id);
|
|
632
|
+
return characterStatePresent.has(id) ? characterState.get(id) : void 0;
|
|
466
633
|
},
|
|
467
634
|
areaIntersections: (id) => {
|
|
635
|
+
requireLive();
|
|
468
636
|
refreshAreaIntersections();
|
|
469
|
-
return areaIntersections.get(id) ??
|
|
637
|
+
return areaIntersections.get(id) ?? emptyAreaIntersections;
|
|
470
638
|
},
|
|
471
639
|
drainCollisionEvents: (buffer) => {
|
|
640
|
+
requireLive();
|
|
472
641
|
requirePhysicsEventBuffer(buffer);
|
|
473
642
|
return raw.drainCollisionEvents(buffer);
|
|
474
643
|
},
|
|
@@ -477,10 +646,12 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
477
646
|
disposed = true;
|
|
478
647
|
raw.dispose();
|
|
479
648
|
bodyIds.clear();
|
|
649
|
+
bodyTransformRows.clear();
|
|
480
650
|
bodyHandles.clear();
|
|
481
|
-
|
|
651
|
+
jointBodies.clear();
|
|
482
652
|
characterIds.clear();
|
|
483
653
|
characterState.clear();
|
|
654
|
+
characterStatePresent.clear();
|
|
484
655
|
areaIntersections.clear();
|
|
485
656
|
characterStateDirty = false;
|
|
486
657
|
areaIntersectionsDirty = false;
|
|
@@ -513,13 +684,27 @@ installPhysicsSimulationBackend({
|
|
|
513
684
|
}
|
|
514
685
|
});
|
|
515
686
|
|
|
687
|
+
// src/transformRecord.ts
|
|
688
|
+
function bulkTransformValue(values, index) {
|
|
689
|
+
const value = values[index];
|
|
690
|
+
if (value === void 0 || !Number.isFinite(value))
|
|
691
|
+
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
692
|
+
return value;
|
|
693
|
+
}
|
|
694
|
+
|
|
516
695
|
// src/Area3D.ts
|
|
517
696
|
var MAX_CONTACT_LOG = 1e3;
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
if (
|
|
521
|
-
throw new Error("
|
|
522
|
-
|
|
697
|
+
var IDENTITY_ROTATION = { w: 1, x: 0, y: 0, z: 0 };
|
|
698
|
+
function resolveInitialTransform(source, fallbackPosition) {
|
|
699
|
+
if (source.object !== void 0 && source.position !== void 0)
|
|
700
|
+
throw new Error("A physics node accepts either object or position, not both.");
|
|
701
|
+
if (source.object !== void 0)
|
|
702
|
+
return { position: source.object.position, rotation: source.object.quaternion };
|
|
703
|
+
if (source.position !== void 0)
|
|
704
|
+
return { position: source.position, rotation: IDENTITY_ROTATION };
|
|
705
|
+
if (fallbackPosition !== void 0)
|
|
706
|
+
return { position: fallbackPosition, rotation: IDENTITY_ROTATION };
|
|
707
|
+
throw new Error("A physics node requires either object or position.");
|
|
523
708
|
}
|
|
524
709
|
var Area3D = class {
|
|
525
710
|
entity;
|
|
@@ -540,13 +725,18 @@ var Area3D = class {
|
|
|
540
725
|
this.#simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
541
726
|
this.#physics = options.physics;
|
|
542
727
|
this.entity = options.entity;
|
|
543
|
-
Object.assign(this.#position, options
|
|
544
|
-
const
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
728
|
+
Object.assign(this.#position, resolveInitialTransform(options, { x: 0, y: 0, z: 0 }).position);
|
|
729
|
+
const base = options.shape.descriptor;
|
|
730
|
+
const groups = interactionGroups(
|
|
731
|
+
options.collisionLayer ?? base.collisionLayer,
|
|
732
|
+
options.collisionMask ?? base.collisionMask
|
|
733
|
+
);
|
|
734
|
+
const shape = {
|
|
735
|
+
...base,
|
|
736
|
+
collisionLayer: groups >>> 16,
|
|
737
|
+
collisionMask: groups & 65535,
|
|
738
|
+
sensor: true
|
|
739
|
+
};
|
|
550
740
|
const registration = this.#simulation.createBody({
|
|
551
741
|
mass: 0,
|
|
552
742
|
entity: this.entity,
|
|
@@ -556,7 +746,6 @@ var Area3D = class {
|
|
|
556
746
|
shape,
|
|
557
747
|
type: "kinematic"
|
|
558
748
|
});
|
|
559
|
-
options.shape.bindRaw(registration.rawShape);
|
|
560
749
|
this.body = registration.body;
|
|
561
750
|
this.collider = registration.collider;
|
|
562
751
|
this.#physics?.addArea(this);
|
|
@@ -583,14 +772,22 @@ var Area3D = class {
|
|
|
583
772
|
/** Called by the shared plugin before a bulk step. */
|
|
584
773
|
writeKinematic(buffer, offset) {
|
|
585
774
|
if (this.#disposed) return;
|
|
586
|
-
buffer.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
775
|
+
buffer[offset] = this.body.id;
|
|
776
|
+
buffer[offset + 1] = this.#position.x;
|
|
777
|
+
buffer[offset + 2] = this.#position.y;
|
|
778
|
+
buffer[offset + 3] = this.#position.z;
|
|
779
|
+
buffer[offset + 4] = 0;
|
|
780
|
+
buffer[offset + 5] = 0;
|
|
781
|
+
buffer[offset + 6] = 0;
|
|
782
|
+
buffer[offset + 7] = 1;
|
|
590
783
|
}
|
|
591
784
|
applyTransform(values, offset) {
|
|
592
|
-
const
|
|
593
|
-
|
|
785
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
786
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
787
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
788
|
+
this.#position.x = x;
|
|
789
|
+
this.#position.y = y;
|
|
790
|
+
this.#position.z = z;
|
|
594
791
|
}
|
|
595
792
|
handleCollision(body, started) {
|
|
596
793
|
if (this.#disposed || !this.#monitoring) return;
|
|
@@ -631,14 +828,6 @@ var Area3D = class {
|
|
|
631
828
|
this.#listeners.bodyExited.clear();
|
|
632
829
|
}
|
|
633
830
|
};
|
|
634
|
-
|
|
635
|
-
// src/CharacterBody3D.ts
|
|
636
|
-
function finiteTransform2(values, offset) {
|
|
637
|
-
const result = Array.from({ length: 8 }, (_, index) => values[offset + index]);
|
|
638
|
-
if (result.some((value) => value === void 0 || !Number.isFinite(value)))
|
|
639
|
-
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
640
|
-
return result;
|
|
641
|
-
}
|
|
642
831
|
var CharacterBody3D = class {
|
|
643
832
|
body;
|
|
644
833
|
collider;
|
|
@@ -650,6 +839,12 @@ var CharacterBody3D = class {
|
|
|
650
839
|
maxFallSpeed;
|
|
651
840
|
oneWayLayers;
|
|
652
841
|
grounded = false;
|
|
842
|
+
/** Contact normal from the completed physics step; reset to +Y while airborne. */
|
|
843
|
+
groundNormal = new Vector3(0, 1, 0);
|
|
844
|
+
/** Portable handle for the body currently supporting this character. */
|
|
845
|
+
groundBody;
|
|
846
|
+
/** Radians between `groundNormal` and world up (+Y). */
|
|
847
|
+
slopeAngle = 0;
|
|
653
848
|
#simulation;
|
|
654
849
|
#physics;
|
|
655
850
|
#desired = { x: 0, y: 0, z: 0 };
|
|
@@ -710,18 +905,29 @@ var CharacterBody3D = class {
|
|
|
710
905
|
this.#physics?.add(this);
|
|
711
906
|
}
|
|
712
907
|
move(desiredTranslation) {
|
|
713
|
-
this.#desired =
|
|
908
|
+
this.#desired.x = desiredTranslation.x;
|
|
909
|
+
this.#desired.y = desiredTranslation.y;
|
|
910
|
+
this.#desired.z = desiredTranslation.z;
|
|
714
911
|
this.#sliding = false;
|
|
715
912
|
}
|
|
913
|
+
/**
|
|
914
|
+
* Queue velocity-based motion for the shared bulk physics step.
|
|
915
|
+
*
|
|
916
|
+
* @remarks
|
|
917
|
+
* This does not move `object` synchronously. The solver writes the transform after the step, so
|
|
918
|
+
* reading `object.position` immediately after this call still observes the previous transform.
|
|
919
|
+
* Three.js `Vector3` instances are mutable, so do not retain `object.position` itself as the
|
|
920
|
+
* snapshot: use `const before = object.position.clone()` (or copy its `x`, `y`, and `z` scalars).
|
|
921
|
+
* Compare that snapshot with `object.position` only after the deferred bulk physics step,
|
|
922
|
+
* typically on the next frame, to measure the motion that the solver applied.
|
|
923
|
+
*/
|
|
716
924
|
moveAndSlide(dt) {
|
|
717
925
|
if (!Number.isFinite(dt) || dt < 0)
|
|
718
926
|
throw new Error("CharacterBody3D.moveAndSlide requires a finite non-negative dt.");
|
|
719
927
|
this.velocity.y = Math.max(this.velocity.y + this.gravity * dt, -this.maxFallSpeed);
|
|
720
|
-
this.#desired =
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
z: this.velocity.z * dt
|
|
724
|
-
};
|
|
928
|
+
this.#desired.x = this.velocity.x * dt;
|
|
929
|
+
this.#desired.y = this.velocity.y * dt;
|
|
930
|
+
this.#desired.z = this.velocity.z * dt;
|
|
725
931
|
this.#sliding = true;
|
|
726
932
|
}
|
|
727
933
|
/** Called by the shared plugin before a bulk step. */
|
|
@@ -730,19 +936,17 @@ var CharacterBody3D = class {
|
|
|
730
936
|
this.#beforeY = this.object.position.y;
|
|
731
937
|
this.#desiredY = this.#desired.y;
|
|
732
938
|
const carry = this.#sliding && this.grounded && this.velocity.y <= 0 && this.#groundCollider !== void 0 ? this.#physics?.kinematicMotion?.(this.#groundCollider) : void 0;
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
offset
|
|
745
|
-
);
|
|
939
|
+
const carryX = carry?.x ?? 0;
|
|
940
|
+
const carryY = carry?.y ?? 0;
|
|
941
|
+
const carryZ = carry?.z ?? 0;
|
|
942
|
+
buffer[offset] = this.body.id;
|
|
943
|
+
buffer[offset + 1] = this.object.position.x + this.#desired.x + carryX;
|
|
944
|
+
buffer[offset + 2] = this.object.position.y + this.#desired.y + carryY;
|
|
945
|
+
buffer[offset + 3] = this.object.position.z + this.#desired.z + carryZ;
|
|
946
|
+
buffer[offset + 4] = this.object.quaternion.x;
|
|
947
|
+
buffer[offset + 5] = this.object.quaternion.y;
|
|
948
|
+
buffer[offset + 6] = this.object.quaternion.z;
|
|
949
|
+
buffer[offset + 7] = this.object.quaternion.w;
|
|
746
950
|
}
|
|
747
951
|
syncToPhysics() {
|
|
748
952
|
}
|
|
@@ -753,24 +957,44 @@ var CharacterBody3D = class {
|
|
|
753
957
|
this.#simulation.setBodyTransform(this.body.id, position);
|
|
754
958
|
this.object.position.set(position.x, position.y, position.z);
|
|
755
959
|
this.velocity.set(0, 0, 0);
|
|
756
|
-
this.#desired =
|
|
960
|
+
this.#desired.x = 0;
|
|
961
|
+
this.#desired.y = 0;
|
|
962
|
+
this.#desired.z = 0;
|
|
757
963
|
this.#sliding = false;
|
|
758
|
-
this.#
|
|
759
|
-
this.grounded = false;
|
|
964
|
+
this.#clearGroundState();
|
|
760
965
|
}
|
|
761
966
|
applyTransform(values, offset) {
|
|
762
|
-
const
|
|
967
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
968
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
969
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
970
|
+
const qx = bulkTransformValue(values, offset + 4);
|
|
971
|
+
const qy = bulkTransformValue(values, offset + 5);
|
|
972
|
+
const qz = bulkTransformValue(values, offset + 6);
|
|
973
|
+
const qw = bulkTransformValue(values, offset + 7);
|
|
763
974
|
const state = this.#simulation.readCharacterState?.(this.body.id);
|
|
764
975
|
this.grounded = state?.grounded ?? (this.#sliding && this.#desiredY < 0 && y - this.#beforeY > this.#desiredY + 1e-4);
|
|
765
976
|
this.#groundCollider = state?.groundCollider;
|
|
977
|
+
if (this.grounded && state?.groundNormal !== void 0) {
|
|
978
|
+
this.groundNormal.set(state.groundNormal.x, state.groundNormal.y, state.groundNormal.z);
|
|
979
|
+
this.groundBody = state.groundBody;
|
|
980
|
+
this.slopeAngle = Math.acos(Math.max(-1, Math.min(1, this.groundNormal.y)));
|
|
981
|
+
} else if (!this.grounded) {
|
|
982
|
+
this.#clearGroundState();
|
|
983
|
+
}
|
|
766
984
|
if (this.#sliding && this.grounded && this.velocity.y < 0) this.velocity.y = 0;
|
|
767
985
|
this.object.position.set(x, y, z);
|
|
768
986
|
this.object.quaternion.set(qx, qy, qz, qw);
|
|
769
|
-
this.#desired =
|
|
987
|
+
this.#desired.x = 0;
|
|
988
|
+
this.#desired.y = 0;
|
|
989
|
+
this.#desired.z = 0;
|
|
770
990
|
this.#sliding = false;
|
|
771
991
|
}
|
|
772
992
|
syncFromPhysics() {
|
|
773
|
-
|
|
993
|
+
if (this.#simulation.readBodyTransform === void 0)
|
|
994
|
+
throw new Error(
|
|
995
|
+
"TN_PHYSICS_READ_TRANSFORM_MISSING: the active physics backend cannot read body transforms."
|
|
996
|
+
);
|
|
997
|
+
const transform = this.#simulation.readBodyTransform(this.body.id);
|
|
774
998
|
if (transform === void 0) return;
|
|
775
999
|
this.object.position.set(transform.position.x, transform.position.y, transform.position.z);
|
|
776
1000
|
this.object.quaternion.set(
|
|
@@ -785,6 +1009,14 @@ var CharacterBody3D = class {
|
|
|
785
1009
|
this.#disposed = true;
|
|
786
1010
|
this.#physics?.remove(this);
|
|
787
1011
|
this.#simulation.removeBody(this.body.id);
|
|
1012
|
+
this.#clearGroundState();
|
|
1013
|
+
}
|
|
1014
|
+
#clearGroundState() {
|
|
1015
|
+
this.grounded = false;
|
|
1016
|
+
this.#groundCollider = void 0;
|
|
1017
|
+
this.groundBody = void 0;
|
|
1018
|
+
this.groundNormal.set(0, 1, 0);
|
|
1019
|
+
this.slopeAngle = 0;
|
|
788
1020
|
}
|
|
789
1021
|
};
|
|
790
1022
|
|
|
@@ -984,6 +1216,227 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
984
1216
|
return this;
|
|
985
1217
|
}
|
|
986
1218
|
};
|
|
1219
|
+
function finite(name, value) {
|
|
1220
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
1221
|
+
throw new Error(`Buoyancy3D.${name} must be finite.`);
|
|
1222
|
+
return value;
|
|
1223
|
+
}
|
|
1224
|
+
function nonNegative(name, value) {
|
|
1225
|
+
finite(name, value);
|
|
1226
|
+
if (value < 0) throw new Error(`Buoyancy3D.${name} must be non-negative.`);
|
|
1227
|
+
return value;
|
|
1228
|
+
}
|
|
1229
|
+
function positive(name, value) {
|
|
1230
|
+
finite(name, value);
|
|
1231
|
+
if (value <= 0) throw new Error(`Buoyancy3D.${name} must be positive.`);
|
|
1232
|
+
return value;
|
|
1233
|
+
}
|
|
1234
|
+
function pointVector(value) {
|
|
1235
|
+
let x;
|
|
1236
|
+
let y;
|
|
1237
|
+
let z;
|
|
1238
|
+
if ("x" in value) {
|
|
1239
|
+
x = value.x;
|
|
1240
|
+
y = value.y;
|
|
1241
|
+
z = value.z;
|
|
1242
|
+
} else {
|
|
1243
|
+
x = value[0];
|
|
1244
|
+
y = value[1];
|
|
1245
|
+
z = value[2];
|
|
1246
|
+
}
|
|
1247
|
+
return new Vector3(
|
|
1248
|
+
finite("hullPoints.position.x", x),
|
|
1249
|
+
finite("hullPoints.position.y", y),
|
|
1250
|
+
finite("hullPoints.position.z", z)
|
|
1251
|
+
);
|
|
1252
|
+
}
|
|
1253
|
+
function shapeVolume(body) {
|
|
1254
|
+
const shape = body.shape.descriptor;
|
|
1255
|
+
if (shape.kind === "box") return 8 * shape.x * shape.y * shape.z;
|
|
1256
|
+
if (shape.kind === "sphere") return 4 / 3 * Math.PI * shape.x ** 3;
|
|
1257
|
+
if (shape.kind === "capsule") return Math.PI * shape.y ** 2 * (2 * shape.x + 4 / 3 * shape.y);
|
|
1258
|
+
return 1;
|
|
1259
|
+
}
|
|
1260
|
+
function clamp01(value) {
|
|
1261
|
+
return Math.max(0, Math.min(1, value));
|
|
1262
|
+
}
|
|
1263
|
+
var Buoyancy3D = class {
|
|
1264
|
+
body;
|
|
1265
|
+
hullPoints;
|
|
1266
|
+
density;
|
|
1267
|
+
drag;
|
|
1268
|
+
gravity;
|
|
1269
|
+
#surface;
|
|
1270
|
+
#physics;
|
|
1271
|
+
#points;
|
|
1272
|
+
#rotation = new Quaternion();
|
|
1273
|
+
#elapsed = 0;
|
|
1274
|
+
#submergedFraction = 0;
|
|
1275
|
+
#enabled;
|
|
1276
|
+
#disposed = false;
|
|
1277
|
+
constructor(options) {
|
|
1278
|
+
if (options.hullPoints.length === 0)
|
|
1279
|
+
throw new Error("Buoyancy3D.hullPoints must contain at least one point.");
|
|
1280
|
+
const surface = options.surface ?? options.heightSource ?? options.field;
|
|
1281
|
+
if (surface === void 0) throw new Error("Buoyancy3D requires a surface height source.");
|
|
1282
|
+
const physics = options.physics ?? options.body.physics;
|
|
1283
|
+
if (physics === void 0 || physics.addBuoyancy === void 0)
|
|
1284
|
+
throw new Error("Buoyancy3D requires the body's physics context.");
|
|
1285
|
+
this.body = options.body;
|
|
1286
|
+
this.hullPoints = options.hullPoints;
|
|
1287
|
+
this.density = positive("density", options.density ?? 1e3);
|
|
1288
|
+
this.drag = nonNegative("drag", options.drag ?? 0);
|
|
1289
|
+
this.gravity = positive("gravity", options.gravity ?? 9.81);
|
|
1290
|
+
this.#surface = surface;
|
|
1291
|
+
this.#physics = physics;
|
|
1292
|
+
this.#enabled = options.buoyancy ?? true;
|
|
1293
|
+
const totalVolume = positive("volume", options.volume ?? shapeVolume(options.body));
|
|
1294
|
+
const yValues = options.hullPoints.map(({ position }) => pointVector(position).y);
|
|
1295
|
+
const minimumY = Math.min(...yValues);
|
|
1296
|
+
const maximumY = Math.max(...yValues);
|
|
1297
|
+
const defaultSpacing = options.pointSpacing ?? Math.max(maximumY - minimumY, 1);
|
|
1298
|
+
const pointVolume = totalVolume / options.hullPoints.length;
|
|
1299
|
+
this.#points = options.hullPoints.map((point) => {
|
|
1300
|
+
const position = pointVector(point.position);
|
|
1301
|
+
const volume = positive("hullPoints.volume", point.volume ?? pointVolume);
|
|
1302
|
+
const spacing = positive("hullPoints.spacing", point.spacing ?? defaultSpacing);
|
|
1303
|
+
return {
|
|
1304
|
+
force: { x: 0, y: 0, z: 0 },
|
|
1305
|
+
position,
|
|
1306
|
+
spacing,
|
|
1307
|
+
submergedFraction: 0,
|
|
1308
|
+
volume,
|
|
1309
|
+
worldPosition: new Vector3()
|
|
1310
|
+
};
|
|
1311
|
+
});
|
|
1312
|
+
this.body.buoyancy = this;
|
|
1313
|
+
physics.addBuoyancy(this);
|
|
1314
|
+
this.#measure();
|
|
1315
|
+
}
|
|
1316
|
+
get enabled() {
|
|
1317
|
+
return this.#enabled;
|
|
1318
|
+
}
|
|
1319
|
+
set enabled(value) {
|
|
1320
|
+
this.#enabled = value;
|
|
1321
|
+
}
|
|
1322
|
+
get buoyancy() {
|
|
1323
|
+
return this.#enabled;
|
|
1324
|
+
}
|
|
1325
|
+
set buoyancy(value) {
|
|
1326
|
+
this.#enabled = value;
|
|
1327
|
+
}
|
|
1328
|
+
get submergedFraction() {
|
|
1329
|
+
return this.#submergedFraction;
|
|
1330
|
+
}
|
|
1331
|
+
get time() {
|
|
1332
|
+
return this.#elapsed;
|
|
1333
|
+
}
|
|
1334
|
+
/** Called by `rapier()` immediately before its fixed-step backend call. */
|
|
1335
|
+
apply(deltaTime) {
|
|
1336
|
+
if (this.#disposed) return;
|
|
1337
|
+
if (!Number.isFinite(deltaTime) || deltaTime < 0)
|
|
1338
|
+
throw new Error("Buoyancy3D.apply requires a finite non-negative deltaTime.");
|
|
1339
|
+
this.#measure();
|
|
1340
|
+
if (this.#enabled) {
|
|
1341
|
+
const velocity = this.body.linearVelocity;
|
|
1342
|
+
for (const point of this.#points) {
|
|
1343
|
+
const submergedVolume = point.volume * point.submergedFraction;
|
|
1344
|
+
if (submergedVolume === 0) continue;
|
|
1345
|
+
point.force.x = -velocity.x * this.drag * submergedVolume;
|
|
1346
|
+
point.force.y = -velocity.y * this.drag * submergedVolume;
|
|
1347
|
+
point.force.z = -velocity.z * this.drag * submergedVolume;
|
|
1348
|
+
point.force.y += this.density * this.gravity * submergedVolume;
|
|
1349
|
+
this.body.applyForceAtPoint(point.force, point.worldPosition);
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
this.#elapsed += deltaTime;
|
|
1353
|
+
if (!Number.isFinite(this.#elapsed)) throw new Error("Buoyancy3D time overflowed.");
|
|
1354
|
+
}
|
|
1355
|
+
dispose() {
|
|
1356
|
+
if (this.#disposed) return;
|
|
1357
|
+
this.#disposed = true;
|
|
1358
|
+
this.#physics.removeBuoyancy?.(this);
|
|
1359
|
+
if (this.body.buoyancy === this) this.body.buoyancy = void 0;
|
|
1360
|
+
}
|
|
1361
|
+
#measure() {
|
|
1362
|
+
const points = this.#points;
|
|
1363
|
+
const object = this.body.object;
|
|
1364
|
+
if (object === void 0) throw new Error("Buoyancy3D requires a body with an object.");
|
|
1365
|
+
this.#rotation.copy(object.quaternion);
|
|
1366
|
+
let volume = 0;
|
|
1367
|
+
let submerged = 0;
|
|
1368
|
+
for (const point of points) {
|
|
1369
|
+
point.worldPosition.copy(point.position).applyQuaternion(this.#rotation).add(object.position);
|
|
1370
|
+
const result = this.#surface.sample(
|
|
1371
|
+
point.worldPosition.x,
|
|
1372
|
+
point.worldPosition.z,
|
|
1373
|
+
this.#elapsed
|
|
1374
|
+
);
|
|
1375
|
+
const height = finite("surface.height", result.height);
|
|
1376
|
+
const fraction = clamp01(0.5 + (height - point.worldPosition.y) / point.spacing);
|
|
1377
|
+
point.submergedFraction = fraction;
|
|
1378
|
+
volume += point.volume;
|
|
1379
|
+
submerged += point.volume * fraction;
|
|
1380
|
+
}
|
|
1381
|
+
this.#submergedFraction = volume === 0 ? 0 : submerged / volume;
|
|
1382
|
+
}
|
|
1383
|
+
};
|
|
1384
|
+
|
|
1385
|
+
// src/Joint3D.ts
|
|
1386
|
+
var ZERO_ANCHOR = { x: 0, y: 0, z: 0 };
|
|
1387
|
+
var X_AXIS = { x: 1, y: 0, z: 0 };
|
|
1388
|
+
var IDENTITY_FRAME = { x: 0, y: 0, z: 0, w: 1 };
|
|
1389
|
+
function bodyId(value, label) {
|
|
1390
|
+
const candidate = typeof value === "object" && value !== null && "body" in value ? value.body : value;
|
|
1391
|
+
if (typeof candidate !== "object" || candidate === null || !("id" in candidate) || typeof candidate.id !== "number")
|
|
1392
|
+
throw new Error(`TN_PHYSICS_INVALID_BODY: ${label} must expose a body handle.`);
|
|
1393
|
+
return candidate.id;
|
|
1394
|
+
}
|
|
1395
|
+
var Joint3D = class _Joint3D {
|
|
1396
|
+
id;
|
|
1397
|
+
#simulation;
|
|
1398
|
+
#disposed = false;
|
|
1399
|
+
constructor(simulation, id) {
|
|
1400
|
+
this.#simulation = simulation;
|
|
1401
|
+
this.id = id;
|
|
1402
|
+
}
|
|
1403
|
+
static #create(options, type, extra = {}) {
|
|
1404
|
+
const simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
1405
|
+
const jointOptions = {
|
|
1406
|
+
anchorA: options.anchorA ?? ZERO_ANCHOR,
|
|
1407
|
+
anchorB: options.anchorB ?? ZERO_ANCHOR,
|
|
1408
|
+
bodyA: bodyId(options.bodyA, "bodyA"),
|
|
1409
|
+
bodyB: bodyId(options.bodyB, "bodyB"),
|
|
1410
|
+
...extra,
|
|
1411
|
+
type
|
|
1412
|
+
};
|
|
1413
|
+
requirePhysicsJointCreateOptions(jointOptions);
|
|
1414
|
+
const id = simulation.createJoint(jointOptions);
|
|
1415
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
1416
|
+
throw new Error("TN_PHYSICS_INVALID_JOINT: backend returned an invalid joint id.");
|
|
1417
|
+
return new _Joint3D(simulation, id);
|
|
1418
|
+
}
|
|
1419
|
+
static pin(options) {
|
|
1420
|
+
return _Joint3D.#create(options, "pin");
|
|
1421
|
+
}
|
|
1422
|
+
static hinge(options) {
|
|
1423
|
+
return _Joint3D.#create(options, "hinge", {
|
|
1424
|
+
axis: options.axis ?? X_AXIS,
|
|
1425
|
+
limit: options.limit
|
|
1426
|
+
});
|
|
1427
|
+
}
|
|
1428
|
+
static fixed(options) {
|
|
1429
|
+
return _Joint3D.#create(options, "fixed", {
|
|
1430
|
+
frameA: options.frameA ?? IDENTITY_FRAME,
|
|
1431
|
+
frameB: options.frameB ?? IDENTITY_FRAME
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
dispose() {
|
|
1435
|
+
if (this.#disposed) return;
|
|
1436
|
+
this.#disposed = true;
|
|
1437
|
+
this.#simulation.removeJoint(this.id);
|
|
1438
|
+
}
|
|
1439
|
+
};
|
|
987
1440
|
|
|
988
1441
|
// src/PhysicsDirectSpaceState3D.ts
|
|
989
1442
|
function rayQuery(options) {
|
|
@@ -1028,26 +1481,41 @@ var PhysicsDirectSpaceState3D = class {
|
|
|
1028
1481
|
};
|
|
1029
1482
|
|
|
1030
1483
|
// src/RigidBody3D.ts
|
|
1031
|
-
function finiteTransform3(values, offset) {
|
|
1032
|
-
const result = Array.from({ length: 8 }, (_, index) => values[offset + index]);
|
|
1033
|
-
if (result.some((value) => value === void 0 || !Number.isFinite(value)))
|
|
1034
|
-
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
1035
|
-
return result;
|
|
1036
|
-
}
|
|
1037
1484
|
var RigidBody3D = class {
|
|
1038
1485
|
body;
|
|
1039
1486
|
collider;
|
|
1487
|
+
/** The supplied transform; position-only fixed bodies have no runtime object. */
|
|
1040
1488
|
object;
|
|
1489
|
+
shape;
|
|
1041
1490
|
type;
|
|
1491
|
+
mass;
|
|
1492
|
+
/** The effective continuous-collision setting; dynamic defaults true, fixed and kinematic are always false. */
|
|
1493
|
+
continuousCollision;
|
|
1494
|
+
buoyancy;
|
|
1042
1495
|
#simulation;
|
|
1043
1496
|
#physics;
|
|
1497
|
+
#object;
|
|
1044
1498
|
#lastPosition;
|
|
1499
|
+
// Scratch returned by kinematicMotion(); the plugin retains it only within one update pass.
|
|
1500
|
+
#motion = { x: 0, y: 0, z: 0 };
|
|
1501
|
+
#zeroMotion = { x: 0, y: 0, z: 0 };
|
|
1045
1502
|
#disposed = false;
|
|
1046
1503
|
constructor(options) {
|
|
1504
|
+
const initial = resolveInitialTransform(options);
|
|
1505
|
+
const type = options.type ?? "dynamic";
|
|
1506
|
+
if (options.object === void 0 && type !== "fixed")
|
|
1507
|
+
throw new Error("RigidBody3D position-only bodies must be fixed.");
|
|
1047
1508
|
this.#simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
1048
1509
|
this.#physics = options.physics;
|
|
1049
1510
|
this.object = options.object;
|
|
1050
|
-
this.
|
|
1511
|
+
this.shape = options.shape;
|
|
1512
|
+
this.#object = options.object;
|
|
1513
|
+
this.type = type;
|
|
1514
|
+
this.mass = options.mass ?? 0;
|
|
1515
|
+
if (options.continuousCollision !== void 0 && typeof options.continuousCollision !== "boolean")
|
|
1516
|
+
throw new Error("RigidBody3D continuousCollision must be a boolean.");
|
|
1517
|
+
this.continuousCollision = effectiveContinuousCollision(type, options.continuousCollision);
|
|
1518
|
+
this.buoyancy = void 0;
|
|
1051
1519
|
const shape = options.shape.descriptor;
|
|
1052
1520
|
if (options.collisionLayer !== void 0 || options.collisionMask !== void 0) {
|
|
1053
1521
|
const layer = options.collisionLayer ?? shape.collisionLayer;
|
|
@@ -1056,47 +1524,48 @@ var RigidBody3D = class {
|
|
|
1056
1524
|
}
|
|
1057
1525
|
const registration = this.#simulation.createBody({
|
|
1058
1526
|
entity: options.entity,
|
|
1059
|
-
mass:
|
|
1060
|
-
position:
|
|
1061
|
-
rotation:
|
|
1527
|
+
mass: this.mass,
|
|
1528
|
+
position: initial.position,
|
|
1529
|
+
rotation: initial.rotation,
|
|
1062
1530
|
sensor: shape.sensor,
|
|
1063
1531
|
shape,
|
|
1064
|
-
type: this.type
|
|
1532
|
+
type: this.type,
|
|
1533
|
+
continuousCollision: this.continuousCollision
|
|
1065
1534
|
});
|
|
1066
1535
|
options.shape.bindRaw(registration.rawShape);
|
|
1067
1536
|
this.body = registration.body;
|
|
1068
1537
|
this.collider = registration.collider;
|
|
1069
1538
|
this.#lastPosition = {
|
|
1070
|
-
x:
|
|
1071
|
-
y:
|
|
1072
|
-
z:
|
|
1539
|
+
x: initial.position.x,
|
|
1540
|
+
y: initial.position.y,
|
|
1541
|
+
z: initial.position.z
|
|
1073
1542
|
};
|
|
1074
1543
|
this.#physics?.add(this);
|
|
1075
1544
|
}
|
|
1545
|
+
get physics() {
|
|
1546
|
+
return this.#physics;
|
|
1547
|
+
}
|
|
1076
1548
|
/** Called by the shared plugin before a bulk step. */
|
|
1077
1549
|
writeKinematic(buffer, offset) {
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
this.object.quaternion.w
|
|
1089
|
-
],
|
|
1090
|
-
offset
|
|
1091
|
-
);
|
|
1550
|
+
const object = this.#object;
|
|
1551
|
+
if (this.#disposed || object === void 0) return;
|
|
1552
|
+
buffer[offset] = this.body.id;
|
|
1553
|
+
buffer[offset + 1] = object.position.x;
|
|
1554
|
+
buffer[offset + 2] = object.position.y;
|
|
1555
|
+
buffer[offset + 3] = object.position.z;
|
|
1556
|
+
buffer[offset + 4] = object.quaternion.x;
|
|
1557
|
+
buffer[offset + 5] = object.quaternion.y;
|
|
1558
|
+
buffer[offset + 6] = object.quaternion.z;
|
|
1559
|
+
buffer[offset + 7] = object.quaternion.w;
|
|
1092
1560
|
}
|
|
1093
1561
|
/** Displacement since the last backend transform, used for moving-platform carry. */
|
|
1094
1562
|
kinematicMotion() {
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1563
|
+
const object = this.#object;
|
|
1564
|
+
if (object === void 0) return this.#zeroMotion;
|
|
1565
|
+
this.#motion.x = object.position.x - this.#lastPosition.x;
|
|
1566
|
+
this.#motion.y = object.position.y - this.#lastPosition.y;
|
|
1567
|
+
this.#motion.z = object.position.z - this.#lastPosition.z;
|
|
1568
|
+
return this.#motion;
|
|
1100
1569
|
}
|
|
1101
1570
|
syncToPhysics() {
|
|
1102
1571
|
}
|
|
@@ -1116,6 +1585,11 @@ var RigidBody3D = class {
|
|
|
1116
1585
|
this.#requireLive("applyForce");
|
|
1117
1586
|
this.#simulation.applyBodyForce(this.body.id, force);
|
|
1118
1587
|
}
|
|
1588
|
+
/** Godot's force-at-position equivalent. Continuous push in world space; cleared each step. */
|
|
1589
|
+
applyForceAtPoint(force, point) {
|
|
1590
|
+
this.#requireLive("applyForceAtPoint");
|
|
1591
|
+
this.#simulation.applyBodyForceAtPoint(this.body.id, force, point);
|
|
1592
|
+
}
|
|
1119
1593
|
/** Godot's `linear_velocity`. */
|
|
1120
1594
|
get linearVelocity() {
|
|
1121
1595
|
this.#requireLive("linearVelocity");
|
|
@@ -1129,34 +1603,219 @@ var RigidBody3D = class {
|
|
|
1129
1603
|
if (this.#disposed) throw new Error(`RigidBody3D.${operation} cannot be used after dispose.`);
|
|
1130
1604
|
}
|
|
1131
1605
|
syncFromPhysics() {
|
|
1132
|
-
const
|
|
1606
|
+
const object = this.#object;
|
|
1607
|
+
if (object === void 0) return;
|
|
1608
|
+
if (this.#simulation.readBodyTransform === void 0)
|
|
1609
|
+
throw new Error(
|
|
1610
|
+
"TN_PHYSICS_READ_TRANSFORM_MISSING: the active physics backend cannot read body transforms."
|
|
1611
|
+
);
|
|
1612
|
+
const transform = this.#simulation.readBodyTransform(this.body.id);
|
|
1133
1613
|
if (transform === void 0) return;
|
|
1134
|
-
|
|
1135
|
-
|
|
1614
|
+
object.position.set(transform.position.x, transform.position.y, transform.position.z);
|
|
1615
|
+
object.quaternion.set(
|
|
1136
1616
|
transform.rotation.x,
|
|
1137
1617
|
transform.rotation.y,
|
|
1138
1618
|
transform.rotation.z,
|
|
1139
1619
|
transform.rotation.w
|
|
1140
1620
|
);
|
|
1141
|
-
this.#lastPosition =
|
|
1621
|
+
this.#lastPosition.x = transform.position.x;
|
|
1622
|
+
this.#lastPosition.y = transform.position.y;
|
|
1623
|
+
this.#lastPosition.z = transform.position.z;
|
|
1142
1624
|
}
|
|
1143
1625
|
applyTransform(values, offset) {
|
|
1144
|
-
const
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1626
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
1627
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
1628
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
1629
|
+
const qx = bulkTransformValue(values, offset + 4);
|
|
1630
|
+
const qy = bulkTransformValue(values, offset + 5);
|
|
1631
|
+
const qz = bulkTransformValue(values, offset + 6);
|
|
1632
|
+
const qw = bulkTransformValue(values, offset + 7);
|
|
1633
|
+
const object = this.#object;
|
|
1634
|
+
if (object === void 0) return;
|
|
1635
|
+
object.position.set(x, y, z);
|
|
1636
|
+
object.quaternion.set(qx, qy, qz, qw);
|
|
1637
|
+
this.#lastPosition.x = x;
|
|
1638
|
+
this.#lastPosition.y = y;
|
|
1639
|
+
this.#lastPosition.z = z;
|
|
1148
1640
|
}
|
|
1149
1641
|
dispose() {
|
|
1150
1642
|
if (this.#disposed) return;
|
|
1151
1643
|
this.#disposed = true;
|
|
1644
|
+
this.buoyancy?.dispose();
|
|
1645
|
+
this.buoyancy = void 0;
|
|
1152
1646
|
this.#physics?.remove(this);
|
|
1153
1647
|
this.#simulation.removeBody(this.body.id);
|
|
1154
1648
|
}
|
|
1155
1649
|
};
|
|
1156
1650
|
|
|
1651
|
+
// src/static-colliders.ts
|
|
1652
|
+
var DEFAULT_REACHABLE_CEILING = 4.5;
|
|
1653
|
+
var scratchInstance = new Matrix4();
|
|
1654
|
+
var scratchWorld = new Matrix4();
|
|
1655
|
+
var scratchPosition = new Vector3();
|
|
1656
|
+
var scratchScale = new Vector3();
|
|
1657
|
+
var scratchRotation = new Quaternion();
|
|
1658
|
+
var scratchBounds = new Box3();
|
|
1659
|
+
function optionsFor(filter) {
|
|
1660
|
+
return typeof filter === "function" ? { predicate: filter } : filter ?? {};
|
|
1661
|
+
}
|
|
1662
|
+
function validateCeiling(value) {
|
|
1663
|
+
if (!Number.isFinite(value))
|
|
1664
|
+
throw new Error(
|
|
1665
|
+
`buildStaticColliders reachableCeiling must be finite, received ${String(value)}.`
|
|
1666
|
+
);
|
|
1667
|
+
return value;
|
|
1668
|
+
}
|
|
1669
|
+
function selectPredicate(options) {
|
|
1670
|
+
const predicate = options.predicate ?? options.shouldCollide ?? (() => true);
|
|
1671
|
+
return (object) => {
|
|
1672
|
+
const selected = predicate(object);
|
|
1673
|
+
if (typeof selected !== "boolean")
|
|
1674
|
+
throw new Error(`buildStaticColliders predicate must return a boolean for '${object.name}'.`);
|
|
1675
|
+
return selected;
|
|
1676
|
+
};
|
|
1677
|
+
}
|
|
1678
|
+
function configureCarrier(carrier, transform, scale) {
|
|
1679
|
+
transform.decompose(scratchPosition, scratchRotation, scale);
|
|
1680
|
+
carrier.position.copy(scratchPosition);
|
|
1681
|
+
carrier.quaternion.copy(scratchRotation);
|
|
1682
|
+
carrier.scale.copy(scale);
|
|
1683
|
+
carrier.updateMatrixWorld(true);
|
|
1684
|
+
}
|
|
1685
|
+
function isReachable(carrier, ceiling) {
|
|
1686
|
+
scratchBounds.setFromObject(carrier, true);
|
|
1687
|
+
return !scratchBounds.isEmpty() && scratchBounds.min.y < ceiling;
|
|
1688
|
+
}
|
|
1689
|
+
function addCollider(context, source, transform, options) {
|
|
1690
|
+
const carrier = new Mesh(source.geometry);
|
|
1691
|
+
const shapeSource = new Mesh(source.geometry);
|
|
1692
|
+
configureCarrier(carrier, transform, scratchScale);
|
|
1693
|
+
shapeSource.scale.copy(scratchScale);
|
|
1694
|
+
if (!isReachable(carrier, options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING))
|
|
1695
|
+
return void 0;
|
|
1696
|
+
return new RigidBody3D({
|
|
1697
|
+
collisionLayer: options.collisionLayer,
|
|
1698
|
+
collisionMask: options.collisionMask,
|
|
1699
|
+
object: carrier,
|
|
1700
|
+
physics: context.physics,
|
|
1701
|
+
shape: CollisionShape3D.fromMesh(shapeSource, "trimesh"),
|
|
1702
|
+
type: "fixed"
|
|
1703
|
+
});
|
|
1704
|
+
}
|
|
1705
|
+
function buildStaticColliders(context, root, filter) {
|
|
1706
|
+
if (context?.physics === void 0)
|
|
1707
|
+
throw new Error("buildStaticColliders requires a physics context.");
|
|
1708
|
+
if (!(root instanceof Object3D))
|
|
1709
|
+
throw new Error("buildStaticColliders requires an Object3D root.");
|
|
1710
|
+
const options = optionsFor(filter);
|
|
1711
|
+
const ceiling = validateCeiling(options.reachableCeiling ?? DEFAULT_REACHABLE_CEILING);
|
|
1712
|
+
const predicate = selectPredicate(options);
|
|
1713
|
+
const bodies = [];
|
|
1714
|
+
root.updateMatrixWorld(true);
|
|
1715
|
+
try {
|
|
1716
|
+
root.traverse((object) => {
|
|
1717
|
+
if (!predicate(object) || !(object instanceof Mesh)) return;
|
|
1718
|
+
if (object instanceof InstancedMesh) {
|
|
1719
|
+
for (let index = 0; index < object.count; index += 1) {
|
|
1720
|
+
object.getMatrixAt(index, scratchInstance);
|
|
1721
|
+
scratchWorld.multiplyMatrices(object.matrixWorld, scratchInstance);
|
|
1722
|
+
const body2 = addCollider(context, object, scratchWorld, {
|
|
1723
|
+
...options,
|
|
1724
|
+
reachableCeiling: ceiling
|
|
1725
|
+
});
|
|
1726
|
+
if (body2 !== void 0) bodies.push(body2);
|
|
1727
|
+
}
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
const body = addCollider(context, object, object.matrixWorld, {
|
|
1731
|
+
...options,
|
|
1732
|
+
reachableCeiling: ceiling
|
|
1733
|
+
});
|
|
1734
|
+
if (body !== void 0) bodies.push(body);
|
|
1735
|
+
});
|
|
1736
|
+
} catch (error) {
|
|
1737
|
+
for (const body of bodies) body.dispose();
|
|
1738
|
+
throw error;
|
|
1739
|
+
}
|
|
1740
|
+
console.info(`TN_STATIC_COLLIDERS:${JSON.stringify({ count: bodies.length })}`);
|
|
1741
|
+
if (bodies.length === 0)
|
|
1742
|
+
throw new Error("buildStaticColliders produced zero colliders; check the scene and predicate.");
|
|
1743
|
+
return bodies;
|
|
1744
|
+
}
|
|
1745
|
+
var ONE = new Vector3(1, 1, 1);
|
|
1746
|
+
var PhysicsSoftBodyCollision = class {
|
|
1747
|
+
capacity;
|
|
1748
|
+
#bodies;
|
|
1749
|
+
#bodyMatrix = new Matrix4();
|
|
1750
|
+
#localMatrix = new Matrix4();
|
|
1751
|
+
#corner = new Vector3();
|
|
1752
|
+
#minimum = new Vector3();
|
|
1753
|
+
#maximum = new Vector3();
|
|
1754
|
+
constructor(bodies) {
|
|
1755
|
+
if (bodies.length === 0)
|
|
1756
|
+
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1757
|
+
for (const body of bodies) {
|
|
1758
|
+
if (body.shape.descriptor.kind !== "box")
|
|
1759
|
+
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1760
|
+
if (body.object === void 0)
|
|
1761
|
+
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1762
|
+
}
|
|
1763
|
+
this.#bodies = bodies;
|
|
1764
|
+
this.capacity = bodies.length;
|
|
1765
|
+
}
|
|
1766
|
+
writeBoxes(target, worldToLocal) {
|
|
1767
|
+
if (target.length !== this.capacity * 8)
|
|
1768
|
+
throw new Error(
|
|
1769
|
+
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1770
|
+
);
|
|
1771
|
+
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1772
|
+
const body = this.#bodies[index];
|
|
1773
|
+
const object = body.object;
|
|
1774
|
+
if (object === void 0)
|
|
1775
|
+
throw new Error("softBodyCollision body lost its object transform.");
|
|
1776
|
+
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1777
|
+
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1778
|
+
this.#minimum.set(
|
|
1779
|
+
Number.POSITIVE_INFINITY,
|
|
1780
|
+
Number.POSITIVE_INFINITY,
|
|
1781
|
+
Number.POSITIVE_INFINITY
|
|
1782
|
+
);
|
|
1783
|
+
this.#maximum.set(
|
|
1784
|
+
Number.NEGATIVE_INFINITY,
|
|
1785
|
+
Number.NEGATIVE_INFINITY,
|
|
1786
|
+
Number.NEGATIVE_INFINITY
|
|
1787
|
+
);
|
|
1788
|
+
const shape = body.shape.descriptor;
|
|
1789
|
+
for (let corner = 0; corner < 8; corner += 1) {
|
|
1790
|
+
this.#corner.set(
|
|
1791
|
+
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
1792
|
+
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
1793
|
+
(corner & 4) === 0 ? -shape.z : shape.z
|
|
1794
|
+
).applyMatrix4(this.#localMatrix);
|
|
1795
|
+
this.#minimum.min(this.#corner);
|
|
1796
|
+
this.#maximum.max(this.#corner);
|
|
1797
|
+
}
|
|
1798
|
+
const offset = index * 8;
|
|
1799
|
+
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
1800
|
+
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
1801
|
+
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
1802
|
+
target[offset + 3] = 0;
|
|
1803
|
+
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
1804
|
+
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
1805
|
+
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
1806
|
+
target[offset + 7] = 0;
|
|
1807
|
+
}
|
|
1808
|
+
return this.#bodies.length;
|
|
1809
|
+
}
|
|
1810
|
+
};
|
|
1811
|
+
function softBodyCollision(...bodies) {
|
|
1812
|
+
return new PhysicsSoftBodyCollision(bodies);
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1157
1815
|
// src/plugin.ts
|
|
1158
1816
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1159
1817
|
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
1818
|
+
var PHYSICS_BUFFER_RESIZE_ATTEMPTS = 12;
|
|
1160
1819
|
function growFloat(buffer, records) {
|
|
1161
1820
|
if (buffer.length >= records * PHYSICS_TRANSFORM_STRIDE) return buffer;
|
|
1162
1821
|
return new Float32Array(
|
|
@@ -1185,6 +1844,8 @@ function rapier(options = {}) {
|
|
|
1185
1844
|
const bodies = /* @__PURE__ */ new Set();
|
|
1186
1845
|
const bodiesById = /* @__PURE__ */ new Map();
|
|
1187
1846
|
const areas = /* @__PURE__ */ new Map();
|
|
1847
|
+
const buoyancies = /* @__PURE__ */ new Set();
|
|
1848
|
+
const areaMembershipBuffers = /* @__PURE__ */ new Map();
|
|
1188
1849
|
const kinematicMotions = /* @__PURE__ */ new Map();
|
|
1189
1850
|
let kinematic = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
1190
1851
|
let visible = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
@@ -1202,7 +1863,14 @@ function rapier(options = {}) {
|
|
|
1202
1863
|
bodies.add(body);
|
|
1203
1864
|
bodiesById.set(body.body.id, body);
|
|
1204
1865
|
},
|
|
1205
|
-
addArea: (area) =>
|
|
1866
|
+
addArea: (area) => {
|
|
1867
|
+
areas.set(area.body.id, area);
|
|
1868
|
+
areaMembershipBuffers.set(area.body.id, {
|
|
1869
|
+
current: /* @__PURE__ */ new Map(),
|
|
1870
|
+
previous: /* @__PURE__ */ new Map()
|
|
1871
|
+
});
|
|
1872
|
+
},
|
|
1873
|
+
addBuoyancy: (buoyancy) => buoyancies.add(buoyancy),
|
|
1206
1874
|
eventQueue: physicsHandle(selected.rawEventQueue),
|
|
1207
1875
|
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
1208
1876
|
numBodies: () => bodies.size,
|
|
@@ -1213,8 +1881,10 @@ function rapier(options = {}) {
|
|
|
1213
1881
|
},
|
|
1214
1882
|
removeArea: (area) => {
|
|
1215
1883
|
areas.delete(area.body.id);
|
|
1884
|
+
areaMembershipBuffers.delete(area.body.id);
|
|
1216
1885
|
removeContactsFor(area.body.id);
|
|
1217
1886
|
},
|
|
1887
|
+
removeBuoyancy: (buoyancy) => buoyancies.delete(buoyancy),
|
|
1218
1888
|
directSpaceState: new PhysicsDirectSpaceState3D(selected),
|
|
1219
1889
|
simulation: selected,
|
|
1220
1890
|
world: physicsWorldHandle(selected.rawWorld, selected)
|
|
@@ -1260,20 +1930,30 @@ function rapier(options = {}) {
|
|
|
1260
1930
|
kinematicCount: count,
|
|
1261
1931
|
kinematicTransforms: kinematic
|
|
1262
1932
|
};
|
|
1933
|
+
for (const buoyancy of buoyancies) buoyancy.apply(dt);
|
|
1263
1934
|
simulation.step(dt, input);
|
|
1264
1935
|
visible = growFloat(visible, bodies.size + areas.size);
|
|
1265
|
-
|
|
1936
|
+
let visibleResizeAttempts = 0;
|
|
1937
|
+
for (; ; ) {
|
|
1938
|
+
try {
|
|
1939
|
+
visibleCount = simulation.readVisibleTransforms(visible);
|
|
1940
|
+
break;
|
|
1941
|
+
} catch (error) {
|
|
1942
|
+
if (!isSmallBufferError2(error) || visibleResizeAttempts >= PHYSICS_BUFFER_RESIZE_ATTEMPTS)
|
|
1943
|
+
throw error;
|
|
1944
|
+
visibleResizeAttempts += 1;
|
|
1945
|
+
visible = growFloat(visible, Math.ceil(visible.length / PHYSICS_TRANSFORM_STRIDE) * 2);
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1266
1948
|
for (let index = 0; index < visibleCount; index += 1) {
|
|
1267
1949
|
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
1268
1950
|
const id = visibleId(visible, offset);
|
|
1269
1951
|
const body = bodiesById.get(id);
|
|
1270
|
-
if (body !== void 0)
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
if (area === void 0)
|
|
1274
|
-
throw new Error("IPhysicsSimulation returned an unknown visible body id.");
|
|
1275
|
-
area.applyTransform(visible, offset);
|
|
1952
|
+
if (body !== void 0) {
|
|
1953
|
+
body.applyTransform(visible, offset);
|
|
1954
|
+
continue;
|
|
1276
1955
|
}
|
|
1956
|
+
areas.get(id)?.applyTransform(visible, offset);
|
|
1277
1957
|
}
|
|
1278
1958
|
events = growEvents(events, bodies.size + areas.size);
|
|
1279
1959
|
const maximumEventValues = Math.max(
|
|
@@ -1311,12 +1991,18 @@ function rapier(options = {}) {
|
|
|
1311
1991
|
}
|
|
1312
1992
|
if (simulation.areaIntersections !== void 0) {
|
|
1313
1993
|
for (const area of areas.values()) {
|
|
1314
|
-
const
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1994
|
+
const buffers = areaMembershipBuffers.get(area.body.id);
|
|
1995
|
+
if (buffers === void 0)
|
|
1996
|
+
throw new Error("Physics area membership buffers lost a registered area.");
|
|
1997
|
+
const current = buffers.current;
|
|
1998
|
+
current.clear();
|
|
1999
|
+
for (const bodyId2 of simulation.areaIntersections(area.body.id)) {
|
|
2000
|
+
const body = bodiesById.get(bodyId2);
|
|
2001
|
+
if (body !== void 0) current.set(bodyId2, body);
|
|
1318
2002
|
}
|
|
1319
2003
|
area.reconcileIntersections(current);
|
|
2004
|
+
buffers.current = buffers.previous;
|
|
2005
|
+
buffers.previous = current;
|
|
1320
2006
|
}
|
|
1321
2007
|
}
|
|
1322
2008
|
kinematicMotions.clear();
|
|
@@ -1328,8 +2014,11 @@ function rapier(options = {}) {
|
|
|
1328
2014
|
// before any input. A scene therefore gets a pristine simulation, which makes restart
|
|
1329
2015
|
// deterministic without a game having to discover an API for it.
|
|
1330
2016
|
sceneExit: (ctx) => {
|
|
2017
|
+
for (const buoyancy of [...buoyancies]) buoyancy.dispose();
|
|
1331
2018
|
for (const area of [...areas.values()]) area.dispose();
|
|
1332
2019
|
for (const body of [...bodies]) body.dispose();
|
|
2020
|
+
areaMembershipBuffers.clear();
|
|
2021
|
+
buoyancies.clear();
|
|
1333
2022
|
kinematicMotions.clear();
|
|
1334
2023
|
activeContacts.clear();
|
|
1335
2024
|
debugSeries = [];
|
|
@@ -1346,8 +2035,11 @@ function rapier(options = {}) {
|
|
|
1346
2035
|
dispose: () => {
|
|
1347
2036
|
unregisterObservations?.();
|
|
1348
2037
|
unregisterObservations = void 0;
|
|
2038
|
+
for (const buoyancy of [...buoyancies]) buoyancy.dispose();
|
|
1349
2039
|
for (const area of [...areas.values()]) area.dispose();
|
|
1350
2040
|
for (const body of [...bodies]) body.dispose();
|
|
2041
|
+
areaMembershipBuffers.clear();
|
|
2042
|
+
buoyancies.clear();
|
|
1351
2043
|
simulation?.dispose();
|
|
1352
2044
|
simulation = void 0;
|
|
1353
2045
|
context = void 0;
|
|
@@ -1445,9 +2137,9 @@ function rapier(options = {}) {
|
|
|
1445
2137
|
}
|
|
1446
2138
|
};
|
|
1447
2139
|
}
|
|
1448
|
-
function removeContactsFor(
|
|
2140
|
+
function removeContactsFor(bodyId2) {
|
|
1449
2141
|
for (const [key, pair] of activeContacts) {
|
|
1450
|
-
if (pair.includes(
|
|
2142
|
+
if (pair.includes(bodyId2)) activeContacts.delete(key);
|
|
1451
2143
|
}
|
|
1452
2144
|
}
|
|
1453
2145
|
}
|
|
@@ -1457,14 +2149,18 @@ function growSleepStates(buffer, bodyCount) {
|
|
|
1457
2149
|
return new Float32Array(Math.max(required, buffer.length * 2));
|
|
1458
2150
|
}
|
|
1459
2151
|
function physicsEntityId(ctx, body) {
|
|
2152
|
+
const object = body.object;
|
|
2153
|
+
const stableId = body.body.entity ?? `physics.body.${body.body.id}`;
|
|
1460
2154
|
for (const id of Object.keys(ctx.entities.snapshot())) {
|
|
1461
2155
|
const entity = ctx.entities.get(id);
|
|
1462
|
-
if (entity === body || entity ===
|
|
1463
|
-
if (entity !== void 0 && Object.values(entity).some(
|
|
2156
|
+
if (entity === body || object !== void 0 && entity === object) return id;
|
|
2157
|
+
if (entity !== void 0 && Object.values(entity).some(
|
|
2158
|
+
(value) => value === body || object !== void 0 && value === object
|
|
2159
|
+
)) {
|
|
1464
2160
|
return id;
|
|
1465
2161
|
}
|
|
1466
2162
|
}
|
|
1467
|
-
return
|
|
2163
|
+
return stableId;
|
|
1468
2164
|
}
|
|
1469
2165
|
|
|
1470
|
-
export { Area3D, CharacterBody3D, CollisionShape3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier };
|
|
2166
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, buildStaticColliders, interactionGroups, rapier, softBodyCollision };
|