@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,1626 @@
1
+ #include "lag_compensation.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstdint>
6
+ #include <cstring>
7
+ #include <span>
8
+ #include <vector>
9
+
10
+ namespace rundot::kinetix::physics {
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Pure Deterministic Math Utilities
14
+ // ---------------------------------------------------------------------------
15
+
16
+ double detSqrt(double v) noexcept {
17
+ if (v <= 0.0) return 0.0;
18
+ return std::sqrt(v);
19
+ }
20
+
21
+ LagCompVec3 vec3Add(const LagCompVec3& a, const LagCompVec3& b) noexcept {
22
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
23
+ }
24
+
25
+ LagCompVec3 vec3Sub(const LagCompVec3& a, const LagCompVec3& b) noexcept {
26
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
27
+ }
28
+
29
+ LagCompVec3 vec3Scale(const LagCompVec3& v, double s) noexcept {
30
+ return {v.x * s, v.y * s, v.z * s};
31
+ }
32
+
33
+ double vec3Dot(const LagCompVec3& a, const LagCompVec3& b) noexcept {
34
+ return a.x * b.x + a.y * b.y + a.z * b.z;
35
+ }
36
+
37
+ LagCompVec3 vec3Cross(const LagCompVec3& a, const LagCompVec3& b) noexcept {
38
+ return {
39
+ a.y * b.z - a.z * b.y,
40
+ a.z * b.x - a.x * b.z,
41
+ a.x * b.y - a.y * b.x,
42
+ };
43
+ }
44
+
45
+ double vec3LengthSq(const LagCompVec3& v) noexcept {
46
+ return vec3Dot(v, v);
47
+ }
48
+
49
+ double vec3Length(const LagCompVec3& v) noexcept {
50
+ return detSqrt(vec3LengthSq(v));
51
+ }
52
+
53
+ LagCompVec3 vec3Normalize(const LagCompVec3& v) noexcept {
54
+ const double len = vec3Length(v);
55
+ if (len <= 1e-15) return {0.0, 0.0, 0.0};
56
+ return vec3Scale(v, 1.0 / len);
57
+ }
58
+
59
+ LagCompQuat quatIdentity() noexcept {
60
+ return {0.0, 0.0, 0.0, 1.0};
61
+ }
62
+
63
+ LagCompQuat quatConjugate(const LagCompQuat& q) noexcept {
64
+ return {-q.x, -q.y, -q.z, q.w};
65
+ }
66
+
67
+ double quatDot(const LagCompQuat& a, const LagCompQuat& b) noexcept {
68
+ return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
69
+ }
70
+
71
+ LagCompQuat quatNormalize(const LagCompQuat& q) noexcept {
72
+ const double lenSq = quatDot(q, q);
73
+ if (lenSq <= 1e-15) return {0.0, 0.0, 0.0, 1.0};
74
+ const double invLen = 1.0 / detSqrt(lenSq);
75
+ return {q.x * invLen, q.y * invLen, q.z * invLen, q.w * invLen};
76
+ }
77
+
78
+ LagCompQuat quatMultiply(const LagCompQuat& a, const LagCompQuat& b) noexcept {
79
+ return {
80
+ a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
81
+ a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
82
+ a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
83
+ a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
84
+ };
85
+ }
86
+
87
+ LagCompVec3 rotateVec3(const LagCompQuat& q, const LagCompVec3& v) noexcept {
88
+ const LagCompVec3 qv{q.x, q.y, q.z};
89
+ const LagCompVec3 t = vec3Scale(vec3Cross(qv, v), 2.0);
90
+ return vec3Add(v, vec3Add(vec3Scale(t, q.w), vec3Cross(qv, t)));
91
+ }
92
+
93
+ LagCompVec3 invRotateVec3(const LagCompQuat& q, const LagCompVec3& v) noexcept {
94
+ return rotateVec3(quatConjugate(q), v);
95
+ }
96
+
97
+ LagCompQuat quatSlerp(const LagCompQuat& q1, const LagCompQuat& q2In, double t) noexcept {
98
+ LagCompQuat q2 = q2In;
99
+ double cosHalfTheta = quatDot(q1, q2);
100
+ if (cosHalfTheta < 0.0) {
101
+ q2.x = -q2.x;
102
+ q2.y = -q2.y;
103
+ q2.z = -q2.z;
104
+ q2.w = -q2.w;
105
+ cosHalfTheta = -cosHalfTheta;
106
+ }
107
+ if (cosHalfTheta >= 0.9995) {
108
+ const LagCompQuat result{
109
+ q1.x + t * (q2.x - q1.x),
110
+ q1.y + t * (q2.y - q1.y),
111
+ q1.z + t * (q2.z - q1.z),
112
+ q1.w + t * (q2.w - q1.w),
113
+ };
114
+ return quatNormalize(result);
115
+ }
116
+ const double sinHalfTheta = detSqrt(1.0 - cosHalfTheta * cosHalfTheta);
117
+ const double halfTheta = std::acos(cosHalfTheta);
118
+ const double ratioA = std::sin((1.0 - t) * halfTheta) / sinHalfTheta;
119
+ const double ratioB = std::sin(t * halfTheta) / sinHalfTheta;
120
+ return {
121
+ q1.x * ratioA + q2.x * ratioB,
122
+ q1.y * ratioA + q2.y * ratioB,
123
+ q1.z * ratioA + q2.z * ratioB,
124
+ q1.w * ratioA + q2.w * ratioB,
125
+ };
126
+ }
127
+
128
+ LagCompAABB computeBodyAABB(
129
+ const LagCompVec3& pos,
130
+ const LagCompQuat& rot,
131
+ ShapeKind shape,
132
+ const LagCompVec3& dims,
133
+ const LagCompVec3& offset) noexcept {
134
+ const LagCompVec3 center = vec3Add(pos, rotateVec3(rot, offset));
135
+ LagCompAABB aabb{};
136
+
137
+ if (shape == ShapeKind::Sphere) {
138
+ const double r = dims.x > 0.0 ? dims.x : 0.5;
139
+ aabb.minX = center.x - r; aabb.maxX = center.x + r;
140
+ aabb.minY = center.y - r; aabb.maxY = center.y + r;
141
+ aabb.minZ = center.z - r; aabb.maxZ = center.z + r;
142
+ } else if (shape == ShapeKind::Box) {
143
+ const double hx = dims.x > 0.0 ? dims.x : 0.5;
144
+ const double hy = dims.y > 0.0 ? dims.y : 0.5;
145
+ const double hz = dims.z > 0.0 ? dims.z : 0.5;
146
+
147
+ const LagCompVec3 ax = rotateVec3(rot, {1.0, 0.0, 0.0});
148
+ const LagCompVec3 ay = rotateVec3(rot, {0.0, 1.0, 0.0});
149
+ const LagCompVec3 az = rotateVec3(rot, {0.0, 0.0, 1.0});
150
+
151
+ const double ex = std::abs(ax.x) * hx + std::abs(ay.x) * hy + std::abs(az.x) * hz;
152
+ const double ey = std::abs(ax.y) * hx + std::abs(ay.y) * hy + std::abs(az.y) * hz;
153
+ const double ez = std::abs(ax.z) * hx + std::abs(ay.z) * hy + std::abs(az.z) * hz;
154
+
155
+ aabb.minX = center.x - ex; aabb.maxX = center.x + ex;
156
+ aabb.minY = center.y - ey; aabb.maxY = center.y + ey;
157
+ aabb.minZ = center.z - ez; aabb.maxZ = center.z + ez;
158
+ } else if (shape == ShapeKind::Capsule || shape == ShapeKind::Cylinder) {
159
+ const double r = dims.x > 0.0 ? dims.x : 0.5;
160
+ const double hh = dims.y > 0.0 ? dims.y : 0.5;
161
+
162
+ const LagCompVec3 p1 = vec3Add(center, rotateVec3(rot, {0.0, -hh, 0.0}));
163
+ const LagCompVec3 p2 = vec3Add(center, rotateVec3(rot, {0.0, +hh, 0.0}));
164
+
165
+ aabb.minX = std::min(p1.x, p2.x) - r; aabb.maxX = std::max(p1.x, p2.x) + r;
166
+ aabb.minY = std::min(p1.y, p2.y) - r; aabb.maxY = std::max(p1.y, p2.y) + r;
167
+ aabb.minZ = std::min(p1.z, p2.z) - r; aabb.maxZ = std::max(p1.z, p2.z) + r;
168
+ } else {
169
+ // Other shapes: approximate with bounding radius or proxy extents
170
+ const double maxDim = std::max({dims.x, dims.y, dims.z, 0.5});
171
+ aabb.minX = center.x - maxDim; aabb.maxX = center.x + maxDim;
172
+ aabb.minY = center.y - maxDim; aabb.maxY = center.y + maxDim;
173
+ aabb.minZ = center.z - maxDim; aabb.maxZ = center.z + maxDim;
174
+ }
175
+ return aabb;
176
+ }
177
+
178
+ // ---------------------------------------------------------------------------
179
+ // Ray-Primitive Intersection Helpers
180
+ // ---------------------------------------------------------------------------
181
+
182
+ static bool rayAABB(
183
+ const LagCompVec3& origin,
184
+ const LagCompVec3& dir,
185
+ const LagCompAABB& aabb,
186
+ double maxDist,
187
+ double& outTNear) noexcept {
188
+ double tmin = 0.0;
189
+ double tmax = maxDist;
190
+
191
+ // X slab
192
+ if (std::abs(dir.x) < 1e-15) {
193
+ if (origin.x < aabb.minX || origin.x > aabb.maxX) return false;
194
+ } else {
195
+ const double invD = 1.0 / dir.x;
196
+ double t1 = (aabb.minX - origin.x) * invD;
197
+ double t2 = (aabb.maxX - origin.x) * invD;
198
+ if (t1 > t2) std::swap(t1, t2);
199
+ tmin = std::max(tmin, t1);
200
+ tmax = std::min(tmax, t2);
201
+ if (tmin > tmax) return false;
202
+ }
203
+
204
+ // Y slab
205
+ if (std::abs(dir.y) < 1e-15) {
206
+ if (origin.y < aabb.minY || origin.y > aabb.maxY) return false;
207
+ } else {
208
+ const double invD = 1.0 / dir.y;
209
+ double t1 = (aabb.minY - origin.y) * invD;
210
+ double t2 = (aabb.maxY - origin.y) * invD;
211
+ if (t1 > t2) std::swap(t1, t2);
212
+ tmin = std::max(tmin, t1);
213
+ tmax = std::min(tmax, t2);
214
+ if (tmin > tmax) return false;
215
+ }
216
+
217
+ // Z slab
218
+ if (std::abs(dir.z) < 1e-15) {
219
+ if (origin.z < aabb.minZ || origin.z > aabb.maxZ) return false;
220
+ } else {
221
+ const double invD = 1.0 / dir.z;
222
+ double t1 = (aabb.minZ - origin.z) * invD;
223
+ double t2 = (aabb.maxZ - origin.z) * invD;
224
+ if (t1 > t2) std::swap(t1, t2);
225
+ tmin = std::max(tmin, t1);
226
+ tmax = std::min(tmax, t2);
227
+ if (tmin > tmax) return false;
228
+ }
229
+
230
+ outTNear = tmin;
231
+ return true;
232
+ }
233
+
234
+ static bool raySphere(
235
+ const LagCompVec3& origin,
236
+ const LagCompVec3& dir,
237
+ const LagCompVec3& center,
238
+ double radius,
239
+ double maxDist,
240
+ double& outDist,
241
+ LagCompVec3& outNormal) noexcept {
242
+ const LagCompVec3 m = vec3Sub(origin, center);
243
+ const double b = vec3Dot(m, dir);
244
+ const double c = vec3Dot(m, m) - radius * radius;
245
+ if (c > 0.0 && b > 0.0) return false;
246
+ const double discr = b * b - c;
247
+ if (discr < 0.0) return false;
248
+ double t = -b - detSqrt(discr);
249
+ if (t < 0.0) t = -b + detSqrt(discr);
250
+ if (t < 0.0 || t > maxDist) return false;
251
+ outDist = t;
252
+ const LagCompVec3 hitPt = vec3Add(origin, vec3Scale(dir, t));
253
+ outNormal = vec3Normalize(vec3Sub(hitPt, center));
254
+ return true;
255
+ }
256
+
257
+ static bool rayBox(
258
+ const LagCompVec3& origin,
259
+ const LagCompVec3& dir,
260
+ const LagCompVec3& center,
261
+ const LagCompQuat& orientation,
262
+ const LagCompVec3& halfExtents,
263
+ double maxDist,
264
+ double& outDist,
265
+ LagCompVec3& outNormal) noexcept {
266
+ const LagCompVec3 localOrigin = invRotateVec3(orientation, vec3Sub(origin, center));
267
+ const LagCompVec3 localDir = invRotateVec3(orientation, dir);
268
+
269
+ double tmin = 0.0;
270
+ double tmax = maxDist;
271
+ LagCompVec3 normalMin{-1.0, 0.0, 0.0};
272
+
273
+ // X slab
274
+ if (std::abs(localDir.x) < 1e-15) {
275
+ if (std::abs(localOrigin.x) > halfExtents.x) return false;
276
+ } else {
277
+ const double invD = 1.0 / localDir.x;
278
+ double t1 = (-halfExtents.x - localOrigin.x) * invD;
279
+ double t2 = (halfExtents.x - localOrigin.x) * invD;
280
+ LagCompVec3 n1{-1.0, 0.0, 0.0};
281
+ if (t1 > t2) {
282
+ std::swap(t1, t2);
283
+ n1 = {1.0, 0.0, 0.0};
284
+ }
285
+ if (t1 > tmin) {
286
+ tmin = t1;
287
+ normalMin = n1;
288
+ }
289
+ tmax = std::min(tmax, t2);
290
+ if (tmin > tmax) return false;
291
+ }
292
+
293
+ // Y slab
294
+ if (std::abs(localDir.y) < 1e-15) {
295
+ if (std::abs(localOrigin.y) > halfExtents.y) return false;
296
+ } else {
297
+ const double invD = 1.0 / localDir.y;
298
+ double t1 = (-halfExtents.y - localOrigin.y) * invD;
299
+ double t2 = (halfExtents.y - localOrigin.y) * invD;
300
+ LagCompVec3 n1{0.0, -1.0, 0.0};
301
+ if (t1 > t2) {
302
+ std::swap(t1, t2);
303
+ n1 = {0.0, 1.0, 0.0};
304
+ }
305
+ if (t1 > tmin) {
306
+ tmin = t1;
307
+ normalMin = n1;
308
+ }
309
+ tmax = std::min(tmax, t2);
310
+ if (tmin > tmax) return false;
311
+ }
312
+
313
+ // Z slab
314
+ if (std::abs(localDir.z) < 1e-15) {
315
+ if (std::abs(localOrigin.z) > halfExtents.z) return false;
316
+ } else {
317
+ const double invD = 1.0 / localDir.z;
318
+ double t1 = (-halfExtents.z - localOrigin.z) * invD;
319
+ double t2 = (halfExtents.z - localOrigin.z) * invD;
320
+ LagCompVec3 n1{0.0, 0.0, -1.0};
321
+ if (t1 > t2) {
322
+ std::swap(t1, t2);
323
+ n1 = {0.0, 0.0, 1.0};
324
+ }
325
+ if (t1 > tmin) {
326
+ tmin = t1;
327
+ normalMin = n1;
328
+ }
329
+ tmax = std::min(tmax, t2);
330
+ if (tmin > tmax) return false;
331
+ }
332
+
333
+ outDist = tmin;
334
+ outNormal = rotateVec3(orientation, normalMin);
335
+ return true;
336
+ }
337
+
338
+ static bool rayCapsule(
339
+ const LagCompVec3& origin,
340
+ const LagCompVec3& dir,
341
+ const LagCompVec3& center,
342
+ const LagCompQuat& orientation,
343
+ double radius,
344
+ double halfHeight,
345
+ double maxDist,
346
+ double& outDist,
347
+ LagCompVec3& outNormal) noexcept {
348
+ const LagCompVec3 localOrigin = invRotateVec3(orientation, vec3Sub(origin, center));
349
+ const LagCompVec3 localDir = invRotateVec3(orientation, dir);
350
+
351
+ double bestT = maxDist + 1.0;
352
+ LagCompVec3 bestNormal{0.0, 1.0, 0.0};
353
+
354
+ // Cylinder body: x^2 + z^2 = r^2, -halfHeight <= y <= halfHeight
355
+ const double a = localDir.x * localDir.x + localDir.z * localDir.z;
356
+ const double b = 2.0 * (localOrigin.x * localDir.x + localOrigin.z * localDir.z);
357
+ const double c = localOrigin.x * localOrigin.x + localOrigin.z * localDir.z - radius * radius;
358
+
359
+ if (a > 1e-15) {
360
+ const double discr = b * b - 4.0 * a * c;
361
+ if (discr >= 0.0) {
362
+ const double sqrtDiscr = detSqrt(discr);
363
+ const double t1 = (-b - sqrtDiscr) / (2.0 * a);
364
+ const double t2 = (-b + sqrtDiscr) / (2.0 * a);
365
+
366
+ auto checkCylHit = [&](double t) {
367
+ if (t >= 0.0 && t < bestT) {
368
+ const double y = localOrigin.y + t * localDir.y;
369
+ if (y >= -halfHeight && y <= halfHeight) {
370
+ bestT = t;
371
+ bestNormal = vec3Normalize({localOrigin.x + t * localDir.x, 0.0, localOrigin.z + t * localDir.z});
372
+ }
373
+ }
374
+ };
375
+ checkCylHit(t1);
376
+ checkCylHit(t2);
377
+ }
378
+ }
379
+
380
+ // Top hemisphere
381
+ double capT = 0.0;
382
+ LagCompVec3 capN{};
383
+ if (raySphere(localOrigin, localDir, {0.0, halfHeight, 0.0}, radius, maxDist, capT, capN)) {
384
+ const double y = localOrigin.y + capT * localDir.y;
385
+ if (y >= halfHeight && capT < bestT) {
386
+ bestT = capT;
387
+ bestNormal = capN;
388
+ }
389
+ }
390
+
391
+ // Bottom hemisphere
392
+ if (raySphere(localOrigin, localDir, {0.0, -halfHeight, 0.0}, radius, maxDist, capT, capN)) {
393
+ const double y = localOrigin.y + capT * localDir.y;
394
+ if (y <= -halfHeight && capT < bestT) {
395
+ bestT = capT;
396
+ bestNormal = capN;
397
+ }
398
+ }
399
+
400
+ if (bestT <= maxDist) {
401
+ outDist = bestT;
402
+ outNormal = rotateVec3(orientation, bestNormal);
403
+ return true;
404
+ }
405
+ return false;
406
+ }
407
+
408
+ static bool rayCylinder(
409
+ const LagCompVec3& origin,
410
+ const LagCompVec3& dir,
411
+ const LagCompVec3& center,
412
+ const LagCompQuat& orientation,
413
+ double radius,
414
+ double halfHeight,
415
+ double maxDist,
416
+ double& outDist,
417
+ LagCompVec3& outNormal) noexcept {
418
+ const LagCompVec3 localOrigin = invRotateVec3(orientation, vec3Sub(origin, center));
419
+ const LagCompVec3 localDir = invRotateVec3(orientation, dir);
420
+
421
+ double bestT = maxDist + 1.0;
422
+ LagCompVec3 bestNormal{0.0, 1.0, 0.0};
423
+
424
+ // Cylinder surface
425
+ const double a = localDir.x * localDir.x + localDir.z * localDir.z;
426
+ const double b = 2.0 * (localOrigin.x * localDir.x + localOrigin.z * localDir.z);
427
+ const double c = localOrigin.x * localOrigin.x + localOrigin.z * localDir.z - radius * radius;
428
+
429
+ if (a > 1e-15) {
430
+ const double discr = b * b - 4.0 * a * c;
431
+ if (discr >= 0.0) {
432
+ const double sqrtDiscr = detSqrt(discr);
433
+ const double t1 = (-b - sqrtDiscr) / (2.0 * a);
434
+ const double t2 = (-b + sqrtDiscr) / (2.0 * a);
435
+
436
+ auto checkCylHit = [&](double t) {
437
+ if (t >= 0.0 && t < bestT) {
438
+ const double y = localOrigin.y + t * localDir.y;
439
+ if (y >= -halfHeight && y <= halfHeight) {
440
+ bestT = t;
441
+ bestNormal = vec3Normalize({localOrigin.x + t * localDir.x, 0.0, localOrigin.z + t * localDir.z});
442
+ }
443
+ }
444
+ };
445
+ checkCylHit(t1);
446
+ checkCylHit(t2);
447
+ }
448
+ }
449
+
450
+ // Top disc cap (y = +halfHeight)
451
+ if (std::abs(localDir.y) > 1e-15) {
452
+ const double t = (halfHeight - localOrigin.y) / localDir.y;
453
+ if (t >= 0.0 && t < bestT) {
454
+ const double hx = localOrigin.x + t * localDir.x;
455
+ const double hz = localOrigin.z + t * localDir.z;
456
+ if (hx * hx + hz * hz <= radius * radius) {
457
+ bestT = t;
458
+ bestNormal = {0.0, 1.0, 0.0};
459
+ }
460
+ }
461
+ }
462
+
463
+ // Bottom disc cap (y = -halfHeight)
464
+ if (std::abs(localDir.y) > 1e-15) {
465
+ const double t = (-halfHeight - localOrigin.y) / localDir.y;
466
+ if (t >= 0.0 && t < bestT) {
467
+ const double hx = localOrigin.x + t * localDir.x;
468
+ const double hz = localOrigin.z + t * localDir.z;
469
+ if (hx * hx + hz * hz <= radius * radius) {
470
+ bestT = t;
471
+ bestNormal = {0.0, -1.0, 0.0};
472
+ }
473
+ }
474
+ }
475
+
476
+ if (bestT <= maxDist) {
477
+ outDist = bestT;
478
+ outNormal = rotateVec3(orientation, bestNormal);
479
+ return true;
480
+ }
481
+ return false;
482
+ }
483
+
484
+ // ---------------------------------------------------------------------------
485
+ // LagCompensationBuffer Implementation
486
+ // ---------------------------------------------------------------------------
487
+
488
+ LagCompensationBuffer::LagCompensationBuffer(
489
+ std::uint32_t maxFrames,
490
+ std::uint32_t maxBodies)
491
+ : maxFrames_(std::min(maxFrames, kLagCompensationMaxFrames)),
492
+ maxBodies_(std::min(maxBodies, kLagCompensationMaxBodies)),
493
+ frames_(maxFrames_) {
494
+ clear();
495
+ }
496
+
497
+ void LagCompensationBuffer::clear() noexcept {
498
+ frameCount_ = 0;
499
+ headIndex_ = 0;
500
+ oldestTick_ = 0;
501
+ newestTick_ = 0;
502
+ globalTopologyGen_ = 0;
503
+ for (auto& f : frames_) {
504
+ f.valid = false;
505
+ f.bodyCount = 0;
506
+ }
507
+ }
508
+
509
+ const LagCompFrameSnapshot* LagCompensationBuffer::findFrame(std::uint64_t tick) const noexcept {
510
+ if (frameCount_ == 0 || tick < oldestTick_ || tick > newestTick_) return nullptr;
511
+ for (std::uint32_t i = 0; i < frameCount_; ++i) {
512
+ const std::uint32_t idx = (headIndex_ + maxFrames_ - 1 - i) % maxFrames_;
513
+ if (frames_[idx].valid && frames_[idx].tick == tick) {
514
+ return &frames_[idx];
515
+ }
516
+ }
517
+ return nullptr;
518
+ }
519
+
520
+ LagCompFrameSnapshot* LagCompensationBuffer::findFrameMut(std::uint64_t tick) noexcept {
521
+ if (frameCount_ == 0 || tick < oldestTick_ || tick > newestTick_) return nullptr;
522
+ for (std::uint32_t i = 0; i < frameCount_; ++i) {
523
+ const std::uint32_t idx = (headIndex_ + maxFrames_ - 1 - i) % maxFrames_;
524
+ if (frames_[idx].valid && frames_[idx].tick == tick) {
525
+ return &frames_[idx];
526
+ }
527
+ }
528
+ return nullptr;
529
+ }
530
+
531
+ bool LagCompensationBuffer::hasTick(std::uint64_t tick) const noexcept {
532
+ return findFrame(tick) != nullptr;
533
+ }
534
+
535
+ std::uint64_t LagCompensationBuffer::oldestTick() const noexcept {
536
+ return oldestTick_;
537
+ }
538
+
539
+ std::uint64_t LagCompensationBuffer::newestTick() const noexcept {
540
+ return newestTick_;
541
+ }
542
+
543
+ std::uint32_t LagCompensationBuffer::frameCount() const noexcept {
544
+ return frameCount_;
545
+ }
546
+
547
+ LagCompStatus LagCompensationBuffer::recordSnapshot(
548
+ std::uint64_t tick,
549
+ double timestamp,
550
+ const double* bodyData,
551
+ std::uint32_t bodyCount,
552
+ const std::uint64_t* bodyIds,
553
+ std::uint32_t globalGeometryGen,
554
+ const std::uint32_t* perBodyGeometryGen) noexcept {
555
+ if (bodyData == nullptr && bodyCount > 0) return LagCompStatus::InvalidInput;
556
+ const std::uint32_t clampedBodies = std::min(bodyCount, maxBodies_);
557
+
558
+ const std::uint32_t writeIndex = headIndex_;
559
+ LagCompFrameSnapshot& frame = frames_[writeIndex];
560
+ frame.tick = tick;
561
+ frame.timestamp = timestamp;
562
+ frame.bodyCount = clampedBodies;
563
+ frame.geometryGeneration = globalGeometryGen;
564
+ frame.valid = true;
565
+
566
+ // Detect topology change compared to previous frame
567
+ bool topologyChanged = false;
568
+ if (frameCount_ > 0) {
569
+ const std::uint32_t prevIndex = (headIndex_ + maxFrames_ - 1) % maxFrames_;
570
+ const auto& prevFrame = frames_[prevIndex];
571
+ if (prevFrame.valid) {
572
+ if (prevFrame.bodyCount != clampedBodies) {
573
+ topologyChanged = true;
574
+ } else if (bodyIds != nullptr) {
575
+ for (std::uint32_t i = 0; i < clampedBodies; ++i) {
576
+ if (prevFrame.bodies[i].bodyId != bodyIds[i]) {
577
+ topologyChanged = true;
578
+ break;
579
+ }
580
+ }
581
+ }
582
+ }
583
+ }
584
+ if (topologyChanged || frameCount_ == 0) {
585
+ globalTopologyGen_ += 1;
586
+ }
587
+ frame.topologyGeneration = globalTopologyGen_;
588
+
589
+ for (std::uint32_t i = 0; i < clampedBodies; ++i) {
590
+ const std::uint32_t offset = i * kBodyStride;
591
+ LagCompBodySnapshot& b = frame.bodies[i];
592
+
593
+ b.position = {bodyData[offset + 0], bodyData[offset + 1], bodyData[offset + 2]};
594
+ b.linearVelocity = {bodyData[offset + 3], bodyData[offset + 4], bodyData[offset + 5]};
595
+ b.shapeDimensions = {bodyData[offset + 6], bodyData[offset + 7], bodyData[offset + 8]};
596
+ b.motionType = static_cast<std::uint32_t>(bodyData[offset + 9]);
597
+ b.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[offset + 10]));
598
+ b.layer = static_cast<std::uint32_t>(bodyData[offset + 11]);
599
+ b.mask = static_cast<std::uint32_t>(bodyData[offset + 12]);
600
+ b.shapeOffset = {bodyData[offset + 15], bodyData[offset + 16], bodyData[offset + 17]};
601
+ b.angularVelocity = {bodyData[offset + 19], bodyData[offset + 20], bodyData[offset + 21]};
602
+
603
+ LagCompQuat rawQuat{
604
+ bodyData[offset + 22],
605
+ bodyData[offset + 23],
606
+ bodyData[offset + 24],
607
+ bodyData[offset + 25]
608
+ };
609
+ b.orientation = quatNormalize(rawQuat);
610
+
611
+ b.geometryOffset = (b.shape == ShapeKind::Hull || b.shape == ShapeKind::Mesh
612
+ || b.shape == ShapeKind::Compound || b.shape == ShapeKind::Heightfield)
613
+ ? static_cast<std::uint32_t>(b.shapeDimensions.x) : 0;
614
+ b.geometryCount = (b.shape == ShapeKind::Hull || b.shape == ShapeKind::Mesh
615
+ || b.shape == ShapeKind::Compound || b.shape == ShapeKind::Heightfield)
616
+ ? static_cast<std::uint32_t>(b.shapeDimensions.y) : 0;
617
+
618
+ b.bodyId = (bodyIds != nullptr) ? bodyIds[i] : static_cast<std::uint64_t>(i + 1);
619
+ b.geometryGeneration = (perBodyGeometryGen != nullptr) ? perBodyGeometryGen[i] : globalGeometryGen;
620
+ b.active = true;
621
+
622
+ // Copy raw 44 doubles
623
+ for (std::uint32_t c = 0; c < kBodyStride; ++c) {
624
+ b.rawData[c] = bodyData[offset + c];
625
+ }
626
+ b.rawData[44] = static_cast<double>(b.bodyId & 0xffffffffu);
627
+ b.rawData[45] = static_cast<double>(b.bodyId >> 32);
628
+ b.rawData[46] = static_cast<double>(b.geometryGeneration);
629
+ b.rawData[47] = 1.0;
630
+
631
+ b.aabb = computeBodyAABB(b.position, b.orientation, b.shape, b.shapeDimensions, b.shapeOffset);
632
+ }
633
+
634
+ // Ring buffer advance
635
+ headIndex_ = (headIndex_ + 1) % maxFrames_;
636
+ if (frameCount_ < maxFrames_) {
637
+ frameCount_ += 1;
638
+ } else {
639
+ // Overwritten oldest frame; update oldestTick_
640
+ oldestTick_ = frames_[headIndex_].tick;
641
+ }
642
+ if (frameCount_ == 1) {
643
+ oldestTick_ = tick;
644
+ }
645
+ newestTick_ = tick;
646
+
647
+ return LagCompStatus::Success;
648
+ }
649
+
650
+ LagCompStatus LagCompensationBuffer::findTicksForTime(
651
+ double timestamp,
652
+ std::uint64_t& outTickA,
653
+ std::uint64_t& outTickB,
654
+ double& outAlpha) const noexcept {
655
+ if (frameCount_ == 0) return LagCompStatus::TickOutOfRange;
656
+
657
+ const LagCompFrameSnapshot* newest = findFrame(newestTick_);
658
+ const LagCompFrameSnapshot* oldest = findFrame(oldestTick_);
659
+ if (!newest || !oldest) return LagCompStatus::TickOutOfRange;
660
+
661
+ if (timestamp >= newest->timestamp) {
662
+ outTickA = newest->tick;
663
+ outTickB = newest->tick;
664
+ outAlpha = 0.0;
665
+ return LagCompStatus::Success;
666
+ }
667
+ if (timestamp <= oldest->timestamp) {
668
+ outTickA = oldest->tick;
669
+ outTickB = oldest->tick;
670
+ outAlpha = 0.0;
671
+ return LagCompStatus::Success;
672
+ }
673
+
674
+ for (std::uint32_t i = 0; i < frameCount_ - 1; ++i) {
675
+ const std::uint32_t idxA = (headIndex_ + maxFrames_ - 2 - i) % maxFrames_;
676
+ const std::uint32_t idxB = (idxA + 1) % maxFrames_;
677
+ const auto& fA = frames_[idxA];
678
+ const auto& fB = frames_[idxB];
679
+ if (fA.valid && fB.valid && fA.timestamp <= timestamp && fB.timestamp >= timestamp) {
680
+ outTickA = fA.tick;
681
+ outTickB = fB.tick;
682
+ const double dt = fB.timestamp - fA.timestamp;
683
+ outAlpha = (dt > 1e-9) ? (timestamp - fA.timestamp) / dt : 0.0;
684
+ return LagCompStatus::Success;
685
+ }
686
+ }
687
+
688
+ outTickA = newest->tick;
689
+ outTickB = newest->tick;
690
+ outAlpha = 0.0;
691
+ return LagCompStatus::Success;
692
+ }
693
+
694
+ LagCompStatus LagCompensationBuffer::queryHistoricalTopology(
695
+ std::uint64_t tick,
696
+ std::uint32_t& outBodyCount,
697
+ std::span<std::uint64_t> outBodyIds) const noexcept {
698
+ const LagCompFrameSnapshot* frame = findFrame(tick);
699
+ if (!frame) return LagCompStatus::TickOutOfRange;
700
+
701
+ outBodyCount = frame->bodyCount;
702
+ const std::size_t copyCount = std::min(outBodyIds.size(), static_cast<std::size_t>(frame->bodyCount));
703
+ for (std::size_t i = 0; i < copyCount; ++i) {
704
+ outBodyIds[i] = frame->bodies[i].bodyId;
705
+ }
706
+ return LagCompStatus::Success;
707
+ }
708
+
709
+ LagCompStatus LagCompensationBuffer::queryHistoricalBody(
710
+ std::uint64_t tick,
711
+ std::uint64_t bodyId,
712
+ LagCompBodySnapshot& outBody) const noexcept {
713
+ const LagCompFrameSnapshot* frame = findFrame(tick);
714
+ if (!frame) return LagCompStatus::TickOutOfRange;
715
+
716
+ for (std::uint32_t i = 0; i < frame->bodyCount; ++i) {
717
+ if (frame->bodies[i].active && frame->bodies[i].bodyId == bodyId) {
718
+ outBody = frame->bodies[i];
719
+ return LagCompStatus::Success;
720
+ }
721
+ }
722
+ return LagCompStatus::BodyNotFound;
723
+ }
724
+
725
+ LagCompStatus LagCompensationBuffer::queryHistoricalBodyByIndex(
726
+ std::uint64_t tick,
727
+ std::uint32_t bodyIndex,
728
+ LagCompBodySnapshot& outBody) const noexcept {
729
+ const LagCompFrameSnapshot* frame = findFrame(tick);
730
+ if (!frame) return LagCompStatus::TickOutOfRange;
731
+ if (bodyIndex >= frame->bodyCount) return LagCompStatus::BodyNotFound;
732
+
733
+ outBody = frame->bodies[bodyIndex];
734
+ return LagCompStatus::Success;
735
+ }
736
+
737
+ LagCompStatus LagCompensationBuffer::queryHistoricalColliderFilter(
738
+ std::uint64_t tick,
739
+ std::uint64_t bodyId,
740
+ std::uint32_t& outLayer,
741
+ std::uint32_t& outMask,
742
+ std::uint32_t& outMotionType) const noexcept {
743
+ LagCompBodySnapshot b{};
744
+ const LagCompStatus status = queryHistoricalBody(tick, bodyId, b);
745
+ if (status != LagCompStatus::Success) return status;
746
+
747
+ outLayer = b.layer;
748
+ outMask = b.mask;
749
+ outMotionType = b.motionType;
750
+ return LagCompStatus::Success;
751
+ }
752
+
753
+ LagCompStatus LagCompensationBuffer::queryHistoricalGeometry(
754
+ std::uint64_t tick,
755
+ std::uint64_t bodyId,
756
+ ShapeKind& outShapeKind,
757
+ LagCompVec3& outShapeDims,
758
+ LagCompVec3& outShapeOffset,
759
+ std::uint32_t& outGeometryOffset,
760
+ std::uint32_t& outGeometryCount,
761
+ std::uint32_t& outGeometryGeneration) const noexcept {
762
+ LagCompBodySnapshot b{};
763
+ const LagCompStatus status = queryHistoricalBody(tick, bodyId, b);
764
+ if (status != LagCompStatus::Success) return status;
765
+
766
+ outShapeKind = b.shape;
767
+ outShapeDims = b.shapeDimensions;
768
+ outShapeOffset = b.shapeOffset;
769
+ outGeometryOffset = b.geometryOffset;
770
+ outGeometryCount = b.geometryCount;
771
+ outGeometryGeneration = b.geometryGeneration;
772
+ return LagCompStatus::Success;
773
+ }
774
+
775
+ LagCompStatus LagCompensationBuffer::queryHistoricalPose(
776
+ std::uint64_t tick,
777
+ std::uint64_t bodyId,
778
+ LagCompVec3& outPosition,
779
+ LagCompQuat& outOrientation,
780
+ LagCompVec3& outLinearVel,
781
+ LagCompVec3& outAngularVel) const noexcept {
782
+ LagCompBodySnapshot b{};
783
+ const LagCompStatus status = queryHistoricalBody(tick, bodyId, b);
784
+ if (status != LagCompStatus::Success) return status;
785
+
786
+ outPosition = b.position;
787
+ outOrientation = b.orientation;
788
+ outLinearVel = b.linearVelocity;
789
+ outAngularVel = b.angularVelocity;
790
+ return LagCompStatus::Success;
791
+ }
792
+
793
+ LagCompStatus LagCompensationBuffer::queryInterpolatedPose(
794
+ std::uint64_t tickA,
795
+ std::uint64_t tickB,
796
+ double alpha,
797
+ std::uint64_t bodyId,
798
+ LagCompVec3& outPosition,
799
+ LagCompQuat& outOrientation) const noexcept {
800
+ if (alpha <= 0.0) {
801
+ LagCompVec3 lv{}, av{};
802
+ return queryHistoricalPose(tickA, bodyId, outPosition, outOrientation, lv, av);
803
+ }
804
+ if (alpha >= 1.0) {
805
+ LagCompVec3 lv{}, av{};
806
+ return queryHistoricalPose(tickB, bodyId, outPosition, outOrientation, lv, av);
807
+ }
808
+
809
+ LagCompBodySnapshot bA{}, bB{};
810
+ const LagCompStatus stA = queryHistoricalBody(tickA, bodyId, bA);
811
+ const LagCompStatus stB = queryHistoricalBody(tickB, bodyId, bB);
812
+ if (stA != LagCompStatus::Success || stB != LagCompStatus::Success) {
813
+ return LagCompStatus::BodyNotFound;
814
+ }
815
+
816
+ outPosition = {
817
+ (1.0 - alpha) * bA.position.x + alpha * bB.position.x,
818
+ (1.0 - alpha) * bA.position.y + alpha * bB.position.y,
819
+ (1.0 - alpha) * bA.position.z + alpha * bB.position.z,
820
+ };
821
+ outOrientation = quatSlerp(bA.orientation, bB.orientation, alpha);
822
+ return LagCompStatus::Success;
823
+ }
824
+
825
+ LagCompStatus LagCompensationBuffer::rewindScene(
826
+ std::uint64_t tick,
827
+ double* outBodyDataSoA,
828
+ std::uint32_t maxBodies,
829
+ std::uint32_t& outBodyCount) const noexcept {
830
+ if (outBodyDataSoA == nullptr) return LagCompStatus::InvalidInput;
831
+ const LagCompFrameSnapshot* frame = findFrame(tick);
832
+ if (!frame) return LagCompStatus::TickOutOfRange;
833
+
834
+ const std::uint32_t count = std::min(frame->bodyCount, maxBodies);
835
+ outBodyCount = count;
836
+ for (std::uint32_t i = 0; i < count; ++i) {
837
+ const std::uint32_t dstOffset = i * kBodyStride;
838
+ for (std::uint32_t c = 0; c < kBodyStride; ++c) {
839
+ outBodyDataSoA[dstOffset + c] = frame->bodies[i].rawData[c];
840
+ }
841
+ }
842
+ return LagCompStatus::Success;
843
+ }
844
+
845
+ void LagCompensationBuffer::interpolateBody(
846
+ const LagCompBodySnapshot& bodyA,
847
+ const LagCompBodySnapshot& bodyB,
848
+ double alpha,
849
+ LagCompBodySnapshot& outBody) const noexcept {
850
+ outBody = (alpha < 0.5) ? bodyA : bodyB;
851
+ outBody.position = {
852
+ (1.0 - alpha) * bodyA.position.x + alpha * bodyB.position.x,
853
+ (1.0 - alpha) * bodyA.position.y + alpha * bodyB.position.y,
854
+ (1.0 - alpha) * bodyA.position.z + alpha * bodyB.position.z,
855
+ };
856
+ outBody.linearVelocity = {
857
+ (1.0 - alpha) * bodyA.linearVelocity.x + alpha * bodyB.linearVelocity.x,
858
+ (1.0 - alpha) * bodyA.linearVelocity.y + alpha * bodyB.linearVelocity.y,
859
+ (1.0 - alpha) * bodyA.linearVelocity.z + alpha * bodyB.linearVelocity.z,
860
+ };
861
+ outBody.angularVelocity = {
862
+ (1.0 - alpha) * bodyA.angularVelocity.x + alpha * bodyB.angularVelocity.x,
863
+ (1.0 - alpha) * bodyA.angularVelocity.y + alpha * bodyB.angularVelocity.y,
864
+ (1.0 - alpha) * bodyA.angularVelocity.z + alpha * bodyB.angularVelocity.z,
865
+ };
866
+ outBody.orientation = quatSlerp(bodyA.orientation, bodyB.orientation, alpha);
867
+ outBody.aabb = computeBodyAABB(outBody.position, outBody.orientation, outBody.shape,
868
+ outBody.shapeDimensions, outBody.shapeOffset);
869
+
870
+ // Update rawData with interpolated pose
871
+ outBody.rawData[0] = outBody.position.x;
872
+ outBody.rawData[1] = outBody.position.y;
873
+ outBody.rawData[2] = outBody.position.z;
874
+ outBody.rawData[3] = outBody.linearVelocity.x;
875
+ outBody.rawData[4] = outBody.linearVelocity.y;
876
+ outBody.rawData[5] = outBody.linearVelocity.z;
877
+ outBody.rawData[19] = outBody.angularVelocity.x;
878
+ outBody.rawData[20] = outBody.angularVelocity.y;
879
+ outBody.rawData[21] = outBody.angularVelocity.z;
880
+ outBody.rawData[22] = outBody.orientation.x;
881
+ outBody.rawData[23] = outBody.orientation.y;
882
+ outBody.rawData[24] = outBody.orientation.z;
883
+ outBody.rawData[25] = outBody.orientation.w;
884
+ }
885
+
886
+ LagCompStatus LagCompensationBuffer::rewindSceneInterpolated(
887
+ std::uint64_t tickA,
888
+ std::uint64_t tickB,
889
+ double alpha,
890
+ double* outBodyDataSoA,
891
+ std::uint32_t maxBodies,
892
+ std::uint32_t& outBodyCount) const noexcept {
893
+ if (outBodyDataSoA == nullptr) return LagCompStatus::InvalidInput;
894
+ if (alpha <= 0.0) return rewindScene(tickA, outBodyDataSoA, maxBodies, outBodyCount);
895
+ if (alpha >= 1.0) return rewindScene(tickB, outBodyDataSoA, maxBodies, outBodyCount);
896
+
897
+ const LagCompFrameSnapshot* frameA = findFrame(tickA);
898
+ const LagCompFrameSnapshot* frameB = findFrame(tickB);
899
+ if (!frameA || !frameB) return LagCompStatus::TickOutOfRange;
900
+
901
+ std::uint32_t count = 0;
902
+ for (std::uint32_t i = 0; i < frameA->bodyCount && count < maxBodies; ++i) {
903
+ const auto& bA = frameA->bodies[i];
904
+ if (!bA.active) continue;
905
+
906
+ // Match by bodyId in frameB
907
+ const LagCompBodySnapshot* matchB = nullptr;
908
+ for (std::uint32_t j = 0; j < frameB->bodyCount; ++j) {
909
+ if (frameB->bodies[j].active && frameB->bodies[j].bodyId == bA.bodyId) {
910
+ matchB = &frameB->bodies[j];
911
+ break;
912
+ }
913
+ }
914
+
915
+ LagCompBodySnapshot interpBody{};
916
+ if (matchB) {
917
+ interpolateBody(bA, *matchB, alpha, interpBody);
918
+ } else {
919
+ interpBody = bA; // Exists only in frameA
920
+ }
921
+
922
+ const std::uint32_t dstOffset = count * kBodyStride;
923
+ for (std::uint32_t c = 0; c < kBodyStride; ++c) {
924
+ outBodyDataSoA[dstOffset + c] = interpBody.rawData[c];
925
+ }
926
+ count += 1;
927
+ }
928
+ outBodyCount = count;
929
+ return LagCompStatus::Success;
930
+ }
931
+
932
+ LagCompStatus LagCompensationBuffer::raycast(
933
+ const LagCompRaycastQuery& query,
934
+ std::span<LagCompQueryResult> outResults,
935
+ std::uint32_t& outResultCount) const noexcept {
936
+ outResultCount = 0;
937
+ if (outResults.empty()) return LagCompStatus::InvalidInput;
938
+
939
+ const LagCompVec3 rawDir = query.direction;
940
+ const double dirLen = vec3Length(rawDir);
941
+ if (dirLen <= 1e-15) return LagCompStatus::InvalidInput;
942
+ const LagCompVec3 dir = vec3Scale(rawDir, 1.0 / dirLen);
943
+
944
+ const LagCompFrameSnapshot* frameA = findFrame(query.historicalTick);
945
+ if (!frameA) return LagCompStatus::TickOutOfRange;
946
+
947
+ const LagCompFrameSnapshot* frameB = (query.subTickAlpha > 1e-6)
948
+ ? findFrame(query.historicalTick + 1) : nullptr;
949
+
950
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> hits{};
951
+ std::uint32_t hitCount = 0;
952
+
953
+ for (std::uint32_t i = 0; i < frameA->bodyCount && hitCount < kLagCompensationMaxResults; ++i) {
954
+ const auto& bA = frameA->bodies[i];
955
+ if (!bA.active) continue;
956
+ if (bA.bodyId == query.excludedBodyId) continue;
957
+ if (query.excludedBodyIndex >= 0 && static_cast<std::int32_t>(i) == query.excludedBodyIndex) continue;
958
+
959
+ // Filter layer match
960
+ if ((query.layerMask & bA.layer) == 0) continue;
961
+
962
+ // Determine target body pose (interpolated if sub-tick)
963
+ LagCompBodySnapshot targetBody = bA;
964
+ if (frameB != nullptr) {
965
+ const LagCompBodySnapshot* matchB = nullptr;
966
+ for (std::uint32_t j = 0; j < frameB->bodyCount; ++j) {
967
+ if (frameB->bodies[j].active && frameB->bodies[j].bodyId == bA.bodyId) {
968
+ matchB = &frameB->bodies[j];
969
+ break;
970
+ }
971
+ }
972
+ if (matchB) {
973
+ interpolateBody(bA, *matchB, query.subTickAlpha, targetBody);
974
+ }
975
+ }
976
+
977
+ // Broadphase AABB rejection
978
+ double tNear = 0.0;
979
+ if (!rayAABB(query.origin, dir, targetBody.aabb, query.maxDistance, tNear)) {
980
+ continue;
981
+ }
982
+
983
+ // Narrowphase primitive test
984
+ const LagCompVec3 center = vec3Add(targetBody.position,
985
+ rotateVec3(targetBody.orientation, targetBody.shapeOffset));
986
+ double dist = 0.0;
987
+ LagCompVec3 normal{};
988
+ bool hit = false;
989
+
990
+ if (targetBody.shape == ShapeKind::Box) {
991
+ hit = rayBox(query.origin, dir, center, targetBody.orientation,
992
+ targetBody.shapeDimensions, query.maxDistance, dist, normal);
993
+ } else if (targetBody.shape == ShapeKind::Sphere) {
994
+ hit = raySphere(query.origin, dir, center, targetBody.shapeDimensions.x,
995
+ query.maxDistance, dist, normal);
996
+ } else if (targetBody.shape == ShapeKind::Capsule) {
997
+ hit = rayCapsule(query.origin, dir, center, targetBody.orientation,
998
+ targetBody.shapeDimensions.x, targetBody.shapeDimensions.y,
999
+ query.maxDistance, dist, normal);
1000
+ } else if (targetBody.shape == ShapeKind::Cylinder) {
1001
+ hit = rayCylinder(query.origin, dir, center, targetBody.orientation,
1002
+ targetBody.shapeDimensions.x, targetBody.shapeDimensions.y,
1003
+ query.maxDistance, dist, normal);
1004
+ } else {
1005
+ // Mesh/Hull proxy: OBB test
1006
+ hit = rayBox(query.origin, dir, center, targetBody.orientation,
1007
+ targetBody.shapeDimensions, query.maxDistance, dist, normal);
1008
+ }
1009
+
1010
+ if (hit && dist <= query.maxDistance) {
1011
+ LagCompQueryResult res{};
1012
+ res.hit = true;
1013
+ res.distance = dist;
1014
+ res.point = vec3Add(query.origin, vec3Scale(dir, dist));
1015
+ res.normal = normal;
1016
+ res.bodyIndex = i;
1017
+ res.bodyId = targetBody.bodyId;
1018
+ res.geometryGeneration = targetBody.geometryGeneration;
1019
+ res.shapeKind = targetBody.shape;
1020
+
1021
+ hits[hitCount++] = res;
1022
+ }
1023
+ }
1024
+
1025
+ // Deterministic sort by distance ascending, tie-break by bodyIndex
1026
+ std::stable_sort(hits.begin(), hits.begin() + hitCount,
1027
+ [](const LagCompQueryResult& a, const LagCompQueryResult& b) {
1028
+ if (std::abs(a.distance - b.distance) > 1e-9) {
1029
+ return a.distance < b.distance;
1030
+ }
1031
+ return a.bodyIndex < b.bodyIndex;
1032
+ });
1033
+
1034
+ const std::uint32_t finalCount = std::min(hitCount, static_cast<std::uint32_t>(outResults.size()));
1035
+ for (std::uint32_t k = 0; k < finalCount; ++k) {
1036
+ outResults[k] = hits[k];
1037
+ }
1038
+ outResultCount = finalCount;
1039
+
1040
+ return (finalCount > 0) ? LagCompStatus::Success : LagCompStatus::NoHit;
1041
+ }
1042
+
1043
+ LagCompStatus LagCompensationBuffer::overlap(
1044
+ const LagCompOverlapQuery& query,
1045
+ std::span<LagCompQueryResult> outResults,
1046
+ std::uint32_t& outResultCount) const noexcept {
1047
+ outResultCount = 0;
1048
+ if (outResults.empty()) return LagCompStatus::InvalidInput;
1049
+
1050
+ const LagCompFrameSnapshot* frameA = findFrame(query.historicalTick);
1051
+ if (!frameA) return LagCompStatus::TickOutOfRange;
1052
+
1053
+ const LagCompFrameSnapshot* frameB = (query.subTickAlpha > 1e-6)
1054
+ ? findFrame(query.historicalTick + 1) : nullptr;
1055
+
1056
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> hits{};
1057
+ std::uint32_t hitCount = 0;
1058
+ const double queryRadius = query.shapeDims.x > 0.0 ? query.shapeDims.x : 0.5;
1059
+
1060
+ for (std::uint32_t i = 0; i < frameA->bodyCount && hitCount < kLagCompensationMaxResults; ++i) {
1061
+ const auto& bA = frameA->bodies[i];
1062
+ if (!bA.active) continue;
1063
+ if (bA.bodyId == query.excludedBodyId) continue;
1064
+ if ((query.layerMask & bA.layer) == 0) continue;
1065
+
1066
+ LagCompBodySnapshot targetBody = bA;
1067
+ if (frameB != nullptr) {
1068
+ const LagCompBodySnapshot* matchB = nullptr;
1069
+ for (std::uint32_t j = 0; j < frameB->bodyCount; ++j) {
1070
+ if (frameB->bodies[j].active && frameB->bodies[j].bodyId == bA.bodyId) {
1071
+ matchB = &frameB->bodies[j];
1072
+ break;
1073
+ }
1074
+ }
1075
+ if (matchB) {
1076
+ interpolateBody(bA, *matchB, query.subTickAlpha, targetBody);
1077
+ }
1078
+ }
1079
+
1080
+ const LagCompVec3 bodyCenter = vec3Add(targetBody.position,
1081
+ rotateVec3(targetBody.orientation, targetBody.shapeOffset));
1082
+
1083
+ bool overlapped = false;
1084
+ LagCompVec3 hitPoint = bodyCenter;
1085
+ LagCompVec3 hitNormal{0.0, 1.0, 0.0};
1086
+ double overlapDist = 0.0;
1087
+
1088
+ if (targetBody.shape == ShapeKind::Sphere) {
1089
+ const double targetRadius = targetBody.shapeDimensions.x > 0.0 ? targetBody.shapeDimensions.x : 0.5;
1090
+ const LagCompVec3 diff = vec3Sub(query.origin, bodyCenter);
1091
+ const double dist = vec3Length(diff);
1092
+ if (dist <= queryRadius + targetRadius) {
1093
+ overlapped = true;
1094
+ overlapDist = dist;
1095
+ hitNormal = (dist > 1e-15) ? vec3Scale(diff, 1.0 / dist) : LagCompVec3{0.0, 1.0, 0.0};
1096
+ hitPoint = vec3Add(bodyCenter, vec3Scale(hitNormal, targetRadius));
1097
+ }
1098
+ } else {
1099
+ // Box or proxy: evaluate distance from query.origin to box AABB in local coordinates
1100
+ const LagCompVec3 localOrigin = invRotateVec3(targetBody.orientation, vec3Sub(query.origin, bodyCenter));
1101
+ const LagCompVec3 h = targetBody.shapeDimensions;
1102
+ const LagCompVec3 closest{
1103
+ std::clamp(localOrigin.x, -h.x, h.x),
1104
+ std::clamp(localOrigin.y, -h.y, h.y),
1105
+ std::clamp(localOrigin.z, -h.z, h.z),
1106
+ };
1107
+ const LagCompVec3 localDiff = vec3Sub(localOrigin, closest);
1108
+ const double d = vec3Length(localDiff);
1109
+ if (d <= queryRadius) {
1110
+ overlapped = true;
1111
+ overlapDist = d;
1112
+ hitNormal = (d > 1e-15) ? rotateVec3(targetBody.orientation, vec3Scale(localDiff, 1.0 / d))
1113
+ : rotateVec3(targetBody.orientation, {0.0, 1.0, 0.0});
1114
+ hitPoint = vec3Add(bodyCenter, rotateVec3(targetBody.orientation, closest));
1115
+ }
1116
+ }
1117
+
1118
+ if (overlapped) {
1119
+ LagCompQueryResult res{};
1120
+ res.hit = true;
1121
+ res.distance = overlapDist;
1122
+ res.point = hitPoint;
1123
+ res.normal = hitNormal;
1124
+ res.bodyIndex = i;
1125
+ res.bodyId = targetBody.bodyId;
1126
+ res.geometryGeneration = targetBody.geometryGeneration;
1127
+ res.shapeKind = targetBody.shape;
1128
+
1129
+ hits[hitCount++] = res;
1130
+ }
1131
+ }
1132
+
1133
+ std::stable_sort(hits.begin(), hits.begin() + hitCount,
1134
+ [](const LagCompQueryResult& a, const LagCompQueryResult& b) {
1135
+ if (std::abs(a.distance - b.distance) > 1e-9) {
1136
+ return a.distance < b.distance;
1137
+ }
1138
+ return a.bodyIndex < b.bodyIndex;
1139
+ });
1140
+
1141
+ const std::uint32_t finalCount = std::min(hitCount, static_cast<std::uint32_t>(outResults.size()));
1142
+ for (std::uint32_t k = 0; k < finalCount; ++k) {
1143
+ outResults[k] = hits[k];
1144
+ }
1145
+ outResultCount = finalCount;
1146
+
1147
+ return (finalCount > 0) ? LagCompStatus::Success : LagCompStatus::NoHit;
1148
+ }
1149
+
1150
+ LagCompStatus LagCompensationBuffer::shapecast(
1151
+ const LagCompShapecastQuery& query,
1152
+ std::span<LagCompQueryResult> outResults,
1153
+ std::uint32_t& outResultCount) const noexcept {
1154
+ outResultCount = 0;
1155
+ if (outResults.empty()) return LagCompStatus::InvalidInput;
1156
+
1157
+ const double radius = query.shapeDims.x > 0.0 ? query.shapeDims.x : 0.5;
1158
+
1159
+ // Sphere sweep: equivalent to raycast against colliders expanded by sphere radius
1160
+ LagCompRaycastQuery rayQuery{};
1161
+ rayQuery.origin = query.origin;
1162
+ rayQuery.direction = query.direction;
1163
+ rayQuery.maxDistance = query.maxDistance;
1164
+ rayQuery.layerMask = query.layerMask;
1165
+ rayQuery.flags = query.flags;
1166
+ rayQuery.excludedBodyId = query.excludedBodyId;
1167
+ rayQuery.historicalTick = query.historicalTick;
1168
+ rayQuery.subTickAlpha = query.subTickAlpha;
1169
+
1170
+ // We perform the sweep by casting against expanded shapes
1171
+ const LagCompFrameSnapshot* frameA = findFrame(query.historicalTick);
1172
+ if (!frameA) return LagCompStatus::TickOutOfRange;
1173
+
1174
+ const LagCompFrameSnapshot* frameB = (query.subTickAlpha > 1e-6)
1175
+ ? findFrame(query.historicalTick + 1) : nullptr;
1176
+
1177
+ const LagCompVec3 rawDir = query.direction;
1178
+ const double dirLen = vec3Length(rawDir);
1179
+ if (dirLen <= 1e-15) return LagCompStatus::InvalidInput;
1180
+ const LagCompVec3 dir = vec3Scale(rawDir, 1.0 / dirLen);
1181
+
1182
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> hits{};
1183
+ std::uint32_t hitCount = 0;
1184
+
1185
+ for (std::uint32_t i = 0; i < frameA->bodyCount && hitCount < kLagCompensationMaxResults; ++i) {
1186
+ const auto& bA = frameA->bodies[i];
1187
+ if (!bA.active) continue;
1188
+ if (bA.bodyId == query.excludedBodyId) continue;
1189
+ if ((query.layerMask & bA.layer) == 0) continue;
1190
+
1191
+ LagCompBodySnapshot targetBody = bA;
1192
+ if (frameB != nullptr) {
1193
+ const LagCompBodySnapshot* matchB = nullptr;
1194
+ for (std::uint32_t j = 0; j < frameB->bodyCount; ++j) {
1195
+ if (frameB->bodies[j].active && frameB->bodies[j].bodyId == bA.bodyId) {
1196
+ matchB = &frameB->bodies[j];
1197
+ break;
1198
+ }
1199
+ }
1200
+ if (matchB) {
1201
+ interpolateBody(bA, *matchB, query.subTickAlpha, targetBody);
1202
+ }
1203
+ }
1204
+
1205
+ // Expand AABB by cast radius
1206
+ LagCompAABB expAABB = targetBody.aabb;
1207
+ expAABB.minX -= radius; expAABB.maxX += radius;
1208
+ expAABB.minY -= radius; expAABB.maxY += radius;
1209
+ expAABB.minZ -= radius; expAABB.maxZ += radius;
1210
+
1211
+ double tNear = 0.0;
1212
+ if (!rayAABB(query.origin, dir, expAABB, query.maxDistance, tNear)) {
1213
+ continue;
1214
+ }
1215
+
1216
+ const LagCompVec3 center = vec3Add(targetBody.position,
1217
+ rotateVec3(targetBody.orientation, targetBody.shapeOffset));
1218
+ double dist = 0.0;
1219
+ LagCompVec3 normal{};
1220
+ bool hit = false;
1221
+
1222
+ if (targetBody.shape == ShapeKind::Sphere) {
1223
+ const double expRadius = targetBody.shapeDimensions.x + radius;
1224
+ hit = raySphere(query.origin, dir, center, expRadius, query.maxDistance, dist, normal);
1225
+ } else {
1226
+ const LagCompVec3 expDims{
1227
+ targetBody.shapeDimensions.x + radius,
1228
+ targetBody.shapeDimensions.y + radius,
1229
+ targetBody.shapeDimensions.z + radius
1230
+ };
1231
+ hit = rayBox(query.origin, dir, center, targetBody.orientation, expDims, query.maxDistance, dist, normal);
1232
+ }
1233
+
1234
+ if (hit && dist <= query.maxDistance) {
1235
+ LagCompQueryResult res{};
1236
+ res.hit = true;
1237
+ res.distance = dist;
1238
+ res.point = vec3Add(query.origin, vec3Scale(dir, dist));
1239
+ res.normal = normal;
1240
+ res.bodyIndex = i;
1241
+ res.bodyId = targetBody.bodyId;
1242
+ res.geometryGeneration = targetBody.geometryGeneration;
1243
+ res.shapeKind = targetBody.shape;
1244
+
1245
+ hits[hitCount++] = res;
1246
+ }
1247
+ }
1248
+
1249
+ std::stable_sort(hits.begin(), hits.begin() + hitCount,
1250
+ [](const LagCompQueryResult& a, const LagCompQueryResult& b) {
1251
+ if (std::abs(a.distance - b.distance) > 1e-9) {
1252
+ return a.distance < b.distance;
1253
+ }
1254
+ return a.bodyIndex < b.bodyIndex;
1255
+ });
1256
+
1257
+ const std::uint32_t finalCount = std::min(hitCount, static_cast<std::uint32_t>(outResults.size()));
1258
+ for (std::uint32_t k = 0; k < finalCount; ++k) {
1259
+ outResults[k] = hits[k];
1260
+ }
1261
+ outResultCount = finalCount;
1262
+
1263
+ return (finalCount > 0) ? LagCompStatus::Success : LagCompStatus::NoHit;
1264
+ }
1265
+
1266
+ // ---------------------------------------------------------------------------
1267
+ // Global Default Buffer and C ABI Implementation
1268
+ // ---------------------------------------------------------------------------
1269
+
1270
+ static LagCompensationBuffer& defaultBuffer() noexcept {
1271
+ static LagCompensationBuffer buffer{kLagCompensationMaxFrames, kLagCompensationMaxBodies};
1272
+ return buffer;
1273
+ }
1274
+
1275
+ extern "C" {
1276
+
1277
+ std::uint32_t rundot_kinetix_ext_k09_abi_version() {
1278
+ return kLagCompensationAbiVersion;
1279
+ }
1280
+
1281
+ std::uint32_t rundot_kinetix_ext_k09_body_stride() {
1282
+ return kLagCompensationBodyStride;
1283
+ }
1284
+
1285
+ std::uint32_t rundot_kinetix_ext_k09_header_stride() {
1286
+ return kLagCompensationHeaderStride;
1287
+ }
1288
+
1289
+ std::uint32_t rundot_kinetix_ext_k09_query_stride() {
1290
+ return kLagCompensationQueryStride;
1291
+ }
1292
+
1293
+ std::uint32_t rundot_kinetix_ext_k09_result_stride() {
1294
+ return kLagCompensationResultStride;
1295
+ }
1296
+
1297
+ std::uint32_t rundot_kinetix_ext_k09_max_frames() {
1298
+ return kLagCompensationMaxFrames;
1299
+ }
1300
+
1301
+ std::uint32_t rundot_kinetix_ext_k09_max_bodies() {
1302
+ return kLagCompensationMaxBodies;
1303
+ }
1304
+
1305
+ std::uint32_t rundot_kinetix_ext_k09_max_results() {
1306
+ return kLagCompensationMaxResults;
1307
+ }
1308
+
1309
+ std::uint32_t rundot_kinetix_ext_lag_compensation_stride() {
1310
+ return kLagCompensationBodyStride;
1311
+ }
1312
+
1313
+ std::uint32_t rundot_kinetix_ext_lag_compensation_size() {
1314
+ return sizeof(LagCompBodySnapshot);
1315
+ }
1316
+
1317
+ std::int32_t rundot_kinetix_ext_k09_reset() {
1318
+ defaultBuffer().clear();
1319
+ return 0;
1320
+ }
1321
+
1322
+ std::int32_t rundot_kinetix_ext_k09_record_frame(
1323
+ std::uint64_t tick,
1324
+ double timestamp,
1325
+ const double* bodyData,
1326
+ std::uint32_t bodyCount,
1327
+ const std::uint64_t* bodyIds,
1328
+ std::uint32_t globalGeomGen) {
1329
+ return static_cast<std::int32_t>(defaultBuffer().recordSnapshot(
1330
+ tick, timestamp, bodyData, bodyCount, bodyIds, globalGeomGen));
1331
+ }
1332
+
1333
+ std::int32_t rundot_kinetix_ext_k09_has_tick(std::uint64_t tick) {
1334
+ return defaultBuffer().hasTick(tick) ? 1 : 0;
1335
+ }
1336
+
1337
+ std::uint64_t rundot_kinetix_ext_k09_oldest_tick() {
1338
+ return defaultBuffer().oldestTick();
1339
+ }
1340
+
1341
+ std::uint64_t rundot_kinetix_ext_k09_newest_tick() {
1342
+ return defaultBuffer().newestTick();
1343
+ }
1344
+
1345
+ std::uint32_t rundot_kinetix_ext_k09_frame_count() {
1346
+ return defaultBuffer().frameCount();
1347
+ }
1348
+
1349
+ std::int32_t rundot_kinetix_ext_k09_query_topology(
1350
+ std::uint64_t tick,
1351
+ std::uint32_t* outBodyCount,
1352
+ std::uint64_t* outBodyIds,
1353
+ std::uint32_t maxIds) {
1354
+ if (outBodyCount == nullptr || outBodyIds == nullptr) return -5;
1355
+ std::span<std::uint64_t> idSpan(outBodyIds, maxIds);
1356
+ return static_cast<std::int32_t>(defaultBuffer().queryHistoricalTopology(tick, *outBodyCount, idSpan));
1357
+ }
1358
+
1359
+ std::int32_t rundot_kinetix_ext_k09_query_body(
1360
+ std::uint64_t tick,
1361
+ std::uint64_t bodyId,
1362
+ double* outBodyRecord48) {
1363
+ if (outBodyRecord48 == nullptr) return -5;
1364
+ LagCompBodySnapshot b{};
1365
+ const LagCompStatus st = defaultBuffer().queryHistoricalBody(tick, bodyId, b);
1366
+ if (st != LagCompStatus::Success) return static_cast<std::int32_t>(st);
1367
+ for (std::uint32_t c = 0; c < kLagCompensationBodyStride; ++c) {
1368
+ outBodyRecord48[c] = b.rawData[c];
1369
+ }
1370
+ return 0;
1371
+ }
1372
+
1373
+ std::int32_t rundot_kinetix_ext_k09_query_body_by_index(
1374
+ std::uint64_t tick,
1375
+ std::uint32_t bodyIndex,
1376
+ double* outBodyRecord48) {
1377
+ if (outBodyRecord48 == nullptr) return -5;
1378
+ LagCompBodySnapshot b{};
1379
+ const LagCompStatus st = defaultBuffer().queryHistoricalBodyByIndex(tick, bodyIndex, b);
1380
+ if (st != LagCompStatus::Success) return static_cast<std::int32_t>(st);
1381
+ for (std::uint32_t c = 0; c < kLagCompensationBodyStride; ++c) {
1382
+ outBodyRecord48[c] = b.rawData[c];
1383
+ }
1384
+ return 0;
1385
+ }
1386
+
1387
+ std::int32_t rundot_kinetix_ext_k09_query_filter(
1388
+ std::uint64_t tick,
1389
+ std::uint64_t bodyId,
1390
+ std::uint32_t* outLayer,
1391
+ std::uint32_t* outMask,
1392
+ std::uint32_t* outMotionType) {
1393
+ if (outLayer == nullptr || outMask == nullptr || outMotionType == nullptr) return -5;
1394
+ return static_cast<std::int32_t>(defaultBuffer().queryHistoricalColliderFilter(
1395
+ tick, bodyId, *outLayer, *outMask, *outMotionType));
1396
+ }
1397
+
1398
+ std::int32_t rundot_kinetix_ext_k09_query_geometry(
1399
+ std::uint64_t tick,
1400
+ std::uint64_t bodyId,
1401
+ std::uint32_t* outShapeKind,
1402
+ double* outShapeDims3,
1403
+ double* outShapeOffset3,
1404
+ std::uint32_t* outGeomOffset,
1405
+ std::uint32_t* outGeomCount,
1406
+ std::uint32_t* outGeomGen) {
1407
+ if (outShapeKind == nullptr || outShapeDims3 == nullptr || outShapeOffset3 == nullptr
1408
+ || outGeomOffset == nullptr || outGeomCount == nullptr || outGeomGen == nullptr) {
1409
+ return -5;
1410
+ }
1411
+ ShapeKind sk{};
1412
+ LagCompVec3 dims{}, offset{};
1413
+ const LagCompStatus st = defaultBuffer().queryHistoricalGeometry(
1414
+ tick, bodyId, sk, dims, offset, *outGeomOffset, *outGeomCount, *outGeomGen);
1415
+ if (st != LagCompStatus::Success) return static_cast<std::int32_t>(st);
1416
+
1417
+ *outShapeKind = static_cast<std::uint32_t>(sk);
1418
+ outShapeDims3[0] = dims.x; outShapeDims3[1] = dims.y; outShapeDims3[2] = dims.z;
1419
+ outShapeOffset3[0] = offset.x; outShapeOffset3[1] = offset.y; outShapeOffset3[2] = offset.z;
1420
+ return 0;
1421
+ }
1422
+
1423
+ std::int32_t rundot_kinetix_ext_k09_query_pose(
1424
+ std::uint64_t tick,
1425
+ std::uint64_t bodyId,
1426
+ double* outPosition3,
1427
+ double* outOrientation4,
1428
+ double* outLinearVel3,
1429
+ double* outAngularVel3) {
1430
+ if (outPosition3 == nullptr || outOrientation4 == nullptr
1431
+ || outLinearVel3 == nullptr || outAngularVel3 == nullptr) {
1432
+ return -5;
1433
+ }
1434
+ LagCompVec3 pos{}, lv{}, av{};
1435
+ LagCompQuat rot{};
1436
+ const LagCompStatus st = defaultBuffer().queryHistoricalPose(tick, bodyId, pos, rot, lv, av);
1437
+ if (st != LagCompStatus::Success) return static_cast<std::int32_t>(st);
1438
+
1439
+ outPosition3[0] = pos.x; outPosition3[1] = pos.y; outPosition3[2] = pos.z;
1440
+ outOrientation4[0] = rot.x; outOrientation4[1] = rot.y; outOrientation4[2] = rot.z; outOrientation4[3] = rot.w;
1441
+ outLinearVel3[0] = lv.x; outLinearVel3[1] = lv.y; outLinearVel3[2] = lv.z;
1442
+ outAngularVel3[0] = av.x; outAngularVel3[1] = av.y; outAngularVel3[2] = av.z;
1443
+ return 0;
1444
+ }
1445
+
1446
+ std::int32_t rundot_kinetix_ext_k09_query_interpolated_pose(
1447
+ std::uint64_t tickA,
1448
+ std::uint64_t tickB,
1449
+ double alpha,
1450
+ std::uint64_t bodyId,
1451
+ double* outPosition3,
1452
+ double* outOrientation4) {
1453
+ if (outPosition3 == nullptr || outOrientation4 == nullptr) return -5;
1454
+ LagCompVec3 pos{};
1455
+ LagCompQuat rot{};
1456
+ const LagCompStatus st = defaultBuffer().queryInterpolatedPose(tickA, tickB, alpha, bodyId, pos, rot);
1457
+ if (st != LagCompStatus::Success) return static_cast<std::int32_t>(st);
1458
+
1459
+ outPosition3[0] = pos.x; outPosition3[1] = pos.y; outPosition3[2] = pos.z;
1460
+ outOrientation4[0] = rot.x; outOrientation4[1] = rot.y; outOrientation4[2] = rot.z; outOrientation4[3] = rot.w;
1461
+ return 0;
1462
+ }
1463
+
1464
+ std::int32_t rundot_kinetix_ext_k09_rewind_scene(
1465
+ std::uint64_t tick,
1466
+ double* outBodyData44,
1467
+ std::uint32_t maxBodies,
1468
+ std::uint32_t* outBodyCount) {
1469
+ if (outBodyData44 == nullptr || outBodyCount == nullptr) return -5;
1470
+ return static_cast<std::int32_t>(defaultBuffer().rewindScene(tick, outBodyData44, maxBodies, *outBodyCount));
1471
+ }
1472
+
1473
+ std::int32_t rundot_kinetix_ext_k09_rewind_scene_interpolated(
1474
+ std::uint64_t tickA,
1475
+ std::uint64_t tickB,
1476
+ double alpha,
1477
+ double* outBodyData44,
1478
+ std::uint32_t maxBodies,
1479
+ std::uint32_t* outBodyCount) {
1480
+ if (outBodyData44 == nullptr || outBodyCount == nullptr) return -5;
1481
+ return static_cast<std::int32_t>(defaultBuffer().rewindSceneInterpolated(
1482
+ tickA, tickB, alpha, outBodyData44, maxBodies, *outBodyCount));
1483
+ }
1484
+
1485
+ std::int32_t rundot_kinetix_ext_k09_raycast(
1486
+ const double* queryData24,
1487
+ double* outResultData12,
1488
+ std::uint32_t maxResults,
1489
+ std::uint32_t* outResultCount) {
1490
+ if (queryData24 == nullptr || outResultData12 == nullptr || outResultCount == nullptr) return -5;
1491
+
1492
+ LagCompRaycastQuery q{};
1493
+ q.origin = {queryData24[4], queryData24[5], queryData24[6]};
1494
+ q.direction = {queryData24[7], queryData24[8], queryData24[9]};
1495
+ q.maxDistance = queryData24[10];
1496
+ q.layerMask = static_cast<std::uint32_t>(queryData24[15]);
1497
+ q.flags = static_cast<std::uint32_t>(queryData24[16]);
1498
+ const std::uint64_t idLow = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[17]));
1499
+ const std::uint64_t idHigh = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[18]));
1500
+ q.excludedBodyId = idLow | (idHigh << 32);
1501
+ q.historicalTick = static_cast<std::uint64_t>(queryData24[19]);
1502
+ q.subTickAlpha = queryData24[20];
1503
+
1504
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> results{};
1505
+ std::uint32_t count = 0;
1506
+ const LagCompStatus st = defaultBuffer().raycast(q, results, count);
1507
+
1508
+ const std::uint32_t copyCount = std::min(count, maxResults);
1509
+ *outResultCount = copyCount;
1510
+
1511
+ for (std::uint32_t i = 0; i < copyCount; ++i) {
1512
+ const std::uint32_t off = i * kLagCompensationResultStride;
1513
+ outResultData12[off + 0] = results[i].hit ? 1.0 : 0.0;
1514
+ outResultData12[off + 1] = results[i].distance;
1515
+ outResultData12[off + 2] = results[i].point.x;
1516
+ outResultData12[off + 3] = results[i].point.y;
1517
+ outResultData12[off + 4] = results[i].point.z;
1518
+ outResultData12[off + 5] = results[i].normal.x;
1519
+ outResultData12[off + 6] = results[i].normal.y;
1520
+ outResultData12[off + 7] = results[i].normal.z;
1521
+ outResultData12[off + 8] = static_cast<double>(results[i].bodyIndex);
1522
+ outResultData12[off + 9] = static_cast<double>(results[i].bodyId & 0xffffffffu);
1523
+ outResultData12[off + 10] = static_cast<double>(results[i].bodyId >> 32);
1524
+ outResultData12[off + 11] = static_cast<double>(results[i].geometryGeneration);
1525
+ }
1526
+
1527
+ return static_cast<std::int32_t>(st);
1528
+ }
1529
+
1530
+ std::int32_t rundot_kinetix_ext_k09_overlap(
1531
+ const double* queryData24,
1532
+ double* outResultData12,
1533
+ std::uint32_t maxResults,
1534
+ std::uint32_t* outResultCount) {
1535
+ if (queryData24 == nullptr || outResultData12 == nullptr || outResultCount == nullptr) return -5;
1536
+
1537
+ LagCompOverlapQuery q{};
1538
+ q.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(queryData24[0]));
1539
+ q.shapeDims = {queryData24[1], queryData24[2], queryData24[3]};
1540
+ q.origin = {queryData24[4], queryData24[5], queryData24[6]};
1541
+ q.orientation = {queryData24[11], queryData24[12], queryData24[13], queryData24[14]};
1542
+ q.layerMask = static_cast<std::uint32_t>(queryData24[15]);
1543
+ q.flags = static_cast<std::uint32_t>(queryData24[16]);
1544
+ const std::uint64_t idLow = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[17]));
1545
+ const std::uint64_t idHigh = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[18]));
1546
+ q.excludedBodyId = idLow | (idHigh << 32);
1547
+ q.historicalTick = static_cast<std::uint64_t>(queryData24[19]);
1548
+ q.subTickAlpha = queryData24[20];
1549
+
1550
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> results{};
1551
+ std::uint32_t count = 0;
1552
+ const LagCompStatus st = defaultBuffer().overlap(q, results, count);
1553
+
1554
+ const std::uint32_t copyCount = std::min(count, maxResults);
1555
+ *outResultCount = copyCount;
1556
+
1557
+ for (std::uint32_t i = 0; i < copyCount; ++i) {
1558
+ const std::uint32_t off = i * kLagCompensationResultStride;
1559
+ outResultData12[off + 0] = results[i].hit ? 1.0 : 0.0;
1560
+ outResultData12[off + 1] = results[i].distance;
1561
+ outResultData12[off + 2] = results[i].point.x;
1562
+ outResultData12[off + 3] = results[i].point.y;
1563
+ outResultData12[off + 4] = results[i].point.z;
1564
+ outResultData12[off + 5] = results[i].normal.x;
1565
+ outResultData12[off + 6] = results[i].normal.y;
1566
+ outResultData12[off + 7] = results[i].normal.z;
1567
+ outResultData12[off + 8] = static_cast<double>(results[i].bodyIndex);
1568
+ outResultData12[off + 9] = static_cast<double>(results[i].bodyId & 0xffffffffu);
1569
+ outResultData12[off + 10] = static_cast<double>(results[i].bodyId >> 32);
1570
+ outResultData12[off + 11] = static_cast<double>(results[i].geometryGeneration);
1571
+ }
1572
+
1573
+ return static_cast<std::int32_t>(st);
1574
+ }
1575
+
1576
+ std::int32_t rundot_kinetix_ext_k09_shapecast(
1577
+ const double* queryData24,
1578
+ double* outResultData12,
1579
+ std::uint32_t maxResults,
1580
+ std::uint32_t* outResultCount) {
1581
+ if (queryData24 == nullptr || outResultData12 == nullptr || outResultCount == nullptr) return -5;
1582
+
1583
+ LagCompShapecastQuery q{};
1584
+ q.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(queryData24[0]));
1585
+ q.shapeDims = {queryData24[1], queryData24[2], queryData24[3]};
1586
+ q.origin = {queryData24[4], queryData24[5], queryData24[6]};
1587
+ q.direction = {queryData24[7], queryData24[8], queryData24[9]};
1588
+ q.maxDistance = queryData24[10];
1589
+ q.orientation = {queryData24[11], queryData24[12], queryData24[13], queryData24[14]};
1590
+ q.layerMask = static_cast<std::uint32_t>(queryData24[15]);
1591
+ q.flags = static_cast<std::uint32_t>(queryData24[16]);
1592
+ const std::uint64_t idLow = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[17]));
1593
+ const std::uint64_t idHigh = static_cast<std::uint64_t>(static_cast<std::uint32_t>(queryData24[18]));
1594
+ q.excludedBodyId = idLow | (idHigh << 32);
1595
+ q.historicalTick = static_cast<std::uint64_t>(queryData24[19]);
1596
+ q.subTickAlpha = queryData24[20];
1597
+
1598
+ std::array<LagCompQueryResult, kLagCompensationMaxResults> results{};
1599
+ std::uint32_t count = 0;
1600
+ const LagCompStatus st = defaultBuffer().shapecast(q, results, count);
1601
+
1602
+ const std::uint32_t copyCount = std::min(count, maxResults);
1603
+ *outResultCount = copyCount;
1604
+
1605
+ for (std::uint32_t i = 0; i < copyCount; ++i) {
1606
+ const std::uint32_t off = i * kLagCompensationResultStride;
1607
+ outResultData12[off + 0] = results[i].hit ? 1.0 : 0.0;
1608
+ outResultData12[off + 1] = results[i].distance;
1609
+ outResultData12[off + 2] = results[i].point.x;
1610
+ outResultData12[off + 3] = results[i].point.y;
1611
+ outResultData12[off + 4] = results[i].point.z;
1612
+ outResultData12[off + 5] = results[i].normal.x;
1613
+ outResultData12[off + 6] = results[i].normal.y;
1614
+ outResultData12[off + 7] = results[i].normal.z;
1615
+ outResultData12[off + 8] = static_cast<double>(results[i].bodyIndex);
1616
+ outResultData12[off + 9] = static_cast<double>(results[i].bodyId & 0xffffffffu);
1617
+ outResultData12[off + 10] = static_cast<double>(results[i].bodyId >> 32);
1618
+ outResultData12[off + 11] = static_cast<double>(results[i].geometryGeneration);
1619
+ }
1620
+
1621
+ return static_cast<std::int32_t>(st);
1622
+ }
1623
+
1624
+ } // extern "C"
1625
+
1626
+ } // namespace rundot::kinetix::physics