@ignifx/physics 0.7.0 → 0.9.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/dist/index.d.ts +82 -42
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +158 -201
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
- package/skills/physics/SKILL.md +12 -9
package/dist/index.js
CHANGED
|
@@ -462,9 +462,7 @@ const PhysicsErrorCode = {
|
|
|
462
462
|
/** A `MeshCollider` has no geometry to build a shape from. */
|
|
463
463
|
colliderGeometryUnavailable: "IGX-0906",
|
|
464
464
|
/** A physics body was built for an entity that has a parent, whose node pose is not world space. */
|
|
465
|
-
bodyOnChildEntity: "IGX-0907"
|
|
466
|
-
/** The ADR-0013 collision drain refused to bind because Babylon Lite's internals moved. */
|
|
467
|
-
internalDrainUnavailable: "IGX-0908"
|
|
465
|
+
bodyOnChildEntity: "IGX-0907"
|
|
468
466
|
};
|
|
469
467
|
/**
|
|
470
468
|
* The one-line message template of every code, as `ExtensionContext.registerErrorCodes` wants it.
|
|
@@ -479,8 +477,7 @@ const PHYSICS_ERROR_MESSAGES = Object.freeze({
|
|
|
479
477
|
"IGX-0904": "{file} is not an ignifx.physicsmaterial document this build can read.",
|
|
480
478
|
"IGX-0905": "physics.collisionMatrix names the layer {layer}, which the project does not declare.",
|
|
481
479
|
"IGX-0906": "{entity} has a MeshCollider with no geometry to build a shape from.",
|
|
482
|
-
"IGX-0907": "{entity} carries a physics body but is not a root entity; Havok writes its local pose."
|
|
483
|
-
"IGX-0908": "The adapter-internal collision drain does not recognise Babylon Lite {version}."
|
|
480
|
+
"IGX-0907": "{entity} carries a physics body but is not a root entity; Havok writes its local pose."
|
|
484
481
|
});
|
|
485
482
|
/**
|
|
486
483
|
* Builds an `IgnifxError` carrying one of this package's codes.
|
|
@@ -1354,8 +1351,14 @@ function onTriggerEvents(world, sink) {
|
|
|
1354
1351
|
});
|
|
1355
1352
|
}
|
|
1356
1353
|
/**
|
|
1357
|
-
* Registers the
|
|
1358
|
-
*
|
|
1354
|
+
* Registers the collision drain (`onPhysicsCollision`, `index.d.ts` 9315). Each
|
|
1355
|
+
* `PhysicsCollisionInfo` (10030) names both bodies as `collider` and `collidedAgainst`, and they are
|
|
1356
|
+
* resolved to nodes the same way the trigger drain resolves `bodyA`/`bodyB`. Lite skips an event
|
|
1357
|
+
* whose bodies it no longer tracks (`lib/physics/havok-collision.js:47-52`), so both are present.
|
|
1358
|
+
*
|
|
1359
|
+
* @remarks
|
|
1360
|
+
* `colliderIndex` and `collidedAgainstIndex` are thin-instance indices. ignifx never creates
|
|
1361
|
+
* thin-instance bodies, so they are always `0` and are ignored.
|
|
1359
1362
|
*
|
|
1360
1363
|
* @param world - The world handle.
|
|
1361
1364
|
* @param sink - Called once per collision event.
|
|
@@ -1369,8 +1372,8 @@ function onCollisionEvents(world, sink) {
|
|
|
1369
1372
|
point: info.point,
|
|
1370
1373
|
normal: info.normal,
|
|
1371
1374
|
impulse: info.impulse,
|
|
1372
|
-
nodeA:
|
|
1373
|
-
nodeB:
|
|
1375
|
+
nodeA: info.collider.node,
|
|
1376
|
+
nodeB: info.collidedAgainst.node
|
|
1374
1377
|
});
|
|
1375
1378
|
});
|
|
1376
1379
|
}
|
|
@@ -2141,6 +2144,18 @@ function controllerBody(controller) {
|
|
|
2141
2144
|
return controller.getBody();
|
|
2142
2145
|
}
|
|
2143
2146
|
/**
|
|
2147
|
+
* The node of a controller's capsule body. Lite creates it for the controller, separate from the
|
|
2148
|
+
* entity's node, so it is what a ray hit on the capsule reports (`RaycastResult.body.node`).
|
|
2149
|
+
*
|
|
2150
|
+
* @param controller - The controller.
|
|
2151
|
+
* @returns The capsule body's node.
|
|
2152
|
+
*
|
|
2153
|
+
* @internal
|
|
2154
|
+
*/
|
|
2155
|
+
function controllerNode(controller) {
|
|
2156
|
+
return controller.getBody().node;
|
|
2157
|
+
}
|
|
2158
|
+
/**
|
|
2144
2159
|
* Reads the controller's resolved position.
|
|
2145
2160
|
*
|
|
2146
2161
|
* @param controller - The controller.
|
|
@@ -2233,127 +2248,8 @@ function toSupportState(value) {
|
|
|
2233
2248
|
return value === SupportState.sliding ? SupportState.sliding : SupportState.unsupported;
|
|
2234
2249
|
}
|
|
2235
2250
|
//#endregion
|
|
2236
|
-
//#region src/lite/internal/collision-drain.ts
|
|
2237
|
-
/** Lite's `offA`: where the first contact's slots begin. */
|
|
2238
|
-
const OFFSET_A = 2;
|
|
2239
|
-
/** Lite's `offB`: where the second contact's slots begin. */
|
|
2240
|
-
const OFFSET_B = 18;
|
|
2241
|
-
/** The Babylon Lite version this drain was written and tested against. */
|
|
2242
|
-
const PINNED_LITE_VERSION = "1.27.0";
|
|
2243
|
-
/**
|
|
2244
|
-
* Reports whether a world exposes the internals this drain reads.
|
|
2245
|
-
*
|
|
2246
|
-
* @param world - The Havok world handle.
|
|
2247
|
-
* @returns `true` when every field the drain needs is present and of the expected kind.
|
|
2248
|
-
*
|
|
2249
|
-
* @internal
|
|
2250
|
-
*/
|
|
2251
|
-
function probeCollisionLayout(world) {
|
|
2252
|
-
const internals = asWorldInternals(world);
|
|
2253
|
-
const havok = internals._hknp;
|
|
2254
|
-
if (havok === void 0 || internals._hkWorld === void 0 || !Array.isArray(internals._bodies)) return false;
|
|
2255
|
-
if (typeof havok.HP_World_GetCollisionEvents !== "function") return false;
|
|
2256
|
-
if (typeof havok.HP_World_GetNextCollisionEvent !== "function") return false;
|
|
2257
|
-
const started = havok.EventType["COLLISION_STARTED"];
|
|
2258
|
-
const continued = havok.EventType["COLLISION_CONTINUED"];
|
|
2259
|
-
return typeof started?.value === "number" && typeof continued?.value === "number";
|
|
2260
|
-
}
|
|
2261
|
-
/**
|
|
2262
|
-
* Registers the internal drain on a world's post-step hook.
|
|
2263
|
-
*
|
|
2264
|
-
* @param world - The Havok world handle.
|
|
2265
|
-
* @param sink - Called once per collision event, with both nodes resolved.
|
|
2266
|
-
* @throws IgnifxError with code `IGX-0908` when {@link probeCollisionLayout} says the internals
|
|
2267
|
-
* moved, which is what makes a Babylon Lite upgrade fail loudly rather than silently.
|
|
2268
|
-
*
|
|
2269
|
-
* @internal
|
|
2270
|
-
*/
|
|
2271
|
-
function drainCollisionsWithBodies(world, sink) {
|
|
2272
|
-
if (!probeCollisionLayout(world)) throw physicsError(PhysicsErrorCode.internalDrainUnavailable, "physics({ collisionIdentities: \"internal\" }) needs Babylon Lite's Havok internals, which this version does not expose.", {
|
|
2273
|
-
context: { version: PINNED_LITE_VERSION },
|
|
2274
|
-
hint: "Pin @babylonjs/lite to 1.27.0, or use the default collisionIdentities: \"upstream\"."
|
|
2275
|
-
});
|
|
2276
|
-
const internals = asWorldInternals(world);
|
|
2277
|
-
const havok = internals._hknp;
|
|
2278
|
-
const native = internals._hkWorld;
|
|
2279
|
-
if (havok === void 0 || native === void 0) return;
|
|
2280
|
-
const startedValue = havok.EventType["COLLISION_STARTED"]?.value ?? -1;
|
|
2281
|
-
const continuedValue = havok.EventType["COLLISION_CONTINUED"]?.value ?? -1;
|
|
2282
|
-
afterStep(world, () => {
|
|
2283
|
-
let address = havok.HP_World_GetCollisionEvents(native)[1];
|
|
2284
|
-
while (address !== 0) {
|
|
2285
|
-
const ints = new Int32Array(havok.HEAPU8.buffer, address);
|
|
2286
|
-
const floats = new Float32Array(havok.HEAPU8.buffer, address);
|
|
2287
|
-
const type = ints[0] ?? -1;
|
|
2288
|
-
sink({
|
|
2289
|
-
phase: type === startedValue ? 0 : type === continuedValue ? 1 : 2,
|
|
2290
|
-
point: {
|
|
2291
|
-
x: floats[10] ?? 0,
|
|
2292
|
-
y: floats[11] ?? 0,
|
|
2293
|
-
z: floats[12] ?? 0
|
|
2294
|
-
},
|
|
2295
|
-
normal: {
|
|
2296
|
-
x: floats[13] ?? 0,
|
|
2297
|
-
y: floats[14] ?? 0,
|
|
2298
|
-
z: floats[15] ?? 0
|
|
2299
|
-
},
|
|
2300
|
-
impulse: floats[34] ?? 0,
|
|
2301
|
-
nodeA: nodeById(internals, ints[OFFSET_A] ?? 0),
|
|
2302
|
-
nodeB: nodeById(internals, ints[OFFSET_B] ?? 0)
|
|
2303
|
-
});
|
|
2304
|
-
address = havok.HP_World_GetNextCollisionEvent(native, address);
|
|
2305
|
-
}
|
|
2306
|
-
});
|
|
2307
|
-
}
|
|
2308
|
-
/**
|
|
2309
|
-
* Resolves a native body id to the node Lite bound it to, the same linear scan Lite's own
|
|
2310
|
-
* `findBodyById` performs (`lib/physics/havok-trigger.js`, `havok-queries.js`).
|
|
2311
|
-
*
|
|
2312
|
-
* @param internals - The world internals.
|
|
2313
|
-
* @param id - The native body id from the event buffer.
|
|
2314
|
-
* @returns The node, or `null` when the body is no longer tracked.
|
|
2315
|
-
*/
|
|
2316
|
-
function nodeById(internals, id) {
|
|
2317
|
-
const bodies = internals._bodies ?? [];
|
|
2318
|
-
for (let index = 0; index < bodies.length; index += 1) {
|
|
2319
|
-
const body = bodies[index];
|
|
2320
|
-
const native = body?._hkBody?.[0];
|
|
2321
|
-
if (native === id) return body?.node ?? null;
|
|
2322
|
-
if (typeof native === "bigint" && native === BigInt(id)) return body?.node ?? null;
|
|
2323
|
-
}
|
|
2324
|
-
return null;
|
|
2325
|
-
}
|
|
2326
|
-
/**
|
|
2327
|
-
* Views a Lite world as the internal record this drain reads.
|
|
2328
|
-
*
|
|
2329
|
-
* @remarks
|
|
2330
|
-
* The waived boundary crossing of ADR-0013, in one expression. It needs no assertion: Lite declares
|
|
2331
|
-
* `PhysicsWorld` as an empty interface and every field below is optional, so the view is structural
|
|
2332
|
-
* — and {@link probeCollisionLayout} is what makes reading it safe.
|
|
2333
|
-
*
|
|
2334
|
-
* @param world - The opaque handle.
|
|
2335
|
-
* @returns The same object, typed.
|
|
2336
|
-
*/
|
|
2337
|
-
function asWorldInternals(world) {
|
|
2338
|
-
return world;
|
|
2339
|
-
}
|
|
2340
|
-
//#endregion
|
|
2341
2251
|
//#region src/runtime/runtime.ts
|
|
2342
2252
|
/**
|
|
2343
|
-
* The per-app physics runtime: the Havok world, the bodies, the character controllers, the event
|
|
2344
|
-
* drains, and the three things the systems ask it to do each frame — restore, step, interpolate
|
|
2345
|
-
* (`docs/architecture/09-physics.md` §1).
|
|
2346
|
-
*
|
|
2347
|
-
* Everything here is `@internal`; the public surface is `PhysicsService`, `Rigidbody`, the
|
|
2348
|
-
* colliders, and `CharacterController`.
|
|
2349
|
-
*/
|
|
2350
|
-
/**
|
|
2351
|
-
* How collision events learn which bodies took part (`09-physics.md` §4, ADR-0013).
|
|
2352
|
-
*
|
|
2353
|
-
* @public
|
|
2354
|
-
*/
|
|
2355
|
-
const COLLISION_IDENTITY_MODES = ["upstream", "internal"];
|
|
2356
|
-
/**
|
|
2357
2253
|
* The counters `09-physics.md` §9 and the plan's diagnostics deliverable name.
|
|
2358
2254
|
*
|
|
2359
2255
|
* @public
|
|
@@ -2389,13 +2285,13 @@ var PhysicsRuntime = class {
|
|
|
2389
2285
|
#settings;
|
|
2390
2286
|
#simulation;
|
|
2391
2287
|
#world;
|
|
2392
|
-
#identities;
|
|
2393
2288
|
#counters;
|
|
2394
2289
|
#records = [];
|
|
2395
2290
|
#byEntity = /* @__PURE__ */ new Map();
|
|
2396
2291
|
#byNode = /* @__PURE__ */ new Map();
|
|
2397
2292
|
#dirty = /* @__PURE__ */ new Set();
|
|
2398
2293
|
#controllers = [];
|
|
2294
|
+
#controllersByNode = /* @__PURE__ */ new Map();
|
|
2399
2295
|
#dirtyControllers = /* @__PURE__ */ new Set();
|
|
2400
2296
|
#collideMasks = new Int32Array(LAYER_COUNT).fill(-1);
|
|
2401
2297
|
#triggerSlots = [];
|
|
@@ -2427,16 +2323,14 @@ var PhysicsRuntime = class {
|
|
|
2427
2323
|
/**
|
|
2428
2324
|
* Builds the runtime, applies the world settings, and registers the event drains.
|
|
2429
2325
|
*
|
|
2430
|
-
* @param options - The context, settings, scene, world,
|
|
2431
|
-
* @throws IgnifxError with code `IGX-0905` when the collision matrix names an unknown layer
|
|
2432
|
-
* `IGX-0908` when the internal drain does not recognise this Babylon Lite build.
|
|
2326
|
+
* @param options - The context, settings, scene, world, and counters.
|
|
2327
|
+
* @throws IgnifxError with code `IGX-0905` when the collision matrix names an unknown layer.
|
|
2433
2328
|
*/
|
|
2434
2329
|
constructor(options) {
|
|
2435
2330
|
this.#ctx = options.ctx;
|
|
2436
2331
|
this.#settings = options.settings;
|
|
2437
2332
|
this.#simulation = options.simulation;
|
|
2438
2333
|
this.#world = options.world;
|
|
2439
|
-
this.#identities = options.collisionIdentities;
|
|
2440
2334
|
this.#counters = options.counters;
|
|
2441
2335
|
this.#contacts.push(this.#contactPoint);
|
|
2442
2336
|
setGravity(this.#world, this.#settings.gravity);
|
|
@@ -2449,10 +2343,7 @@ var PhysicsRuntime = class {
|
|
|
2449
2343
|
onTriggerEvents(this.#world, (record) => {
|
|
2450
2344
|
this.#bufferTrigger(record);
|
|
2451
2345
|
});
|
|
2452
|
-
|
|
2453
|
-
this.#bufferContact(record);
|
|
2454
|
-
});
|
|
2455
|
-
else onCollisionEvents(this.#world, (record) => {
|
|
2346
|
+
onCollisionEvents(this.#world, (record) => {
|
|
2456
2347
|
this.#bufferContact(record);
|
|
2457
2348
|
});
|
|
2458
2349
|
afterStep(this.#world, () => {});
|
|
@@ -2617,6 +2508,35 @@ var PhysicsRuntime = class {
|
|
|
2617
2508
|
recordOfNode(node) {
|
|
2618
2509
|
return node === null ? null : this.#byNode.get(node) ?? null;
|
|
2619
2510
|
}
|
|
2511
|
+
/**
|
|
2512
|
+
* What a query hit on a Lite node: a body record, or a character controller's capsule.
|
|
2513
|
+
*
|
|
2514
|
+
* @param node - The node of the body that was hit.
|
|
2515
|
+
* @returns The target, or `null` when the node belongs to neither.
|
|
2516
|
+
*/
|
|
2517
|
+
targetOfNode(node) {
|
|
2518
|
+
if (node === null) return null;
|
|
2519
|
+
return this.#byNode.get(node) ?? this.#controllersByNode.get(node) ?? null;
|
|
2520
|
+
}
|
|
2521
|
+
/**
|
|
2522
|
+
* How many entries the query index holds: every body record, then every character controller.
|
|
2523
|
+
*
|
|
2524
|
+
* @returns The count.
|
|
2525
|
+
*/
|
|
2526
|
+
get queryTargetCount() {
|
|
2527
|
+
return this.#records.length + this.#controllers.length;
|
|
2528
|
+
}
|
|
2529
|
+
/**
|
|
2530
|
+
* One entry of the query index. Bodies come first and controllers after, each in creation order,
|
|
2531
|
+
* so `overlap` lists them in a stable order (`09-physics.md` §8).
|
|
2532
|
+
*
|
|
2533
|
+
* @param index - An index below {@link PhysicsRuntime.queryTargetCount}.
|
|
2534
|
+
* @returns The target, or `undefined` past the end.
|
|
2535
|
+
*/
|
|
2536
|
+
queryTargetAt(index) {
|
|
2537
|
+
const bodies = this.#records.length;
|
|
2538
|
+
return index < bodies ? this.#records[index] : this.#controllers[index - bodies];
|
|
2539
|
+
}
|
|
2620
2540
|
/** Counts one query for the diagnostics group. */
|
|
2621
2541
|
countQuery() {
|
|
2622
2542
|
this.#queries += 1;
|
|
@@ -2741,7 +2661,10 @@ var PhysicsRuntime = class {
|
|
|
2741
2661
|
*/
|
|
2742
2662
|
resizeController(component, preserveFeet) {
|
|
2743
2663
|
const record = this.#controllerOf(component);
|
|
2744
|
-
if (record !== null)
|
|
2664
|
+
if (record !== null) {
|
|
2665
|
+
resizeController(record.controller, component.height, component.radius, preserveFeet);
|
|
2666
|
+
capsuleHalfExtents(component.height, component.radius, record.halfExtents);
|
|
2667
|
+
}
|
|
2745
2668
|
}
|
|
2746
2669
|
/**
|
|
2747
2670
|
* Teleports a controller and resets its interpolation history.
|
|
@@ -2775,6 +2698,7 @@ var PhysicsRuntime = class {
|
|
|
2775
2698
|
record.disconnect();
|
|
2776
2699
|
disposeController(record.controller);
|
|
2777
2700
|
this.#controllers.splice(index, 1);
|
|
2701
|
+
this.#controllersByNode.delete(record.node);
|
|
2778
2702
|
return;
|
|
2779
2703
|
}
|
|
2780
2704
|
}
|
|
@@ -2794,6 +2718,7 @@ var PhysicsRuntime = class {
|
|
|
2794
2718
|
}
|
|
2795
2719
|
}
|
|
2796
2720
|
this.#controllers.length = 0;
|
|
2721
|
+
this.#controllersByNode.clear();
|
|
2797
2722
|
for (let index = this.#records.length - 1; index >= 0; index -= 1) {
|
|
2798
2723
|
const record = this.#records[index];
|
|
2799
2724
|
if (record !== void 0) this.#teardown(record);
|
|
@@ -3127,6 +3052,10 @@ var PhysicsRuntime = class {
|
|
|
3127
3052
|
if (record === void 0 || record.entity.layer === record.layer) continue;
|
|
3128
3053
|
this.markDirty(record.entity);
|
|
3129
3054
|
}
|
|
3055
|
+
for (let index = 0; index < this.#controllers.length; index += 1) {
|
|
3056
|
+
const record = this.#controllers[index];
|
|
3057
|
+
if (record !== void 0) record.layer = record.entity.layer;
|
|
3058
|
+
}
|
|
3130
3059
|
}
|
|
3131
3060
|
/**
|
|
3132
3061
|
* Snapshots every body's authoritative pose and reports implicit-static bodies that moved.
|
|
@@ -3213,6 +3142,11 @@ var PhysicsRuntime = class {
|
|
|
3213
3142
|
const record = {
|
|
3214
3143
|
component,
|
|
3215
3144
|
controller,
|
|
3145
|
+
node: controllerNode(controller),
|
|
3146
|
+
entity: component.entity,
|
|
3147
|
+
colliders: NO_COLLIDERS,
|
|
3148
|
+
layer: component.entity.layer,
|
|
3149
|
+
halfExtents: capsuleHalfExtents(component.height, component.radius, new Vec3()),
|
|
3216
3150
|
previousPosition: centre.clone(),
|
|
3217
3151
|
currentPosition: centre.clone(),
|
|
3218
3152
|
interpolate: this.#settings.interpolation && component.interpolation === "interpolate",
|
|
@@ -3222,6 +3156,7 @@ var PhysicsRuntime = class {
|
|
|
3222
3156
|
this.#reportPush(record, push);
|
|
3223
3157
|
});
|
|
3224
3158
|
this.#controllers.push(record);
|
|
3159
|
+
this.#controllersByNode.set(record.node, record);
|
|
3225
3160
|
}
|
|
3226
3161
|
/**
|
|
3227
3162
|
* Turns one Lite push event into a `CharacterCollision`.
|
|
@@ -3264,7 +3199,7 @@ var PhysicsRuntime = class {
|
|
|
3264
3199
|
/**
|
|
3265
3200
|
* Buffers a collision event.
|
|
3266
3201
|
*
|
|
3267
|
-
* @param event - What the
|
|
3202
|
+
* @param event - What the collision drain reported.
|
|
3268
3203
|
*/
|
|
3269
3204
|
#bufferContact(event) {
|
|
3270
3205
|
const slot = this.#contactSlot(this.#contactCount);
|
|
@@ -3352,28 +3287,11 @@ var PhysicsRuntime = class {
|
|
|
3352
3287
|
const slot = this.#contactSlots[index];
|
|
3353
3288
|
if (slot === void 0) continue;
|
|
3354
3289
|
const kind = contactCallback(slot.phase);
|
|
3355
|
-
if (slot.first === null && slot.second === null) {
|
|
3356
|
-
this.#broadcastContact(slot, kind);
|
|
3357
|
-
continue;
|
|
3358
|
-
}
|
|
3359
3290
|
this.#deliverContact(slot, slot.first, slot.second, kind);
|
|
3360
3291
|
this.#deliverContact(slot, slot.second, slot.first, kind);
|
|
3361
3292
|
}
|
|
3362
3293
|
}
|
|
3363
3294
|
/**
|
|
3364
|
-
* Delivers one collision to every entity that opted into collision events — the only thing that
|
|
3365
|
-
* can be done in `"upstream"` mode, where Lite reports no identities at all (`09-physics.md` §4).
|
|
3366
|
-
*
|
|
3367
|
-
* @param slot - The buffered contact.
|
|
3368
|
-
* @param kind - Which callback to deliver.
|
|
3369
|
-
*/
|
|
3370
|
-
#broadcastContact(slot, kind) {
|
|
3371
|
-
for (let index = 0; index < this.#records.length; index += 1) {
|
|
3372
|
-
const record = this.#records[index];
|
|
3373
|
-
if (record !== void 0 && record.collisionEvents) this.#deliverContact(slot, record, null, kind);
|
|
3374
|
-
}
|
|
3375
|
-
}
|
|
3376
|
-
/**
|
|
3377
3295
|
* Delivers one collision to one entity.
|
|
3378
3296
|
*
|
|
3379
3297
|
* @param slot - The buffered contact.
|
|
@@ -3430,6 +3348,21 @@ var PhysicsRuntime = class {
|
|
|
3430
3348
|
};
|
|
3431
3349
|
/** Lite's own `characterStrength` default, which `pushStrength: 1` reproduces (`index.d.ts` 8320). */
|
|
3432
3350
|
const DEFAULT_CHARACTER_STRENGTH = 1e38;
|
|
3351
|
+
/** The collider list of a character capsule, which is built from the controller, not from colliders. */
|
|
3352
|
+
const NO_COLLIDERS = Object.freeze([]);
|
|
3353
|
+
/**
|
|
3354
|
+
* The half-extents of a controller capsule's bounding box. Lite places the capsule's segment ends at
|
|
3355
|
+
* `±(height / 2 - radius)` (`createPhysicsCharacterController`), and the capsule does not scale with
|
|
3356
|
+
* the entity.
|
|
3357
|
+
*
|
|
3358
|
+
* @param height - The capsule's total height.
|
|
3359
|
+
* @param radius - Its radius.
|
|
3360
|
+
* @param out - The vector to write.
|
|
3361
|
+
* @returns `out`.
|
|
3362
|
+
*/
|
|
3363
|
+
function capsuleHalfExtents(height, radius, out) {
|
|
3364
|
+
return out.set(radius, Math.abs(height / 2 - radius) + radius, radius);
|
|
3365
|
+
}
|
|
3433
3366
|
/**
|
|
3434
3367
|
* The colliders that make up an entity's body: every enabled `Collider` on the entity itself.
|
|
3435
3368
|
*
|
|
@@ -3828,6 +3761,8 @@ function bodyNodeOf(body) {
|
|
|
3828
3761
|
*/
|
|
3829
3762
|
/** How far a ray travels when the caller names no distance, in metres. */
|
|
3830
3763
|
const DEFAULT_RAY_DISTANCE = 1e4;
|
|
3764
|
+
/** How far past a skipped character capsule's surface a ray resumes, in metres. */
|
|
3765
|
+
const RAY_RESUME_DISTANCE = .001;
|
|
3831
3766
|
/** How much slack the bounds index allows when it resolves a sweep contact to an entity. */
|
|
3832
3767
|
const BOUNDS_EPSILON = .001;
|
|
3833
3768
|
/** Every bit set: the mask a query uses when the caller names no layers. */
|
|
@@ -3861,6 +3796,7 @@ var PhysicsService = class {
|
|
|
3861
3796
|
};
|
|
3862
3797
|
#point = new Vec3();
|
|
3863
3798
|
#normal = new Vec3();
|
|
3799
|
+
#rayFrom = new Vec3();
|
|
3864
3800
|
#overlapExtents = new Vec3();
|
|
3865
3801
|
#overlapResults = [];
|
|
3866
3802
|
#debugViewer;
|
|
@@ -3920,6 +3856,11 @@ var PhysicsService = class {
|
|
|
3920
3856
|
/**
|
|
3921
3857
|
* Casts a ray and returns the first entity it hits (`09-physics.md` §5).
|
|
3922
3858
|
*
|
|
3859
|
+
* @remarks
|
|
3860
|
+
* The ray hits `Rigidbody` and collider-only bodies and `CharacterController` capsules. A capsule
|
|
3861
|
+
* hit reports the character's entity with `collider: null`. A ray passes through a capsule when
|
|
3862
|
+
* `layerMask` leaves out the character's layer, or when the ray starts inside it.
|
|
3863
|
+
*
|
|
3923
3864
|
* @param origin - The world-space origin.
|
|
3924
3865
|
* @param direction - The direction; it is normalised for you.
|
|
3925
3866
|
* @param maxDistance - How far to travel; defaults to 10 km.
|
|
@@ -3929,7 +3870,8 @@ var PhysicsService = class {
|
|
|
3929
3870
|
*/
|
|
3930
3871
|
raycast(origin, direction, maxDistance = DEFAULT_RAY_DISTANCE, options) {
|
|
3931
3872
|
this.#requireStep("raycast");
|
|
3932
|
-
this.#rt()
|
|
3873
|
+
const runtime = this.#rt();
|
|
3874
|
+
runtime.countQuery();
|
|
3933
3875
|
const length = Math.hypot(direction.x, direction.y, direction.z);
|
|
3934
3876
|
if (length === 0) return null;
|
|
3935
3877
|
const scale = maxDistance / length;
|
|
@@ -3938,25 +3880,36 @@ var PhysicsService = class {
|
|
|
3938
3880
|
y: origin.y + direction.y * scale,
|
|
3939
3881
|
z: origin.z + direction.z * scale
|
|
3940
3882
|
};
|
|
3941
|
-
|
|
3942
|
-
|
|
3943
|
-
|
|
3944
|
-
|
|
3945
|
-
|
|
3946
|
-
|
|
3947
|
-
|
|
3948
|
-
|
|
3949
|
-
|
|
3950
|
-
|
|
3951
|
-
|
|
3952
|
-
|
|
3883
|
+
const from = this.#rayFrom.set(origin.x, origin.y, origin.z);
|
|
3884
|
+
let travelled = 0;
|
|
3885
|
+
for (let pass = 0; pass <= runtime.queryTargetCount; pass += 1) {
|
|
3886
|
+
castRay(runtime.world, from, to, ALL_LAYERS, maskOf(options), options?.hitTriggers === true, this.#point, this.#normal, this.#rayResult);
|
|
3887
|
+
if (!this.#rayResult.hit) return null;
|
|
3888
|
+
const target = runtime.targetOfNode(this.#rayResult.node);
|
|
3889
|
+
if (target === null) return null;
|
|
3890
|
+
if (this.#accepts(target, options)) return {
|
|
3891
|
+
entity: target.entity,
|
|
3892
|
+
collider: target.colliders[0] ?? null,
|
|
3893
|
+
point: this.#point.clone(),
|
|
3894
|
+
normal: this.#normal.clone(),
|
|
3895
|
+
distance: travelled + this.#rayResult.distance,
|
|
3896
|
+
triangleIndex: this.#rayResult.triangleIndex
|
|
3897
|
+
};
|
|
3898
|
+
travelled += this.#rayResult.distance + RAY_RESUME_DISTANCE;
|
|
3899
|
+
if (travelled >= maxDistance) return null;
|
|
3900
|
+
const step = RAY_RESUME_DISTANCE / length;
|
|
3901
|
+
from.set(this.#point.x + direction.x * step, this.#point.y + direction.y * step, this.#point.z + direction.z * step);
|
|
3902
|
+
}
|
|
3903
|
+
return null;
|
|
3953
3904
|
}
|
|
3954
3905
|
/**
|
|
3955
3906
|
* Sweeps a shape and returns the first contact (`09-physics.md` §5).
|
|
3956
3907
|
*
|
|
3957
3908
|
* @remarks
|
|
3958
3909
|
* Lite's `shapeCast` reports no body (`index.d.ts` 11497), so `entity` is resolved against the
|
|
3959
|
-
* extension's body-bounds index and is bounds-accurate rather than shape-accurate.
|
|
3910
|
+
* extension's body-bounds index and is bounds-accurate rather than shape-accurate. The index holds
|
|
3911
|
+
* every `Rigidbody` and collider-only body and every `CharacterController` capsule; a capsule
|
|
3912
|
+
* contact reports the character's entity with `collider: null`.
|
|
3960
3913
|
*
|
|
3961
3914
|
* The sweep itself cannot be filtered by layer — Lite's `ShapeCastQuery` carries no collision
|
|
3962
3915
|
* masks — so a body outside `layerMask` still stops the sweep; it is merely reported with
|
|
@@ -3983,11 +3936,11 @@ var PhysicsService = class {
|
|
|
3983
3936
|
destroyShape(this.#rt().world, handle);
|
|
3984
3937
|
}
|
|
3985
3938
|
if (!this.#sweepResult.hit) return null;
|
|
3986
|
-
const
|
|
3939
|
+
const target = this.#targetAt(this.#point, options, ignored);
|
|
3987
3940
|
const distance = Math.hypot(to.x - from.x, to.y - from.y, to.z - from.z) * this.#sweepResult.fraction;
|
|
3988
3941
|
return {
|
|
3989
|
-
entity:
|
|
3990
|
-
collider:
|
|
3942
|
+
entity: target?.entity ?? null,
|
|
3943
|
+
collider: target?.colliders[0] ?? null,
|
|
3991
3944
|
point: this.#point.clone(),
|
|
3992
3945
|
normal: this.#normal.clone(),
|
|
3993
3946
|
fraction: this.#sweepResult.fraction,
|
|
@@ -4000,29 +3953,33 @@ var PhysicsService = class {
|
|
|
4000
3953
|
* @remarks
|
|
4001
3954
|
* Lite's `shapeProximity` reports one hit and no identity (`index.d.ts` 11540,
|
|
4002
3955
|
* `lib/physics/havok-queries.js:7`), so this is answered from the extension's own bounds index:
|
|
4003
|
-
* every
|
|
4004
|
-
* — a body whose box overlaps but whose shape
|
|
3956
|
+
* every `Rigidbody`, collider-only body and `CharacterController` capsule whose world bounding box
|
|
3957
|
+
* intersects the query shape's. It is conservative — a body whose box overlaps but whose shape
|
|
3958
|
+
* does not is listed.
|
|
4005
3959
|
*
|
|
4006
3960
|
* @param shape - The query shape.
|
|
4007
3961
|
* @param position - Its world position.
|
|
4008
3962
|
* @param rotation - Its world rotation; accepted for forward compatibility and currently unused,
|
|
4009
3963
|
* because the bounds test is axis-aligned.
|
|
4010
3964
|
* @param options - Layer mask and trigger behaviour.
|
|
4011
|
-
* @returns The overlapping entities, in
|
|
3965
|
+
* @returns The overlapping entities, each once: bodies in creation order, then characters in
|
|
3966
|
+
* creation order. The array is reused between calls.
|
|
4012
3967
|
* @throws IgnifxError with code `IGX-0902` in development when no fixed step has run yet.
|
|
4013
3968
|
*/
|
|
4014
3969
|
overlap(shape, position, rotation, options) {
|
|
4015
3970
|
this.#requireStep("overlap");
|
|
4016
|
-
this.#rt()
|
|
3971
|
+
const runtime = this.#rt();
|
|
3972
|
+
runtime.countQuery();
|
|
4017
3973
|
queryHalfExtents(shape, this.#overlapExtents);
|
|
4018
|
-
this.#overlapResults
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
if (
|
|
3974
|
+
const results = this.#overlapResults;
|
|
3975
|
+
results.length = 0;
|
|
3976
|
+
const count = runtime.queryTargetCount;
|
|
3977
|
+
for (let index = 0; index < count; index += 1) {
|
|
3978
|
+
const target = runtime.queryTargetAt(index);
|
|
3979
|
+
if (target === void 0 || !this.#accepts(target, options)) continue;
|
|
3980
|
+
if (boxesOverlap(position, this.#overlapExtents, target.currentPosition, target.halfExtents) && !results.includes(target.entity)) results.push(target.entity);
|
|
4024
3981
|
}
|
|
4025
|
-
return
|
|
3982
|
+
return results;
|
|
4026
3983
|
}
|
|
4027
3984
|
/**
|
|
4028
3985
|
* How far the nearest body is from a positioned shape, which is the one thing Lite's
|
|
@@ -4160,36 +4117,37 @@ var PhysicsService = class {
|
|
|
4160
4117
|
* @param options - Layer mask and trigger behaviour.
|
|
4161
4118
|
* @param ignored - The entity the sweep was told to pass through, which can therefore not be the
|
|
4162
4119
|
* one it hit.
|
|
4163
|
-
* @returns The
|
|
4120
|
+
* @returns The body or capsule, or `null`.
|
|
4164
4121
|
*/
|
|
4165
|
-
#
|
|
4166
|
-
const
|
|
4122
|
+
#targetAt(point, options, ignored = null) {
|
|
4123
|
+
const runtime = this.#rt();
|
|
4124
|
+
const count = runtime.queryTargetCount;
|
|
4167
4125
|
let best = null;
|
|
4168
4126
|
let bestDistance = Number.POSITIVE_INFINITY;
|
|
4169
|
-
for (let index = 0; index <
|
|
4170
|
-
const
|
|
4171
|
-
if (
|
|
4172
|
-
const centre =
|
|
4173
|
-
const extents =
|
|
4127
|
+
for (let index = 0; index < count; index += 1) {
|
|
4128
|
+
const target = runtime.queryTargetAt(index);
|
|
4129
|
+
if (target === void 0 || target.entity === ignored || !this.#accepts(target, options)) continue;
|
|
4130
|
+
const centre = target.currentPosition;
|
|
4131
|
+
const extents = target.halfExtents;
|
|
4174
4132
|
if (Math.abs(point.x - centre.x) > extents.x + BOUNDS_EPSILON || Math.abs(point.y - centre.y) > extents.y + BOUNDS_EPSILON || Math.abs(point.z - centre.z) > extents.z + BOUNDS_EPSILON) continue;
|
|
4175
4133
|
const distance = Math.hypot(point.x - centre.x, point.y - centre.y, point.z - centre.z);
|
|
4176
4134
|
if (distance < bestDistance) {
|
|
4177
4135
|
bestDistance = distance;
|
|
4178
|
-
best =
|
|
4136
|
+
best = target;
|
|
4179
4137
|
}
|
|
4180
4138
|
}
|
|
4181
4139
|
return best;
|
|
4182
4140
|
}
|
|
4183
4141
|
/**
|
|
4184
|
-
* Whether a body passes a query's layer and trigger filters.
|
|
4142
|
+
* Whether a body or capsule passes a query's layer and trigger filters.
|
|
4185
4143
|
*
|
|
4186
|
-
* @param
|
|
4144
|
+
* @param target - The body or capsule.
|
|
4187
4145
|
* @param options - The query options.
|
|
4188
|
-
* @returns `true` when
|
|
4146
|
+
* @returns `true` when it may be reported.
|
|
4189
4147
|
*/
|
|
4190
|
-
#accepts(
|
|
4191
|
-
if ((maskOf(options) & 1 <<
|
|
4192
|
-
return options?.hitTriggers === true || !allTriggers(
|
|
4148
|
+
#accepts(target, options) {
|
|
4149
|
+
if ((maskOf(options) & 1 << target.layer) === 0) return false;
|
|
4150
|
+
return options?.hitTriggers === true || !allTriggers(target.colliders);
|
|
4193
4151
|
}
|
|
4194
4152
|
};
|
|
4195
4153
|
/**
|
|
@@ -4426,7 +4384,6 @@ const physics = defineExtension((options = {}) => {
|
|
|
4426
4384
|
settings,
|
|
4427
4385
|
simulation,
|
|
4428
4386
|
world,
|
|
4429
|
-
collisionIdentities: options.collisionIdentities ?? "upstream",
|
|
4430
4387
|
counters
|
|
4431
4388
|
});
|
|
4432
4389
|
ctx.setSimulationScene(simulation.scene);
|
|
@@ -4529,6 +4486,6 @@ function describeSchemas() {
|
|
|
4529
4486
|
};
|
|
4530
4487
|
}
|
|
4531
4488
|
//#endregion
|
|
4532
|
-
export { BODY_TYPES, BoxCollider, CAPSULE_DIRECTIONS, COLLISION_EVENT_MODES,
|
|
4489
|
+
export { BODY_TYPES, BoxCollider, CAPSULE_DIRECTIONS, COLLISION_EVENT_MODES, CapsuleCollider, CharacterController, Collider, CylinderCollider, HAVOK_WASM_AUTO, HeightfieldCollider, INTERPOLATION_MODES, KINEMATIC_SYNC_MODES, MeshCollider, PHYSICS_DIAGNOSTICS_COUNTERS, PHYSICS_DIAGNOSTICS_GROUP, PHYSICS_ERROR_MESSAGES, PHYSICS_MATERIAL_ASSET_TYPE, PHYSICS_MATERIAL_FILE_EXTENSION, PHYSICS_MATERIAL_FILE_FORMAT, PHYSICS_MATERIAL_FORMAT_VERSION, PHYSICS_SETTINGS_SECTION, PhysicsErrorCode, PhysicsMaterial, PhysicsService, Rigidbody, SUPPORT_STATES, SphereCollider, colliderFields, createPhysicsMaterialLoader, defaultPhysicsSettings, describePhysicsMaterialFileFormat, describeSchemas, parsePhysicsMaterial, physics, physicsError, physicsSettingsSchema };
|
|
4533
4490
|
|
|
4534
4491
|
//# sourceMappingURL=index.js.map
|