@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.
Files changed (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -0,0 +1,1100 @@
1
+ /**
2
+ * Lag Compensation and Historical Queries (K09 capability).
3
+ *
4
+ * Implements querying exact historical core state including topology, collider filters,
5
+ * pose, and geometry generation, scene rewinding (discrete and sub-tick interpolated),
6
+ * and historical spatial queries (raycast, overlap, shapecast) routed through the compiled
7
+ * C++ core in physics-extended.wasm.
8
+ *
9
+ * Determinism guarantees:
10
+ * - Double precision arithmetic only.
11
+ * - Exact bit-identical parity between native C++ and WASM peers.
12
+ * - Strict index-ordered container traversal.
13
+ * - Fail-closed: Throws explicit error if extended WASM core is unavailable;
14
+ * no JS-based fallback logic that could desync peers.
15
+ */
16
+
17
+ import {
18
+ ensureExtendedInstance,
19
+ extendedPhysicsReady,
20
+ } from './extended-wasm-instance.mjs';
21
+ import {
22
+ physicsExtendedWasmModuleBase64,
23
+ } from './physics-extended-wasm-module.generated.mjs';
24
+
25
+ export { ensureExtendedInstance, extendedPhysicsReady };
26
+
27
+ // ---------------------------------------------------------------------------
28
+ // Constants & ABI Layout
29
+ // ---------------------------------------------------------------------------
30
+
31
+ export const LAG_COMPENSATION_ABI_VERSION = 1;
32
+ export const LAG_COMPENSATION_BODY_STRIDE = 48;
33
+ export const LAG_COMPENSATION_HEADER_STRIDE = 8;
34
+ export const LAG_COMPENSATION_QUERY_STRIDE = 24;
35
+ export const LAG_COMPENSATION_RESULT_STRIDE = 12;
36
+ export const LAG_COMPENSATION_MAX_FRAMES = 128;
37
+ export const LAG_COMPENSATION_MAX_BODIES = 256;
38
+ export const LAG_COMPENSATION_MAX_RESULTS = 64;
39
+ export const LAG_COMPENSATION_INPUT_BODY_STRIDE = 44;
40
+
41
+ export const LagCompStatus = Object.freeze({
42
+ Success: 0,
43
+ InvalidTick: -1,
44
+ TickOutOfRange: -2,
45
+ BodyNotFound: -3,
46
+ BufferOverflow: -4,
47
+ InvalidInput: -5,
48
+ NoHit: -6,
49
+ });
50
+
51
+ export const ShapeKind = Object.freeze({
52
+ Box: 0,
53
+ Sphere: 1,
54
+ Capsule: 2,
55
+ Hull: 3,
56
+ Mesh: 4,
57
+ Compound: 5,
58
+ Heightfield: 6,
59
+ Cylinder: 7,
60
+ Cone: 8,
61
+ TaperedCapsule: 9,
62
+ });
63
+
64
+ export const wasmEntryPointsUsed = Object.freeze([
65
+ 'rundot_kinetix_ext_lag_compensation_abi_version',
66
+ 'rundot_kinetix_ext_lag_compensation_body_stride',
67
+ 'rundot_kinetix_ext_lag_compensation_frame_count',
68
+ 'rundot_kinetix_ext_lag_compensation_has_tick',
69
+ 'rundot_kinetix_ext_lag_compensation_header_stride',
70
+ 'rundot_kinetix_ext_lag_compensation_max_bodies',
71
+ 'rundot_kinetix_ext_lag_compensation_max_frames',
72
+ 'rundot_kinetix_ext_lag_compensation_max_results',
73
+ 'rundot_kinetix_ext_lag_compensation_newest_tick',
74
+ 'rundot_kinetix_ext_lag_compensation_oldest_tick',
75
+ 'rundot_kinetix_ext_lag_compensation_overlap',
76
+ 'rundot_kinetix_ext_lag_compensation_query_body',
77
+ 'rundot_kinetix_ext_lag_compensation_query_body_by_index',
78
+ 'rundot_kinetix_ext_lag_compensation_query_filter',
79
+ 'rundot_kinetix_ext_lag_compensation_query_geometry',
80
+ 'rundot_kinetix_ext_lag_compensation_query_interpolated_pose',
81
+ 'rundot_kinetix_ext_lag_compensation_query_pose',
82
+ 'rundot_kinetix_ext_lag_compensation_query_stride',
83
+ 'rundot_kinetix_ext_lag_compensation_query_topology',
84
+ 'rundot_kinetix_ext_lag_compensation_raycast',
85
+ 'rundot_kinetix_ext_lag_compensation_record_frame',
86
+ 'rundot_kinetix_ext_lag_compensation_reset',
87
+ 'rundot_kinetix_ext_lag_compensation_result_stride',
88
+ 'rundot_kinetix_ext_lag_compensation_rewind_scene',
89
+ 'rundot_kinetix_ext_lag_compensation_rewind_scene_interpolated',
90
+ 'rundot_kinetix_ext_lag_compensation_shapecast',
91
+ 'rundot_kinetix_ext_lag_compensation_size',
92
+ 'rundot_kinetix_ext_lag_compensation_stride',
93
+ ]);
94
+
95
+ // ---------------------------------------------------------------------------
96
+ // WASM Instance Management
97
+ // ---------------------------------------------------------------------------
98
+
99
+ let defaultExtendedInstance = null;
100
+
101
+ export function setExtendedInstance(instance) {
102
+ defaultExtendedInstance = instance;
103
+ }
104
+
105
+ export async function initLagCompensation() {
106
+ if (defaultExtendedInstance !== null) return defaultExtendedInstance;
107
+ await extendedPhysicsReady();
108
+ defaultExtendedInstance = await ensureExtendedInstance();
109
+ return defaultExtendedInstance;
110
+ }
111
+
112
+ function decodeBase64(value) {
113
+ if (typeof atob === 'function') {
114
+ const decoded = atob(value);
115
+ const bytes = new Uint8Array(decoded.length);
116
+ for (let i = 0; i < decoded.length; i += 1) bytes[i] = decoded.charCodeAt(i);
117
+ return bytes;
118
+ }
119
+ if (typeof Buffer !== 'undefined') return Uint8Array.from(Buffer.from(value, 'base64'));
120
+ throw new Error('KINETIX_EXTENDED_PHYSICS_BASE64_UNAVAILABLE: Base64 decoding is unavailable in this environment');
121
+ }
122
+
123
+ export function requireExtendedInstance(instance = null) {
124
+ if (instance !== null) return instance;
125
+ if (defaultExtendedInstance !== null) return defaultExtendedInstance;
126
+ try {
127
+ const bytes = decodeBase64(physicsExtendedWasmModuleBase64);
128
+ const module = new WebAssembly.Module(bytes);
129
+ const inst = new WebAssembly.Instance(module, {});
130
+ inst.exports._initialize?.();
131
+ defaultExtendedInstance = inst;
132
+ return defaultExtendedInstance;
133
+ } catch (error) {
134
+ const detail = error instanceof Error ? error.message : String(error);
135
+ throw new Error(
136
+ `KINETIX_EXTENDED_PHYSICS_NOT_INITIALIZED: Extended WASM physics instance is not initialized (${detail}). Await extendedPhysicsReady() or initLagCompensation() before calling lag compensation.`,
137
+ { cause: error }
138
+ );
139
+ }
140
+ }
141
+
142
+ // ---------------------------------------------------------------------------
143
+ // Memory Allocation & Marshalling Helpers
144
+ // ---------------------------------------------------------------------------
145
+
146
+ function align16(value) {
147
+ return (value + 15) & ~15;
148
+ }
149
+
150
+ function withScratchBuffer(instance, totalBytes, fn) {
151
+ const exports = instance.exports;
152
+ const alignedBytes = align16(totalBytes);
153
+ if (typeof exports.malloc === 'function' && typeof exports.free === 'function') {
154
+ const ptr = exports.malloc(alignedBytes);
155
+ if (!ptr) {
156
+ throw new Error('KINETIX_LAG_COMPENSATION_OOM: malloc returned 0');
157
+ }
158
+ try {
159
+ return fn(ptr);
160
+ } finally {
161
+ exports.free(ptr);
162
+ }
163
+ }
164
+
165
+ if (!instance._lagScratchBase) {
166
+ instance._lagScratchBase = Math.max(exports.memory.buffer.byteLength, 12 * 1024 * 1024);
167
+ instance._lagScratchOffset = instance._lagScratchBase;
168
+ }
169
+
170
+ const prevOffset = instance._lagScratchOffset;
171
+ const ptr = prevOffset;
172
+ const needed = ptr + alignedBytes;
173
+ if (needed > exports.memory.buffer.byteLength) {
174
+ const pages = Math.ceil((needed - exports.memory.buffer.byteLength) / 65536);
175
+ exports.memory.grow(pages);
176
+ }
177
+ instance._lagScratchOffset = needed;
178
+
179
+ try {
180
+ return fn(ptr);
181
+ } finally {
182
+ instance._lagScratchOffset = prevOffset;
183
+ }
184
+ }
185
+
186
+ export function packBodyData(bodies) {
187
+ if (bodies instanceof Float64Array) return bodies;
188
+ if (Array.isArray(bodies) && typeof bodies[0] === 'number') {
189
+ return new Float64Array(bodies);
190
+ }
191
+ if (bodies && typeof bodies === 'object' && bodies.state && Array.isArray(bodies.state.bodies)) {
192
+ bodies = bodies.state.bodies;
193
+ }
194
+ const bodyCount = bodies ? bodies.length : 0;
195
+ const buffer = new Float64Array(bodyCount * LAG_COMPENSATION_INPUT_BODY_STRIDE);
196
+ for (let i = 0; i < bodyCount; i += 1) {
197
+ const b = bodies[i];
198
+ const off = i * LAG_COMPENSATION_INPUT_BODY_STRIDE;
199
+ buffer[off + 0] = b.position?.x ?? b.x ?? 0.0;
200
+ buffer[off + 1] = b.position?.y ?? b.y ?? 0.0;
201
+ buffer[off + 2] = b.position?.z ?? b.z ?? 0.0;
202
+ buffer[off + 3] = b.linearVelocity?.x ?? b.velocity?.x ?? b.vx ?? 0.0;
203
+ buffer[off + 4] = b.linearVelocity?.y ?? b.velocity?.y ?? b.vy ?? 0.0;
204
+ buffer[off + 5] = b.linearVelocity?.z ?? b.velocity?.z ?? b.vz ?? 0.0;
205
+
206
+ const dims = b.shapeDimensions ?? b.shapeDims ?? b.dimensions ?? b.halfExtents ?? b.extents;
207
+ if (typeof dims === 'number') {
208
+ buffer[off + 6] = dims;
209
+ buffer[off + 7] = dims;
210
+ buffer[off + 8] = dims;
211
+ } else if (dims) {
212
+ buffer[off + 6] = dims.x ?? b.radius ?? 0.5;
213
+ buffer[off + 7] = dims.y ?? b.halfHeight ?? 0.5;
214
+ buffer[off + 8] = dims.z ?? 0.5;
215
+ } else {
216
+ buffer[off + 6] = b.radius ?? 0.5;
217
+ buffer[off + 7] = b.halfHeight ?? 0.5;
218
+ buffer[off + 8] = 0.5;
219
+ }
220
+
221
+ buffer[off + 9] = typeof b.motionType === 'number'
222
+ ? b.motionType
223
+ : (b.dynamic ? 1 : (b.kinematic ? 2 : 0));
224
+
225
+ let shapeKind = ShapeKind.Box;
226
+ if (typeof b.shape === 'number') shapeKind = b.shape;
227
+ else if (typeof b.shapeKind === 'number') shapeKind = b.shapeKind;
228
+ else if (typeof b.shape === 'string') {
229
+ const s = b.shape.toLowerCase();
230
+ if (s === 'box') shapeKind = ShapeKind.Box;
231
+ else if (s === 'sphere') shapeKind = ShapeKind.Sphere;
232
+ else if (s === 'capsule') shapeKind = ShapeKind.Capsule;
233
+ else if (s === 'hull') shapeKind = ShapeKind.Hull;
234
+ else if (s === 'mesh') shapeKind = ShapeKind.Mesh;
235
+ else if (s === 'compound') shapeKind = ShapeKind.Compound;
236
+ else if (s === 'heightfield') shapeKind = ShapeKind.Heightfield;
237
+ else if (s === 'cylinder') shapeKind = ShapeKind.Cylinder;
238
+ else if (s === 'cone') shapeKind = ShapeKind.Cone;
239
+ else if (s === 'taperedcapsule') shapeKind = ShapeKind.TaperedCapsule;
240
+ }
241
+ buffer[off + 10] = shapeKind;
242
+ buffer[off + 11] = b.layer ?? 1;
243
+ buffer[off + 12] = b.mask ?? 1;
244
+
245
+ const sOff = b.shapeOffset ?? b.offset;
246
+ buffer[off + 15] = sOff?.x ?? 0.0;
247
+ buffer[off + 16] = sOff?.y ?? 0.0;
248
+ buffer[off + 17] = sOff?.z ?? 0.0;
249
+
250
+ buffer[off + 19] = b.angularVelocity?.x ?? b.angularVelX ?? 0.0;
251
+ buffer[off + 20] = b.angularVelocity?.y ?? b.angularVelY ?? 0.0;
252
+ buffer[off + 21] = b.angularVelocity?.z ?? b.angularVelZ ?? 0.0;
253
+
254
+ const ori = b.orientation ?? b.rotation;
255
+ buffer[off + 22] = ori?.x ?? 0.0;
256
+ buffer[off + 23] = ori?.y ?? 0.0;
257
+ buffer[off + 24] = ori?.z ?? 0.0;
258
+ buffer[off + 25] = ori?.w ?? 1.0;
259
+ }
260
+ return buffer;
261
+ }
262
+
263
+ function packQueryData(query, defaultShape = ShapeKind.Box) {
264
+ const qData = new Float64Array(LAG_COMPENSATION_QUERY_STRIDE);
265
+ let shapeKind = defaultShape;
266
+ if (typeof query.shape === 'number') shapeKind = query.shape;
267
+ else if (typeof query.shapeKind === 'number') shapeKind = query.shapeKind;
268
+ else if (typeof query.shape === 'string') {
269
+ const s = query.shape.toLowerCase();
270
+ if (s === 'box') shapeKind = ShapeKind.Box;
271
+ else if (s === 'sphere') shapeKind = ShapeKind.Sphere;
272
+ else if (s === 'capsule') shapeKind = ShapeKind.Capsule;
273
+ else if (s === 'hull') shapeKind = ShapeKind.Hull;
274
+ else if (s === 'mesh') shapeKind = ShapeKind.Mesh;
275
+ else if (s === 'compound') shapeKind = ShapeKind.Compound;
276
+ else if (s === 'heightfield') shapeKind = ShapeKind.Heightfield;
277
+ else if (s === 'cylinder') shapeKind = ShapeKind.Cylinder;
278
+ else if (s === 'cone') shapeKind = ShapeKind.Cone;
279
+ else if (s === 'taperedcapsule') shapeKind = ShapeKind.TaperedCapsule;
280
+ }
281
+ qData[0] = shapeKind;
282
+
283
+ const dims = query.shapeDimensions ?? query.shapeDims ?? query.dimensions;
284
+ if (typeof dims === 'number') {
285
+ qData[1] = dims;
286
+ qData[2] = dims;
287
+ qData[3] = dims;
288
+ } else if (dims) {
289
+ qData[1] = dims.x ?? query.radius ?? 0.5;
290
+ qData[2] = dims.y ?? query.halfHeight ?? 0.5;
291
+ qData[3] = dims.z ?? 0.5;
292
+ } else {
293
+ qData[1] = query.radius ?? 0.5;
294
+ qData[2] = query.halfHeight ?? 0.5;
295
+ qData[3] = 0.5;
296
+ }
297
+
298
+ const origin = query.origin ?? query.position ?? query.start;
299
+ if (origin) {
300
+ qData[4] = origin.x ?? 0.0;
301
+ qData[5] = origin.y ?? 0.0;
302
+ qData[6] = origin.z ?? 0.0;
303
+ }
304
+
305
+ const dir = query.direction ?? query.dir;
306
+ if (dir) {
307
+ qData[7] = dir.x ?? 0.0;
308
+ qData[8] = dir.y ?? 0.0;
309
+ qData[9] = dir.z ?? 0.0;
310
+ }
311
+
312
+ qData[10] = query.maxDistance ?? query.distance ?? 1000.0;
313
+
314
+ const rot = query.orientation ?? query.rotation;
315
+ if (rot) {
316
+ qData[11] = rot.x ?? 0.0;
317
+ qData[12] = rot.y ?? 0.0;
318
+ qData[13] = rot.z ?? 0.0;
319
+ qData[14] = rot.w ?? 1.0;
320
+ } else {
321
+ qData[14] = 1.0;
322
+ }
323
+
324
+ qData[15] = query.layerMask ?? query.mask ?? 0xffffffff;
325
+ qData[16] = query.flags ?? 0b1000;
326
+
327
+ const exId = query.excludedBodyId ? BigInt(query.excludedBodyId) : 0n;
328
+ qData[17] = Number(exId & 0xffffffffn);
329
+ qData[18] = Number((exId >> 32n) & 0xffffffffn);
330
+
331
+ const tick = query.historicalTick !== undefined
332
+ ? BigInt(query.historicalTick)
333
+ : (query.tick !== undefined ? BigInt(query.tick) : 0n);
334
+ qData[19] = Number(tick);
335
+ qData[20] = query.subTickAlpha ?? query.alpha ?? 0.0;
336
+
337
+ return qData;
338
+ }
339
+
340
+ function unpackBodyRecord48(rec48) {
341
+ const bodyIdLow = BigInt(Math.round(rec48[44]));
342
+ const bodyIdHigh = BigInt(Math.round(rec48[45]));
343
+ const bodyId = (bodyIdHigh << 32n) | bodyIdLow;
344
+ const shapeKind = Math.round(rec48[10]);
345
+ const shapeDimensions = { x: rec48[6], y: rec48[7], z: rec48[8] };
346
+ const shapeOffset = { x: rec48[15], y: rec48[16], z: rec48[17] };
347
+
348
+ const isComplexShape = (
349
+ shapeKind === ShapeKind.Hull ||
350
+ shapeKind === ShapeKind.Mesh ||
351
+ shapeKind === ShapeKind.Compound ||
352
+ shapeKind === ShapeKind.Heightfield
353
+ );
354
+ const geometryOffset = isComplexShape ? Math.round(shapeDimensions.x) : 0;
355
+ const geometryCount = isComplexShape ? Math.round(shapeDimensions.y) : 0;
356
+
357
+ return {
358
+ status: 0,
359
+ bodyId,
360
+ position: { x: rec48[0], y: rec48[1], z: rec48[2] },
361
+ linearVelocity: { x: rec48[3], y: rec48[4], z: rec48[5] },
362
+ shapeDimensions,
363
+ motionType: Math.round(rec48[9]),
364
+ shapeKind,
365
+ shape: shapeKind,
366
+ layer: Math.round(rec48[11]),
367
+ mask: Math.round(rec48[12]),
368
+ shapeOffset,
369
+ angularVelocity: { x: rec48[19], y: rec48[20], z: rec48[21] },
370
+ orientation: { x: rec48[22], y: rec48[23], z: rec48[24], w: rec48[25] },
371
+ geometryOffset,
372
+ geometryCount,
373
+ geometryGeneration: Math.round(rec48[46]),
374
+ active: rec48[47] !== 0.0,
375
+ rawData: Float64Array.from(rec48),
376
+ };
377
+ }
378
+
379
+ function unpackResults(resultF64, count) {
380
+ const hits = [];
381
+ for (let i = 0; i < count; i += 1) {
382
+ const off = i * LAG_COMPENSATION_RESULT_STRIDE;
383
+ const bodyIdLow = BigInt(Math.round(resultF64[off + 9]));
384
+ const bodyIdHigh = BigInt(Math.round(resultF64[off + 10]));
385
+ const bodyId = (bodyIdHigh << 32n) | bodyIdLow;
386
+ hits.push({
387
+ hit: resultF64[off + 0] !== 0.0,
388
+ distance: resultF64[off + 1],
389
+ point: {
390
+ x: resultF64[off + 2],
391
+ y: resultF64[off + 3],
392
+ z: resultF64[off + 4],
393
+ },
394
+ normal: {
395
+ x: resultF64[off + 5],
396
+ y: resultF64[off + 6],
397
+ z: resultF64[off + 7],
398
+ },
399
+ bodyIndex: Math.round(resultF64[off + 8]),
400
+ bodyId,
401
+ geometryGeneration: Math.round(resultF64[off + 11]),
402
+ });
403
+ }
404
+ return hits;
405
+ }
406
+
407
+ // ---------------------------------------------------------------------------
408
+ // Dynamic Stride, Size & Limit Queries (routed directly to WASM)
409
+ // ---------------------------------------------------------------------------
410
+
411
+ export function lagCompensationAbiVersion(instance = null) {
412
+ const inst = requireExtendedInstance(instance);
413
+ return inst.exports.rundot_kinetix_ext_lag_compensation_abi_version();
414
+ }
415
+
416
+ export function lagCompensationBodyStride(instance = null) {
417
+ const inst = requireExtendedInstance(instance);
418
+ return inst.exports.rundot_kinetix_ext_lag_compensation_body_stride();
419
+ }
420
+
421
+ export function lagCompensationHeaderStride(instance = null) {
422
+ const inst = requireExtendedInstance(instance);
423
+ return inst.exports.rundot_kinetix_ext_lag_compensation_header_stride();
424
+ }
425
+
426
+ export function lagCompensationQueryStride(instance = null) {
427
+ const inst = requireExtendedInstance(instance);
428
+ return inst.exports.rundot_kinetix_ext_lag_compensation_query_stride();
429
+ }
430
+
431
+ export function lagCompensationResultStride(instance = null) {
432
+ const inst = requireExtendedInstance(instance);
433
+ return inst.exports.rundot_kinetix_ext_lag_compensation_result_stride();
434
+ }
435
+
436
+ export function lagCompensationMaxFrames(instance = null) {
437
+ const inst = requireExtendedInstance(instance);
438
+ return inst.exports.rundot_kinetix_ext_lag_compensation_max_frames();
439
+ }
440
+
441
+ export function lagCompensationMaxBodies(instance = null) {
442
+ const inst = requireExtendedInstance(instance);
443
+ return inst.exports.rundot_kinetix_ext_lag_compensation_max_bodies();
444
+ }
445
+
446
+ export function lagCompensationMaxResults(instance = null) {
447
+ const inst = requireExtendedInstance(instance);
448
+ return inst.exports.rundot_kinetix_ext_lag_compensation_max_results();
449
+ }
450
+
451
+ export function lagCompensationStride(instance = null) {
452
+ const inst = requireExtendedInstance(instance);
453
+ return inst.exports.rundot_kinetix_ext_lag_compensation_stride();
454
+ }
455
+
456
+ export function lagCompensationSize(instance = null) {
457
+ const inst = requireExtendedInstance(instance);
458
+ return inst.exports.rundot_kinetix_ext_lag_compensation_size();
459
+ }
460
+
461
+ export const getLagCompensationAbiVersion = lagCompensationAbiVersion;
462
+ export const getLagCompensationBodyStride = lagCompensationBodyStride;
463
+ export const getLagCompensationHeaderStride = lagCompensationHeaderStride;
464
+ export const getLagCompensationQueryStride = lagCompensationQueryStride;
465
+ export const getLagCompensationResultStride = lagCompensationResultStride;
466
+ export const getLagCompensationMaxFrames = lagCompensationMaxFrames;
467
+ export const getLagCompensationMaxBodies = lagCompensationMaxBodies;
468
+ export const getLagCompensationMaxResults = lagCompensationMaxResults;
469
+ export const getLagCompensationStride = lagCompensationStride;
470
+ export const getLagCompensationSize = lagCompensationSize;
471
+
472
+ // ---------------------------------------------------------------------------
473
+ // Buffer State Management
474
+ // ---------------------------------------------------------------------------
475
+
476
+ export function resetLagCompensation(instance = null) {
477
+ const inst = requireExtendedInstance(instance);
478
+ return inst.exports.rundot_kinetix_ext_lag_compensation_reset();
479
+ }
480
+
481
+ export function recordLagCompensationFrame(tick, timestamp, bodyData, options = {}, instance = null) {
482
+ const inst = requireExtendedInstance(instance);
483
+ const exp = inst.exports;
484
+
485
+ const packedData = packBodyData(bodyData);
486
+ const bodyCount = options.bodyCount !== undefined
487
+ ? options.bodyCount
488
+ : Math.floor(packedData.length / LAG_COMPENSATION_INPUT_BODY_STRIDE);
489
+ const globalGeomGen = options.globalGeomGen ?? 0;
490
+
491
+ const bodyIds = options.bodyIds;
492
+ const hasIds = bodyIds && (Array.isArray(bodyIds) || ArrayBuffer.isView(bodyIds)) && bodyIds.length >= bodyCount;
493
+
494
+ const bodyBytes = packedData.byteLength;
495
+ const idBytes = hasIds ? align16(bodyCount * 8) : 0;
496
+ const totalBytes = bodyBytes + idBytes;
497
+
498
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
499
+ const f64 = new Float64Array(exp.memory.buffer, ptr, packedData.length);
500
+ f64.set(packedData);
501
+
502
+ let idPtr = 0;
503
+ if (hasIds) {
504
+ idPtr = ptr + bodyBytes;
505
+ const u64 = new BigUint64Array(exp.memory.buffer, idPtr, bodyCount);
506
+ for (let i = 0; i < bodyCount; i += 1) {
507
+ u64[i] = BigInt(bodyIds[i]);
508
+ }
509
+ }
510
+
511
+ const status = exp.rundot_kinetix_ext_lag_compensation_record_frame(
512
+ BigInt(tick),
513
+ timestamp,
514
+ ptr,
515
+ bodyCount,
516
+ idPtr,
517
+ globalGeomGen
518
+ );
519
+
520
+ if (options.throwOnError && status !== LagCompStatus.Success) {
521
+ throw new Error(`KINETIX_LAG_COMPENSATION_RECORD_FRAME_FAILED status=${status}`);
522
+ }
523
+ return status;
524
+ });
525
+ }
526
+
527
+ export function hasLagCompensationTick(tick, instance = null) {
528
+ const inst = requireExtendedInstance(instance);
529
+ return inst.exports.rundot_kinetix_ext_lag_compensation_has_tick(BigInt(tick)) !== 0;
530
+ }
531
+
532
+ export function getLagCompensationOldestTick(instance = null) {
533
+ const inst = requireExtendedInstance(instance);
534
+ return BigInt(inst.exports.rundot_kinetix_ext_lag_compensation_oldest_tick());
535
+ }
536
+
537
+ export function getLagCompensationNewestTick(instance = null) {
538
+ const inst = requireExtendedInstance(instance);
539
+ return BigInt(inst.exports.rundot_kinetix_ext_lag_compensation_newest_tick());
540
+ }
541
+
542
+ export function getLagCompensationFrameCount(instance = null) {
543
+ const inst = requireExtendedInstance(instance);
544
+ return Number(inst.exports.rundot_kinetix_ext_lag_compensation_frame_count());
545
+ }
546
+
547
+ // ---------------------------------------------------------------------------
548
+ // Historical State Queries
549
+ // ---------------------------------------------------------------------------
550
+
551
+ export function queryHistoricalTopology(tick, options = {}, instance = null) {
552
+ const inst = requireExtendedInstance(instance);
553
+ const exp = inst.exports;
554
+
555
+ const maxIds = options.maxIds ?? exp.rundot_kinetix_ext_lag_compensation_max_bodies();
556
+ const countBytes = 4;
557
+ const idsBytes = maxIds * 8;
558
+ const totalBytes = align16(countBytes) + align16(idsBytes);
559
+
560
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
561
+ const ptrCount = ptr;
562
+ const ptrIds = align16(ptrCount + countBytes);
563
+
564
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_topology(
565
+ BigInt(tick),
566
+ ptrCount,
567
+ ptrIds,
568
+ maxIds
569
+ );
570
+
571
+ if (status !== LagCompStatus.Success) {
572
+ if (options.throwOnError) {
573
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_TOPOLOGY_FAILED status=${status}`);
574
+ }
575
+ return options.rawStatus ? { status } : null;
576
+ }
577
+
578
+ const u32 = new Uint32Array(exp.memory.buffer, ptrCount, 1);
579
+ const bodyCount = u32[0];
580
+ const bigU64 = new BigUint64Array(exp.memory.buffer, ptrIds, bodyCount);
581
+ const bodyIds = Array.from(bigU64);
582
+
583
+ return {
584
+ status: 0,
585
+ bodyCount,
586
+ bodyIds,
587
+ };
588
+ });
589
+ }
590
+
591
+ export function queryHistoricalBody(tick, bodyId, options = {}, instance = null) {
592
+ const inst = requireExtendedInstance(instance);
593
+ const exp = inst.exports;
594
+ const stride = exp.rundot_kinetix_ext_lag_compensation_body_stride();
595
+ const totalBytes = stride * 8;
596
+
597
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
598
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_body(
599
+ BigInt(tick),
600
+ BigInt(bodyId),
601
+ ptr
602
+ );
603
+
604
+ if (status !== LagCompStatus.Success) {
605
+ if (options.throwOnError) {
606
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_BODY_FAILED status=${status}`);
607
+ }
608
+ return options.rawStatus ? { status } : null;
609
+ }
610
+
611
+ const rec48 = new Float64Array(exp.memory.buffer, ptr, stride);
612
+ return unpackBodyRecord48(rec48);
613
+ });
614
+ }
615
+
616
+ export function queryHistoricalBodyByIndex(tick, bodyIndex, options = {}, instance = null) {
617
+ const inst = requireExtendedInstance(instance);
618
+ const exp = inst.exports;
619
+ const stride = exp.rundot_kinetix_ext_lag_compensation_body_stride();
620
+ const totalBytes = stride * 8;
621
+
622
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
623
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_body_by_index(
624
+ BigInt(tick),
625
+ bodyIndex,
626
+ ptr
627
+ );
628
+
629
+ if (status !== LagCompStatus.Success) {
630
+ if (options.throwOnError) {
631
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_BODY_BY_INDEX_FAILED status=${status}`);
632
+ }
633
+ return options.rawStatus ? { status } : null;
634
+ }
635
+
636
+ const rec48 = new Float64Array(exp.memory.buffer, ptr, stride);
637
+ return unpackBodyRecord48(rec48);
638
+ });
639
+ }
640
+
641
+ export function queryHistoricalColliderFilter(tick, bodyId, options = {}, instance = null) {
642
+ const inst = requireExtendedInstance(instance);
643
+ const exp = inst.exports;
644
+ const totalBytes = 16;
645
+
646
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
647
+ const ptrLayer = ptr;
648
+ const ptrMask = ptr + 4;
649
+ const ptrMotion = ptr + 8;
650
+
651
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_filter(
652
+ BigInt(tick),
653
+ BigInt(bodyId),
654
+ ptrLayer,
655
+ ptrMask,
656
+ ptrMotion
657
+ );
658
+
659
+ if (status !== LagCompStatus.Success) {
660
+ if (options.throwOnError) {
661
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_FILTER_FAILED status=${status}`);
662
+ }
663
+ return options.rawStatus ? { status } : null;
664
+ }
665
+
666
+ const u32 = new Uint32Array(exp.memory.buffer, ptr, 3);
667
+ return {
668
+ status: 0,
669
+ layer: u32[0],
670
+ mask: u32[1],
671
+ motionType: u32[2],
672
+ };
673
+ });
674
+ }
675
+
676
+ export const queryHistoricalFilter = queryHistoricalColliderFilter;
677
+
678
+ export function queryHistoricalGeometry(tick, bodyId, options = {}, instance = null) {
679
+ const inst = requireExtendedInstance(instance);
680
+ const exp = inst.exports;
681
+
682
+ const ptrShapeKind = 0;
683
+ const ptrShapeDims = align16(ptrShapeKind + 4);
684
+ const ptrShapeOffset = align16(ptrShapeDims + 24);
685
+ const ptrGeomOffset = align16(ptrShapeOffset + 24);
686
+ const ptrGeomCount = align16(ptrGeomOffset + 4);
687
+ const ptrGeomGen = align16(ptrGeomCount + 4);
688
+ const totalBytes = align16(ptrGeomGen + 4);
689
+
690
+ return withScratchBuffer(inst, totalBytes, (basePtr) => {
691
+ const actShapeKind = basePtr + ptrShapeKind;
692
+ const actShapeDims = basePtr + ptrShapeDims;
693
+ const actShapeOffset = basePtr + ptrShapeOffset;
694
+ const actGeomOffset = basePtr + ptrGeomOffset;
695
+ const actGeomCount = basePtr + ptrGeomCount;
696
+ const actGeomGen = basePtr + ptrGeomGen;
697
+
698
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_geometry(
699
+ BigInt(tick),
700
+ BigInt(bodyId),
701
+ actShapeKind,
702
+ actShapeDims,
703
+ actShapeOffset,
704
+ actGeomOffset,
705
+ actGeomCount,
706
+ actGeomGen
707
+ );
708
+
709
+ if (status !== LagCompStatus.Success) {
710
+ if (options.throwOnError) {
711
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_GEOMETRY_FAILED status=${status}`);
712
+ }
713
+ return options.rawStatus ? { status } : null;
714
+ }
715
+
716
+ const u32Kind = new Uint32Array(exp.memory.buffer, actShapeKind, 1)[0];
717
+ const f64Dims = new Float64Array(exp.memory.buffer, actShapeDims, 3);
718
+ const f64Offset = new Float64Array(exp.memory.buffer, actShapeOffset, 3);
719
+ const u32GeomOffset = new Uint32Array(exp.memory.buffer, actGeomOffset, 1)[0];
720
+ const u32GeomCount = new Uint32Array(exp.memory.buffer, actGeomCount, 1)[0];
721
+ const u32GeomGen = new Uint32Array(exp.memory.buffer, actGeomGen, 1)[0];
722
+
723
+ return {
724
+ status: 0,
725
+ shapeKind: u32Kind,
726
+ shape: u32Kind,
727
+ shapeDimensions: { x: f64Dims[0], y: f64Dims[1], z: f64Dims[2] },
728
+ shapeOffset: { x: f64Offset[0], y: f64Offset[1], z: f64Offset[2] },
729
+ geometryOffset: u32GeomOffset,
730
+ geometryCount: u32GeomCount,
731
+ geometryGeneration: u32GeomGen,
732
+ };
733
+ });
734
+ }
735
+
736
+ export function queryHistoricalPose(tick, bodyId, options = {}, instance = null) {
737
+ const inst = requireExtendedInstance(instance);
738
+ const exp = inst.exports;
739
+ const totalDoubles = 3 + 4 + 3 + 3;
740
+ const totalBytes = totalDoubles * 8;
741
+
742
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
743
+ const ptrPos = ptr;
744
+ const ptrRot = ptrPos + 3 * 8;
745
+ const ptrLinVel = ptrRot + 4 * 8;
746
+ const ptrAngVel = ptrLinVel + 3 * 8;
747
+
748
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_pose(
749
+ BigInt(tick),
750
+ BigInt(bodyId),
751
+ ptrPos,
752
+ ptrRot,
753
+ ptrLinVel,
754
+ ptrAngVel
755
+ );
756
+
757
+ if (status !== LagCompStatus.Success) {
758
+ if (options.throwOnError) {
759
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_POSE_FAILED status=${status}`);
760
+ }
761
+ return options.rawStatus ? { status } : null;
762
+ }
763
+
764
+ const pos = new Float64Array(exp.memory.buffer, ptrPos, 3);
765
+ const rot = new Float64Array(exp.memory.buffer, ptrRot, 4);
766
+ const linVel = new Float64Array(exp.memory.buffer, ptrLinVel, 3);
767
+ const angVel = new Float64Array(exp.memory.buffer, ptrAngVel, 3);
768
+
769
+ return {
770
+ status: 0,
771
+ position: { x: pos[0], y: pos[1], z: pos[2] },
772
+ orientation: { x: rot[0], y: rot[1], z: rot[2], w: rot[3] },
773
+ linearVelocity: { x: linVel[0], y: linVel[1], z: linVel[2] },
774
+ angularVelocity: { x: angVel[0], y: angVel[1], z: angVel[2] },
775
+ };
776
+ });
777
+ }
778
+
779
+ export function queryHistoricalInterpolatedPose(tickA, tickB, alpha, bodyId, options = {}, instance = null) {
780
+ const inst = requireExtendedInstance(instance);
781
+ const exp = inst.exports;
782
+ const totalBytes = (3 + 4) * 8;
783
+
784
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
785
+ const ptrPos = ptr;
786
+ const ptrRot = ptrPos + 3 * 8;
787
+
788
+ const status = exp.rundot_kinetix_ext_lag_compensation_query_interpolated_pose(
789
+ BigInt(tickA),
790
+ BigInt(tickB),
791
+ alpha,
792
+ BigInt(bodyId),
793
+ ptrPos,
794
+ ptrRot
795
+ );
796
+
797
+ if (status !== LagCompStatus.Success) {
798
+ if (options.throwOnError) {
799
+ throw new Error(`KINETIX_LAG_COMPENSATION_QUERY_INTERPOLATED_POSE_FAILED status=${status}`);
800
+ }
801
+ return options.rawStatus ? { status } : null;
802
+ }
803
+
804
+ const pos = new Float64Array(exp.memory.buffer, ptrPos, 3);
805
+ const rot = new Float64Array(exp.memory.buffer, ptrRot, 4);
806
+
807
+ return {
808
+ status: 0,
809
+ position: { x: pos[0], y: pos[1], z: pos[2] },
810
+ orientation: { x: rot[0], y: rot[1], z: rot[2], w: rot[3] },
811
+ };
812
+ });
813
+ }
814
+
815
+ export function rewindHistoricalScene(tick, options = {}, instance = null) {
816
+ const inst = requireExtendedInstance(instance);
817
+ const exp = inst.exports;
818
+
819
+ const maxBodies = options.maxBodies ?? exp.rundot_kinetix_ext_lag_compensation_max_bodies();
820
+ const dataBytes = maxBodies * LAG_COMPENSATION_INPUT_BODY_STRIDE * 8;
821
+ const countBytes = 4;
822
+ const totalBytes = dataBytes + align16(countBytes);
823
+
824
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
825
+ const ptrData = ptr;
826
+ const ptrCount = align16(ptrData + dataBytes);
827
+
828
+ const status = exp.rundot_kinetix_ext_lag_compensation_rewind_scene(
829
+ BigInt(tick),
830
+ ptrData,
831
+ maxBodies,
832
+ ptrCount
833
+ );
834
+
835
+ if (status !== LagCompStatus.Success) {
836
+ if (options.throwOnError) {
837
+ throw new Error(`KINETIX_LAG_COMPENSATION_REWIND_SCENE_FAILED status=${status}`);
838
+ }
839
+ return options.rawStatus ? { status } : null;
840
+ }
841
+
842
+ const bodyCount = new Uint32Array(exp.memory.buffer, ptrCount, 1)[0];
843
+ const bodyData = new Float64Array(bodyCount * LAG_COMPENSATION_INPUT_BODY_STRIDE);
844
+ bodyData.set(new Float64Array(exp.memory.buffer, ptrData, bodyCount * LAG_COMPENSATION_INPUT_BODY_STRIDE));
845
+
846
+ return {
847
+ status: 0,
848
+ bodyCount,
849
+ bodyData,
850
+ };
851
+ });
852
+ }
853
+
854
+ export const rewindScene = rewindHistoricalScene;
855
+
856
+ export function rewindHistoricalSceneInterpolated(tickA, tickB, alpha, options = {}, instance = null) {
857
+ const inst = requireExtendedInstance(instance);
858
+ const exp = inst.exports;
859
+
860
+ const maxBodies = options.maxBodies ?? exp.rundot_kinetix_ext_lag_compensation_max_bodies();
861
+ const dataBytes = maxBodies * LAG_COMPENSATION_INPUT_BODY_STRIDE * 8;
862
+ const countBytes = 4;
863
+ const totalBytes = dataBytes + align16(countBytes);
864
+
865
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
866
+ const ptrData = ptr;
867
+ const ptrCount = align16(ptrData + dataBytes);
868
+
869
+ const status = exp.rundot_kinetix_ext_lag_compensation_rewind_scene_interpolated(
870
+ BigInt(tickA),
871
+ BigInt(tickB),
872
+ alpha,
873
+ ptrData,
874
+ maxBodies,
875
+ ptrCount
876
+ );
877
+
878
+ if (status !== LagCompStatus.Success) {
879
+ if (options.throwOnError) {
880
+ throw new Error(`KINETIX_LAG_COMPENSATION_REWIND_SCENE_INTERPOLATED_FAILED status=${status}`);
881
+ }
882
+ return options.rawStatus ? { status } : null;
883
+ }
884
+
885
+ const bodyCount = new Uint32Array(exp.memory.buffer, ptrCount, 1)[0];
886
+ const bodyData = new Float64Array(bodyCount * LAG_COMPENSATION_INPUT_BODY_STRIDE);
887
+ bodyData.set(new Float64Array(exp.memory.buffer, ptrData, bodyCount * LAG_COMPENSATION_INPUT_BODY_STRIDE));
888
+
889
+ return {
890
+ status: 0,
891
+ bodyCount,
892
+ bodyData,
893
+ };
894
+ });
895
+ }
896
+
897
+ export const rewindSceneInterpolated = rewindHistoricalSceneInterpolated;
898
+
899
+ // ---------------------------------------------------------------------------
900
+ // Historical Spatial Queries (Raycast, Overlap, Shapecast)
901
+ // ---------------------------------------------------------------------------
902
+
903
+ export function raycastHistorical(query, instance = null) {
904
+ const inst = requireExtendedInstance(instance);
905
+ const exp = inst.exports;
906
+
907
+ const qData = packQueryData(query, ShapeKind.Box);
908
+ const maxResults = query.maxResults ?? exp.rundot_kinetix_ext_lag_compensation_max_results();
909
+
910
+ const queryBytes = LAG_COMPENSATION_QUERY_STRIDE * 8;
911
+ const resultBytes = maxResults * LAG_COMPENSATION_RESULT_STRIDE * 8;
912
+ const countBytes = 4;
913
+ const totalBytes = align16(queryBytes) + align16(resultBytes) + align16(countBytes);
914
+
915
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
916
+ const ptrQuery = ptr;
917
+ const ptrResults = align16(ptrQuery + queryBytes);
918
+ const ptrCount = align16(ptrResults + resultBytes);
919
+
920
+ const f64Q = new Float64Array(exp.memory.buffer, ptrQuery, LAG_COMPENSATION_QUERY_STRIDE);
921
+ f64Q.set(qData);
922
+
923
+ const status = exp.rundot_kinetix_ext_lag_compensation_raycast(
924
+ ptrQuery,
925
+ ptrResults,
926
+ maxResults,
927
+ ptrCount
928
+ );
929
+
930
+ if (status < 0 && status !== LagCompStatus.NoHit) {
931
+ throw new Error(`KINETIX_LAG_COMPENSATION_RAYCAST_FAILED status=${status}`);
932
+ }
933
+
934
+ const hitCount = new Uint32Array(exp.memory.buffer, ptrCount, 1)[0];
935
+ const rf64 = new Float64Array(exp.memory.buffer, ptrResults, hitCount * LAG_COMPENSATION_RESULT_STRIDE);
936
+ const hits = unpackResults(rf64, hitCount);
937
+ hits.status = status;
938
+ return hits;
939
+ });
940
+ }
941
+
942
+ export function overlapHistorical(query, instance = null) {
943
+ const inst = requireExtendedInstance(instance);
944
+ const exp = inst.exports;
945
+
946
+ const qData = packQueryData(query, ShapeKind.Sphere);
947
+ const maxResults = query.maxResults ?? exp.rundot_kinetix_ext_lag_compensation_max_results();
948
+
949
+ const queryBytes = LAG_COMPENSATION_QUERY_STRIDE * 8;
950
+ const resultBytes = maxResults * LAG_COMPENSATION_RESULT_STRIDE * 8;
951
+ const countBytes = 4;
952
+ const totalBytes = align16(queryBytes) + align16(resultBytes) + align16(countBytes);
953
+
954
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
955
+ const ptrQuery = ptr;
956
+ const ptrResults = align16(ptrQuery + queryBytes);
957
+ const ptrCount = align16(ptrResults + resultBytes);
958
+
959
+ const f64Q = new Float64Array(exp.memory.buffer, ptrQuery, LAG_COMPENSATION_QUERY_STRIDE);
960
+ f64Q.set(qData);
961
+
962
+ const status = exp.rundot_kinetix_ext_lag_compensation_overlap(
963
+ ptrQuery,
964
+ ptrResults,
965
+ maxResults,
966
+ ptrCount
967
+ );
968
+
969
+ if (status < 0 && status !== LagCompStatus.NoHit) {
970
+ throw new Error(`KINETIX_LAG_COMPENSATION_OVERLAP_FAILED status=${status}`);
971
+ }
972
+
973
+ const hitCount = new Uint32Array(exp.memory.buffer, ptrCount, 1)[0];
974
+ const rf64 = new Float64Array(exp.memory.buffer, ptrResults, hitCount * LAG_COMPENSATION_RESULT_STRIDE);
975
+ const hits = unpackResults(rf64, hitCount);
976
+ hits.status = status;
977
+ return hits;
978
+ });
979
+ }
980
+
981
+ export function shapecastHistorical(query, instance = null) {
982
+ const inst = requireExtendedInstance(instance);
983
+ const exp = inst.exports;
984
+
985
+ const qData = packQueryData(query, ShapeKind.Sphere);
986
+ const maxResults = query.maxResults ?? exp.rundot_kinetix_ext_lag_compensation_max_results();
987
+
988
+ const queryBytes = LAG_COMPENSATION_QUERY_STRIDE * 8;
989
+ const resultBytes = maxResults * LAG_COMPENSATION_RESULT_STRIDE * 8;
990
+ const countBytes = 4;
991
+ const totalBytes = align16(queryBytes) + align16(resultBytes) + align16(countBytes);
992
+
993
+ return withScratchBuffer(inst, totalBytes, (ptr) => {
994
+ const ptrQuery = ptr;
995
+ const ptrResults = align16(ptrQuery + queryBytes);
996
+ const ptrCount = align16(ptrResults + resultBytes);
997
+
998
+ const f64Q = new Float64Array(exp.memory.buffer, ptrQuery, LAG_COMPENSATION_QUERY_STRIDE);
999
+ f64Q.set(qData);
1000
+
1001
+ const status = exp.rundot_kinetix_ext_lag_compensation_shapecast(
1002
+ ptrQuery,
1003
+ ptrResults,
1004
+ maxResults,
1005
+ ptrCount
1006
+ );
1007
+
1008
+ if (status < 0 && status !== LagCompStatus.NoHit) {
1009
+ throw new Error(`KINETIX_LAG_COMPENSATION_SHAPECAST_FAILED status=${status}`);
1010
+ }
1011
+
1012
+ const hitCount = new Uint32Array(exp.memory.buffer, ptrCount, 1)[0];
1013
+ const rf64 = new Float64Array(exp.memory.buffer, ptrResults, hitCount * LAG_COMPENSATION_RESULT_STRIDE);
1014
+ const hits = unpackResults(rf64, hitCount);
1015
+ hits.status = status;
1016
+ return hits;
1017
+ });
1018
+ }
1019
+
1020
+ // ---------------------------------------------------------------------------
1021
+ // Object-Oriented Interface: LagCompensationBuffer
1022
+ // ---------------------------------------------------------------------------
1023
+
1024
+ export class LagCompensationBuffer {
1025
+ constructor(instance = null) {
1026
+ this.instance = requireExtendedInstance(instance);
1027
+ }
1028
+
1029
+ reset() {
1030
+ return resetLagCompensation(this.instance);
1031
+ }
1032
+
1033
+ recordFrame(tick, timestamp, bodyData, options) {
1034
+ return recordLagCompensationFrame(tick, timestamp, bodyData, options, this.instance);
1035
+ }
1036
+
1037
+ hasTick(tick) {
1038
+ return hasLagCompensationTick(tick, this.instance);
1039
+ }
1040
+
1041
+ getOldestTick() {
1042
+ return getLagCompensationOldestTick(this.instance);
1043
+ }
1044
+
1045
+ getNewestTick() {
1046
+ return getLagCompensationNewestTick(this.instance);
1047
+ }
1048
+
1049
+ getFrameCount() {
1050
+ return getLagCompensationFrameCount(this.instance);
1051
+ }
1052
+
1053
+ queryTopology(tick, options) {
1054
+ return queryHistoricalTopology(tick, options, this.instance);
1055
+ }
1056
+
1057
+ queryBody(tick, bodyId, options) {
1058
+ return queryHistoricalBody(tick, bodyId, options, this.instance);
1059
+ }
1060
+
1061
+ queryBodyByIndex(tick, bodyIndex, options) {
1062
+ return queryHistoricalBodyByIndex(tick, bodyIndex, options, this.instance);
1063
+ }
1064
+
1065
+ queryFilter(tick, bodyId, options) {
1066
+ return queryHistoricalColliderFilter(tick, bodyId, options, this.instance);
1067
+ }
1068
+
1069
+ queryGeometry(tick, bodyId, options) {
1070
+ return queryHistoricalGeometry(tick, bodyId, options, this.instance);
1071
+ }
1072
+
1073
+ queryPose(tick, bodyId, options) {
1074
+ return queryHistoricalPose(tick, bodyId, options, this.instance);
1075
+ }
1076
+
1077
+ queryInterpolatedPose(tickA, tickB, alpha, bodyId, options) {
1078
+ return queryHistoricalInterpolatedPose(tickA, tickB, alpha, bodyId, options, this.instance);
1079
+ }
1080
+
1081
+ rewindScene(tick, options) {
1082
+ return rewindHistoricalScene(tick, options, this.instance);
1083
+ }
1084
+
1085
+ rewindSceneInterpolated(tickA, tickB, alpha, options) {
1086
+ return rewindHistoricalSceneInterpolated(tickA, tickB, alpha, options, this.instance);
1087
+ }
1088
+
1089
+ raycast(query) {
1090
+ return raycastHistorical(query, this.instance);
1091
+ }
1092
+
1093
+ overlap(query) {
1094
+ return overlapHistorical(query, this.instance);
1095
+ }
1096
+
1097
+ shapecast(query) {
1098
+ return shapecastHistorical(query, this.instance);
1099
+ }
1100
+ }