@threenative/physics 0.1.0 → 0.2.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/dist/Area3D-CvwDbfU6.d.ts +577 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +614 -72
- package/dist/native/index.d.ts +1 -1
- package/dist/native/index.js +533 -49
- package/dist/navigation/index.d.ts +1 -1
- package/package.json +3 -3
- package/dist/Area3D-B8LJ2C4S.d.ts +0 -403
package/dist/native/index.js
CHANGED
|
@@ -5,8 +5,8 @@ function physicsHandle(raw) {
|
|
|
5
5
|
function physicsWorldHandle(raw, simulation) {
|
|
6
6
|
return { raw, simulation };
|
|
7
7
|
}
|
|
8
|
-
function physicsBodyHandle(id, raw) {
|
|
9
|
-
return { id, raw };
|
|
8
|
+
function physicsBodyHandle(id, raw, entity) {
|
|
9
|
+
return { entity, id, raw };
|
|
10
10
|
}
|
|
11
11
|
function physicsColliderHandle(id, raw) {
|
|
12
12
|
return { id, raw };
|
|
@@ -24,45 +24,159 @@ function interactionGroups(layer, mask) {
|
|
|
24
24
|
// src/simulation.ts
|
|
25
25
|
var PHYSICS_TRANSFORM_STRIDE = 8;
|
|
26
26
|
var PHYSICS_COLLISION_EVENT_STRIDE = 4;
|
|
27
|
+
var PHYSICS_SLEEP_STATE_STRIDE = 2;
|
|
28
|
+
var MAX_PHYSICS_QUERY_RESULTS = 1024;
|
|
29
|
+
function requireFiniteVector(vector, label) {
|
|
30
|
+
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
|
+
throw new Error(`TN_PHYSICS_NON_FINITE: ${label} must be a finite { x, y, z }.`);
|
|
32
|
+
}
|
|
27
33
|
var selectedBackend;
|
|
28
34
|
function installPhysicsSimulationBackend(backend) {
|
|
29
35
|
selectedBackend = backend;
|
|
30
36
|
}
|
|
31
37
|
function physicsSimulationBackend() {
|
|
32
38
|
if (selectedBackend === void 0)
|
|
33
|
-
throw new Error("TN_PHYSICS_BACKEND_MISSING: no
|
|
39
|
+
throw new Error("TN_PHYSICS_BACKEND_MISSING: no IPhysicsSimulation backend was selected");
|
|
34
40
|
return selectedBackend;
|
|
35
41
|
}
|
|
36
42
|
function requirePhysicsStepInput(deltaTime, inputSnapshot, bodyExists) {
|
|
37
43
|
if (!Number.isFinite(deltaTime) || deltaTime <= 0)
|
|
38
|
-
throw new Error("
|
|
44
|
+
throw new Error("IPhysicsSimulation.step requires a positive finite deltaTime.");
|
|
39
45
|
if (inputSnapshot === void 0) return;
|
|
40
46
|
if (!(inputSnapshot.kinematicTransforms instanceof Float32Array))
|
|
41
|
-
throw new Error("
|
|
47
|
+
throw new Error("IPhysicsSimulation input must use a Float32Array.");
|
|
42
48
|
if (!Number.isSafeInteger(inputSnapshot.kinematicCount) || inputSnapshot.kinematicCount < 0 || inputSnapshot.kinematicCount > Math.floor(inputSnapshot.kinematicTransforms.length / PHYSICS_TRANSFORM_STRIDE)) {
|
|
43
|
-
throw new Error("
|
|
49
|
+
throw new Error("IPhysicsSimulation input has an invalid kinematic record count.");
|
|
44
50
|
}
|
|
45
51
|
for (let index = 0; index < inputSnapshot.kinematicCount; index += 1) {
|
|
46
52
|
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
47
53
|
for (let scalar = 0; scalar < PHYSICS_TRANSFORM_STRIDE; scalar += 1) {
|
|
48
54
|
if (!Number.isFinite(inputSnapshot.kinematicTransforms[offset + scalar]))
|
|
49
|
-
throw new Error("
|
|
55
|
+
throw new Error("IPhysicsSimulation input contains a non-finite transform.");
|
|
50
56
|
}
|
|
51
57
|
const id = inputSnapshot.kinematicTransforms[offset];
|
|
52
58
|
if (!Number.isInteger(id) || id < 0)
|
|
53
|
-
throw new Error("
|
|
54
|
-
if (!bodyExists(id)) throw new Error("
|
|
59
|
+
throw new Error("IPhysicsSimulation input contains an invalid body id.");
|
|
60
|
+
if (!bodyExists(id)) throw new Error("IPhysicsSimulation input contains an unknown body id.");
|
|
55
61
|
}
|
|
56
62
|
}
|
|
57
63
|
function requirePhysicsRenderBuffer(renderBuffer, bodyCount) {
|
|
58
64
|
if (!(renderBuffer instanceof Float32Array))
|
|
59
|
-
throw new Error("
|
|
65
|
+
throw new Error("IPhysicsSimulation output must use a Float32Array.");
|
|
60
66
|
if (renderBuffer.length < bodyCount * PHYSICS_TRANSFORM_STRIDE)
|
|
61
|
-
throw new Error("
|
|
67
|
+
throw new Error("IPhysicsSimulation output buffer is too small for visible transforms.");
|
|
68
|
+
}
|
|
69
|
+
function requirePhysicsSleepStateBuffer(buffer, bodyCount) {
|
|
70
|
+
if (!(buffer instanceof Float32Array))
|
|
71
|
+
throw new Error("IPhysicsSimulation sleep states must use a Float32Array.");
|
|
72
|
+
if (buffer.length < bodyCount * PHYSICS_SLEEP_STATE_STRIDE)
|
|
73
|
+
throw new Error("IPhysicsSimulation sleep state buffer is too small.");
|
|
62
74
|
}
|
|
63
75
|
function requirePhysicsEventBuffer(buffer) {
|
|
64
76
|
if (!(buffer instanceof Uint32Array))
|
|
65
|
-
throw new Error("
|
|
77
|
+
throw new Error("IPhysicsSimulation events must use a Uint32Array.");
|
|
78
|
+
}
|
|
79
|
+
function requirePhysicsBodySensor(options) {
|
|
80
|
+
if (options.sensor !== options.shape.sensor)
|
|
81
|
+
throw new Error(
|
|
82
|
+
`TN_PHYSICS_SENSOR_CONFLICT: options.sensor (${options.sensor}) must match shape.sensor (${options.shape.sensor}).`
|
|
83
|
+
);
|
|
84
|
+
return options.sensor;
|
|
85
|
+
}
|
|
86
|
+
var QUERY_SHAPE_KINDS = /* @__PURE__ */ new Set([
|
|
87
|
+
"box",
|
|
88
|
+
"sphere",
|
|
89
|
+
"capsule",
|
|
90
|
+
"trimesh",
|
|
91
|
+
"convexHull",
|
|
92
|
+
"heightfield"
|
|
93
|
+
]);
|
|
94
|
+
function queryObject(value, label) {
|
|
95
|
+
if (typeof value !== "object" || value === null)
|
|
96
|
+
throw new Error(`IPhysicsSimulation ${label} must be an object.`);
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
function queryVector(value, label) {
|
|
100
|
+
const object = queryObject(value, `${label} vector`);
|
|
101
|
+
const x = object.x;
|
|
102
|
+
const y = object.y;
|
|
103
|
+
const z = object.z;
|
|
104
|
+
if (typeof x !== "number" || typeof y !== "number" || typeof z !== "number" || !Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z))
|
|
105
|
+
throw new Error(`IPhysicsSimulation ${label} vector must contain finite x, y, and z values.`);
|
|
106
|
+
return { x, y, z };
|
|
107
|
+
}
|
|
108
|
+
function queryRotation(value) {
|
|
109
|
+
const object = queryObject(value, "shape rotation");
|
|
110
|
+
const x = object.x;
|
|
111
|
+
const y = object.y;
|
|
112
|
+
const z = object.z;
|
|
113
|
+
const w = object.w;
|
|
114
|
+
if (typeof x !== "number" || typeof y !== "number" || typeof z !== "number" || typeof w !== "number" || !Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z) || !Number.isFinite(w))
|
|
115
|
+
throw new Error("IPhysicsSimulation shape rotation must contain finite x, y, z, and w values.");
|
|
116
|
+
const lengthSquared = x * x + y * y + z * z + w * w;
|
|
117
|
+
if (lengthSquared <= Number.EPSILON)
|
|
118
|
+
throw new Error("IPhysicsSimulation shape rotation must not have zero length.");
|
|
119
|
+
return { w, x, y, z };
|
|
120
|
+
}
|
|
121
|
+
function requirePhysicsCollisionMask(value) {
|
|
122
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0 || value > 65535)
|
|
123
|
+
throw new Error("IPhysicsSimulation collisionMask must be an integer from 0 through 65535.");
|
|
124
|
+
return value;
|
|
125
|
+
}
|
|
126
|
+
function requirePhysicsMaxResults(value) {
|
|
127
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 1 || value > MAX_PHYSICS_QUERY_RESULTS)
|
|
128
|
+
throw new Error(
|
|
129
|
+
`IPhysicsSimulation maxResults must be an integer from 1 through ${MAX_PHYSICS_QUERY_RESULTS}.`
|
|
130
|
+
);
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
function queryShape(value) {
|
|
134
|
+
const shape = queryObject(value, "query shape");
|
|
135
|
+
const kind = shape.kind;
|
|
136
|
+
if (typeof kind !== "string" || !QUERY_SHAPE_KINDS.has(kind))
|
|
137
|
+
throw new Error("IPhysicsSimulation query shape has an unknown kind.");
|
|
138
|
+
const x = shape.x;
|
|
139
|
+
const y = shape.y;
|
|
140
|
+
const z = shape.z;
|
|
141
|
+
if (typeof x !== "number" || typeof y !== "number" || typeof z !== "number" || !Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(z))
|
|
142
|
+
throw new Error("IPhysicsSimulation query shape dimensions must be finite.");
|
|
143
|
+
if (kind === "box" && (x <= 0 || y <= 0 || z <= 0))
|
|
144
|
+
throw new Error("IPhysicsSimulation query box dimensions must be positive.");
|
|
145
|
+
if (kind === "sphere" && x <= 0)
|
|
146
|
+
throw new Error("IPhysicsSimulation query sphere radius must be positive.");
|
|
147
|
+
if (kind === "capsule" && (x < 0 || y <= 0))
|
|
148
|
+
throw new Error("IPhysicsSimulation query capsule dimensions are invalid.");
|
|
149
|
+
return shape;
|
|
150
|
+
}
|
|
151
|
+
function requirePhysicsRayQuery(value) {
|
|
152
|
+
const query = queryObject(value, "ray query");
|
|
153
|
+
const from = queryVector(query.from, "ray from");
|
|
154
|
+
const to = queryVector(query.to, "ray to");
|
|
155
|
+
if (from.x === to.x && from.y === to.y && from.z === to.z)
|
|
156
|
+
throw new Error("IPhysicsSimulation ray must have non-zero length.");
|
|
157
|
+
return {
|
|
158
|
+
collisionMask: requirePhysicsCollisionMask(query.collisionMask),
|
|
159
|
+
from,
|
|
160
|
+
to
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function requirePhysicsShapeQuery(value) {
|
|
164
|
+
const query = queryObject(value, "shape query");
|
|
165
|
+
return {
|
|
166
|
+
collisionMask: requirePhysicsCollisionMask(query.collisionMask),
|
|
167
|
+
maxResults: requirePhysicsMaxResults(query.maxResults),
|
|
168
|
+
position: queryVector(query.position, "shape position"),
|
|
169
|
+
rotation: queryRotation(query.rotation),
|
|
170
|
+
shape: queryShape(query.shape)
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function requirePhysicsPointQuery(value) {
|
|
174
|
+
const query = queryObject(value, "point query");
|
|
175
|
+
return {
|
|
176
|
+
collisionMask: requirePhysicsCollisionMask(query.collisionMask),
|
|
177
|
+
maxResults: requirePhysicsMaxResults(query.maxResults),
|
|
178
|
+
position: queryVector(query.position, "point position")
|
|
179
|
+
};
|
|
66
180
|
}
|
|
67
181
|
function requirePhysicsSimulation(physics, world) {
|
|
68
182
|
if (physics?.simulation !== void 0) return physics.simulation;
|
|
@@ -73,7 +187,7 @@ function requirePhysicsSimulation(physics, world) {
|
|
|
73
187
|
if (world !== void 0 && backend?.simulationForWorld !== void 0)
|
|
74
188
|
return backend.simulationForWorld(candidate);
|
|
75
189
|
throw new Error(
|
|
76
|
-
"Physics nodes require
|
|
190
|
+
"Physics nodes require an IPhysicsContext. Passing a raw backend world is deprecated and backend-specific."
|
|
77
191
|
);
|
|
78
192
|
}
|
|
79
193
|
|
|
@@ -86,7 +200,7 @@ function nativePhysicsHost() {
|
|
|
86
200
|
return host;
|
|
87
201
|
}
|
|
88
202
|
function nativeSimulation(value) {
|
|
89
|
-
if (typeof value !== "object" || value === null || !("configureCharacter" in value) || typeof value.configureCharacter !== "function" || !("createBody" in value) || typeof value.createBody !== "function" || !("dispose" in value) || typeof value.dispose !== "function" || !("drainCollisionEvents" in value) || typeof value.drainCollisionEvents !== "function" || !("removeBody" in value) || typeof value.removeBody !== "function" || !("readVisibleTransforms" in value) || typeof value.readVisibleTransforms !== "function" || !("readCharacterStates" in value) || typeof value.readCharacterStates !== "function" || !("readAreaIntersections" in value) || typeof value.readAreaIntersections !== "function" || !("step" in value) || typeof value.step !== "function") {
|
|
203
|
+
if (typeof value !== "object" || value === null || !("configureCharacter" in value) || typeof value.configureCharacter !== "function" || !("createBody" in value) || typeof value.createBody !== "function" || !("dispose" in value) || typeof value.dispose !== "function" || !("drainCollisionEvents" in value) || typeof value.drainCollisionEvents !== "function" || !("removeBody" in value) || typeof value.removeBody !== "function" || !("readVisibleTransforms" in value) || typeof value.readVisibleTransforms !== "function" || !("readBodySleepStates" in value) || typeof value.readBodySleepStates !== "function" || !("readCharacterStates" in value) || typeof value.readCharacterStates !== "function" || !("readAreaIntersections" in value) || typeof value.readAreaIntersections !== "function" || !("intersectRay" in value) || typeof value.intersectRay !== "function" || !("intersectShape" in value) || typeof value.intersectShape !== "function" || !("intersectPoint" in value) || typeof value.intersectPoint !== "function" || !("step" in value) || typeof value.step !== "function") {
|
|
90
204
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: physics world is not a native simulation");
|
|
91
205
|
}
|
|
92
206
|
return value;
|
|
@@ -96,6 +210,9 @@ function primitiveShape(shape) {
|
|
|
96
210
|
throw new Error(`TN_NATIVE_PHYSICS_SHAPE_UNSUPPORTED: ${shape.kind} remains OPEN on native`);
|
|
97
211
|
return shape;
|
|
98
212
|
}
|
|
213
|
+
function opaqueNativeShape(shape) {
|
|
214
|
+
return Object.freeze({ backend: "native", kind: shape.kind });
|
|
215
|
+
}
|
|
99
216
|
function isSmallBufferError(error) {
|
|
100
217
|
return error instanceof Error && /buffer is too small/i.test(error.message);
|
|
101
218
|
}
|
|
@@ -106,18 +223,45 @@ function growUint32(buffer, minimum) {
|
|
|
106
223
|
}
|
|
107
224
|
function createNativePhysicsSimulation(raw, version) {
|
|
108
225
|
const bodyIds = /* @__PURE__ */ new Set();
|
|
226
|
+
const bodyHandles = /* @__PURE__ */ new Map();
|
|
109
227
|
const areaIds = /* @__PURE__ */ new Set();
|
|
110
228
|
const characterIds = /* @__PURE__ */ new Set();
|
|
111
229
|
const characterState = /* @__PURE__ */ new Map();
|
|
112
230
|
let characterStates = new Float32Array(48);
|
|
231
|
+
const rayOutput = new Float32Array(8);
|
|
113
232
|
let areaPairs = new Uint32Array(32);
|
|
114
233
|
const areaIntersections = /* @__PURE__ */ new Map();
|
|
115
234
|
let characterStateDirty = true;
|
|
116
235
|
let areaIntersectionsDirty = true;
|
|
236
|
+
let disposed = false;
|
|
237
|
+
const requireLive = () => {
|
|
238
|
+
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
239
|
+
};
|
|
117
240
|
const invalidateObservations = () => {
|
|
118
241
|
characterStateDirty = true;
|
|
119
242
|
areaIntersectionsDirty = true;
|
|
120
243
|
};
|
|
244
|
+
const bodyHandle = (id) => {
|
|
245
|
+
const handle = bodyHandles.get(id);
|
|
246
|
+
if (handle === void 0)
|
|
247
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: query returned an unknown body");
|
|
248
|
+
return handle;
|
|
249
|
+
};
|
|
250
|
+
const finiteQueryVector = (value, label) => {
|
|
251
|
+
if (!Number.isFinite(value.x) || !Number.isFinite(value.y) || !Number.isFinite(value.z))
|
|
252
|
+
throw new Error(`TN_NATIVE_PHYSICS_INVALID: ${label} contains a non-finite vector`);
|
|
253
|
+
return { x: value.x, y: value.y, z: value.z };
|
|
254
|
+
};
|
|
255
|
+
const nativeHit = (hit) => {
|
|
256
|
+
if (!Number.isSafeInteger(hit.bodyId) || hit.bodyId < 0)
|
|
257
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: query returned an invalid body id");
|
|
258
|
+
const body = bodyHandle(hit.bodyId);
|
|
259
|
+
return {
|
|
260
|
+
body,
|
|
261
|
+
entity: body.entity,
|
|
262
|
+
position: finiteQueryVector(hit.position, "query hit position")
|
|
263
|
+
};
|
|
264
|
+
};
|
|
121
265
|
const refreshCharacterState = () => {
|
|
122
266
|
if (!characterStateDirty) return;
|
|
123
267
|
const required = Math.max(1, characterIds.size) * 3;
|
|
@@ -170,28 +314,31 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
170
314
|
rawWorld: raw,
|
|
171
315
|
createBody: (options) => {
|
|
172
316
|
const shape = primitiveShape(options.shape);
|
|
317
|
+
const sensor = requirePhysicsBodySensor(options);
|
|
173
318
|
const id = raw.createBody({
|
|
174
319
|
collisionLayer: shape.collisionLayer,
|
|
175
320
|
collisionMask: shape.collisionMask,
|
|
176
321
|
mass: options.mass,
|
|
177
322
|
position: options.position,
|
|
178
323
|
rotation: options.rotation,
|
|
179
|
-
sensor
|
|
324
|
+
sensor,
|
|
180
325
|
shape,
|
|
181
326
|
type: options.type
|
|
182
327
|
});
|
|
183
328
|
if (!Number.isInteger(id) || id < 0)
|
|
184
329
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: runtime returned an invalid body id");
|
|
185
330
|
const rawHandle = { backend: "native", id };
|
|
331
|
+
const bodyHandleValue = physicsBodyHandle(id, rawHandle, options.entity);
|
|
186
332
|
bodyIds.add(id);
|
|
187
|
-
|
|
333
|
+
bodyHandles.set(id, bodyHandleValue);
|
|
334
|
+
if (sensor) areaIds.add(id);
|
|
188
335
|
if (options.type === "character") characterIds.add(id);
|
|
189
336
|
invalidateObservations();
|
|
190
337
|
return {
|
|
191
|
-
body:
|
|
338
|
+
body: bodyHandleValue,
|
|
192
339
|
collider: physicsColliderHandle(id, rawHandle),
|
|
193
340
|
controller: options.type === "character" ? physicsHandle(rawHandle) : void 0,
|
|
194
|
-
rawShape: shape
|
|
341
|
+
rawShape: opaqueNativeShape(shape)
|
|
195
342
|
};
|
|
196
343
|
},
|
|
197
344
|
configureCharacter: (id, options) => {
|
|
@@ -201,6 +348,7 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
201
348
|
removeBody: (id) => {
|
|
202
349
|
raw.removeBody(id);
|
|
203
350
|
bodyIds.delete(id);
|
|
351
|
+
bodyHandles.delete(id);
|
|
204
352
|
areaIds.delete(id);
|
|
205
353
|
characterIds.delete(id);
|
|
206
354
|
characterState.delete(id);
|
|
@@ -211,6 +359,37 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
211
359
|
throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
|
|
212
360
|
raw.setBodyTransform(id, position);
|
|
213
361
|
},
|
|
362
|
+
// Keep the optional raw members: an older runtime must throw rather than accept a call and
|
|
363
|
+
// drop the actuation, which would make the same game diverge by platform.
|
|
364
|
+
applyBodyImpulse: (id, impulse) => {
|
|
365
|
+
requireLive();
|
|
366
|
+
requireFiniteVector(impulse, "impulse");
|
|
367
|
+
if (raw.applyBodyImpulse === void 0)
|
|
368
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
369
|
+
raw.applyBodyImpulse(id, impulse);
|
|
370
|
+
},
|
|
371
|
+
applyBodyForce: (id, force) => {
|
|
372
|
+
requireLive();
|
|
373
|
+
requireFiniteVector(force, "force");
|
|
374
|
+
if (raw.applyBodyForce === void 0)
|
|
375
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
376
|
+
raw.applyBodyForce(id, force);
|
|
377
|
+
},
|
|
378
|
+
setBodyLinearVelocity: (id, velocity) => {
|
|
379
|
+
requireLive();
|
|
380
|
+
requireFiniteVector(velocity, "linearVelocity");
|
|
381
|
+
if (raw.setBodyLinearVelocity === void 0)
|
|
382
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
383
|
+
raw.setBodyLinearVelocity(id, velocity);
|
|
384
|
+
},
|
|
385
|
+
readBodyLinearVelocity: (id) => {
|
|
386
|
+
requireLive();
|
|
387
|
+
if (raw.readBodyLinearVelocity === void 0)
|
|
388
|
+
throw new Error("TN_NATIVE_PHYSICS_ACTUATION_MISSING: runtime ABI is too old");
|
|
389
|
+
const velocity = raw.readBodyLinearVelocity(id);
|
|
390
|
+
requireFiniteVector(velocity, "native linear velocity");
|
|
391
|
+
return velocity;
|
|
392
|
+
},
|
|
214
393
|
step: (deltaTime, inputSnapshot) => {
|
|
215
394
|
requirePhysicsStepInput(deltaTime, inputSnapshot, (id) => bodyIds.has(id));
|
|
216
395
|
raw.step(deltaTime, inputSnapshot);
|
|
@@ -220,6 +399,67 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
220
399
|
requirePhysicsRenderBuffer(buffer, bodyIds.size);
|
|
221
400
|
return raw.readVisibleTransforms(buffer);
|
|
222
401
|
},
|
|
402
|
+
readBodySleepStates: (buffer) => {
|
|
403
|
+
if (disposed) throw new Error("Physics simulation is disposed.");
|
|
404
|
+
requirePhysicsSleepStateBuffer(buffer, bodyIds.size);
|
|
405
|
+
return raw.readBodySleepStates(buffer);
|
|
406
|
+
},
|
|
407
|
+
intersectRay: (value) => {
|
|
408
|
+
const query = requirePhysicsRayQuery(value);
|
|
409
|
+
const hit = raw.intersectRay(query, rayOutput);
|
|
410
|
+
if (typeof hit === "number") {
|
|
411
|
+
if (hit === 0) return void 0;
|
|
412
|
+
if (hit !== 1)
|
|
413
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
|
|
414
|
+
const bodyId = rayOutput[0];
|
|
415
|
+
const position = {
|
|
416
|
+
x: rayOutput[1],
|
|
417
|
+
y: rayOutput[2],
|
|
418
|
+
z: rayOutput[3]
|
|
419
|
+
};
|
|
420
|
+
const normal = {
|
|
421
|
+
x: rayOutput[4],
|
|
422
|
+
y: rayOutput[5],
|
|
423
|
+
z: rayOutput[6]
|
|
424
|
+
};
|
|
425
|
+
const distance = rayOutput[7];
|
|
426
|
+
if (bodyId === void 0 || !Number.isSafeInteger(bodyId) || bodyId < 0 || distance === void 0)
|
|
427
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
|
|
428
|
+
const base2 = nativeHit({ bodyId, position });
|
|
429
|
+
if (!Number.isFinite(distance) || distance < 0)
|
|
430
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
|
|
431
|
+
return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
|
|
432
|
+
}
|
|
433
|
+
if (hit === void 0 || hit === null) return void 0;
|
|
434
|
+
if (!Number.isFinite(hit.distance) || hit.distance < 0)
|
|
435
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid distance");
|
|
436
|
+
const base = nativeHit({ bodyId: hit.bodyId, position: hit.position });
|
|
437
|
+
return {
|
|
438
|
+
...base,
|
|
439
|
+
distance: hit.distance,
|
|
440
|
+
normal: finiteQueryVector(hit.normal, "ray hit normal")
|
|
441
|
+
};
|
|
442
|
+
},
|
|
443
|
+
intersectShape: (value) => {
|
|
444
|
+
const query = requirePhysicsShapeQuery(value);
|
|
445
|
+
const hits = raw.intersectShape({
|
|
446
|
+
collisionMask: query.collisionMask,
|
|
447
|
+
maxResults: query.maxResults,
|
|
448
|
+
position: query.position,
|
|
449
|
+
rotation: query.rotation,
|
|
450
|
+
shape: primitiveShape(query.shape)
|
|
451
|
+
});
|
|
452
|
+
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
453
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
|
|
454
|
+
return hits.map(nativeHit);
|
|
455
|
+
},
|
|
456
|
+
intersectPoint: (value) => {
|
|
457
|
+
const query = requirePhysicsPointQuery(value);
|
|
458
|
+
const hits = raw.intersectPoint(query);
|
|
459
|
+
if (!Array.isArray(hits) || hits.length > query.maxResults)
|
|
460
|
+
throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed point query result");
|
|
461
|
+
return hits.map(nativeHit);
|
|
462
|
+
},
|
|
223
463
|
readCharacterState: (id) => {
|
|
224
464
|
refreshCharacterState();
|
|
225
465
|
return characterState.get(id);
|
|
@@ -233,8 +473,11 @@ function createNativePhysicsSimulation(raw, version) {
|
|
|
233
473
|
return raw.drainCollisionEvents(buffer);
|
|
234
474
|
},
|
|
235
475
|
dispose: () => {
|
|
476
|
+
if (disposed) return;
|
|
477
|
+
disposed = true;
|
|
236
478
|
raw.dispose();
|
|
237
479
|
bodyIds.clear();
|
|
480
|
+
bodyHandles.clear();
|
|
238
481
|
areaIds.clear();
|
|
239
482
|
characterIds.clear();
|
|
240
483
|
characterState.clear();
|
|
@@ -259,6 +502,7 @@ installPhysicsSimulationBackend({
|
|
|
259
502
|
const host = nativePhysicsHost();
|
|
260
503
|
return wrapNativeSimulation(nativeSimulation(host.createSimulation(options)), host.version);
|
|
261
504
|
},
|
|
505
|
+
createShape: (shape) => Object.freeze({ backend: "native", kind: shape.kind }),
|
|
262
506
|
simulationForWorld: (world) => {
|
|
263
507
|
if (typeof world !== "object" || world === null)
|
|
264
508
|
throw new Error("TN_NATIVE_PHYSICS_INVALID: native world must be an object");
|
|
@@ -274,7 +518,7 @@ var MAX_CONTACT_LOG = 1e3;
|
|
|
274
518
|
function finiteTransform(values, offset) {
|
|
275
519
|
const result = Array.from({ length: 8 }, (_, index) => values[offset + index]);
|
|
276
520
|
if (result.some((value) => value === void 0 || !Number.isFinite(value)))
|
|
277
|
-
throw new Error("
|
|
521
|
+
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
278
522
|
return result;
|
|
279
523
|
}
|
|
280
524
|
var Area3D = class {
|
|
@@ -305,6 +549,7 @@ var Area3D = class {
|
|
|
305
549
|
}
|
|
306
550
|
const registration = this.#simulation.createBody({
|
|
307
551
|
mass: 0,
|
|
552
|
+
entity: this.entity,
|
|
308
553
|
position: this.#position,
|
|
309
554
|
rotation: { w: 1, x: 0, y: 0, z: 0 },
|
|
310
555
|
sensor: true,
|
|
@@ -391,7 +636,7 @@ var Area3D = class {
|
|
|
391
636
|
function finiteTransform2(values, offset) {
|
|
392
637
|
const result = Array.from({ length: 8 }, (_, index) => values[offset + index]);
|
|
393
638
|
if (result.some((value) => value === void 0 || !Number.isFinite(value)))
|
|
394
|
-
throw new Error("
|
|
639
|
+
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
395
640
|
return result;
|
|
396
641
|
}
|
|
397
642
|
var CharacterBody3D = class {
|
|
@@ -400,6 +645,7 @@ var CharacterBody3D = class {
|
|
|
400
645
|
controller;
|
|
401
646
|
object;
|
|
402
647
|
velocity;
|
|
648
|
+
/** Signed vertical acceleration; see `ICharacterBody3DOptions.gravity`. Down is negative. */
|
|
403
649
|
gravity;
|
|
404
650
|
maxFallSpeed;
|
|
405
651
|
oneWayLayers;
|
|
@@ -427,10 +673,11 @@ var CharacterBody3D = class {
|
|
|
427
673
|
options.shape.setCollisionGroups(interactionGroups(layer, mask));
|
|
428
674
|
}
|
|
429
675
|
const registration = this.#simulation.createBody({
|
|
676
|
+
entity: options.entity,
|
|
430
677
|
mass: 0,
|
|
431
678
|
position: this.object.position,
|
|
432
679
|
rotation: this.object.quaternion,
|
|
433
|
-
sensor:
|
|
680
|
+
sensor: shape.sensor,
|
|
434
681
|
shape,
|
|
435
682
|
type: "character"
|
|
436
683
|
});
|
|
@@ -451,6 +698,7 @@ var CharacterBody3D = class {
|
|
|
451
698
|
maxSlopeClimbAngle: options.maxSlopeClimbAngle ?? Math.PI / 4,
|
|
452
699
|
offset: options.offset ?? 0.01,
|
|
453
700
|
oneWayLayers: this.oneWayLayers,
|
|
701
|
+
pushesDynamicBodies: options.pushesDynamicBodies ?? false,
|
|
454
702
|
snapToGround: options.snapToGround
|
|
455
703
|
};
|
|
456
704
|
try {
|
|
@@ -578,11 +826,11 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
578
826
|
constructor(descriptor) {
|
|
579
827
|
this.#descriptor = descriptor;
|
|
580
828
|
}
|
|
581
|
-
/** Backend-neutral data consumed by the selected
|
|
829
|
+
/** Backend-neutral data consumed by the selected IPhysicsSimulation adapter. */
|
|
582
830
|
get descriptor() {
|
|
583
831
|
return this.#descriptor;
|
|
584
832
|
}
|
|
585
|
-
/** Rapier's descriptor on web, or an opaque native
|
|
833
|
+
/** Backend-specific escape hatch: Rapier's descriptor on web, or an opaque native handle. */
|
|
586
834
|
get raw() {
|
|
587
835
|
if (this.#backendRaw === void 0) {
|
|
588
836
|
try {
|
|
@@ -625,6 +873,20 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
625
873
|
z: 0
|
|
626
874
|
});
|
|
627
875
|
}
|
|
876
|
+
/**
|
|
877
|
+
* A capsule of total height `2 * (halfHeight + radius)`, **centred on the body's origin** like
|
|
878
|
+
* every other shape here — `halfHeight` is the straight section's half-length, measured from
|
|
879
|
+
* that centre, and the hemispherical cap adds `radius` beyond it.
|
|
880
|
+
*
|
|
881
|
+
* So the body origin sits at the character's waist, not at its feet. A visual rig modelled
|
|
882
|
+
* standing on its own origin, parented straight onto the body object, therefore floats exactly
|
|
883
|
+
* `halfHeight + radius` above every surface it lands on. Offset the rig down by that amount:
|
|
884
|
+
*
|
|
885
|
+
* ```ts
|
|
886
|
+
* const shape = CollisionShape3D.capsule(0.26, 0.28);
|
|
887
|
+
* rig.position.y = -(0.26 + 0.28); // feet reach the capsule's bottom cap
|
|
888
|
+
* ```
|
|
889
|
+
*/
|
|
628
890
|
static capsule(halfHeight, radius) {
|
|
629
891
|
if (!Number.isFinite(halfHeight) || halfHeight < 0)
|
|
630
892
|
throw new Error("CollisionShape3D.capsule requires a finite non-negative halfHeight.");
|
|
@@ -723,11 +985,53 @@ var CollisionShape3D = class _CollisionShape3D {
|
|
|
723
985
|
}
|
|
724
986
|
};
|
|
725
987
|
|
|
988
|
+
// src/PhysicsDirectSpaceState3D.ts
|
|
989
|
+
function rayQuery(options) {
|
|
990
|
+
return requirePhysicsRayQuery({
|
|
991
|
+
collisionMask: options.collisionMask === void 0 ? 65535 : options.collisionMask,
|
|
992
|
+
from: options.from,
|
|
993
|
+
to: options.to
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
function shapeQuery(options) {
|
|
997
|
+
if (!(options.shape instanceof CollisionShape3D))
|
|
998
|
+
throw new Error("PhysicsDirectSpaceState3D.intersectShape requires a CollisionShape3D.");
|
|
999
|
+
return requirePhysicsShapeQuery({
|
|
1000
|
+
collisionMask: options.collisionMask === void 0 ? 65535 : options.collisionMask,
|
|
1001
|
+
maxResults: options.maxResults === void 0 ? 16 : options.maxResults,
|
|
1002
|
+
position: options.position,
|
|
1003
|
+
rotation: options.rotation === void 0 ? { w: 1, x: 0, y: 0, z: 0 } : options.rotation,
|
|
1004
|
+
shape: options.shape.descriptor
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
function pointQuery(options) {
|
|
1008
|
+
return requirePhysicsPointQuery({
|
|
1009
|
+
collisionMask: options.collisionMask === void 0 ? 65535 : options.collisionMask,
|
|
1010
|
+
maxResults: options.maxResults === void 0 ? 16 : options.maxResults,
|
|
1011
|
+
position: options.position
|
|
1012
|
+
});
|
|
1013
|
+
}
|
|
1014
|
+
var PhysicsDirectSpaceState3D = class {
|
|
1015
|
+
#simulation;
|
|
1016
|
+
constructor(simulation) {
|
|
1017
|
+
this.#simulation = simulation;
|
|
1018
|
+
}
|
|
1019
|
+
intersectRay(options) {
|
|
1020
|
+
return this.#simulation.intersectRay(rayQuery(options));
|
|
1021
|
+
}
|
|
1022
|
+
intersectShape(options) {
|
|
1023
|
+
return this.#simulation.intersectShape(shapeQuery(options));
|
|
1024
|
+
}
|
|
1025
|
+
intersectPoint(options) {
|
|
1026
|
+
return this.#simulation.intersectPoint(pointQuery(options));
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
|
|
726
1030
|
// src/RigidBody3D.ts
|
|
727
1031
|
function finiteTransform3(values, offset) {
|
|
728
1032
|
const result = Array.from({ length: 8 }, (_, index) => values[offset + index]);
|
|
729
1033
|
if (result.some((value) => value === void 0 || !Number.isFinite(value)))
|
|
730
|
-
throw new Error("
|
|
1034
|
+
throw new Error("IPhysicsSimulation returned a malformed transform.");
|
|
731
1035
|
return result;
|
|
732
1036
|
}
|
|
733
1037
|
var RigidBody3D = class {
|
|
@@ -751,10 +1055,11 @@ var RigidBody3D = class {
|
|
|
751
1055
|
options.shape.setCollisionGroups(interactionGroups(layer, mask));
|
|
752
1056
|
}
|
|
753
1057
|
const registration = this.#simulation.createBody({
|
|
1058
|
+
entity: options.entity,
|
|
754
1059
|
mass: options.mass ?? 0,
|
|
755
1060
|
position: this.object.position,
|
|
756
1061
|
rotation: this.object.quaternion,
|
|
757
|
-
sensor:
|
|
1062
|
+
sensor: shape.sensor,
|
|
758
1063
|
shape,
|
|
759
1064
|
type: this.type
|
|
760
1065
|
});
|
|
@@ -795,6 +1100,34 @@ var RigidBody3D = class {
|
|
|
795
1100
|
}
|
|
796
1101
|
syncToPhysics() {
|
|
797
1102
|
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Godot's `apply_impulse`. The one-shot shove: a thrown crate, a jump pad, a hit reaction.
|
|
1105
|
+
*
|
|
1106
|
+
* These four members exist because there is otherwise no portable way to move a dynamic body
|
|
1107
|
+
* at all. `body.raw` is a Rapier object on web and opaque on native, so reaching through it
|
|
1108
|
+
* forks the game by platform, and a transform write is discarded by the next step.
|
|
1109
|
+
*/
|
|
1110
|
+
applyImpulse(impulse) {
|
|
1111
|
+
this.#requireLive("applyImpulse");
|
|
1112
|
+
this.#simulation.applyBodyImpulse(this.body.id, impulse);
|
|
1113
|
+
}
|
|
1114
|
+
/** Godot's `apply_force`. Continuous push; cleared by the backend each step. */
|
|
1115
|
+
applyForce(force) {
|
|
1116
|
+
this.#requireLive("applyForce");
|
|
1117
|
+
this.#simulation.applyBodyForce(this.body.id, force);
|
|
1118
|
+
}
|
|
1119
|
+
/** Godot's `linear_velocity`. */
|
|
1120
|
+
get linearVelocity() {
|
|
1121
|
+
this.#requireLive("linearVelocity");
|
|
1122
|
+
return this.#simulation.readBodyLinearVelocity(this.body.id);
|
|
1123
|
+
}
|
|
1124
|
+
set linearVelocity(velocity) {
|
|
1125
|
+
this.#requireLive("linearVelocity");
|
|
1126
|
+
this.#simulation.setBodyLinearVelocity(this.body.id, velocity);
|
|
1127
|
+
}
|
|
1128
|
+
#requireLive(operation) {
|
|
1129
|
+
if (this.#disposed) throw new Error(`RigidBody3D.${operation} cannot be used after dispose.`);
|
|
1130
|
+
}
|
|
798
1131
|
syncFromPhysics() {
|
|
799
1132
|
const transform = this.#simulation.readBodyTransform?.(this.body.id);
|
|
800
1133
|
if (transform === void 0) return;
|
|
@@ -822,6 +1155,8 @@ var RigidBody3D = class {
|
|
|
822
1155
|
};
|
|
823
1156
|
|
|
824
1157
|
// src/plugin.ts
|
|
1158
|
+
var PHYSICS_DEBUG_BODY_LIMIT = 100;
|
|
1159
|
+
var PHYSICS_DEBUG_SAMPLE_LIMIT = 100;
|
|
825
1160
|
function growFloat(buffer, records) {
|
|
826
1161
|
if (buffer.length >= records * PHYSICS_TRANSFORM_STRIDE) return buffer;
|
|
827
1162
|
return new Float32Array(
|
|
@@ -840,7 +1175,7 @@ function isSmallBufferError2(error) {
|
|
|
840
1175
|
function visibleId(buffer, offset) {
|
|
841
1176
|
const value = buffer[offset];
|
|
842
1177
|
if (value === void 0 || !Number.isInteger(value) || value < 0)
|
|
843
|
-
throw new Error("
|
|
1178
|
+
throw new Error("IPhysicsSimulation returned an invalid visible body id.");
|
|
844
1179
|
return value;
|
|
845
1180
|
}
|
|
846
1181
|
function rapier(options = {}) {
|
|
@@ -853,32 +1188,51 @@ function rapier(options = {}) {
|
|
|
853
1188
|
const kinematicMotions = /* @__PURE__ */ new Map();
|
|
854
1189
|
let kinematic = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
855
1190
|
let visible = new Float32Array(PHYSICS_TRANSFORM_STRIDE * 16);
|
|
1191
|
+
let visibleCount = 0;
|
|
1192
|
+
let sleepStates = new Float32Array(
|
|
1193
|
+
PHYSICS_SLEEP_STATE_STRIDE * 16
|
|
1194
|
+
);
|
|
856
1195
|
let events = new Uint32Array(64);
|
|
1196
|
+
const activeContacts = /* @__PURE__ */ new Map();
|
|
1197
|
+
let debugSeries = [];
|
|
1198
|
+
let unregisterObservations;
|
|
1199
|
+
function buildContext(selected) {
|
|
1200
|
+
return {
|
|
1201
|
+
add: (body) => {
|
|
1202
|
+
bodies.add(body);
|
|
1203
|
+
bodiesById.set(body.body.id, body);
|
|
1204
|
+
},
|
|
1205
|
+
addArea: (area) => areas.set(area.body.id, area),
|
|
1206
|
+
eventQueue: physicsHandle(selected.rawEventQueue),
|
|
1207
|
+
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
1208
|
+
numBodies: () => bodies.size,
|
|
1209
|
+
remove: (body) => {
|
|
1210
|
+
bodies.delete(body);
|
|
1211
|
+
bodiesById.delete(body.body.id);
|
|
1212
|
+
removeContactsFor(body.body.id);
|
|
1213
|
+
},
|
|
1214
|
+
removeArea: (area) => {
|
|
1215
|
+
areas.delete(area.body.id);
|
|
1216
|
+
removeContactsFor(area.body.id);
|
|
1217
|
+
},
|
|
1218
|
+
directSpaceState: new PhysicsDirectSpaceState3D(selected),
|
|
1219
|
+
simulation: selected,
|
|
1220
|
+
world: physicsWorldHandle(selected.rawWorld, selected)
|
|
1221
|
+
};
|
|
1222
|
+
}
|
|
857
1223
|
return {
|
|
858
1224
|
setup: async (ctx, runtime) => {
|
|
859
1225
|
await backend.initialize();
|
|
860
1226
|
const selected = backend.createSimulation(options);
|
|
861
1227
|
simulation = selected;
|
|
862
1228
|
if (runtime !== void 0) runtime.rapier = selected.version;
|
|
863
|
-
context =
|
|
864
|
-
add: (body) => {
|
|
865
|
-
bodies.add(body);
|
|
866
|
-
bodiesById.set(body.body.id, body);
|
|
867
|
-
},
|
|
868
|
-
addArea: (area) => areas.set(area.body.id, area),
|
|
869
|
-
eventQueue: physicsHandle(selected.rawEventQueue),
|
|
870
|
-
kinematicMotion: (colliderHandle) => kinematicMotions.get(colliderHandle),
|
|
871
|
-
numBodies: () => bodies.size,
|
|
872
|
-
remove: (body) => {
|
|
873
|
-
bodies.delete(body);
|
|
874
|
-
bodiesById.delete(body.body.id);
|
|
875
|
-
},
|
|
876
|
-
removeArea: (area) => areas.delete(area.body.id),
|
|
877
|
-
simulation: selected,
|
|
878
|
-
world: physicsWorldHandle(selected.rawWorld, selected)
|
|
879
|
-
};
|
|
1229
|
+
context = buildContext(selected);
|
|
880
1230
|
ctx.physics = context;
|
|
881
|
-
|
|
1231
|
+
unregisterObservations = runtime?.observations?.contribute({
|
|
1232
|
+
capabilities: ["runtime.physics"],
|
|
1233
|
+
sample: (request) => physicsObservations(ctx, runtime, request)
|
|
1234
|
+
});
|
|
1235
|
+
return unregisterObservations;
|
|
882
1236
|
},
|
|
883
1237
|
update: (_ctx, dt) => {
|
|
884
1238
|
if (simulation === void 0) throw new Error("Physics plugin updated before setup.");
|
|
@@ -908,7 +1262,7 @@ function rapier(options = {}) {
|
|
|
908
1262
|
};
|
|
909
1263
|
simulation.step(dt, input);
|
|
910
1264
|
visible = growFloat(visible, bodies.size + areas.size);
|
|
911
|
-
|
|
1265
|
+
visibleCount = simulation.readVisibleTransforms(visible);
|
|
912
1266
|
for (let index = 0; index < visibleCount; index += 1) {
|
|
913
1267
|
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
914
1268
|
const id = visibleId(visible, offset);
|
|
@@ -917,7 +1271,7 @@ function rapier(options = {}) {
|
|
|
917
1271
|
else {
|
|
918
1272
|
const area = areas.get(id);
|
|
919
1273
|
if (area === void 0)
|
|
920
|
-
throw new Error("
|
|
1274
|
+
throw new Error("IPhysicsSimulation returned an unknown visible body id.");
|
|
921
1275
|
area.applyTransform(visible, offset);
|
|
922
1276
|
}
|
|
923
1277
|
}
|
|
@@ -942,11 +1296,14 @@ function rapier(options = {}) {
|
|
|
942
1296
|
const right = events[offset + 1];
|
|
943
1297
|
const started = events[offset + 2] === 1;
|
|
944
1298
|
if (left === void 0 || right === void 0)
|
|
945
|
-
throw new Error("
|
|
1299
|
+
throw new Error("IPhysicsSimulation returned a malformed collision event.");
|
|
946
1300
|
const leftArea = areas.get(left);
|
|
947
1301
|
const rightArea = areas.get(right);
|
|
948
1302
|
const leftBody = bodiesById.get(left);
|
|
949
1303
|
const rightBody = bodiesById.get(right);
|
|
1304
|
+
const contactKey = left < right ? `${left}:${right}` : `${right}:${left}`;
|
|
1305
|
+
if (started) activeContacts.set(contactKey, left < right ? [left, right] : [right, left]);
|
|
1306
|
+
else activeContacts.delete(contactKey);
|
|
950
1307
|
if (leftArea !== void 0 && rightBody !== void 0)
|
|
951
1308
|
leftArea.handleCollision(rightBody, started);
|
|
952
1309
|
if (rightArea !== void 0 && leftBody !== void 0)
|
|
@@ -964,12 +1321,31 @@ function rapier(options = {}) {
|
|
|
964
1321
|
}
|
|
965
1322
|
kinematicMotions.clear();
|
|
966
1323
|
},
|
|
967
|
-
|
|
1324
|
+
// Disposing the bodies is not enough to make a restarted scene reproducible. The solver,
|
|
1325
|
+
// island manager and broad phase carry state from everything that has already happened, so
|
|
1326
|
+
// replaying an identical authored layout in a reused world settles differently -- measured
|
|
1327
|
+
// in a sandbox build as settle hashes a2f87bad vs 658eb6f8 at 240 vs 266 ticks, diverging
|
|
1328
|
+
// before any input. A scene therefore gets a pristine simulation, which makes restart
|
|
1329
|
+
// deterministic without a game having to discover an API for it.
|
|
1330
|
+
sceneExit: (ctx) => {
|
|
968
1331
|
for (const area of [...areas.values()]) area.dispose();
|
|
969
1332
|
for (const body of [...bodies]) body.dispose();
|
|
970
1333
|
kinematicMotions.clear();
|
|
1334
|
+
activeContacts.clear();
|
|
1335
|
+
debugSeries = [];
|
|
1336
|
+
if (options.deterministicRestart !== true) return;
|
|
1337
|
+
areas.clear();
|
|
1338
|
+
bodies.clear();
|
|
1339
|
+
bodiesById.clear();
|
|
1340
|
+
simulation?.dispose();
|
|
1341
|
+
const replacement = backend.createSimulation(options);
|
|
1342
|
+
simulation = replacement;
|
|
1343
|
+
context = buildContext(replacement);
|
|
1344
|
+
ctx.physics = context;
|
|
971
1345
|
},
|
|
972
1346
|
dispose: () => {
|
|
1347
|
+
unregisterObservations?.();
|
|
1348
|
+
unregisterObservations = void 0;
|
|
973
1349
|
for (const area of [...areas.values()]) area.dispose();
|
|
974
1350
|
for (const body of [...bodies]) body.dispose();
|
|
975
1351
|
simulation?.dispose();
|
|
@@ -979,8 +1355,116 @@ function rapier(options = {}) {
|
|
|
979
1355
|
bodies.clear();
|
|
980
1356
|
bodiesById.clear();
|
|
981
1357
|
kinematicMotions.clear();
|
|
1358
|
+
activeContacts.clear();
|
|
1359
|
+
debugSeries = [];
|
|
982
1360
|
}
|
|
983
1361
|
};
|
|
1362
|
+
function physicsObservations(ctx, runtime, request) {
|
|
1363
|
+
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
1364
|
+
if (request.include?.includes("physicsDebugSeries") !== true) return {};
|
|
1365
|
+
if (request.label !== void 0) {
|
|
1366
|
+
if (debugSeries.some(({ label }) => label === request.label)) {
|
|
1367
|
+
throw new Error(
|
|
1368
|
+
`TN_PHYSICS_DEBUG_LABEL_DUPLICATE: '${request.label}' was already sampled.`
|
|
1369
|
+
);
|
|
1370
|
+
}
|
|
1371
|
+
if (debugSeries.length >= PHYSICS_DEBUG_SAMPLE_LIMIT) {
|
|
1372
|
+
throw new Error(
|
|
1373
|
+
`TN_PHYSICS_DEBUG_SAMPLE_LIMIT: at most ${PHYSICS_DEBUG_SAMPLE_LIMIT} labelled samples are retained.`
|
|
1374
|
+
);
|
|
1375
|
+
}
|
|
1376
|
+
debugSeries.push({
|
|
1377
|
+
label: request.label,
|
|
1378
|
+
snapshot: physicsDebugSnapshot(ctx),
|
|
1379
|
+
tick: runtime.tick()
|
|
1380
|
+
});
|
|
1381
|
+
}
|
|
1382
|
+
return { physicsDebugSeries: debugSeries.map((sample) => ({ ...sample })) };
|
|
1383
|
+
}
|
|
1384
|
+
function physicsDebugSnapshot(ctx) {
|
|
1385
|
+
if (simulation === void 0) throw new Error("Physics observations sampled before setup.");
|
|
1386
|
+
const ordered = [...bodies].sort((left, right) => left.body.id - right.body.id);
|
|
1387
|
+
const retained = ordered.slice(0, PHYSICS_DEBUG_BODY_LIMIT);
|
|
1388
|
+
const retainedIds = new Set(retained.map(({ body }) => body.id));
|
|
1389
|
+
const entityById = new Map(
|
|
1390
|
+
retained.map((body) => [body.body.id, physicsEntityId(ctx, body)])
|
|
1391
|
+
);
|
|
1392
|
+
sleepStates = growSleepStates(sleepStates, bodies.size + areas.size);
|
|
1393
|
+
const sleepCount = simulation.readBodySleepStates(sleepStates);
|
|
1394
|
+
const sleepingById = /* @__PURE__ */ new Map();
|
|
1395
|
+
for (let index = 0; index < sleepCount; index += 1) {
|
|
1396
|
+
const offset = index * PHYSICS_SLEEP_STATE_STRIDE;
|
|
1397
|
+
const id = sleepStates[offset];
|
|
1398
|
+
const sleeping = sleepStates[offset + 1];
|
|
1399
|
+
if (id === void 0 || !Number.isInteger(id) || sleeping !== 0 && sleeping !== 1) {
|
|
1400
|
+
throw new Error("IPhysicsSimulation returned a malformed sleep-state record.");
|
|
1401
|
+
}
|
|
1402
|
+
sleepingById.set(id, sleeping === 1);
|
|
1403
|
+
}
|
|
1404
|
+
const positionById = /* @__PURE__ */ new Map();
|
|
1405
|
+
for (let index = 0; index < visibleCount; index += 1) {
|
|
1406
|
+
const offset = index * PHYSICS_TRANSFORM_STRIDE;
|
|
1407
|
+
const id = visibleId(visible, offset);
|
|
1408
|
+
const position = [visible[offset + 1], visible[offset + 2], visible[offset + 3]];
|
|
1409
|
+
if (position.some((value) => value === void 0 || !Number.isFinite(value))) {
|
|
1410
|
+
throw new Error("IPhysicsSimulation returned a malformed debug position.");
|
|
1411
|
+
}
|
|
1412
|
+
positionById.set(id, position);
|
|
1413
|
+
}
|
|
1414
|
+
const primitives = [];
|
|
1415
|
+
for (const body of retained) {
|
|
1416
|
+
const entity = entityById.get(body.body.id);
|
|
1417
|
+
if (entity === void 0) throw new Error("A retained physics body lost its entity id.");
|
|
1418
|
+
const sleeping = sleepingById.get(body.body.id) ?? false;
|
|
1419
|
+
primitives.push({
|
|
1420
|
+
category: "sleep",
|
|
1421
|
+
entity,
|
|
1422
|
+
id: `sleep:${entity}`,
|
|
1423
|
+
value: sleeping ? 1 : 0
|
|
1424
|
+
});
|
|
1425
|
+
const position = positionById.get(body.body.id);
|
|
1426
|
+
if (position !== void 0) {
|
|
1427
|
+
primitives.push({ category: "center-of-mass", entity, position });
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
for (const [left, right] of activeContacts.values()) {
|
|
1431
|
+
if (!retainedIds.has(left) || !retainedIds.has(right)) continue;
|
|
1432
|
+
primitives.push({
|
|
1433
|
+
category: "contact",
|
|
1434
|
+
id: `${entityById.get(left)}:${entityById.get(right)}`
|
|
1435
|
+
});
|
|
1436
|
+
}
|
|
1437
|
+
return {
|
|
1438
|
+
artifact: {
|
|
1439
|
+
overflow: {
|
|
1440
|
+
bodyLimit: PHYSICS_DEBUG_BODY_LIMIT,
|
|
1441
|
+
omittedBodies: Math.max(0, ordered.length - retained.length),
|
|
1442
|
+
totalBodies: ordered.length
|
|
1443
|
+
},
|
|
1444
|
+
primitives
|
|
1445
|
+
}
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
function removeContactsFor(bodyId) {
|
|
1449
|
+
for (const [key, pair] of activeContacts) {
|
|
1450
|
+
if (pair.includes(bodyId)) activeContacts.delete(key);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
function growSleepStates(buffer, bodyCount) {
|
|
1455
|
+
const required = bodyCount * PHYSICS_SLEEP_STATE_STRIDE;
|
|
1456
|
+
if (buffer.length >= required) return buffer;
|
|
1457
|
+
return new Float32Array(Math.max(required, buffer.length * 2));
|
|
1458
|
+
}
|
|
1459
|
+
function physicsEntityId(ctx, body) {
|
|
1460
|
+
for (const id of Object.keys(ctx.entities.snapshot())) {
|
|
1461
|
+
const entity = ctx.entities.get(id);
|
|
1462
|
+
if (entity === body || entity === body.object) return id;
|
|
1463
|
+
if (entity !== void 0 && Object.values(entity).some((value) => value === body || value === body.object)) {
|
|
1464
|
+
return id;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return `physics.body.${body.body.id}`;
|
|
984
1468
|
}
|
|
985
1469
|
|
|
986
|
-
export { Area3D, CharacterBody3D, CollisionShape3D, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, RigidBody3D, interactionGroups, rapier };
|
|
1470
|
+
export { Area3D, CharacterBody3D, CollisionShape3D, MAX_PHYSICS_QUERY_RESULTS, PHYSICS_COLLISION_EVENT_STRIDE, PHYSICS_TRANSFORM_STRIDE, PhysicsDirectSpaceState3D, RigidBody3D, interactionGroups, rapier };
|