@threenative/physics 0.2.1 → 0.3.0
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-CnqoAfEr.d.ts} +205 -69
- package/dist/index.d.ts +45 -3
- package/dist/index.js +775 -172
- package/dist/native/index.d.ts +2 -2
- package/dist/native/index.js +720 -134
- package/dist/navigation/index.d.ts +1 -1
- package/dist/navigation/index.js +201 -4678
- package/package.json +8 -5
package/dist/native/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { Vector3, Quaternion, Matrix4 } from 'three';
|
|
2
|
+
|
|
1
3
|
// src/handles.ts
|
|
2
4
|
function physicsHandle(raw) {
|
|
3
5
|
return { raw };
|
|
@@ -26,10 +28,55 @@ 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
|
+
}
|
|
29
41
|
function requireFiniteVector(vector, label) {
|
|
30
42
|
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
43
|
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
32
44
|
}
|
|
45
|
+
function requireFiniteRotation(rotation, label) {
|
|
46
|
+
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))
|
|
47
|
+
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z, w }.`);
|
|
48
|
+
if (f32LengthSquared([rotation.x, rotation.y, rotation.z, rotation.w]) <= F32_EPSILON)
|
|
49
|
+
throw new Error(`TN_PHYSICS_INVALID: ${label} must not have zero length.`);
|
|
50
|
+
}
|
|
51
|
+
function requirePhysicsJointCreateOptions(options) {
|
|
52
|
+
if (typeof options !== "object" || options === null)
|
|
53
|
+
throw new Error("IPhysicsSimulation joint options must be an object.");
|
|
54
|
+
if (options.type !== "pin" && options.type !== "hinge" && options.type !== "fixed")
|
|
55
|
+
throw new Error("IPhysicsSimulation joint options have an unknown type.");
|
|
56
|
+
if (!Number.isSafeInteger(options.bodyA) || options.bodyA < 0 || !Number.isSafeInteger(options.bodyB) || options.bodyB < 0 || options.bodyA === options.bodyB)
|
|
57
|
+
throw new Error("IPhysicsSimulation joint options must reference two distinct body ids.");
|
|
58
|
+
requireFiniteVector(options.anchorA, "joint anchorA");
|
|
59
|
+
requireFiniteVector(options.anchorB, "joint anchorB");
|
|
60
|
+
if (options.type === "hinge") {
|
|
61
|
+
if (options.axis === void 0) throw new Error("IPhysicsSimulation hinge requires an axis.");
|
|
62
|
+
requireFiniteVector(options.axis, "joint axis");
|
|
63
|
+
if (f32LengthSquared([options.axis.x, options.axis.y, options.axis.z]) <= F32_EPSILON_SQUARED)
|
|
64
|
+
throw new Error("IPhysicsSimulation hinge axis must not have zero length.");
|
|
65
|
+
if (options.limit !== void 0) {
|
|
66
|
+
if (typeof options.limit !== "object" || options.limit === null)
|
|
67
|
+
throw new Error("IPhysicsSimulation hinge limit must contain ordered finite bounds.");
|
|
68
|
+
if (!Number.isFinite(options.limit.lower) || !Number.isFinite(options.limit.upper) || options.limit.lower > options.limit.upper)
|
|
69
|
+
throw new Error("IPhysicsSimulation hinge limit must contain ordered finite bounds.");
|
|
70
|
+
}
|
|
71
|
+
} else if (options.axis !== void 0 || options.limit !== void 0) {
|
|
72
|
+
throw new Error("IPhysicsSimulation joint limits and axes are only valid for a hinge.");
|
|
73
|
+
}
|
|
74
|
+
if (options.type !== "fixed" && (options.frameA !== void 0 || options.frameB !== void 0))
|
|
75
|
+
throw new Error("IPhysicsSimulation joint frames are only valid for a fixed joint.");
|
|
76
|
+
if (options.frameA !== void 0) requireFiniteRotation(options.frameA, "joint frameA");
|
|
77
|
+
if (options.frameB !== void 0) requireFiniteRotation(options.frameB, "joint frameB");
|
|
78
|
+
return options;
|
|
79
|
+
}
|
|
33
80
|
var selectedBackend;
|
|
34
81
|
function installPhysicsSimulationBackend(backend) {
|
|
35
82
|
selectedBackend = backend;
|
|
@@ -221,18 +268,24 @@ function growUint32(buffer, minimum) {
|
|
|
221
268
|
while (length < minimum) length *= 2;
|
|
222
269
|
return length === buffer.length ? buffer : new Uint32Array(length);
|
|
223
270
|
}
|
|
271
|
+
var NATIVE_CHARACTER_STATE_STRIDE = 6;
|
|
224
272
|
function createNativePhysicsSimulation(raw, version) {
|
|
225
273
|
const bodyIds = /* @__PURE__ */ new Set();
|
|
226
274
|
const bodyHandles = /* @__PURE__ */ new Map();
|
|
227
|
-
const
|
|
275
|
+
const jointBodies = /* @__PURE__ */ new Map();
|
|
228
276
|
const characterIds = /* @__PURE__ */ new Set();
|
|
229
277
|
const characterState = /* @__PURE__ */ new Map();
|
|
278
|
+
const characterStatePresent = /* @__PURE__ */ new Set();
|
|
230
279
|
let characterStates = new Float32Array(48);
|
|
231
280
|
const rayOutput = new Float32Array(8);
|
|
232
281
|
let areaPairs = new Uint32Array(32);
|
|
233
282
|
const areaIntersections = /* @__PURE__ */ new Map();
|
|
283
|
+
const emptyAreaIntersections = /* @__PURE__ */ new Set();
|
|
284
|
+
let bodyTransforms = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 8);
|
|
285
|
+
const bodyTransformRows = /* @__PURE__ */ new Map();
|
|
234
286
|
let characterStateDirty = true;
|
|
235
287
|
let areaIntersectionsDirty = true;
|
|
288
|
+
let bodyTransformsDirty = true;
|
|
236
289
|
let disposed = false;
|
|
237
290
|
const requireLive = () => {
|
|
238
291
|
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
@@ -240,6 +293,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
240
293
|
const invalidateObservations = () => {
|
|
241
294
|
characterStateDirty = true;
|
|
242
295
|
areaIntersectionsDirty = true;
|
|
296
|
+
bodyTransformsDirty = true;
|
|
243
297
|
};
|
|
244
298
|
const bodyHandle = (id) => {
|
|
245
299
|
const handle = bodyHandles.get(id);
|
|
@@ -262,23 +316,56 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
262
316
|
position: finiteQueryVector(hit.position, "query hit position")
|
|
263
317
|
};
|
|
264
318
|
};
|
|
319
|
+
const refreshBodyTransforms = () => {
|
|
320
|
+
if (!bodyTransformsDirty) return;
|
|
321
|
+
const required = Math.max(1, bodyIds.size) * PHYSICS_TRANSFORM_STRIDE;
|
|
322
|
+
if (bodyTransforms.length < required) bodyTransforms = new Float32Array(required);
|
|
323
|
+
const count = raw.readVisibleTransforms(bodyTransforms);
|
|
324
|
+
bodyTransformRows.clear();
|
|
325
|
+
for (let index = 0; index < count; index += 1) {
|
|
326
|
+
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
327
|
+
const id = bodyTransforms[offset];
|
|
328
|
+
if (id === void 0 || !Number.isInteger(id) || id < 0)
|
|
329
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed visible transform record");
|
|
330
|
+
bodyTransformRows.set(id, offset);
|
|
331
|
+
}
|
|
332
|
+
bodyTransformsDirty = false;
|
|
333
|
+
};
|
|
334
|
+
const scalarAt = (offset) => {
|
|
335
|
+
const value = bodyTransforms[offset];
|
|
336
|
+
if (value === void 0 || !Number.isFinite(value))
|
|
337
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: visible transform contains a non-finite scalar");
|
|
338
|
+
return value;
|
|
339
|
+
};
|
|
265
340
|
const refreshCharacterState = () => {
|
|
266
341
|
if (!characterStateDirty) return;
|
|
267
|
-
const required = Math.max(1, characterIds.size) *
|
|
342
|
+
const required = Math.max(1, characterIds.size) * NATIVE_CHARACTER_STATE_STRIDE;
|
|
268
343
|
if (characterStates.length < required) characterStates = new Float32Array(required);
|
|
344
|
+
characterStates.fill(Number.NaN, 0, required);
|
|
269
345
|
const stateCount = raw.readCharacterStates(characterStates);
|
|
270
|
-
|
|
346
|
+
characterStatePresent.clear();
|
|
271
347
|
for (let index = 0; index < stateCount; index += 1) {
|
|
272
|
-
const offset = index *
|
|
348
|
+
const offset = index * NATIVE_CHARACTER_STATE_STRIDE;
|
|
273
349
|
const id = characterStates[offset];
|
|
274
350
|
const grounded = characterStates[offset + 1];
|
|
275
351
|
const groundCollider = characterStates[offset + 2];
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
352
|
+
const normalX = characterStates[offset + 3];
|
|
353
|
+
const normalY = characterStates[offset + 4];
|
|
354
|
+
const normalZ = characterStates[offset + 5];
|
|
355
|
+
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))
|
|
356
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed six-float character state");
|
|
357
|
+
let state = characterState.get(id);
|
|
358
|
+
if (state === void 0) {
|
|
359
|
+
state = { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } };
|
|
360
|
+
characterState.set(id, state);
|
|
361
|
+
}
|
|
362
|
+
state.grounded = grounded === 1;
|
|
363
|
+
state.groundCollider = groundCollider < 0 ? void 0 : groundCollider;
|
|
364
|
+
state.groundBody = groundCollider < 0 ? void 0 : bodyHandles.get(groundCollider);
|
|
365
|
+
state.groundNormal.x = normalX;
|
|
366
|
+
state.groundNormal.y = normalY;
|
|
367
|
+
state.groundNormal.z = normalZ;
|
|
368
|
+
characterStatePresent.add(id);
|
|
282
369
|
}
|
|
283
370
|
characterStateDirty = false;
|
|
284
371
|
};
|
|
@@ -296,15 +383,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
296
383
|
areaPairs = growUint32(areaPairs, Math.min(maximum, areaPairs.length * 2));
|
|
297
384
|
}
|
|
298
385
|
}
|
|
299
|
-
areaIntersections.clear();
|
|
300
|
-
for (const areaId of areaIds) areaIntersections.set(areaId, /* @__PURE__ */ new Set());
|
|
386
|
+
for (const members of areaIntersections.values()) members.clear();
|
|
301
387
|
for (let index = 0; index < pairCount; index += 1) {
|
|
302
388
|
const offset = index * 2;
|
|
303
389
|
const areaId = areaPairs[offset];
|
|
304
|
-
const
|
|
305
|
-
if (areaId === void 0 ||
|
|
390
|
+
const bodyId2 = areaPairs[offset + 1];
|
|
391
|
+
if (areaId === void 0 || bodyId2 === void 0)
|
|
306
392
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed area intersection");
|
|
307
|
-
areaIntersections.get(areaId)?.add(
|
|
393
|
+
areaIntersections.get(areaId)?.add(bodyId2);
|
|
308
394
|
}
|
|
309
395
|
areaIntersectionsDirty = false;
|
|
310
396
|
};
|
|
@@ -313,6 +399,11 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
313
399
|
rawEventQueue: raw,
|
|
314
400
|
rawWorld: raw,
|
|
315
401
|
createBody: (options) => {
|
|
402
|
+
requireLive();
|
|
403
|
+
if (!Number.isFinite(options.mass) || options.mass < 0)
|
|
404
|
+
throw new Error("Physics body mass must be a finite non-negative number.");
|
|
405
|
+
requireFiniteVector(options.position, "body position");
|
|
406
|
+
requireFiniteRotation(options.rotation, "body rotation");
|
|
316
407
|
const shape = primitiveShape(options.shape);
|
|
317
408
|
const sensor = requirePhysicsBodySensor(options);
|
|
318
409
|
const id = raw.createBody({
|
|
@@ -331,8 +422,13 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
331
422
|
const bodyHandleValue = physicsBodyHandle(id, rawHandle, options.entity);
|
|
332
423
|
bodyIds.add(id);
|
|
333
424
|
bodyHandles.set(id, bodyHandleValue);
|
|
334
|
-
if (sensor)
|
|
335
|
-
|
|
425
|
+
if (sensor) {
|
|
426
|
+
areaIntersections.set(id, /* @__PURE__ */ new Set());
|
|
427
|
+
}
|
|
428
|
+
if (options.type === "character") {
|
|
429
|
+
characterIds.add(id);
|
|
430
|
+
characterState.set(id, { grounded: false, groundNormal: { x: 0, y: 1, z: 0 } });
|
|
431
|
+
}
|
|
336
432
|
invalidateObservations();
|
|
337
433
|
return {
|
|
338
434
|
body: bodyHandleValue,
|
|
@@ -341,20 +437,49 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
341
437
|
rawShape: opaqueNativeShape(shape)
|
|
342
438
|
};
|
|
343
439
|
},
|
|
440
|
+
createJoint: (jointOptions) => {
|
|
441
|
+
requireLive();
|
|
442
|
+
const normalized = requirePhysicsJointCreateOptions(jointOptions);
|
|
443
|
+
if (!bodyIds.has(normalized.bodyA) || !bodyIds.has(normalized.bodyB))
|
|
444
|
+
throw new Error("TN_PHYSICS_UNKNOWN_BODY: joint references an unknown body.");
|
|
445
|
+
if (raw.createJoint === void 0)
|
|
446
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
447
|
+
const id = raw.createJoint(normalized);
|
|
448
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
449
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid joint id");
|
|
450
|
+
if (jointBodies.has(id))
|
|
451
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned a duplicate joint id");
|
|
452
|
+
jointBodies.set(id, [normalized.bodyA, normalized.bodyB]);
|
|
453
|
+
return id;
|
|
454
|
+
},
|
|
344
455
|
configureCharacter: (id, options) => {
|
|
456
|
+
requireLive();
|
|
345
457
|
raw.configureCharacter(id, options);
|
|
346
458
|
invalidateObservations();
|
|
347
459
|
},
|
|
348
460
|
removeBody: (id) => {
|
|
461
|
+
requireLive();
|
|
349
462
|
raw.removeBody(id);
|
|
350
463
|
bodyIds.delete(id);
|
|
351
464
|
bodyHandles.delete(id);
|
|
352
|
-
|
|
465
|
+
for (const [jointId, bodies] of jointBodies) {
|
|
466
|
+
if (bodies[0] === id || bodies[1] === id) jointBodies.delete(jointId);
|
|
467
|
+
}
|
|
468
|
+
areaIntersections.delete(id);
|
|
353
469
|
characterIds.delete(id);
|
|
354
470
|
characterState.delete(id);
|
|
471
|
+
characterStatePresent.delete(id);
|
|
355
472
|
invalidateObservations();
|
|
356
473
|
},
|
|
474
|
+
removeJoint: (id) => {
|
|
475
|
+
if (disposed || !jointBodies.has(id)) return;
|
|
476
|
+
if (raw.removeJoint === void 0)
|
|
477
|
+
throw new Error("TN_NATIVE_PHYSICS_JOINTS_MISSING: runtime ABI is too old");
|
|
478
|
+
raw.removeJoint(id);
|
|
479
|
+
jointBodies.delete(id);
|
|
480
|
+
},
|
|
357
481
|
setBodyTransform: (id, position) => {
|
|
482
|
+
requireLive();
|
|
358
483
|
if (raw.setBodyTransform === void 0)
|
|
359
484
|
throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
|
|
360
485
|
raw.setBodyTransform(id, position);
|
|
@@ -375,6 +500,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
375
500
|
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
376
501
|
raw.applyBodyForce(id, force);
|
|
377
502
|
},
|
|
503
|
+
applyBodyForceAtPoint: (id, force, point) => {
|
|
504
|
+
requireLive();
|
|
505
|
+
requireFiniteVector(force, "force");
|
|
506
|
+
requireFiniteVector(point, "force point");
|
|
507
|
+
if (raw.applyBodyForceAtPoint === void 0)
|
|
508
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
509
|
+
raw.applyBodyForceAtPoint(id, force, point);
|
|
510
|
+
},
|
|
378
511
|
setBodyLinearVelocity: (id, velocity) => {
|
|
379
512
|
requireLive();
|
|
380
513
|
requireFiniteVector(velocity, "linearVelocity");
|
|
@@ -391,11 +524,13 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
391
524
|
return velocity;
|
|
392
525
|
},
|
|
393
526
|
step: (deltaTime, inputSnapshot) => {
|
|
527
|
+
requireLive();
|
|
394
528
|
requirePhysicsStepInput(deltaTime, inputSnapshot, (id) => bodyIds.has(id));
|
|
395
529
|
raw.step(deltaTime, inputSnapshot);
|
|
396
530
|
invalidateObservations();
|
|
397
531
|
},
|
|
398
532
|
readVisibleTransforms: (buffer) => {
|
|
533
|
+
requireLive();
|
|
399
534
|
requirePhysicsRenderBuffer(buffer, bodyIds.size);
|
|
400
535
|
return raw.readVisibleTransforms(buffer);
|
|
401
536
|
},
|
|
@@ -405,13 +540,14 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
405
540
|
return raw.readBodySleepStates(buffer);
|
|
406
541
|
},
|
|
407
542
|
intersectRay: (value) => {
|
|
543
|
+
requireLive();
|
|
408
544
|
const query = requirePhysicsRayQuery(value);
|
|
409
545
|
const hit = raw.intersectRay(query, rayOutput);
|
|
410
546
|
if (typeof hit === "number") {
|
|
411
547
|
if (hit === 0) return void 0;
|
|
412
548
|
if (hit !== 1)
|
|
413
549
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
|
|
414
|
-
const
|
|
550
|
+
const bodyId2 = rayOutput[0];
|
|
415
551
|
const position = {
|
|
416
552
|
x: rayOutput[1],
|
|
417
553
|
y: rayOutput[2],
|
|
@@ -423,9 +559,9 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
423
559
|
z: rayOutput[6]
|
|
424
560
|
};
|
|
425
561
|
const distance = rayOutput[7];
|
|
426
|
-
if (
|
|
562
|
+
if (bodyId2 === void 0 || !Number.isSafeInteger(bodyId2) || bodyId2 < 0 || distance === void 0)
|
|
427
563
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
|
|
428
|
-
const base2 = nativeHit({ bodyId, position });
|
|
564
|
+
const base2 = nativeHit({ bodyId: bodyId2, position });
|
|
429
565
|
if (!Number.isFinite(distance) || distance < 0)
|
|
430
566
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
|
|
431
567
|
return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
|
|
@@ -441,6 +577,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
441
577
|
};
|
|
442
578
|
},
|
|
443
579
|
intersectShape: (value) => {
|
|
580
|
+
requireLive();
|
|
444
581
|
const query = requirePhysicsShapeQuery(value);
|
|
445
582
|
const hits = raw.intersectShape({
|
|
446
583
|
collisionMask: query.collisionMask,
|
|
@@ -454,21 +591,44 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
454
591
|
return hits.map(nativeHit);
|
|
455
592
|
},
|
|
456
593
|
intersectPoint: (value) => {
|
|
594
|
+
requireLive();
|
|
457
595
|
const query = requirePhysicsPointQuery(value);
|
|
458
596
|
const hits = raw.intersectPoint(query);
|
|
459
597
|
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
460
598
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed point query result");
|
|
461
599
|
return hits.map(nativeHit);
|
|
462
600
|
},
|
|
601
|
+
readBodyTransform: (id) => {
|
|
602
|
+
requireLive();
|
|
603
|
+
refreshBodyTransforms();
|
|
604
|
+
const offset = bodyTransformRows.get(id);
|
|
605
|
+
if (offset === void 0) return void 0;
|
|
606
|
+
return {
|
|
607
|
+
position: {
|
|
608
|
+
x: scalarAt(offset + 1),
|
|
609
|
+
y: scalarAt(offset + 2),
|
|
610
|
+
z: scalarAt(offset + 3)
|
|
611
|
+
},
|
|
612
|
+
rotation: {
|
|
613
|
+
x: scalarAt(offset + 4),
|
|
614
|
+
y: scalarAt(offset + 5),
|
|
615
|
+
z: scalarAt(offset + 6),
|
|
616
|
+
w: scalarAt(offset + 7)
|
|
617
|
+
}
|
|
618
|
+
};
|
|
619
|
+
},
|
|
463
620
|
readCharacterState: (id) => {
|
|
621
|
+
requireLive();
|
|
464
622
|
refreshCharacterState();
|
|
465
|
-
return characterState.get(id);
|
|
623
|
+
return characterStatePresent.has(id) ? characterState.get(id) : void 0;
|
|
466
624
|
},
|
|
467
625
|
areaIntersections: (id) => {
|
|
626
|
+
requireLive();
|
|
468
627
|
refreshAreaIntersections();
|
|
469
|
-
return areaIntersections.get(id) ??
|
|
628
|
+
return areaIntersections.get(id) ?? emptyAreaIntersections;
|
|
470
629
|
},
|
|
471
630
|
drainCollisionEvents: (buffer) => {
|
|
631
|
+
requireLive();
|
|
472
632
|
requirePhysicsEventBuffer(buffer);
|
|
473
633
|
return raw.drainCollisionEvents(buffer);
|
|
474
634
|
},
|
|
@@ -477,10 +637,12 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
477
637
|
disposed = true;
|
|
478
638
|
raw.dispose();
|
|
479
639
|
bodyIds.clear();
|
|
640
|
+
bodyTransformRows.clear();
|
|
480
641
|
bodyHandles.clear();
|
|
481
|
-
|
|
642
|
+
jointBodies.clear();
|
|
482
643
|
characterIds.clear();
|
|
483
644
|
characterState.clear();
|
|
645
|
+
characterStatePresent.clear();
|
|
484
646
|
areaIntersections.clear();
|
|
485
647
|
characterStateDirty = false;
|
|
486
648
|
areaIntersectionsDirty = false;
|
|
@@ -513,13 +675,27 @@ installPhysicsSimulationBackend({
|
|
|
513
675
|
}
|
|
514
676
|
});
|
|
515
677
|
|
|
678
|
+
// src/transformRecord.ts
|
|
679
|
+
function bulkTransformValue(values, index) {
|
|
680
|
+
const value = values[index];
|
|
681
|
+
if (value === void 0 || !Number.isFinite(value))
|
|
682
|
+
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
683
|
+
return value;
|
|
684
|
+
}
|
|
685
|
+
|
|
516
686
|
// src/Area3D.ts
|
|
517
687
|
var MAX_CONTACT_LOG = 1e3;
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
if (
|
|
521
|
-
throw new Error("
|
|
522
|
-
|
|
688
|
+
var IDENTITY_ROTATION = { w: 1, x: 0, y: 0, z: 0 };
|
|
689
|
+
function resolveInitialTransform(source, fallbackPosition) {
|
|
690
|
+
if (source.object !== void 0 && source.position !== void 0)
|
|
691
|
+
throw new Error("A physics node accepts either object or position, not both.");
|
|
692
|
+
if (source.object !== void 0)
|
|
693
|
+
return { position: source.object.position, rotation: source.object.quaternion };
|
|
694
|
+
if (source.position !== void 0)
|
|
695
|
+
return { position: source.position, rotation: IDENTITY_ROTATION };
|
|
696
|
+
if (fallbackPosition !== void 0)
|
|
697
|
+
return { position: fallbackPosition, rotation: IDENTITY_ROTATION };
|
|
698
|
+
throw new Error("A physics node requires either object or position.");
|
|
523
699
|
}
|
|
524
700
|
var Area3D = class {
|
|
525
701
|
entity;
|
|
@@ -540,13 +716,18 @@ var Area3D = class {
|
|
|
540
716
|
this.#simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
541
717
|
this.#physics = options.physics;
|
|
542
718
|
this.entity = options.entity;
|
|
543
|
-
Object.assign(this.#position, options
|
|
544
|
-
const
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
719
|
+
Object.assign(this.#position, resolveInitialTransform(options, { x: 0, y: 0, z: 0 }).position);
|
|
720
|
+
const base = options.shape.descriptor;
|
|
721
|
+
const groups = interactionGroups(
|
|
722
|
+
options.collisionLayer ?? base.collisionLayer,
|
|
723
|
+
options.collisionMask ?? base.collisionMask
|
|
724
|
+
);
|
|
725
|
+
const shape = {
|
|
726
|
+
...base,
|
|
727
|
+
collisionLayer: groups >>> 16,
|
|
728
|
+
collisionMask: groups & 65535,
|
|
729
|
+
sensor: true
|
|
730
|
+
};
|
|
550
731
|
const registration = this.#simulation.createBody({
|
|
551
732
|
mass: 0,
|
|
552
733
|
entity: this.entity,
|
|
@@ -556,7 +737,6 @@ var Area3D = class {
|
|
|
556
737
|
shape,
|
|
557
738
|
type: "kinematic"
|
|
558
739
|
});
|
|
559
|
-
options.shape.bindRaw(registration.rawShape);
|
|
560
740
|
this.body = registration.body;
|
|
561
741
|
this.collider = registration.collider;
|
|
562
742
|
this.#physics?.addArea(this);
|
|
@@ -583,14 +763,22 @@ var Area3D = class {
|
|
|
583
763
|
/** Called by the shared plugin before a bulk step. */
|
|
584
764
|
writeKinematic(buffer, offset) {
|
|
585
765
|
if (this.#disposed) return;
|
|
586
|
-
buffer.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
766
|
+
buffer[offset] = this.body.id;
|
|
767
|
+
buffer[offset + 1] = this.#position.x;
|
|
768
|
+
buffer[offset + 2] = this.#position.y;
|
|
769
|
+
buffer[offset + 3] = this.#position.z;
|
|
770
|
+
buffer[offset + 4] = 0;
|
|
771
|
+
buffer[offset + 5] = 0;
|
|
772
|
+
buffer[offset + 6] = 0;
|
|
773
|
+
buffer[offset + 7] = 1;
|
|
590
774
|
}
|
|
591
775
|
applyTransform(values, offset) {
|
|
592
|
-
const
|
|
593
|
-
|
|
776
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
777
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
778
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
779
|
+
this.#position.x = x;
|
|
780
|
+
this.#position.y = y;
|
|
781
|
+
this.#position.z = z;
|
|
594
782
|
}
|
|
595
783
|
handleCollision(body, started) {
|
|
596
784
|
if (this.#disposed || !this.#monitoring) return;
|
|
@@ -631,14 +819,6 @@ var Area3D = class {
|
|
|
631
819
|
this.#listeners.bodyExited.clear();
|
|
632
820
|
}
|
|
633
821
|
};
|
|
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
822
|
var CharacterBody3D = class {
|
|
643
823
|
body;
|
|
644
824
|
collider;
|
|
@@ -650,6 +830,12 @@ var CharacterBody3D = class {
|
|
|
650
830
|
maxFallSpeed;
|
|
651
831
|
oneWayLayers;
|
|
652
832
|
grounded = false;
|
|
833
|
+
/** Contact normal from the completed physics step; reset to +Y while airborne. */
|
|
834
|
+
groundNormal = new Vector3(0, 1, 0);
|
|
835
|
+
/** Portable handle for the body currently supporting this character. */
|
|
836
|
+
groundBody;
|
|
837
|
+
/** Radians between `groundNormal` and world up (+Y). */
|
|
838
|
+
slopeAngle = 0;
|
|
653
839
|
#simulation;
|
|
654
840
|
#physics;
|
|
655
841
|
#desired = { x: 0, y: 0, z: 0 };
|
|
@@ -710,18 +896,29 @@ var CharacterBody3D = class {
|
|
|
710
896
|
this.#physics?.add(this);
|
|
711
897
|
}
|
|
712
898
|
move(desiredTranslation) {
|
|
713
|
-
this.#desired =
|
|
899
|
+
this.#desired.x = desiredTranslation.x;
|
|
900
|
+
this.#desired.y = desiredTranslation.y;
|
|
901
|
+
this.#desired.z = desiredTranslation.z;
|
|
714
902
|
this.#sliding = false;
|
|
715
903
|
}
|
|
904
|
+
/**
|
|
905
|
+
* Queue velocity-based motion for the shared bulk physics step.
|
|
906
|
+
*
|
|
907
|
+
* @remarks
|
|
908
|
+
* This does not move `object` synchronously. The solver writes the transform after the step, so
|
|
909
|
+
* reading `object.position` immediately after this call still observes the previous transform.
|
|
910
|
+
* Three.js `Vector3` instances are mutable, so do not retain `object.position` itself as the
|
|
911
|
+
* snapshot: use `const before = object.position.clone()` (or copy its `x`, `y`, and `z` scalars).
|
|
912
|
+
* Compare that snapshot with `object.position` only after the deferred bulk physics step,
|
|
913
|
+
* typically on the next frame, to measure the motion that the solver applied.
|
|
914
|
+
*/
|
|
716
915
|
moveAndSlide(dt) {
|
|
717
916
|
if (!Number.isFinite(dt) || dt < 0)
|
|
718
917
|
throw new Error("CharacterBody3D.moveAndSlide requires a finite non-negative dt.");
|
|
719
918
|
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
|
-
};
|
|
919
|
+
this.#desired.x = this.velocity.x * dt;
|
|
920
|
+
this.#desired.y = this.velocity.y * dt;
|
|
921
|
+
this.#desired.z = this.velocity.z * dt;
|
|
725
922
|
this.#sliding = true;
|
|
726
923
|
}
|
|
727
924
|
/** Called by the shared plugin before a bulk step. */
|
|
@@ -730,19 +927,17 @@ var CharacterBody3D = class {
|
|
|
730
927
|
this.#beforeY = this.object.position.y;
|
|
731
928
|
this.#desiredY = this.#desired.y;
|
|
732
929
|
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
|
-
);
|
|
930
|
+
const carryX = carry?.x ?? 0;
|
|
931
|
+
const carryY = carry?.y ?? 0;
|
|
932
|
+
const carryZ = carry?.z ?? 0;
|
|
933
|
+
buffer[offset] = this.body.id;
|
|
934
|
+
buffer[offset + 1] = this.object.position.x + this.#desired.x + carryX;
|
|
935
|
+
buffer[offset + 2] = this.object.position.y + this.#desired.y + carryY;
|
|
936
|
+
buffer[offset + 3] = this.object.position.z + this.#desired.z + carryZ;
|
|
937
|
+
buffer[offset + 4] = this.object.quaternion.x;
|
|
938
|
+
buffer[offset + 5] = this.object.quaternion.y;
|
|
939
|
+
buffer[offset + 6] = this.object.quaternion.z;
|
|
940
|
+
buffer[offset + 7] = this.object.quaternion.w;
|
|
746
941
|
}
|
|
747
942
|
syncToPhysics() {
|
|
748
943
|
}
|
|
@@ -753,24 +948,44 @@ var CharacterBody3D = class {
|
|
|
753
948
|
this.#simulation.setBodyTransform(this.body.id, position);
|
|
754
949
|
this.object.position.set(position.x, position.y, position.z);
|
|
755
950
|
this.velocity.set(0, 0, 0);
|
|
756
|
-
this.#desired =
|
|
951
|
+
this.#desired.x = 0;
|
|
952
|
+
this.#desired.y = 0;
|
|
953
|
+
this.#desired.z = 0;
|
|
757
954
|
this.#sliding = false;
|
|
758
|
-
this.#
|
|
759
|
-
this.grounded = false;
|
|
955
|
+
this.#clearGroundState();
|
|
760
956
|
}
|
|
761
957
|
applyTransform(values, offset) {
|
|
762
|
-
const
|
|
958
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
959
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
960
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
961
|
+
const qx = bulkTransformValue(values, offset + 4);
|
|
962
|
+
const qy = bulkTransformValue(values, offset + 5);
|
|
963
|
+
const qz = bulkTransformValue(values, offset + 6);
|
|
964
|
+
const qw = bulkTransformValue(values, offset + 7);
|
|
763
965
|
const state = this.#simulation.readCharacterState?.(this.body.id);
|
|
764
966
|
this.grounded = state?.grounded ?? (this.#sliding && this.#desiredY < 0 && y - this.#beforeY > this.#desiredY + 1e-4);
|
|
765
967
|
this.#groundCollider = state?.groundCollider;
|
|
968
|
+
if (this.grounded && state?.groundNormal !== void 0) {
|
|
969
|
+
this.groundNormal.set(state.groundNormal.x, state.groundNormal.y, state.groundNormal.z);
|
|
970
|
+
this.groundBody = state.groundBody;
|
|
971
|
+
this.slopeAngle = Math.acos(Math.max(-1, Math.min(1, this.groundNormal.y)));
|
|
972
|
+
} else if (!this.grounded) {
|
|
973
|
+
this.#clearGroundState();
|
|
974
|
+
}
|
|
766
975
|
if (this.#sliding && this.grounded && this.velocity.y < 0) this.velocity.y = 0;
|
|
767
976
|
this.object.position.set(x, y, z);
|
|
768
977
|
this.object.quaternion.set(qx, qy, qz, qw);
|
|
769
|
-
this.#desired =
|
|
978
|
+
this.#desired.x = 0;
|
|
979
|
+
this.#desired.y = 0;
|
|
980
|
+
this.#desired.z = 0;
|
|
770
981
|
this.#sliding = false;
|
|
771
982
|
}
|
|
772
983
|
syncFromPhysics() {
|
|
773
|
-
|
|
984
|
+
if (this.#simulation.readBodyTransform === void 0)
|
|
985
|
+
throw new Error(
|
|
986
|
+
"TN_PHYSICS_READ_TRANSFORM_MISSING: the active physics backend cannot read body transforms."
|
|
987
|
+
);
|
|
988
|
+
const transform = this.#simulation.readBodyTransform(this.body.id);
|
|
774
989
|
if (transform === void 0) return;
|
|
775
990
|
this.object.position.set(transform.position.x, transform.position.y, transform.position.z);
|
|
776
991
|
this.object.quaternion.set(
|
|
@@ -785,6 +1000,14 @@ var CharacterBody3D = class {
|
|
|
785
1000
|
this.#disposed = true;
|
|
786
1001
|
this.#physics?.remove(this);
|
|
787
1002
|
this.#simulation.removeBody(this.body.id);
|
|
1003
|
+
this.#clearGroundState();
|
|
1004
|
+
}
|
|
1005
|
+
#clearGroundState() {
|
|
1006
|
+
this.grounded = false;
|
|
1007
|
+
this.#groundCollider = void 0;
|
|
1008
|
+
this.groundBody = void 0;
|
|
1009
|
+
this.groundNormal.set(0, 1, 0);
|
|
1010
|
+
this.slopeAngle = 0;
|
|
788
1011
|
}
|
|
789
1012
|
};
|
|
790
1013
|
|
|
@@ -984,6 +1207,227 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
984
1207
|
return this;
|
|
985
1208
|
}
|
|
986
1209
|
};
|
|
1210
|
+
function finite(name, value) {
|
|
1211
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
1212
|
+
throw new Error(`Buoyancy3D.${name} must be finite.`);
|
|
1213
|
+
return value;
|
|
1214
|
+
}
|
|
1215
|
+
function nonNegative(name, value) {
|
|
1216
|
+
finite(name, value);
|
|
1217
|
+
if (value < 0) throw new Error(`Buoyancy3D.${name} must be non-negative.`);
|
|
1218
|
+
return value;
|
|
1219
|
+
}
|
|
1220
|
+
function positive(name, value) {
|
|
1221
|
+
finite(name, value);
|
|
1222
|
+
if (value <= 0) throw new Error(`Buoyancy3D.${name} must be positive.`);
|
|
1223
|
+
return value;
|
|
1224
|
+
}
|
|
1225
|
+
function pointVector(value) {
|
|
1226
|
+
let x;
|
|
1227
|
+
let y;
|
|
1228
|
+
let z;
|
|
1229
|
+
if ("x" in value) {
|
|
1230
|
+
x = value.x;
|
|
1231
|
+
y = value.y;
|
|
1232
|
+
z = value.z;
|
|
1233
|
+
} else {
|
|
1234
|
+
x = value[0];
|
|
1235
|
+
y = value[1];
|
|
1236
|
+
z = value[2];
|
|
1237
|
+
}
|
|
1238
|
+
return new Vector3(
|
|
1239
|
+
finite("hullPoints.position.x", x),
|
|
1240
|
+
finite("hullPoints.position.y", y),
|
|
1241
|
+
finite("hullPoints.position.z", z)
|
|
1242
|
+
);
|
|
1243
|
+
}
|
|
1244
|
+
function shapeVolume(body) {
|
|
1245
|
+
const shape = body.shape.descriptor;
|
|
1246
|
+
if (shape.kind === "box") return 8 * shape.x * shape.y * shape.z;
|
|
1247
|
+
if (shape.kind === "sphere") return 4 / 3 * Math.PI * shape.x ** 3;
|
|
1248
|
+
if (shape.kind === "capsule") return Math.PI * shape.y ** 2 * (2 * shape.x + 4 / 3 * shape.y);
|
|
1249
|
+
return 1;
|
|
1250
|
+
}
|
|
1251
|
+
function clamp01(value) {
|
|
1252
|
+
return Math.max(0, Math.min(1, value));
|
|
1253
|
+
}
|
|
1254
|
+
var Buoyancy3D = class {
|
|
1255
|
+
body;
|
|
1256
|
+
hullPoints;
|
|
1257
|
+
density;
|
|
1258
|
+
drag;
|
|
1259
|
+
gravity;
|
|
1260
|
+
#surface;
|
|
1261
|
+
#physics;
|
|
1262
|
+
#points;
|
|
1263
|
+
#rotation = new Quaternion();
|
|
1264
|
+
#elapsed = 0;
|
|
1265
|
+
#submergedFraction = 0;
|
|
1266
|
+
#enabled;
|
|
1267
|
+
#disposed = false;
|
|
1268
|
+
constructor(options) {
|
|
1269
|
+
if (options.hullPoints.length === 0)
|
|
1270
|
+
throw new Error("Buoyancy3D.hullPoints must contain at least one point.");
|
|
1271
|
+
const surface = options.surface ?? options.heightSource ?? options.field;
|
|
1272
|
+
if (surface === void 0) throw new Error("Buoyancy3D requires a surface height source.");
|
|
1273
|
+
const physics = options.physics ?? options.body.physics;
|
|
1274
|
+
if (physics === void 0 || physics.addBuoyancy === void 0)
|
|
1275
|
+
throw new Error("Buoyancy3D requires the body's physics context.");
|
|
1276
|
+
this.body = options.body;
|
|
1277
|
+
this.hullPoints = options.hullPoints;
|
|
1278
|
+
this.density = positive("density", options.density ?? 1e3);
|
|
1279
|
+
this.drag = nonNegative("drag", options.drag ?? 0);
|
|
1280
|
+
this.gravity = positive("gravity", options.gravity ?? 9.81);
|
|
1281
|
+
this.#surface = surface;
|
|
1282
|
+
this.#physics = physics;
|
|
1283
|
+
this.#enabled = options.buoyancy ?? true;
|
|
1284
|
+
const totalVolume = positive("volume", options.volume ?? shapeVolume(options.body));
|
|
1285
|
+
const yValues = options.hullPoints.map(({ position }) => pointVector(position).y);
|
|
1286
|
+
const minimumY = Math.min(...yValues);
|
|
1287
|
+
const maximumY = Math.max(...yValues);
|
|
1288
|
+
const defaultSpacing = options.pointSpacing ?? Math.max(maximumY - minimumY, 1);
|
|
1289
|
+
const pointVolume = totalVolume / options.hullPoints.length;
|
|
1290
|
+
this.#points = options.hullPoints.map((point) => {
|
|
1291
|
+
const position = pointVector(point.position);
|
|
1292
|
+
const volume = positive("hullPoints.volume", point.volume ?? pointVolume);
|
|
1293
|
+
const spacing = positive("hullPoints.spacing", point.spacing ?? defaultSpacing);
|
|
1294
|
+
return {
|
|
1295
|
+
force: { x: 0, y: 0, z: 0 },
|
|
1296
|
+
position,
|
|
1297
|
+
spacing,
|
|
1298
|
+
submergedFraction: 0,
|
|
1299
|
+
volume,
|
|
1300
|
+
worldPosition: new Vector3()
|
|
1301
|
+
};
|
|
1302
|
+
});
|
|
1303
|
+
this.body.buoyancy = this;
|
|
1304
|
+
physics.addBuoyancy(this);
|
|
1305
|
+
this.#measure();
|
|
1306
|
+
}
|
|
1307
|
+
get enabled() {
|
|
1308
|
+
return this.#enabled;
|
|
1309
|
+
}
|
|
1310
|
+
set enabled(value) {
|
|
1311
|
+
this.#enabled = value;
|
|
1312
|
+
}
|
|
1313
|
+
get buoyancy() {
|
|
1314
|
+
return this.#enabled;
|
|
1315
|
+
}
|
|
1316
|
+
set buoyancy(value) {
|
|
1317
|
+
this.#enabled = value;
|
|
1318
|
+
}
|
|
1319
|
+
get submergedFraction() {
|
|
1320
|
+
return this.#submergedFraction;
|
|
1321
|
+
}
|
|
1322
|
+
get time() {
|
|
1323
|
+
return this.#elapsed;
|
|
1324
|
+
}
|
|
1325
|
+
/** Called by `rapier()` immediately before its fixed-step backend call. */
|
|
1326
|
+
apply(deltaTime) {
|
|
1327
|
+
if (this.#disposed) return;
|
|
1328
|
+
if (!Number.isFinite(deltaTime) || deltaTime < 0)
|
|
1329
|
+
throw new Error("Buoyancy3D.apply requires a finite non-negative deltaTime.");
|
|
1330
|
+
this.#measure();
|
|
1331
|
+
if (this.#enabled) {
|
|
1332
|
+
const velocity = this.body.linearVelocity;
|
|
1333
|
+
for (const point of this.#points) {
|
|
1334
|
+
const submergedVolume = point.volume * point.submergedFraction;
|
|
1335
|
+
if (submergedVolume === 0) continue;
|
|
1336
|
+
point.force.x = -velocity.x * this.drag * submergedVolume;
|
|
1337
|
+
point.force.y = -velocity.y * this.drag * submergedVolume;
|
|
1338
|
+
point.force.z = -velocity.z * this.drag * submergedVolume;
|
|
1339
|
+
point.force.y += this.density * this.gravity * submergedVolume;
|
|
1340
|
+
this.body.applyForceAtPoint(point.force, point.worldPosition);
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
this.#elapsed += deltaTime;
|
|
1344
|
+
if (!Number.isFinite(this.#elapsed)) throw new Error("Buoyancy3D time overflowed.");
|
|
1345
|
+
}
|
|
1346
|
+
dispose() {
|
|
1347
|
+
if (this.#disposed) return;
|
|
1348
|
+
this.#disposed = true;
|
|
1349
|
+
this.#physics.removeBuoyancy?.(this);
|
|
1350
|
+
if (this.body.buoyancy === this) this.body.buoyancy = void 0;
|
|
1351
|
+
}
|
|
1352
|
+
#measure() {
|
|
1353
|
+
const points = this.#points;
|
|
1354
|
+
const object = this.body.object;
|
|
1355
|
+
if (object === void 0) throw new Error("Buoyancy3D requires a body with an object.");
|
|
1356
|
+
this.#rotation.copy(object.quaternion);
|
|
1357
|
+
let volume = 0;
|
|
1358
|
+
let submerged = 0;
|
|
1359
|
+
for (const point of points) {
|
|
1360
|
+
point.worldPosition.copy(point.position).applyQuaternion(this.#rotation).add(object.position);
|
|
1361
|
+
const result = this.#surface.sample(
|
|
1362
|
+
point.worldPosition.x,
|
|
1363
|
+
point.worldPosition.z,
|
|
1364
|
+
this.#elapsed
|
|
1365
|
+
);
|
|
1366
|
+
const height = finite("surface.height", result.height);
|
|
1367
|
+
const fraction = clamp01(0.5 + (height - point.worldPosition.y) / point.spacing);
|
|
1368
|
+
point.submergedFraction = fraction;
|
|
1369
|
+
volume += point.volume;
|
|
1370
|
+
submerged += point.volume * fraction;
|
|
1371
|
+
}
|
|
1372
|
+
this.#submergedFraction = volume === 0 ? 0 : submerged / volume;
|
|
1373
|
+
}
|
|
1374
|
+
};
|
|
1375
|
+
|
|
1376
|
+
// src/Joint3D.ts
|
|
1377
|
+
var ZERO_ANCHOR = { x: 0, y: 0, z: 0 };
|
|
1378
|
+
var X_AXIS = { x: 1, y: 0, z: 0 };
|
|
1379
|
+
var IDENTITY_FRAME = { x: 0, y: 0, z: 0, w: 1 };
|
|
1380
|
+
function bodyId(value, label) {
|
|
1381
|
+
const candidate = typeof value === "object" && value !== null && "body" in value ? value.body : value;
|
|
1382
|
+
if (typeof candidate !== "object" || candidate === null || !("id" in candidate) || typeof candidate.id !== "number")
|
|
1383
|
+
throw new Error(`TN_PHYSICS_INVALID_BODY: ${label} must expose a body handle.`);
|
|
1384
|
+
return candidate.id;
|
|
1385
|
+
}
|
|
1386
|
+
var Joint3D = class _Joint3D {
|
|
1387
|
+
id;
|
|
1388
|
+
#simulation;
|
|
1389
|
+
#disposed = false;
|
|
1390
|
+
constructor(simulation, id) {
|
|
1391
|
+
this.#simulation = simulation;
|
|
1392
|
+
this.id = id;
|
|
1393
|
+
}
|
|
1394
|
+
static #create(options, type, extra = {}) {
|
|
1395
|
+
const simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
1396
|
+
const jointOptions = {
|
|
1397
|
+
anchorA: options.anchorA ?? ZERO_ANCHOR,
|
|
1398
|
+
anchorB: options.anchorB ?? ZERO_ANCHOR,
|
|
1399
|
+
bodyA: bodyId(options.bodyA, "bodyA"),
|
|
1400
|
+
bodyB: bodyId(options.bodyB, "bodyB"),
|
|
1401
|
+
...extra,
|
|
1402
|
+
type
|
|
1403
|
+
};
|
|
1404
|
+
requirePhysicsJointCreateOptions(jointOptions);
|
|
1405
|
+
const id = simulation.createJoint(jointOptions);
|
|
1406
|
+
if (!Number.isSafeInteger(id) || id < 0)
|
|
1407
|
+
throw new Error("TN_PHYSICS_INVALID_JOINT: backend returned an invalid joint id.");
|
|
1408
|
+
return new _Joint3D(simulation, id);
|
|
1409
|
+
}
|
|
1410
|
+
static pin(options) {
|
|
1411
|
+
return _Joint3D.#create(options, "pin");
|
|
1412
|
+
}
|
|
1413
|
+
static hinge(options) {
|
|
1414
|
+
return _Joint3D.#create(options, "hinge", {
|
|
1415
|
+
axis: options.axis ?? X_AXIS,
|
|
1416
|
+
limit: options.limit
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
static fixed(options) {
|
|
1420
|
+
return _Joint3D.#create(options, "fixed", {
|
|
1421
|
+
frameA: options.frameA ?? IDENTITY_FRAME,
|
|
1422
|
+
frameB: options.frameB ?? IDENTITY_FRAME
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
dispose() {
|
|
1426
|
+
if (this.#disposed) return;
|
|
1427
|
+
this.#disposed = true;
|
|
1428
|
+
this.#simulation.removeJoint(this.id);
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
987
1431
|
|
|
988
1432
|
// src/PhysicsDirectSpaceState3D.ts
|
|
989
1433
|
function rayQuery(options) {
|
|
@@ -1026,28 +1470,107 @@ var PhysicsDirectSpaceState3D = class {
|
|
|
1026
1470
|
return this.#simulation.intersectPoint(pointQuery(options));
|
|
1027
1471
|
}
|
|
1028
1472
|
};
|
|
1473
|
+
var ONE = new Vector3(1, 1, 1);
|
|
1474
|
+
var PhysicsSoftBodyCollision = class {
|
|
1475
|
+
capacity;
|
|
1476
|
+
#bodies;
|
|
1477
|
+
#bodyMatrix = new Matrix4();
|
|
1478
|
+
#localMatrix = new Matrix4();
|
|
1479
|
+
#corner = new Vector3();
|
|
1480
|
+
#minimum = new Vector3();
|
|
1481
|
+
#maximum = new Vector3();
|
|
1482
|
+
constructor(bodies) {
|
|
1483
|
+
if (bodies.length === 0)
|
|
1484
|
+
throw new Error("softBodyCollision requires at least one RigidBody3D.");
|
|
1485
|
+
for (const body of bodies) {
|
|
1486
|
+
if (body.shape.descriptor.kind !== "box")
|
|
1487
|
+
throw new Error("softBodyCollision currently supports box shapes only.");
|
|
1488
|
+
if (body.object === void 0)
|
|
1489
|
+
throw new Error("softBodyCollision requires an object transform for every body.");
|
|
1490
|
+
}
|
|
1491
|
+
this.#bodies = bodies;
|
|
1492
|
+
this.capacity = bodies.length;
|
|
1493
|
+
}
|
|
1494
|
+
writeBoxes(target, worldToLocal) {
|
|
1495
|
+
if (target.length !== this.capacity * 8)
|
|
1496
|
+
throw new Error(
|
|
1497
|
+
`softBodyCollision expected ${this.capacity * 8} packed values, received ${target.length}.`
|
|
1498
|
+
);
|
|
1499
|
+
for (let index = 0; index < this.#bodies.length; index += 1) {
|
|
1500
|
+
const body = this.#bodies[index];
|
|
1501
|
+
const object = body.object;
|
|
1502
|
+
if (object === void 0)
|
|
1503
|
+
throw new Error("softBodyCollision body lost its object transform.");
|
|
1504
|
+
this.#bodyMatrix.compose(object.position, object.quaternion, ONE);
|
|
1505
|
+
this.#localMatrix.multiplyMatrices(worldToLocal, this.#bodyMatrix);
|
|
1506
|
+
this.#minimum.set(
|
|
1507
|
+
Number.POSITIVE_INFINITY,
|
|
1508
|
+
Number.POSITIVE_INFINITY,
|
|
1509
|
+
Number.POSITIVE_INFINITY
|
|
1510
|
+
);
|
|
1511
|
+
this.#maximum.set(
|
|
1512
|
+
Number.NEGATIVE_INFINITY,
|
|
1513
|
+
Number.NEGATIVE_INFINITY,
|
|
1514
|
+
Number.NEGATIVE_INFINITY
|
|
1515
|
+
);
|
|
1516
|
+
const shape = body.shape.descriptor;
|
|
1517
|
+
for (let corner = 0; corner < 8; corner += 1) {
|
|
1518
|
+
this.#corner.set(
|
|
1519
|
+
(corner & 1) === 0 ? -shape.x : shape.x,
|
|
1520
|
+
(corner & 2) === 0 ? -shape.y : shape.y,
|
|
1521
|
+
(corner & 4) === 0 ? -shape.z : shape.z
|
|
1522
|
+
).applyMatrix4(this.#localMatrix);
|
|
1523
|
+
this.#minimum.min(this.#corner);
|
|
1524
|
+
this.#maximum.max(this.#corner);
|
|
1525
|
+
}
|
|
1526
|
+
const offset = index * 8;
|
|
1527
|
+
target[offset] = (this.#minimum.x + this.#maximum.x) / 2;
|
|
1528
|
+
target[offset + 1] = (this.#minimum.y + this.#maximum.y) / 2;
|
|
1529
|
+
target[offset + 2] = (this.#minimum.z + this.#maximum.z) / 2;
|
|
1530
|
+
target[offset + 3] = 0;
|
|
1531
|
+
target[offset + 4] = (this.#maximum.x - this.#minimum.x) / 2;
|
|
1532
|
+
target[offset + 5] = (this.#maximum.y - this.#minimum.y) / 2;
|
|
1533
|
+
target[offset + 6] = (this.#maximum.z - this.#minimum.z) / 2;
|
|
1534
|
+
target[offset + 7] = 0;
|
|
1535
|
+
}
|
|
1536
|
+
return this.#bodies.length;
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
function softBodyCollision(...bodies) {
|
|
1540
|
+
return new PhysicsSoftBodyCollision(bodies);
|
|
1541
|
+
}
|
|
1029
1542
|
|
|
1030
1543
|
// 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
1544
|
var RigidBody3D = class {
|
|
1038
1545
|
body;
|
|
1039
1546
|
collider;
|
|
1547
|
+
/** The supplied transform; position-only fixed bodies have no runtime object. */
|
|
1040
1548
|
object;
|
|
1549
|
+
shape;
|
|
1041
1550
|
type;
|
|
1551
|
+
mass;
|
|
1552
|
+
buoyancy;
|
|
1042
1553
|
#simulation;
|
|
1043
1554
|
#physics;
|
|
1555
|
+
#object;
|
|
1044
1556
|
#lastPosition;
|
|
1557
|
+
// Scratch returned by kinematicMotion(); the plugin retains it only within one update pass.
|
|
1558
|
+
#motion = { x: 0, y: 0, z: 0 };
|
|
1559
|
+
#zeroMotion = { x: 0, y: 0, z: 0 };
|
|
1045
1560
|
#disposed = false;
|
|
1046
1561
|
constructor(options) {
|
|
1562
|
+
const initial = resolveInitialTransform(options);
|
|
1563
|
+
const type = options.type ?? "dynamic";
|
|
1564
|
+
if (options.object === void 0 && type !== "fixed")
|
|
1565
|
+
throw new Error("RigidBody3D position-only bodies must be fixed.");
|
|
1047
1566
|
this.#simulation = requirePhysicsSimulation(options.physics, options.world);
|
|
1048
1567
|
this.#physics = options.physics;
|
|
1049
1568
|
this.object = options.object;
|
|
1050
|
-
this.
|
|
1569
|
+
this.shape = options.shape;
|
|
1570
|
+
this.#object = options.object;
|
|
1571
|
+
this.type = type;
|
|
1572
|
+
this.mass = options.mass ?? 0;
|
|
1573
|
+
this.buoyancy = void 0;
|
|
1051
1574
|
const shape = options.shape.descriptor;
|
|
1052
1575
|
if (options.collisionLayer !== void 0 || options.collisionMask !== void 0) {
|
|
1053
1576
|
const layer = options.collisionLayer ?? shape.collisionLayer;
|
|
@@ -1056,9 +1579,9 @@ var RigidBody3D = class {
|
|
|
1056
1579
|
}
|
|
1057
1580
|
const registration = this.#simulation.createBody({
|
|
1058
1581
|
entity: options.entity,
|
|
1059
|
-
mass:
|
|
1060
|
-
position:
|
|
1061
|
-
rotation:
|
|
1582
|
+
mass: this.mass,
|
|
1583
|
+
position: initial.position,
|
|
1584
|
+
rotation: initial.rotation,
|
|
1062
1585
|
sensor: shape.sensor,
|
|
1063
1586
|
shape,
|
|
1064
1587
|
type: this.type
|
|
@@ -1067,36 +1590,36 @@ var RigidBody3D = class {
|
|
|
1067
1590
|
this.body = registration.body;
|
|
1068
1591
|
this.collider = registration.collider;
|
|
1069
1592
|
this.#lastPosition = {
|
|
1070
|
-
x:
|
|
1071
|
-
y:
|
|
1072
|
-
z:
|
|
1593
|
+
x: initial.position.x,
|
|
1594
|
+
y: initial.position.y,
|
|
1595
|
+
z: initial.position.z
|
|
1073
1596
|
};
|
|
1074
1597
|
this.#physics?.add(this);
|
|
1075
1598
|
}
|
|
1599
|
+
get physics() {
|
|
1600
|
+
return this.#physics;
|
|
1601
|
+
}
|
|
1076
1602
|
/** Called by the shared plugin before a bulk step. */
|
|
1077
1603
|
writeKinematic(buffer, offset) {
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
this.object.quaternion.w
|
|
1089
|
-
],
|
|
1090
|
-
offset
|
|
1091
|
-
);
|
|
1604
|
+
const object = this.#object;
|
|
1605
|
+
if (this.#disposed || object === void 0) return;
|
|
1606
|
+
buffer[offset] = this.body.id;
|
|
1607
|
+
buffer[offset + 1] = object.position.x;
|
|
1608
|
+
buffer[offset + 2] = object.position.y;
|
|
1609
|
+
buffer[offset + 3] = object.position.z;
|
|
1610
|
+
buffer[offset + 4] = object.quaternion.x;
|
|
1611
|
+
buffer[offset + 5] = object.quaternion.y;
|
|
1612
|
+
buffer[offset + 6] = object.quaternion.z;
|
|
1613
|
+
buffer[offset + 7] = object.quaternion.w;
|
|
1092
1614
|
}
|
|
1093
1615
|
/** Displacement since the last backend transform, used for moving-platform carry. */
|
|
1094
1616
|
kinematicMotion() {
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1617
|
+
const object = this.#object;
|
|
1618
|
+
if (object === void 0) return this.#zeroMotion;
|
|
1619
|
+
this.#motion.x = object.position.x - this.#lastPosition.x;
|
|
1620
|
+
this.#motion.y = object.position.y - this.#lastPosition.y;
|
|
1621
|
+
this.#motion.z = object.position.z - this.#lastPosition.z;
|
|
1622
|
+
return this.#motion;
|
|
1100
1623
|
}
|
|
1101
1624
|
syncToPhysics() {
|
|
1102
1625
|
}
|
|
@@ -1116,6 +1639,11 @@ var RigidBody3D = class {
|
|
|
1116
1639
|
this.#requireLive("applyForce");
|
|
1117
1640
|
this.#simulation.applyBodyForce(this.body.id, force);
|
|
1118
1641
|
}
|
|
1642
|
+
/** Godot's force-at-position equivalent. Continuous push in world space; cleared each step. */
|
|
1643
|
+
applyForceAtPoint(force, point) {
|
|
1644
|
+
this.#requireLive("applyForceAtPoint");
|
|
1645
|
+
this.#simulation.applyBodyForceAtPoint(this.body.id, force, point);
|
|
1646
|
+
}
|
|
1119
1647
|
/** Godot's `linear_velocity`. */
|
|
1120
1648
|
get linearVelocity() {
|
|
1121
1649
|
this.#requireLive("linearVelocity");
|
|
@@ -1129,26 +1657,46 @@ var RigidBody3D = class {
|
|
|
1129
1657
|
if (this.#disposed) throw new Error(`RigidBody3D.${operation} cannot be used after dispose.`);
|
|
1130
1658
|
}
|
|
1131
1659
|
syncFromPhysics() {
|
|
1132
|
-
const
|
|
1660
|
+
const object = this.#object;
|
|
1661
|
+
if (object === void 0) return;
|
|
1662
|
+
if (this.#simulation.readBodyTransform === void 0)
|
|
1663
|
+
throw new Error(
|
|
1664
|
+
"TN_PHYSICS_READ_TRANSFORM_MISSING: the active physics backend cannot read body transforms."
|
|
1665
|
+
);
|
|
1666
|
+
const transform = this.#simulation.readBodyTransform(this.body.id);
|
|
1133
1667
|
if (transform === void 0) return;
|
|
1134
|
-
|
|
1135
|
-
|
|
1668
|
+
object.position.set(transform.position.x, transform.position.y, transform.position.z);
|
|
1669
|
+
object.quaternion.set(
|
|
1136
1670
|
transform.rotation.x,
|
|
1137
1671
|
transform.rotation.y,
|
|
1138
1672
|
transform.rotation.z,
|
|
1139
1673
|
transform.rotation.w
|
|
1140
1674
|
);
|
|
1141
|
-
this.#lastPosition =
|
|
1675
|
+
this.#lastPosition.x = transform.position.x;
|
|
1676
|
+
this.#lastPosition.y = transform.position.y;
|
|
1677
|
+
this.#lastPosition.z = transform.position.z;
|
|
1142
1678
|
}
|
|
1143
1679
|
applyTransform(values, offset) {
|
|
1144
|
-
const
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1680
|
+
const x = bulkTransformValue(values, offset + 1);
|
|
1681
|
+
const y = bulkTransformValue(values, offset + 2);
|
|
1682
|
+
const z = bulkTransformValue(values, offset + 3);
|
|
1683
|
+
const qx = bulkTransformValue(values, offset + 4);
|
|
1684
|
+
const qy = bulkTransformValue(values, offset + 5);
|
|
1685
|
+
const qz = bulkTransformValue(values, offset + 6);
|
|
1686
|
+
const qw = bulkTransformValue(values, offset + 7);
|
|
1687
|
+
const object = this.#object;
|
|
1688
|
+
if (object === void 0) return;
|
|
1689
|
+
object.position.set(x, y, z);
|
|
1690
|
+
object.quaternion.set(qx, qy, qz, qw);
|
|
1691
|
+
this.#lastPosition.x = x;
|
|
1692
|
+
this.#lastPosition.y = y;
|
|
1693
|
+
this.#lastPosition.z = z;
|
|
1148
1694
|
}
|
|
1149
1695
|
dispose() {
|
|
1150
1696
|
if (this.#disposed) return;
|
|
1151
1697
|
this.#disposed = true;
|
|
1698
|
+
this.buoyancy?.dispose();
|
|
1699
|
+
this.buoyancy = void 0;
|
|
1152
1700
|
this.#physics?.remove(this);
|
|
1153
1701
|
this.#simulation.removeBody(this.body.id);
|
|
1154
1702
|
}
|
|
@@ -1157,6 +1705,7 @@ var RigidBody3D = class {
|
|
|
1157
1705
|
// src/plugin.ts
|
|
1158
1706
|
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1159
1707
|
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
1708
|
+
var PHYSICS_BUFFER_RESIZE_ATTEMPTS = 12;
|
|
1160
1709
|
function growFloat(buffer, records) {
|
|
1161
1710
|
if (buffer.length >= records * PHYSICS_TRANSFORM_STRIDE) return buffer;
|
|
1162
1711
|
return new Float32Array(
|
|
@@ -1185,6 +1734,8 @@ function rapier(options = {}) {
|
|
|
1185
1734
|
const bodies = /* @__PURE__ */ new Set();
|
|
1186
1735
|
const bodiesById = /* @__PURE__ */ new Map();
|
|
1187
1736
|
const areas = /* @__PURE__ */ new Map();
|
|
1737
|
+
const buoyancies = /* @__PURE__ */ new Set();
|
|
1738
|
+
const areaMembershipBuffers = /* @__PURE__ */ new Map();
|
|
1188
1739
|
const kinematicMotions = /* @__PURE__ */ new Map();
|
|
1189
1740
|
let kinematic = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
1190
1741
|
let visible = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
@@ -1202,7 +1753,14 @@ function rapier(options = {}) {
|
|
|
1202
1753
|
bodies.add(body);
|
|
1203
1754
|
bodiesById.set(body.body.id, body);
|
|
1204
1755
|
},
|
|
1205
|
-
addArea: (area) =>
|
|
1756
|
+
addArea: (area) => {
|
|
1757
|
+
areas.set(area.body.id, area);
|
|
1758
|
+
areaMembershipBuffers.set(area.body.id, {
|
|
1759
|
+
current: /* @__PURE__ */ new Map(),
|
|
1760
|
+
previous: /* @__PURE__ */ new Map()
|
|
1761
|
+
});
|
|
1762
|
+
},
|
|
1763
|
+
addBuoyancy: (buoyancy) => buoyancies.add(buoyancy),
|
|
1206
1764
|
eventQueue: physicsHandle(selected.rawEventQueue),
|
|
1207
1765
|
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
1208
1766
|
numBodies: () => bodies.size,
|
|
@@ -1213,8 +1771,10 @@ function rapier(options = {}) {
|
|
|
1213
1771
|
},
|
|
1214
1772
|
removeArea: (area) => {
|
|
1215
1773
|
areas.delete(area.body.id);
|
|
1774
|
+
areaMembershipBuffers.delete(area.body.id);
|
|
1216
1775
|
removeContactsFor(area.body.id);
|
|
1217
1776
|
},
|
|
1777
|
+
removeBuoyancy: (buoyancy) => buoyancies.delete(buoyancy),
|
|
1218
1778
|
directSpaceState: new PhysicsDirectSpaceState3D(selected),
|
|
1219
1779
|
simulation: selected,
|
|
1220
1780
|
world: physicsWorldHandle(selected.rawWorld, selected)
|
|
@@ -1260,20 +1820,30 @@ function rapier(options = {}) {
|
|
|
1260
1820
|
kinematicCount: count,
|
|
1261
1821
|
kinematicTransforms: kinematic
|
|
1262
1822
|
};
|
|
1823
|
+
for (const buoyancy of buoyancies) buoyancy.apply(dt);
|
|
1263
1824
|
simulation.step(dt, input);
|
|
1264
1825
|
visible = growFloat(visible, bodies.size + areas.size);
|
|
1265
|
-
|
|
1826
|
+
let visibleResizeAttempts = 0;
|
|
1827
|
+
for (; ; ) {
|
|
1828
|
+
try {
|
|
1829
|
+
visibleCount = simulation.readVisibleTransforms(visible);
|
|
1830
|
+
break;
|
|
1831
|
+
} catch (error) {
|
|
1832
|
+
if (!isSmallBufferError2(error) || visibleResizeAttempts >= PHYSICS_BUFFER_RESIZE_ATTEMPTS)
|
|
1833
|
+
throw error;
|
|
1834
|
+
visibleResizeAttempts += 1;
|
|
1835
|
+
visible = growFloat(visible, Math.ceil(visible.length / PHYSICS_TRANSFORM_STRIDE) * 2);
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1266
1838
|
for (let index = 0; index < visibleCount; index += 1) {
|
|
1267
1839
|
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
1268
1840
|
const id = visibleId(visible, offset);
|
|
1269
1841
|
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);
|
|
1842
|
+
if (body !== void 0) {
|
|
1843
|
+
body.applyTransform(visible, offset);
|
|
1844
|
+
continue;
|
|
1276
1845
|
}
|
|
1846
|
+
areas.get(id)?.applyTransform(visible, offset);
|
|
1277
1847
|
}
|
|
1278
1848
|
events = growEvents(events, bodies.size + areas.size);
|
|
1279
1849
|
const maximumEventValues = Math.max(
|
|
@@ -1311,12 +1881,18 @@ function rapier(options = {}) {
|
|
|
1311
1881
|
}
|
|
1312
1882
|
if (simulation.areaIntersections !== void 0) {
|
|
1313
1883
|
for (const area of areas.values()) {
|
|
1314
|
-
const
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1884
|
+
const buffers = areaMembershipBuffers.get(area.body.id);
|
|
1885
|
+
if (buffers === void 0)
|
|
1886
|
+
throw new Error("Physics area membership buffers lost a registered area.");
|
|
1887
|
+
const current = buffers.current;
|
|
1888
|
+
current.clear();
|
|
1889
|
+
for (const bodyId2 of simulation.areaIntersections(area.body.id)) {
|
|
1890
|
+
const body = bodiesById.get(bodyId2);
|
|
1891
|
+
if (body !== void 0) current.set(bodyId2, body);
|
|
1318
1892
|
}
|
|
1319
1893
|
area.reconcileIntersections(current);
|
|
1894
|
+
buffers.current = buffers.previous;
|
|
1895
|
+
buffers.previous = current;
|
|
1320
1896
|
}
|
|
1321
1897
|
}
|
|
1322
1898
|
kinematicMotions.clear();
|
|
@@ -1328,8 +1904,11 @@ function rapier(options = {}) {
|
|
|
1328
1904
|
// before any input. A scene therefore gets a pristine simulation, which makes restart
|
|
1329
1905
|
// deterministic without a game having to discover an API for it.
|
|
1330
1906
|
sceneExit: (ctx) => {
|
|
1907
|
+
for (const buoyancy of [...buoyancies]) buoyancy.dispose();
|
|
1331
1908
|
for (const area of [...areas.values()]) area.dispose();
|
|
1332
1909
|
for (const body of [...bodies]) body.dispose();
|
|
1910
|
+
areaMembershipBuffers.clear();
|
|
1911
|
+
buoyancies.clear();
|
|
1333
1912
|
kinematicMotions.clear();
|
|
1334
1913
|
activeContacts.clear();
|
|
1335
1914
|
debugSeries = [];
|
|
@@ -1346,8 +1925,11 @@ function rapier(options = {}) {
|
|
|
1346
1925
|
dispose: () => {
|
|
1347
1926
|
unregisterObservations?.();
|
|
1348
1927
|
unregisterObservations = void 0;
|
|
1928
|
+
for (const buoyancy of [...buoyancies]) buoyancy.dispose();
|
|
1349
1929
|
for (const area of [...areas.values()]) area.dispose();
|
|
1350
1930
|
for (const body of [...bodies]) body.dispose();
|
|
1931
|
+
areaMembershipBuffers.clear();
|
|
1932
|
+
buoyancies.clear();
|
|
1351
1933
|
simulation?.dispose();
|
|
1352
1934
|
simulation = void 0;
|
|
1353
1935
|
context = void 0;
|
|
@@ -1445,9 +2027,9 @@ function rapier(options = {}) {
|
|
|
1445
2027
|
}
|
|
1446
2028
|
};
|
|
1447
2029
|
}
|
|
1448
|
-
function removeContactsFor(
|
|
2030
|
+
function removeContactsFor(bodyId2) {
|
|
1449
2031
|
for (const [key, pair] of activeContacts) {
|
|
1450
|
-
if (pair.includes(
|
|
2032
|
+
if (pair.includes(bodyId2)) activeContacts.delete(key);
|
|
1451
2033
|
}
|
|
1452
2034
|
}
|
|
1453
2035
|
}
|
|
@@ -1457,14 +2039,18 @@ function growSleepStates(buffer, bodyCount) {
|
|
|
1457
2039
|
return new Float32Array(Math.max(required, buffer.length * 2));
|
|
1458
2040
|
}
|
|
1459
2041
|
function physicsEntityId(ctx, body) {
|
|
2042
|
+
const object = body.object;
|
|
2043
|
+
const stableId = body.body.entity ?? `physics.body.${body.body.id}`;
|
|
1460
2044
|
for (const id of Object.keys(ctx.entities.snapshot())) {
|
|
1461
2045
|
const entity = ctx.entities.get(id);
|
|
1462
|
-
if (entity === body || entity ===
|
|
1463
|
-
if (entity !== void 0 && Object.values(entity).some(
|
|
2046
|
+
if (entity === body || object !== void 0 && entity === object) return id;
|
|
2047
|
+
if (entity !== void 0 && Object.values(entity).some(
|
|
2048
|
+
(value) => value === body || object !== void 0 && value === object
|
|
2049
|
+
)) {
|
|
1464
2050
|
return id;
|
|
1465
2051
|
}
|
|
1466
2052
|
}
|
|
1467
|
-
return
|
|
2053
|
+
return stableId;
|
|
1468
2054
|
}
|
|
1469
2055
|
|
|
1470
|
-
export { Area3D, CharacterBody3D, CollisionShape3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier };
|
|
2056
|
+
export { Area3D, Buoyancy3D, CharacterBody3D, CollisionShape3D, Joint3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier, softBodyCollision };
|