@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21
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/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
|
@@ -0,0 +1,973 @@
|
|
|
1
|
+
import { defaultChecksum } from './canonical.js';
|
|
2
|
+
import { meters } from './physics/units.js';
|
|
3
|
+
function normalizeKccJumpTuning3D(tuning) {
|
|
4
|
+
const impulse = finiteKccJumpNumber(tuning?.impulse, 10, 'impulse');
|
|
5
|
+
return {
|
|
6
|
+
impulse,
|
|
7
|
+
liftOff: finiteKccJumpNumber(tuning?.liftOff, impulse / 10, 'liftOff'),
|
|
8
|
+
coyoteFrames: kccJumpFrameCount(tuning?.coyoteFrames, 'coyoteFrames', 0),
|
|
9
|
+
// A press arms the jump by writing this into jumpBufferFrames and the gate
|
|
10
|
+
// needs it > 0, so 0 would not mean "no buffering" — it would mean "never
|
|
11
|
+
// jump". 1 is the real floor: the press lives for the frame it happened on.
|
|
12
|
+
bufferFrames: kccJumpFrameCount(tuning?.bufferFrames, 'bufferFrames', 1),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function finiteKccJumpNumber(value, fallback, field) {
|
|
16
|
+
if (value === undefined) {
|
|
17
|
+
return fallback;
|
|
18
|
+
}
|
|
19
|
+
if (!Number.isFinite(value)) {
|
|
20
|
+
throw new Error(`KCC jump.${field} must be a finite number`);
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function kccJumpFrameCount(value, field, minimum) {
|
|
25
|
+
if (value === undefined) {
|
|
26
|
+
return 4;
|
|
27
|
+
}
|
|
28
|
+
if (!Number.isInteger(value) || value < minimum) {
|
|
29
|
+
throw new Error(`KCC jump.${field} must be an integer >= ${minimum}, got ${value}`);
|
|
30
|
+
}
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
function stepDeterministicKccRevision1Raw3D(body, input, world, options, gravityPerFrame, checksumMode) {
|
|
34
|
+
const jump = normalizeKccJumpTuning3D(options.jump);
|
|
35
|
+
let next = applyProcessors(body, input, options.frame, options.processors ?? []);
|
|
36
|
+
const events = [];
|
|
37
|
+
let correctionCount = 0;
|
|
38
|
+
let physicsQueryCount = 0;
|
|
39
|
+
let collisionFilterCalls = 0;
|
|
40
|
+
let collisionFilterRejectedHits = 0;
|
|
41
|
+
const externalImpulse = combineExternalKccImpulses(options.externalImpulses ?? []);
|
|
42
|
+
let coyoteFrames = next.grounded ? jump.coyoteFrames : Math.max(0, next.coyoteFrames - 1);
|
|
43
|
+
let jumpBufferFrames = input.jumpPressed === true ? jump.bufferFrames : Math.max(0, next.jumpBufferFrames - 1);
|
|
44
|
+
let vx = input.moveX + externalImpulse.vx;
|
|
45
|
+
let vz = input.moveZ + externalImpulse.vz;
|
|
46
|
+
let vy = next.vy + gravityPerFrame + externalImpulse.vy;
|
|
47
|
+
let x = next.x + vx;
|
|
48
|
+
let y = next.y + vy;
|
|
49
|
+
let z = next.z + vz;
|
|
50
|
+
let grounded = false;
|
|
51
|
+
let platformId = next.platformId;
|
|
52
|
+
let platformClaimedThisFrame = false;
|
|
53
|
+
if (externalImpulse.applied) {
|
|
54
|
+
events.push('external-impulse');
|
|
55
|
+
}
|
|
56
|
+
for (const platform of [...(world.movingPlatforms ?? [])].sort((left, right) => compareStrings(left.id, right.id))) {
|
|
57
|
+
if (next.grounded && next.platformId === platform.id) {
|
|
58
|
+
x += platform.dxPerFrame;
|
|
59
|
+
z += platform.dzPerFrame;
|
|
60
|
+
vx += platform.dxPerFrame;
|
|
61
|
+
vz += platform.dzPerFrame;
|
|
62
|
+
events.push('platform-transfer');
|
|
63
|
+
events.push('platform-velocity-transfer');
|
|
64
|
+
}
|
|
65
|
+
if (x >= platform.minX && x <= platform.maxX && z >= platform.minZ && z <= platform.maxZ && y <= platform.y && vy <= 0) {
|
|
66
|
+
y = platform.y;
|
|
67
|
+
vy = 0;
|
|
68
|
+
grounded = true;
|
|
69
|
+
platformId = platform.id;
|
|
70
|
+
platformClaimedThisFrame = true;
|
|
71
|
+
events.push('moving-platform');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const slopeY = slopeHeightAt(world.slopes ?? [], x, z, world.maxSlopeRisePerUnit);
|
|
75
|
+
if (slopeY !== undefined && y <= slopeY + world.stepHeight && vy <= 0) {
|
|
76
|
+
y = slopeY;
|
|
77
|
+
vy = 0;
|
|
78
|
+
grounded = true;
|
|
79
|
+
platformId = undefined;
|
|
80
|
+
events.push('slope');
|
|
81
|
+
}
|
|
82
|
+
const step = findStepAt(world.steps ?? [], x, z);
|
|
83
|
+
const geometry = normalizeKccGeometry3D(next);
|
|
84
|
+
if (step !== undefined && step.height <= world.stepHeight && y <= step.height + 1 && vy <= 0) {
|
|
85
|
+
if (step.ceilingY !== undefined && step.ceilingY - step.height < geometry.halfHeight * 2 + geometry.skinWidth) {
|
|
86
|
+
x = next.x;
|
|
87
|
+
z = next.z;
|
|
88
|
+
vx = 0;
|
|
89
|
+
vz = 0;
|
|
90
|
+
correctionCount += 1;
|
|
91
|
+
events.push('step-head-blocked');
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
y = step.height;
|
|
95
|
+
vy = 0;
|
|
96
|
+
grounded = true;
|
|
97
|
+
platformId = undefined;
|
|
98
|
+
events.push('step');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (jumpBufferFrames > 0 && (next.grounded || next.coyoteFrames > 0 || grounded)) {
|
|
102
|
+
vy = jump.impulse;
|
|
103
|
+
y += jump.liftOff;
|
|
104
|
+
grounded = false;
|
|
105
|
+
platformId = undefined;
|
|
106
|
+
jumpBufferFrames = 0;
|
|
107
|
+
coyoteFrames = 0;
|
|
108
|
+
events.push(next.grounded ? 'jump' : 'coyote-jump');
|
|
109
|
+
}
|
|
110
|
+
const previousFeetY = next.y - kccFeetOffset(geometry);
|
|
111
|
+
let groundedOnStatic = false;
|
|
112
|
+
let groundedOnDynamic = false;
|
|
113
|
+
// Resolved before the dynamic path so the higher of the two wins: a crate
|
|
114
|
+
// standing on a floor must not lose its platform to the floor underneath it.
|
|
115
|
+
const staticGround = options.physics !== undefined && options.groundOnStaticBodies === true && vy <= 0
|
|
116
|
+
? staticGroundUnderKcc(options.physics, x, y, z, previousFeetY, geometry, new Set(options.ignoredPhysicsBodyIds ?? []), world.stepHeight, world.maxSlopeRisePerUnit)
|
|
117
|
+
: undefined;
|
|
118
|
+
if (options.physics !== undefined && options.standOnDynamicBodies === true && vy <= 0) {
|
|
119
|
+
const ground = dynamicGroundUnderKcc(options.physics, x, y, z, geometry, new Set(options.ignoredPhysicsBodyIds ?? []));
|
|
120
|
+
if (ground !== undefined && (staticGround === undefined || ground.topY >= staticGround.topY)) {
|
|
121
|
+
groundedOnDynamic = true;
|
|
122
|
+
platformClaimedThisFrame = true;
|
|
123
|
+
y = ground.topY;
|
|
124
|
+
vy = 0;
|
|
125
|
+
grounded = true;
|
|
126
|
+
platformId = ground.bodyId;
|
|
127
|
+
// Carry the rider with the crate this frame (position delta) AND inherit its
|
|
128
|
+
// velocity, mirroring the kinematic moving-platform path — otherwise the
|
|
129
|
+
// rider lags one frame and slides off a moving crate.
|
|
130
|
+
x += ground.deltaX;
|
|
131
|
+
z += ground.deltaZ;
|
|
132
|
+
vx += ground.deltaX;
|
|
133
|
+
vz += ground.deltaZ;
|
|
134
|
+
events.push('stand-on-dynamic');
|
|
135
|
+
if (ground.deltaX !== 0 || ground.deltaZ !== 0) {
|
|
136
|
+
events.push('platform-velocity-transfer');
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
// Applied BEFORE the horizontal slide: the slide resolves overlap horizontally
|
|
141
|
+
// only, so a capsule that fell into terrain would be shoved sideways across
|
|
142
|
+
// the surface instead of being stood on top of it. Skipped when a dynamic
|
|
143
|
+
// body already claimed a higher surface this frame.
|
|
144
|
+
// platformId carries over from the previous frame, so a rider that simply
|
|
145
|
+
// walked off keeps pointing at a platform it has left. Clear it once nothing
|
|
146
|
+
// has claimed it this frame.
|
|
147
|
+
if (options.standOnDynamicBodies === true && !platformClaimedThisFrame) {
|
|
148
|
+
platformId = undefined;
|
|
149
|
+
}
|
|
150
|
+
// `grounded` here means an earlier source (moving platform, slope, step, or a
|
|
151
|
+
// dynamic body) already claimed a surface this frame; static only wins if it
|
|
152
|
+
// is higher. platformId cannot be used for this — it carries over from the
|
|
153
|
+
// previous frame, so stepping off a crate onto terrain would skip grounding.
|
|
154
|
+
if (staticGround !== undefined && (!grounded || staticGround.topY > y)) {
|
|
155
|
+
platformId = undefined;
|
|
156
|
+
y = staticGround.topY;
|
|
157
|
+
vy = 0;
|
|
158
|
+
grounded = true;
|
|
159
|
+
groundedOnStatic = true;
|
|
160
|
+
events.push('static-ground');
|
|
161
|
+
}
|
|
162
|
+
// The KCC's intended (pre-slide) horizontal position and velocity — push
|
|
163
|
+
// impulses are computed here because the slide blocks the KCC off the body.
|
|
164
|
+
const pushCandidate = { x, z, vx, vz };
|
|
165
|
+
if (options.physics !== undefined) {
|
|
166
|
+
const resolved = resolvePhysicsSlide3D(options.physics, next, { x, y, z, vx, vz }, new Set(options.ignoredPhysicsBodyIds ?? []), options.frame, options.collisionFilters ?? []);
|
|
167
|
+
physicsQueryCount += resolved.queryCount;
|
|
168
|
+
collisionFilterCalls += resolved.collisionFilterCalls;
|
|
169
|
+
collisionFilterRejectedHits += resolved.collisionFilterRejectedHits;
|
|
170
|
+
if (resolved.corrected) {
|
|
171
|
+
x = resolved.x;
|
|
172
|
+
z = resolved.z;
|
|
173
|
+
vx = resolved.vx;
|
|
174
|
+
vz = resolved.vz;
|
|
175
|
+
correctionCount += resolved.correctionCount;
|
|
176
|
+
events.push(resolved.swept ? 'physics-sweep' : resolved.slid ? 'physics-slide' : 'physics-correction');
|
|
177
|
+
// The slide moved the footprint, so the height sampled before it belongs
|
|
178
|
+
// to the old x/z. On sloped ground that stale height reads as penetration
|
|
179
|
+
// next frame and freezes movement along walls and ledges — re-sample.
|
|
180
|
+
// Attachment must follow the FINAL footprint. A slide can carry the
|
|
181
|
+
// capsule off the platform it was standing on (leaving it "grounded" in
|
|
182
|
+
// mid-air) or onto one it had missed.
|
|
183
|
+
if (groundedOnDynamic && (x !== pushCandidate.x || z !== pushCandidate.z)) {
|
|
184
|
+
const reattached = dynamicGroundUnderKcc(options.physics, x, y, z, geometry, new Set(options.ignoredPhysicsBodyIds ?? []));
|
|
185
|
+
if (reattached === undefined) {
|
|
186
|
+
grounded = false;
|
|
187
|
+
platformId = undefined;
|
|
188
|
+
groundedOnDynamic = false;
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
y = reattached.topY;
|
|
192
|
+
platformId = reattached.bodyId;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (groundedOnStatic && (x !== pushCandidate.x || z !== pushCandidate.z)) {
|
|
196
|
+
const reground = staticGroundUnderKcc(options.physics, x, y, z,
|
|
197
|
+
// The capsule has already landed, so this is a local correction from
|
|
198
|
+
// the landed feet — not the start-of-frame ones. Reusing those would
|
|
199
|
+
// reopen the fall-crossing window and let a slide under an overhang
|
|
200
|
+
// yank the capsule onto its roof.
|
|
201
|
+
y - kccFeetOffset(geometry), geometry, new Set(options.ignoredPhysicsBodyIds ?? []), world.stepHeight, world.maxSlopeRisePerUnit);
|
|
202
|
+
if (reground !== undefined) {
|
|
203
|
+
y = reground.topY;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const geometryForInteraction = normalizeKccGeometry3D(next);
|
|
209
|
+
let physicsImpulses = [];
|
|
210
|
+
// Stand on a dynamic body directly beneath the feet: attach as a platform and
|
|
211
|
+
// inherit its per-frame delta (its velocity), mirroring the kinematic path.
|
|
212
|
+
// Push the dynamic bodies the KCC walked into this frame.
|
|
213
|
+
if (options.physics !== undefined && (options.maxPushImpulse ?? 0) > 0) {
|
|
214
|
+
physicsImpulses = computeKccPushImpulses(options.physics, pushCandidate.x, y, pushCandidate.z, { vx: pushCandidate.vx, vz: pushCandidate.vz }, geometryForInteraction, options.maxPushImpulse ?? 0, new Set(options.ignoredPhysicsBodyIds ?? []));
|
|
215
|
+
if (physicsImpulses.length > 0) {
|
|
216
|
+
events.push('push-dynamic');
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// Spread `next` first: the capsule's radius/halfHeight/skinWidth live on the
|
|
220
|
+
// body, and rebuilding it from a bare literal dropped them, so a caller's
|
|
221
|
+
// capsule silently reverted to the 1x2 default after a single step.
|
|
222
|
+
const boundsCorrection = clampBodyToBounds({ ...next, x, y, z, vx, vy, vz, grounded, coyoteFrames, jumpBufferFrames, platformId }, world);
|
|
223
|
+
if (boundsCorrection.corrected) {
|
|
224
|
+
correctionCount += boundsCorrection.corrections;
|
|
225
|
+
events.push(...boundsCorrection.events);
|
|
226
|
+
}
|
|
227
|
+
next = boundsCorrection.body;
|
|
228
|
+
const finalBody = {
|
|
229
|
+
...next,
|
|
230
|
+
coyoteFrames: next.grounded ? jump.coyoteFrames : coyoteFrames,
|
|
231
|
+
jumpBufferFrames,
|
|
232
|
+
};
|
|
233
|
+
return {
|
|
234
|
+
body: finalBody,
|
|
235
|
+
events,
|
|
236
|
+
correctionCount,
|
|
237
|
+
physicsQueryCount,
|
|
238
|
+
collisionFilterCalls,
|
|
239
|
+
collisionFilterRejectedHits,
|
|
240
|
+
physicsImpulses,
|
|
241
|
+
...(checksumMode === 'disabled' ? {} : { checksum: defaultChecksum({ body: finalBody, events, correctionCount, physicsQueryCount, collisionFilterCalls, collisionFilterRejectedHits, physicsImpulses }) }),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
const KCC_PUSH_MASS = 1;
|
|
245
|
+
function readKccPhysicsBody(physics, id) {
|
|
246
|
+
const body = physics.readMotion(physics.resolveBody(id));
|
|
247
|
+
return {
|
|
248
|
+
id,
|
|
249
|
+
kind: body.kind,
|
|
250
|
+
x: body.position.x,
|
|
251
|
+
y: body.position.y,
|
|
252
|
+
z: body.position.z,
|
|
253
|
+
vx: body.linearVelocity.x,
|
|
254
|
+
vy: body.linearVelocity.y,
|
|
255
|
+
vz: body.linearVelocity.z,
|
|
256
|
+
orientation: body.orientation,
|
|
257
|
+
shape: body.shape,
|
|
258
|
+
layer: body.layer,
|
|
259
|
+
mask: body.mask,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function comparePhysicsBodyIds(left, right) {
|
|
263
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
264
|
+
}
|
|
265
|
+
function overlappingDynamicBodies(physics, x, y, z, geometry, ignoredBodyIds) {
|
|
266
|
+
const query = {
|
|
267
|
+
shape: { type: 'capsule', radius: meters(geometry.radius + geometry.skinWidth), halfHeight: meters(geometry.halfHeight) },
|
|
268
|
+
x: meters(x),
|
|
269
|
+
y: meters(y),
|
|
270
|
+
z: meters(z),
|
|
271
|
+
hitStatics: false,
|
|
272
|
+
hitDynamics: true,
|
|
273
|
+
hitKinematics: false,
|
|
274
|
+
};
|
|
275
|
+
return physics.overlap(query)
|
|
276
|
+
.map((hit) => readKccPhysicsBody(physics, hit.bodyId))
|
|
277
|
+
.filter((body) => body.kind === 'dynamic' && !ignoredBodyIds.has(body.id))
|
|
278
|
+
.sort((left, right) => comparePhysicsBodyIds(left.id, right.id));
|
|
279
|
+
}
|
|
280
|
+
function computeKccPushImpulses(physics, x, y, z, velocity, geometry, maxPushImpulse, ignoredBodyIds) {
|
|
281
|
+
const impulses = [];
|
|
282
|
+
for (const body of overlappingDynamicBodies(physics, x, y, z, geometry, ignoredBodyIds)) {
|
|
283
|
+
// push along the horizontal direction from the KCC toward the body, scaled
|
|
284
|
+
// by the KCC's horizontal speed, capped by maxPushImpulse.
|
|
285
|
+
const toBodyX = body.x - x;
|
|
286
|
+
const toBodyZ = body.z - z;
|
|
287
|
+
const dirLen = deterministicKccSqrtLength(toBodyX, toBodyZ);
|
|
288
|
+
const dirX = dirLen === 0 ? (velocity.vx >= 0 ? 1 : -1) : toBodyX / dirLen;
|
|
289
|
+
const dirZ = dirLen === 0 ? 0 : toBodyZ / dirLen;
|
|
290
|
+
const speedIntoBody = Math.max(0, velocity.vx * dirX + velocity.vz * dirZ);
|
|
291
|
+
if (speedIntoBody === 0) {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const magnitude = Math.min(speedIntoBody * KCC_PUSH_MASS, maxPushImpulse);
|
|
295
|
+
impulses.push({
|
|
296
|
+
bodyId: body.id,
|
|
297
|
+
// Apply through the body's center of mass so the horizontal push slides the
|
|
298
|
+
// body rather than torquing it — the soft-step solver's livelier angular
|
|
299
|
+
// response otherwise lets an above-COM push tumble a light crate.
|
|
300
|
+
impulse: { x: dirX * magnitude, y: 0, z: dirZ * magnitude },
|
|
301
|
+
point: { x: body.x, y: body.y, z: body.z },
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return impulses;
|
|
305
|
+
}
|
|
306
|
+
function deterministicKccSqrtLength(dx, dz) {
|
|
307
|
+
const squared = dx * dx + dz * dz;
|
|
308
|
+
if (squared <= 0) {
|
|
309
|
+
return 0;
|
|
310
|
+
}
|
|
311
|
+
let estimate = 1;
|
|
312
|
+
let magnitude = squared;
|
|
313
|
+
while (magnitude >= 4) {
|
|
314
|
+
magnitude /= 4;
|
|
315
|
+
estimate *= 2;
|
|
316
|
+
}
|
|
317
|
+
if (magnitude > 1) {
|
|
318
|
+
estimate *= 2;
|
|
319
|
+
}
|
|
320
|
+
for (let iteration = 0; iteration < 8; iteration += 1) {
|
|
321
|
+
estimate = (estimate + squared / estimate) / 2;
|
|
322
|
+
}
|
|
323
|
+
return estimate;
|
|
324
|
+
}
|
|
325
|
+
function groundContactTopAt(physics, body, x, z, minimumTopY, maximumTopY) {
|
|
326
|
+
const q = body.orientation;
|
|
327
|
+
const shape = body.shape;
|
|
328
|
+
if (shape.type !== 'box' && shape.type !== 'sphere' && shape.type !== 'capsule') {
|
|
329
|
+
const y = maximumTopY + KCC_GROUND_CLEARANCE;
|
|
330
|
+
const distance = y - (minimumTopY - KCC_GROUND_CLEARANCE);
|
|
331
|
+
if (!Number.isFinite(y) || !Number.isFinite(distance)) {
|
|
332
|
+
throw new Error('KCC ground query extent is not finite');
|
|
333
|
+
}
|
|
334
|
+
if (distance <= 0)
|
|
335
|
+
return undefined;
|
|
336
|
+
return physics.raycast({ x: meters(x), y: meters(y), z: meters(z), dx: 0, dy: -1, dz: 0, maxDistance: meters(distance),
|
|
337
|
+
onlyBody: physics.resolveBody(body.id) })[0]?.point.y;
|
|
338
|
+
}
|
|
339
|
+
const ext = primitiveHalfExtent(shape, quatToMat3(q));
|
|
340
|
+
if (Math.abs(x - body.x) > ext.x || Math.abs(z - body.z) > ext.z) {
|
|
341
|
+
return undefined;
|
|
342
|
+
}
|
|
343
|
+
return body.y + ext.y;
|
|
344
|
+
}
|
|
345
|
+
const KCC_GROUND_CLEARANCE = 1e-4;
|
|
346
|
+
function kccSlopeClearance(physics, body, x, z, top, geometry, maximumRisePerUnit, neighbourTopAt) {
|
|
347
|
+
const step = geometry.radius + geometry.skinWidth;
|
|
348
|
+
const minimumTopY = top - step * maximumRisePerUnit;
|
|
349
|
+
const maximumTopY = top + step * maximumRisePerUnit;
|
|
350
|
+
const sampleTopAt = (sampleX, sampleZ) => {
|
|
351
|
+
const own = groundContactTopAt(physics, body, sampleX, sampleZ, minimumTopY, maximumTopY);
|
|
352
|
+
if (own !== undefined || neighbourTopAt === undefined) {
|
|
353
|
+
return own;
|
|
354
|
+
}
|
|
355
|
+
return continuousNeighbourTopAt(physics, body, x, z, sampleX, sampleZ, minimumTopY, maximumTopY, neighbourTopAt);
|
|
356
|
+
};
|
|
357
|
+
const axisGradient = (deltaX, deltaZ) => {
|
|
358
|
+
const ahead = sampleTopAt(x + deltaX, z + deltaZ);
|
|
359
|
+
const behind = sampleTopAt(x - deltaX, z - deltaZ);
|
|
360
|
+
const forward = ahead === undefined ? 0 : Math.abs(ahead - top) / step;
|
|
361
|
+
const backward = behind === undefined ? 0 : Math.abs(top - behind) / step;
|
|
362
|
+
return Math.max(forward, backward);
|
|
363
|
+
};
|
|
364
|
+
const gradientX = axisGradient(step, 0);
|
|
365
|
+
const gradientZ = axisGradient(0, step);
|
|
366
|
+
const gradientSq = gradientX * gradientX + gradientZ * gradientZ;
|
|
367
|
+
if (gradientSq <= 0) {
|
|
368
|
+
return 0;
|
|
369
|
+
}
|
|
370
|
+
// radius + skinWidth is the bottom-sphere radius physicsOverlapsKcc probes with.
|
|
371
|
+
return (geometry.radius + geometry.skinWidth) * (solverSqrt(1 + gradientSq) - 1);
|
|
372
|
+
}
|
|
373
|
+
const KCC_SEAM_CONTINUITY_TOLERANCE = 0.05;
|
|
374
|
+
const KCC_SEAM_EDGE_BISECTIONS = 8;
|
|
375
|
+
function continuousNeighbourTopAt(physics, body, x, z, sampleX, sampleZ, minimumTopY, maximumTopY, neighbourTopAt) {
|
|
376
|
+
// The centre is on the selected body (its top is what kccSlopeClearance was
|
|
377
|
+
// given) and the sample is off it, so the ray crosses a footprint edge.
|
|
378
|
+
let inside = 0;
|
|
379
|
+
let outside = 1;
|
|
380
|
+
for (let i = 0; i < KCC_SEAM_EDGE_BISECTIONS; i += 1) {
|
|
381
|
+
const mid = (inside + outside) / 2;
|
|
382
|
+
if (groundContactTopAt(physics, body, x + (sampleX - x) * mid, z + (sampleZ - z) * mid, minimumTopY, maximumTopY) === undefined) {
|
|
383
|
+
outside = mid;
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
inside = mid;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const innerTop = groundContactTopAt(physics, body, x + (sampleX - x) * inside, z + (sampleZ - z) * inside, minimumTopY, maximumTopY);
|
|
390
|
+
const outerTop = neighbourTopAt(x + (sampleX - x) * outside, z + (sampleZ - z) * outside);
|
|
391
|
+
if (innerTop === undefined || outerTop === undefined) {
|
|
392
|
+
return undefined;
|
|
393
|
+
}
|
|
394
|
+
if (Math.abs(innerTop - outerTop) > KCC_SEAM_CONTINUITY_TOLERANCE) {
|
|
395
|
+
return undefined;
|
|
396
|
+
}
|
|
397
|
+
return neighbourTopAt(sampleX, sampleZ);
|
|
398
|
+
}
|
|
399
|
+
function kccFeetOffset(geometry) {
|
|
400
|
+
// physicsOverlapsKcc probes with radius+skinWidth AND halfHeight+skinWidth,
|
|
401
|
+
// and counts exact touching as an overlap — at which point the slide reverts
|
|
402
|
+
// the whole horizontal move, so a capsule resting flush on terrain cannot
|
|
403
|
+
// walk. Rest one clearance above that probe instead of on the surface.
|
|
404
|
+
return geometry.halfHeight + geometry.radius + 2 * geometry.skinWidth + KCC_GROUND_CLEARANCE;
|
|
405
|
+
}
|
|
406
|
+
function staticGroundUnderKcc(physics, x, y, z, previousFeetY, geometry, ignoredBodyIds, snapDistance, maximumRisePerUnit) {
|
|
407
|
+
const feetY = y - kccFeetOffset(geometry);
|
|
408
|
+
// Gravity is unbounded (vy -= 1 per frame), so a fast fall can clear the whole
|
|
409
|
+
// snap band in one step. Accept a surface the feet CROSSED this frame, not
|
|
410
|
+
// just one they ended near — otherwise terrain tunnels at terminal velocity.
|
|
411
|
+
// Requiring the frame to START at or above the surface is what keeps a capsule
|
|
412
|
+
// walking beneath a floating static box from being yanked onto its roof.
|
|
413
|
+
const fallDistance = Math.max(0, previousFeetY - feetY);
|
|
414
|
+
const reach = snapDistance + fallDistance;
|
|
415
|
+
const probe = {
|
|
416
|
+
shape: { type: 'sphere', radius: meters(geometry.radius + reach) },
|
|
417
|
+
x: meters(x),
|
|
418
|
+
y: meters(feetY),
|
|
419
|
+
z: meters(z),
|
|
420
|
+
hitStatics: true,
|
|
421
|
+
hitDynamics: false,
|
|
422
|
+
hitKinematics: false,
|
|
423
|
+
};
|
|
424
|
+
const hitBodies = physics.overlap(probe)
|
|
425
|
+
.map((hit) => readKccPhysicsBody(physics, hit.bodyId))
|
|
426
|
+
.filter((body) => body.kind === 'static' && !ignoredBodyIds.has(body.id));
|
|
427
|
+
const maximumLift = (geometry.radius + geometry.skinWidth)
|
|
428
|
+
* (solverSqrt(1 + 2 * maximumRisePerUnit * maximumRisePerUnit) - 1);
|
|
429
|
+
const grounds = hitBodies
|
|
430
|
+
.map((body) => ({ body, top: groundContactTopAt(physics, body, x, z, feetY - snapDistance - maximumLift, previousFeetY + snapDistance) }))
|
|
431
|
+
.filter((candidate) => candidate.top !== undefined)
|
|
432
|
+
.map((candidate) => ({
|
|
433
|
+
...candidate,
|
|
434
|
+
// Where the selected body has no surface (a sample past its edge), read
|
|
435
|
+
// the highest neighbouring static surface instead — abutting terrain
|
|
436
|
+
// chunks share an edge, and the bottom sphere rides BOTH across a seam.
|
|
437
|
+
lift: kccSlopeClearance(physics, candidate.body, x, z, candidate.top, geometry, maximumRisePerUnit, (sampleX, sampleZ) => {
|
|
438
|
+
let neighbourTop;
|
|
439
|
+
for (const neighbour of hitBodies) {
|
|
440
|
+
if (neighbour.id === candidate.body.id) {
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const top = groundContactTopAt(physics, neighbour, sampleX, sampleZ, candidate.top - (geometry.radius + geometry.skinWidth) * maximumRisePerUnit, candidate.top + (geometry.radius + geometry.skinWidth) * maximumRisePerUnit);
|
|
444
|
+
if (top !== undefined) {
|
|
445
|
+
neighbourTop = neighbourTop === undefined ? top : Math.max(neighbourTop, top);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return neighbourTop;
|
|
449
|
+
}),
|
|
450
|
+
}))
|
|
451
|
+
// The band is measured from where the capsule actually STANDS, so a slope
|
|
452
|
+
// lift bigger than snapDistance cannot drop it out of its own ground the
|
|
453
|
+
// next frame and leave it cycling between grounded and falling.
|
|
454
|
+
.filter((candidate) => feetY <= candidate.top + candidate.lift + snapDistance
|
|
455
|
+
&& previousFeetY >= candidate.top - snapDistance)
|
|
456
|
+
.sort((left, right) => right.top - left.top || comparePhysicsBodyIds(left.body.id, right.body.id));
|
|
457
|
+
const ground = grounds[0];
|
|
458
|
+
if (ground === undefined) {
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
bodyId: `physics:${ground.body.id}`,
|
|
463
|
+
topY: ground.top + kccFeetOffset(geometry) + ground.lift,
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
function primitiveHalfExtent(shape, m) {
|
|
467
|
+
if (shape.type === 'sphere') {
|
|
468
|
+
return { x: shape.radius, y: shape.radius, z: shape.radius };
|
|
469
|
+
}
|
|
470
|
+
if (shape.type === 'box') {
|
|
471
|
+
return {
|
|
472
|
+
x: Math.abs(m[0]) * shape.halfX + Math.abs(m[1]) * shape.halfY + Math.abs(m[2]) * shape.halfZ,
|
|
473
|
+
y: Math.abs(m[3]) * shape.halfX + Math.abs(m[4]) * shape.halfY + Math.abs(m[5]) * shape.halfZ,
|
|
474
|
+
z: Math.abs(m[6]) * shape.halfX + Math.abs(m[7]) * shape.halfY + Math.abs(m[8]) * shape.halfZ,
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
// capsule: segment along local Y, spherical caps radius r
|
|
478
|
+
return {
|
|
479
|
+
x: Math.abs(m[1]) * shape.halfHeight + shape.radius,
|
|
480
|
+
y: Math.abs(m[4]) * shape.halfHeight + shape.radius,
|
|
481
|
+
z: Math.abs(m[7]) * shape.halfHeight + shape.radius,
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
function dynamicGroundUnderKcc(physics, x, y, z, geometry, ignoredBodyIds) {
|
|
485
|
+
const feetY = y - kccFeetOffset(geometry);
|
|
486
|
+
const probe = {
|
|
487
|
+
shape: { type: 'sphere', radius: meters(geometry.radius) },
|
|
488
|
+
x: meters(x),
|
|
489
|
+
y: meters(feetY),
|
|
490
|
+
z: meters(z),
|
|
491
|
+
hitStatics: false,
|
|
492
|
+
hitDynamics: true,
|
|
493
|
+
hitKinematics: false,
|
|
494
|
+
};
|
|
495
|
+
// The KCC must stand OVER the body's horizontal footprint, not beside it —
|
|
496
|
+
// otherwise walking into a crate's side (a feet-level overlap with a body whose
|
|
497
|
+
// top towers above the feet) would vault the capsule onto the top.
|
|
498
|
+
const grounds = physics.overlap(probe)
|
|
499
|
+
.map((hit) => readKccPhysicsBody(physics, hit.bodyId))
|
|
500
|
+
.filter((body) => body.kind === 'dynamic' && !ignoredBodyIds.has(body.id))
|
|
501
|
+
.map((body) => ({ body, top: groundContactTopAt(physics, body, x, z, feetY - geometry.radius, feetY + geometry.radius) }))
|
|
502
|
+
.filter((candidate) => candidate.top !== undefined)
|
|
503
|
+
.sort((left, right) => right.top - left.top || comparePhysicsBodyIds(left.body.id, right.body.id));
|
|
504
|
+
const ground = grounds[0];
|
|
505
|
+
if (ground === undefined) {
|
|
506
|
+
return undefined;
|
|
507
|
+
}
|
|
508
|
+
const fixedDt = physics.info().fixedDt;
|
|
509
|
+
return {
|
|
510
|
+
bodyId: `physics:${ground.body.id}`,
|
|
511
|
+
topY: ground.top + kccFeetOffset(geometry),
|
|
512
|
+
deltaX: ground.body.vx * fixedDt,
|
|
513
|
+
deltaZ: ground.body.vz * fixedDt,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
function applyProcessors(body, input, frame, processors) {
|
|
517
|
+
const ids = new Set();
|
|
518
|
+
let next = body;
|
|
519
|
+
for (const processor of [...processors].sort((left, right) => left.order - right.order || compareStrings(left.id, right.id))) {
|
|
520
|
+
if (ids.has(processor.id)) {
|
|
521
|
+
throw new Error(`Duplicate deterministic KCC processor ${processor.id}`);
|
|
522
|
+
}
|
|
523
|
+
ids.add(processor.id);
|
|
524
|
+
const stateBefore = deterministicKccProcessorStateChecksum(processor);
|
|
525
|
+
next = processor.process(next, input, frame);
|
|
526
|
+
const stateAfter = deterministicKccProcessorStateChecksum(processor);
|
|
527
|
+
if (stateBefore !== stateAfter) {
|
|
528
|
+
throw new Error(`Nondeterministic KCC processor state mutation ${processor.id}`);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return next;
|
|
532
|
+
}
|
|
533
|
+
function combineExternalKccImpulses(impulses) {
|
|
534
|
+
const ids = new Set();
|
|
535
|
+
let vx = 0;
|
|
536
|
+
let vy = 0;
|
|
537
|
+
let vz = 0;
|
|
538
|
+
for (const impulse of [...impulses].sort((left, right) => compareStrings(left.id, right.id))) {
|
|
539
|
+
if (ids.has(impulse.id)) {
|
|
540
|
+
throw new Error(`Duplicate deterministic KCC external impulse ${impulse.id}`);
|
|
541
|
+
}
|
|
542
|
+
ids.add(impulse.id);
|
|
543
|
+
if (!Number.isInteger(impulse.vx) || !Number.isInteger(impulse.vy) || !Number.isInteger(impulse.vz)) {
|
|
544
|
+
throw new Error(`Deterministic KCC external impulse ${impulse.id} must use integer velocity deltas`);
|
|
545
|
+
}
|
|
546
|
+
vx += impulse.vx;
|
|
547
|
+
vy += impulse.vy;
|
|
548
|
+
vz += impulse.vz;
|
|
549
|
+
}
|
|
550
|
+
return { vx, vy, vz, applied: impulses.length > 0 };
|
|
551
|
+
}
|
|
552
|
+
function deterministicKccProcessorStateChecksum(processor) {
|
|
553
|
+
const record = processor;
|
|
554
|
+
const entries = stableKeys(record)
|
|
555
|
+
.filter((key) => key !== 'id' && key !== 'order' && key !== 'process')
|
|
556
|
+
.map((key) => {
|
|
557
|
+
const value = record[key];
|
|
558
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
559
|
+
return { key, value };
|
|
560
|
+
}
|
|
561
|
+
throw new Error(`Nondeterministic KCC processor state ${processor.id}`);
|
|
562
|
+
});
|
|
563
|
+
return defaultChecksum(entries);
|
|
564
|
+
}
|
|
565
|
+
function sortKccCollisionFilters(filters) {
|
|
566
|
+
const ids = new Set();
|
|
567
|
+
const ordered = [...filters].sort((left, right) => left.order - right.order || compareStrings(left.id, right.id));
|
|
568
|
+
for (const filter of ordered) {
|
|
569
|
+
if (ids.has(filter.id)) {
|
|
570
|
+
throw new Error(`Duplicate deterministic KCC collision filter ${filter.id}`);
|
|
571
|
+
}
|
|
572
|
+
ids.add(filter.id);
|
|
573
|
+
}
|
|
574
|
+
return ordered;
|
|
575
|
+
}
|
|
576
|
+
function deterministicKccCollisionFiltersStateChecksum(filters) {
|
|
577
|
+
return defaultChecksum(sortKccCollisionFilters(filters).map((filter) => deterministicKccCollisionFilterStateChecksum(filter)));
|
|
578
|
+
}
|
|
579
|
+
function deterministicKccCollisionFilterStateChecksum(filter) {
|
|
580
|
+
const record = filter;
|
|
581
|
+
const entries = stableKeys(record)
|
|
582
|
+
.filter((key) => key !== 'id' && key !== 'order' && key !== 'shouldCollide')
|
|
583
|
+
.map((key) => {
|
|
584
|
+
const value = record[key];
|
|
585
|
+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
586
|
+
return { key, value };
|
|
587
|
+
}
|
|
588
|
+
throw new Error(`Nondeterministic KCC collision filter state ${filter.id}`);
|
|
589
|
+
});
|
|
590
|
+
return defaultChecksum(entries);
|
|
591
|
+
}
|
|
592
|
+
function assertKccCollisionFiltersUnchanged(filters, stateBefore) {
|
|
593
|
+
const stateAfter = deterministicKccCollisionFiltersStateChecksum(filters);
|
|
594
|
+
if (stateBefore !== stateAfter) {
|
|
595
|
+
throw new Error('Nondeterministic KCC collision filter state mutation');
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
function createKccCollisionCandidate(bodyId, body, context, attemptedX, attemptedY, attemptedZ) {
|
|
599
|
+
const normal = estimateKccCollisionNormal(context.previous, attemptedX, attemptedY, attemptedZ);
|
|
600
|
+
return {
|
|
601
|
+
bodyId,
|
|
602
|
+
frame: context.frame,
|
|
603
|
+
sample: context.sample,
|
|
604
|
+
attemptedX,
|
|
605
|
+
attemptedY,
|
|
606
|
+
attemptedZ,
|
|
607
|
+
previousX: context.previous.x,
|
|
608
|
+
previousY: context.previous.y,
|
|
609
|
+
previousZ: context.previous.z,
|
|
610
|
+
normalX: normal.x,
|
|
611
|
+
normalY: normal.y,
|
|
612
|
+
normalZ: normal.z,
|
|
613
|
+
layer: body.layer,
|
|
614
|
+
mask: body.mask,
|
|
615
|
+
swept: context.swept,
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
function estimateKccCollisionNormal(previous, attemptedX, attemptedY, attemptedZ) {
|
|
619
|
+
const dx = attemptedX - previous.x;
|
|
620
|
+
const dy = attemptedY - previous.y;
|
|
621
|
+
const dz = attemptedZ - previous.z;
|
|
622
|
+
const absX = Math.abs(dx);
|
|
623
|
+
const absY = Math.abs(dy);
|
|
624
|
+
const absZ = Math.abs(dz);
|
|
625
|
+
if (absX >= absZ && absX > 0) {
|
|
626
|
+
return { x: dx >= 0 ? -1 : 1, y: 0, z: 0 };
|
|
627
|
+
}
|
|
628
|
+
if (absZ > 0) {
|
|
629
|
+
return { x: 0, y: 0, z: dz >= 0 ? -1 : 1 };
|
|
630
|
+
}
|
|
631
|
+
if (absY > 0) {
|
|
632
|
+
return { x: 0, y: dy >= 0 ? -1 : 1, z: 0 };
|
|
633
|
+
}
|
|
634
|
+
return { x: 0, y: 0, z: -1 };
|
|
635
|
+
}
|
|
636
|
+
function resolvePhysicsSlide3D(physics, previous, candidate, ignoredBodyIds, frame, collisionFilters) {
|
|
637
|
+
const filterStateBefore = deterministicKccCollisionFiltersStateChecksum(collisionFilters);
|
|
638
|
+
let collisionFilterCalls = 0;
|
|
639
|
+
let collisionFilterRejectedHits = 0;
|
|
640
|
+
const fullOverlaps = physicsOverlapsKcc(physics, candidate.x, candidate.y, candidate.z, {
|
|
641
|
+
ignoredBodyIds,
|
|
642
|
+
previous,
|
|
643
|
+
frame,
|
|
644
|
+
swept: false,
|
|
645
|
+
sample: 0,
|
|
646
|
+
collisionFilters,
|
|
647
|
+
});
|
|
648
|
+
collisionFilterCalls += fullOverlaps.collisionFilterCalls;
|
|
649
|
+
collisionFilterRejectedHits += fullOverlaps.collisionFilterRejectedHits;
|
|
650
|
+
if (fullOverlaps.length === 0) {
|
|
651
|
+
const sweep = sweepPhysicsKcc3D(physics, previous, candidate, ignoredBodyIds, frame, collisionFilters);
|
|
652
|
+
assertKccCollisionFiltersUnchanged(collisionFilters, filterStateBefore);
|
|
653
|
+
if (sweep.corrected) {
|
|
654
|
+
return {
|
|
655
|
+
...sweep,
|
|
656
|
+
collisionFilterCalls: collisionFilterCalls + sweep.collisionFilterCalls,
|
|
657
|
+
collisionFilterRejectedHits: collisionFilterRejectedHits + sweep.collisionFilterRejectedHits,
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
return {
|
|
661
|
+
...candidate,
|
|
662
|
+
corrected: false,
|
|
663
|
+
slid: false,
|
|
664
|
+
swept: false,
|
|
665
|
+
correctionCount: 0,
|
|
666
|
+
queryCount: sweep.queryCount + 1,
|
|
667
|
+
collisionFilterCalls: collisionFilterCalls + sweep.collisionFilterCalls,
|
|
668
|
+
collisionFilterRejectedHits: collisionFilterRejectedHits + sweep.collisionFilterRejectedHits,
|
|
669
|
+
};
|
|
670
|
+
}
|
|
671
|
+
const xOnly = { x: candidate.x, z: previous.z, vx: candidate.vx, vz: 0 };
|
|
672
|
+
const zOnly = { x: previous.x, z: candidate.z, vx: 0, vz: candidate.vz };
|
|
673
|
+
const xOverlaps = physicsOverlapsKcc(physics, xOnly.x, candidate.y, xOnly.z, {
|
|
674
|
+
ignoredBodyIds,
|
|
675
|
+
previous,
|
|
676
|
+
frame,
|
|
677
|
+
swept: false,
|
|
678
|
+
sample: 0,
|
|
679
|
+
collisionFilters,
|
|
680
|
+
});
|
|
681
|
+
const zOverlaps = physicsOverlapsKcc(physics, zOnly.x, candidate.y, zOnly.z, {
|
|
682
|
+
ignoredBodyIds,
|
|
683
|
+
previous,
|
|
684
|
+
frame,
|
|
685
|
+
swept: false,
|
|
686
|
+
sample: 0,
|
|
687
|
+
collisionFilters,
|
|
688
|
+
});
|
|
689
|
+
collisionFilterCalls += xOverlaps.collisionFilterCalls + zOverlaps.collisionFilterCalls;
|
|
690
|
+
collisionFilterRejectedHits += xOverlaps.collisionFilterRejectedHits + zOverlaps.collisionFilterRejectedHits;
|
|
691
|
+
assertKccCollisionFiltersUnchanged(collisionFilters, filterStateBefore);
|
|
692
|
+
if (xOverlaps.length === 0 && zOverlaps.length > 0) {
|
|
693
|
+
return { ...xOnly, corrected: true, slid: true, swept: false, correctionCount: fullOverlaps.length, queryCount: 3, collisionFilterCalls, collisionFilterRejectedHits };
|
|
694
|
+
}
|
|
695
|
+
if (zOverlaps.length === 0 && xOverlaps.length > 0) {
|
|
696
|
+
return { ...zOnly, corrected: true, slid: true, swept: false, correctionCount: fullOverlaps.length, queryCount: 3, collisionFilterCalls, collisionFilterRejectedHits };
|
|
697
|
+
}
|
|
698
|
+
if (xOverlaps.length === 0 && zOverlaps.length === 0) {
|
|
699
|
+
const preferX = Math.abs(candidate.vx) >= Math.abs(candidate.vz);
|
|
700
|
+
return {
|
|
701
|
+
...(preferX ? xOnly : zOnly),
|
|
702
|
+
corrected: true,
|
|
703
|
+
slid: true,
|
|
704
|
+
swept: false,
|
|
705
|
+
correctionCount: fullOverlaps.length,
|
|
706
|
+
queryCount: 3,
|
|
707
|
+
collisionFilterCalls,
|
|
708
|
+
collisionFilterRejectedHits,
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
return {
|
|
712
|
+
x: previous.x,
|
|
713
|
+
z: previous.z,
|
|
714
|
+
vx: 0,
|
|
715
|
+
vz: 0,
|
|
716
|
+
corrected: true,
|
|
717
|
+
slid: false,
|
|
718
|
+
swept: false,
|
|
719
|
+
correctionCount: fullOverlaps.length,
|
|
720
|
+
queryCount: 3,
|
|
721
|
+
collisionFilterCalls,
|
|
722
|
+
collisionFilterRejectedHits,
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
function sweepPhysicsKcc3D(physics, previous, candidate, ignoredBodyIds, frame, collisionFilters) {
|
|
726
|
+
const deltaX = candidate.x - previous.x;
|
|
727
|
+
const deltaZ = candidate.z - previous.z;
|
|
728
|
+
const samples = Math.max(1, Math.ceil(Math.max(Math.abs(deltaX), Math.abs(deltaZ)) * 2));
|
|
729
|
+
const filterStateBefore = deterministicKccCollisionFiltersStateChecksum(collisionFilters);
|
|
730
|
+
let queryCount = 0;
|
|
731
|
+
let collisionFilterCalls = 0;
|
|
732
|
+
let collisionFilterRejectedHits = 0;
|
|
733
|
+
let lastSafeX = previous.x;
|
|
734
|
+
let lastSafeZ = previous.z;
|
|
735
|
+
for (let sample = 1; sample <= samples; sample += 1) {
|
|
736
|
+
const fraction = sample / samples;
|
|
737
|
+
const sampleX = previous.x + deltaX * fraction;
|
|
738
|
+
const sampleZ = previous.z + deltaZ * fraction;
|
|
739
|
+
const overlaps = physicsOverlapsKcc(physics, sampleX, candidate.y, sampleZ, {
|
|
740
|
+
ignoredBodyIds,
|
|
741
|
+
previous,
|
|
742
|
+
frame,
|
|
743
|
+
swept: true,
|
|
744
|
+
sample,
|
|
745
|
+
collisionFilters,
|
|
746
|
+
});
|
|
747
|
+
queryCount += 1;
|
|
748
|
+
collisionFilterCalls += overlaps.collisionFilterCalls;
|
|
749
|
+
collisionFilterRejectedHits += overlaps.collisionFilterRejectedHits;
|
|
750
|
+
if (overlaps.length > 0) {
|
|
751
|
+
const xOnlyOverlaps = physicsOverlapsKcc(physics, sampleX, candidate.y, lastSafeZ, {
|
|
752
|
+
ignoredBodyIds,
|
|
753
|
+
previous,
|
|
754
|
+
frame,
|
|
755
|
+
swept: true,
|
|
756
|
+
sample,
|
|
757
|
+
collisionFilters,
|
|
758
|
+
});
|
|
759
|
+
const zOnlyOverlaps = physicsOverlapsKcc(physics, lastSafeX, candidate.y, sampleZ, {
|
|
760
|
+
ignoredBodyIds,
|
|
761
|
+
previous,
|
|
762
|
+
frame,
|
|
763
|
+
swept: true,
|
|
764
|
+
sample,
|
|
765
|
+
collisionFilters,
|
|
766
|
+
});
|
|
767
|
+
queryCount += 2;
|
|
768
|
+
collisionFilterCalls += xOnlyOverlaps.collisionFilterCalls + zOnlyOverlaps.collisionFilterCalls;
|
|
769
|
+
collisionFilterRejectedHits += xOnlyOverlaps.collisionFilterRejectedHits + zOnlyOverlaps.collisionFilterRejectedHits;
|
|
770
|
+
assertKccCollisionFiltersUnchanged(collisionFilters, filterStateBefore);
|
|
771
|
+
if (xOnlyOverlaps.length === 0 && zOnlyOverlaps.length > 0) {
|
|
772
|
+
return {
|
|
773
|
+
x: sampleX,
|
|
774
|
+
z: lastSafeZ,
|
|
775
|
+
vx: candidate.vx,
|
|
776
|
+
vz: 0,
|
|
777
|
+
corrected: true,
|
|
778
|
+
slid: true,
|
|
779
|
+
swept: true,
|
|
780
|
+
correctionCount: overlaps.length,
|
|
781
|
+
queryCount,
|
|
782
|
+
collisionFilterCalls,
|
|
783
|
+
collisionFilterRejectedHits,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
if (zOnlyOverlaps.length === 0 && xOnlyOverlaps.length > 0) {
|
|
787
|
+
return {
|
|
788
|
+
x: lastSafeX,
|
|
789
|
+
z: sampleZ,
|
|
790
|
+
vx: 0,
|
|
791
|
+
vz: candidate.vz,
|
|
792
|
+
corrected: true,
|
|
793
|
+
slid: true,
|
|
794
|
+
swept: true,
|
|
795
|
+
correctionCount: overlaps.length,
|
|
796
|
+
queryCount,
|
|
797
|
+
collisionFilterCalls,
|
|
798
|
+
collisionFilterRejectedHits,
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
return {
|
|
802
|
+
x: lastSafeX,
|
|
803
|
+
z: lastSafeZ,
|
|
804
|
+
vx: 0,
|
|
805
|
+
vz: 0,
|
|
806
|
+
corrected: true,
|
|
807
|
+
slid: false,
|
|
808
|
+
swept: true,
|
|
809
|
+
correctionCount: overlaps.length,
|
|
810
|
+
queryCount,
|
|
811
|
+
collisionFilterCalls,
|
|
812
|
+
collisionFilterRejectedHits,
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
lastSafeX = sampleX;
|
|
816
|
+
lastSafeZ = sampleZ;
|
|
817
|
+
}
|
|
818
|
+
assertKccCollisionFiltersUnchanged(collisionFilters, filterStateBefore);
|
|
819
|
+
return { ...candidate, corrected: false, slid: false, swept: false, correctionCount: 0, queryCount, collisionFilterCalls, collisionFilterRejectedHits };
|
|
820
|
+
}
|
|
821
|
+
function physicsOverlapsKcc(physics, x, y, z, context) {
|
|
822
|
+
const geometry = normalizeKccGeometry3D(context.previous);
|
|
823
|
+
const query = {
|
|
824
|
+
shape: {
|
|
825
|
+
type: 'capsule',
|
|
826
|
+
radius: meters(geometry.radius + geometry.skinWidth),
|
|
827
|
+
halfHeight: meters(geometry.halfHeight + geometry.skinWidth),
|
|
828
|
+
},
|
|
829
|
+
x: meters(x),
|
|
830
|
+
y: meters(y),
|
|
831
|
+
z: meters(z),
|
|
832
|
+
layerMask: 0b1111,
|
|
833
|
+
hitTriggers: false,
|
|
834
|
+
hitSensors: false,
|
|
835
|
+
};
|
|
836
|
+
let collisionFilterCalls = 0;
|
|
837
|
+
let collisionFilterRejectedHits = 0;
|
|
838
|
+
const filters = sortKccCollisionFilters(context.collisionFilters);
|
|
839
|
+
const overlaps = physics.overlap(query).map((hit) => hit.bodyId).filter((bodyId) => {
|
|
840
|
+
if (context.ignoredBodyIds.has(bodyId)) {
|
|
841
|
+
return false;
|
|
842
|
+
}
|
|
843
|
+
const body = readKccPhysicsBody(physics, bodyId);
|
|
844
|
+
const hit = createKccCollisionCandidate(bodyId, body, context, x, y, z);
|
|
845
|
+
for (const filter of filters) {
|
|
846
|
+
collisionFilterCalls += 1;
|
|
847
|
+
if (!filter.shouldCollide(hit)) {
|
|
848
|
+
collisionFilterRejectedHits += 1;
|
|
849
|
+
return false;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
return true;
|
|
853
|
+
});
|
|
854
|
+
return Object.assign(overlaps, { collisionFilterCalls, collisionFilterRejectedHits });
|
|
855
|
+
}
|
|
856
|
+
function normalizeKccGeometry3D(body) {
|
|
857
|
+
const radius = body.radius ?? 1;
|
|
858
|
+
const halfHeight = body.halfHeight ?? 2;
|
|
859
|
+
const skinWidth = body.skinWidth ?? 0;
|
|
860
|
+
if (!Number.isFinite(radius) || radius <= 0) {
|
|
861
|
+
throw new Error('KCC radius must be a finite positive number');
|
|
862
|
+
}
|
|
863
|
+
if (!Number.isFinite(halfHeight) || halfHeight <= 0) {
|
|
864
|
+
throw new Error('KCC halfHeight must be a finite positive number');
|
|
865
|
+
}
|
|
866
|
+
if (!Number.isFinite(skinWidth) || skinWidth < 0) {
|
|
867
|
+
throw new Error('KCC skinWidth must be a finite non-negative number');
|
|
868
|
+
}
|
|
869
|
+
return { radius, halfHeight, skinWidth };
|
|
870
|
+
}
|
|
871
|
+
function clampBodyToBounds(body, world) {
|
|
872
|
+
const events = [];
|
|
873
|
+
let corrections = 0;
|
|
874
|
+
let { x, y, z, vx, vy, vz, grounded } = body;
|
|
875
|
+
if (x < world.minX || x > world.maxX) {
|
|
876
|
+
x = clamp(x, world.minX, world.maxX);
|
|
877
|
+
vx = 0;
|
|
878
|
+
corrections += 1;
|
|
879
|
+
events.push('wall-slide');
|
|
880
|
+
}
|
|
881
|
+
if (z < world.minZ || z > world.maxZ) {
|
|
882
|
+
z = clamp(z, world.minZ, world.maxZ);
|
|
883
|
+
vz = 0;
|
|
884
|
+
corrections += 1;
|
|
885
|
+
events.push('ledge');
|
|
886
|
+
}
|
|
887
|
+
if (y <= world.minY) {
|
|
888
|
+
y = world.minY;
|
|
889
|
+
vy = 0;
|
|
890
|
+
grounded = true;
|
|
891
|
+
}
|
|
892
|
+
if (y > world.maxY) {
|
|
893
|
+
y = world.maxY;
|
|
894
|
+
vy = 0;
|
|
895
|
+
corrections += 1;
|
|
896
|
+
events.push('ceiling');
|
|
897
|
+
}
|
|
898
|
+
return { body: { ...body, x, y, z, vx, vy, vz, grounded }, corrected: corrections > 0, corrections, events };
|
|
899
|
+
}
|
|
900
|
+
function slopeHeightAt(slopes, x, z, maxSlopeRisePerUnit) {
|
|
901
|
+
for (const slope of [...slopes].sort((left, right) => compareStrings(left.id, right.id))) {
|
|
902
|
+
if (x >= slope.minX && x <= slope.maxX && z >= slope.minZ && z <= slope.maxZ) {
|
|
903
|
+
const risePerUnitZ = slope.risePerUnitZ ?? 0;
|
|
904
|
+
if (Math.max(Math.abs(slope.risePerUnitX), Math.abs(risePerUnitZ)) > maxSlopeRisePerUnit) {
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
return slope.baseY + ((x - slope.minX) * slope.risePerUnitX) + ((z - slope.minZ) * risePerUnitZ);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
return undefined;
|
|
911
|
+
}
|
|
912
|
+
function findStepAt(steps, x, z) {
|
|
913
|
+
return [...steps]
|
|
914
|
+
.sort((left, right) => compareStrings(left.id, right.id))
|
|
915
|
+
.find((step) => x >= step.minX && x <= step.maxX && z >= step.minZ && z <= step.maxZ);
|
|
916
|
+
}
|
|
917
|
+
function clamp(value, min, max) {
|
|
918
|
+
return Math.max(min, Math.min(max, value));
|
|
919
|
+
}
|
|
920
|
+
function stableKeys(record) {
|
|
921
|
+
return Reflect.ownKeys(record)
|
|
922
|
+
.filter((key) => typeof key === 'string')
|
|
923
|
+
.sort(compareStrings);
|
|
924
|
+
}
|
|
925
|
+
function compareStrings(left, right) {
|
|
926
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
927
|
+
}
|
|
928
|
+
function solverSqrt(value) {
|
|
929
|
+
if (value <= 0) {
|
|
930
|
+
return 0;
|
|
931
|
+
}
|
|
932
|
+
// Non-finite guard: `while (magnitude >= 4) magnitude /= 4` never terminates for
|
|
933
|
+
// Infinity (Inf/4 === Inf). Returning value keeps sqrt(Inf) = Inf / sqrt(NaN) = NaN
|
|
934
|
+
// without spinning, so a diverged body surfaces as a bad checksum, not a hang.
|
|
935
|
+
if (!Number.isFinite(value)) {
|
|
936
|
+
return value;
|
|
937
|
+
}
|
|
938
|
+
let estimate = 1;
|
|
939
|
+
let magnitude = value;
|
|
940
|
+
while (magnitude >= 4) {
|
|
941
|
+
magnitude /= 4;
|
|
942
|
+
estimate *= 2;
|
|
943
|
+
}
|
|
944
|
+
if (magnitude > 1) {
|
|
945
|
+
estimate *= 2;
|
|
946
|
+
}
|
|
947
|
+
for (let iteration = 0; iteration < 8; iteration += 1) {
|
|
948
|
+
estimate = (estimate + value / estimate) / 2;
|
|
949
|
+
}
|
|
950
|
+
return estimate;
|
|
951
|
+
}
|
|
952
|
+
function quatToMat3(q) {
|
|
953
|
+
const { x, y, z, w } = q;
|
|
954
|
+
return [
|
|
955
|
+
1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y),
|
|
956
|
+
2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x),
|
|
957
|
+
2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y),
|
|
958
|
+
];
|
|
959
|
+
}
|
|
960
|
+
export function stepDeterministicKccRevision1_3D(body, input, world, options, gravityPerFrame, checksumMode) {
|
|
961
|
+
const result = stepDeterministicKccRevision1Raw3D(body, input, world, options, gravityPerFrame, checksumMode);
|
|
962
|
+
if (checksumMode === 'disabled') {
|
|
963
|
+
const { checksum: _checksum, ...base } = result;
|
|
964
|
+
return { ...base, hasChecksum: false, checksumMode: 'disabled' };
|
|
965
|
+
}
|
|
966
|
+
if (result.checksum === undefined) {
|
|
967
|
+
throw new Error('KCC required checksum is absent');
|
|
968
|
+
}
|
|
969
|
+
if (options.checksumMode === 'required') {
|
|
970
|
+
return { ...result, checksum: result.checksum, hasChecksum: true, checksumMode: 'required' };
|
|
971
|
+
}
|
|
972
|
+
return { ...result, checksum: result.checksum };
|
|
973
|
+
}
|