@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,1353 @@
1
+ #include "platformer_2d.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <array>
5
+ #include <cmath>
6
+ #include <cstdint>
7
+ #include <cstring>
8
+ #include <span>
9
+
10
+ namespace rundot::kinetix::physics::platformer2d {
11
+
12
+ namespace {
13
+
14
+ inline double solverSqrt(double value) {
15
+ if (value <= 0.0) return 0.0;
16
+ return std::sqrt(value);
17
+ }
18
+
19
+ inline double moveTowards(double current, double target, double maxDelta) {
20
+ if (std::abs(target - current) <= maxDelta) {
21
+ return target;
22
+ }
23
+ return current + ((target > current) ? maxDelta : -maxDelta);
24
+ }
25
+
26
+ inline PlatformerVec2 rotateVec2(const PlatformerVec2& v, double cosA, double sinA) {
27
+ return {v.x * cosA - v.y * sinA, v.x * sinA + v.y * cosA};
28
+ }
29
+
30
+ } // namespace
31
+
32
+ PlatformerVec2 addVec2(const PlatformerVec2& a, const PlatformerVec2& b) {
33
+ return {a.x + b.x, a.y + b.y};
34
+ }
35
+
36
+ PlatformerVec2 subVec2(const PlatformerVec2& a, const PlatformerVec2& b) {
37
+ return {a.x - b.x, a.y - b.y};
38
+ }
39
+
40
+ PlatformerVec2 scaleVec2(const PlatformerVec2& v, double s) {
41
+ return {v.x * s, v.y * s};
42
+ }
43
+
44
+ double dotVec2(const PlatformerVec2& a, const PlatformerVec2& b) {
45
+ return a.x * b.x + a.y * b.y;
46
+ }
47
+
48
+ double crossVec2(const PlatformerVec2& a, const PlatformerVec2& b) {
49
+ return a.x * b.y - a.y * b.x;
50
+ }
51
+
52
+ double lengthSquaredVec2(const PlatformerVec2& v) {
53
+ return dotVec2(v, v);
54
+ }
55
+
56
+ double lengthVec2(const PlatformerVec2& v) {
57
+ return solverSqrt(lengthSquaredVec2(v));
58
+ }
59
+
60
+ PlatformerVec2 normalizeVec2(const PlatformerVec2& v) {
61
+ const double lenSq = lengthSquaredVec2(v);
62
+ if (lenSq <= 1e-24) return {0.0, 0.0};
63
+ const double invLen = 1.0 / solverSqrt(lenSq);
64
+ return {v.x * invLen, v.y * invLen};
65
+ }
66
+
67
+ namespace {
68
+
69
+ // Ray/sweep cast against axis-aligned box
70
+ bool castAABB(
71
+ const PlatformerVec2& start,
72
+ const PlatformerVec2& delta,
73
+ const PlatformerVec2& boxMin,
74
+ const PlatformerVec2& boxMax,
75
+ Platformer2DHit& hit) {
76
+ double tNear = -1e30;
77
+ double tFar = 1e30;
78
+ PlatformerVec2 nearNormal{0.0, 0.0};
79
+
80
+ // X axis slab
81
+ if (std::abs(delta.x) < 1e-12) {
82
+ if (start.x < boxMin.x || start.x > boxMax.x) return false;
83
+ } else {
84
+ double invDx = 1.0 / delta.x;
85
+ double t1 = (boxMin.x - start.x) * invDx;
86
+ double t2 = (boxMax.x - start.x) * invDx;
87
+ PlatformerVec2 norm1{-1.0, 0.0};
88
+ PlatformerVec2 norm2{1.0, 0.0};
89
+ if (t1 > t2) {
90
+ std::swap(t1, t2);
91
+ std::swap(norm1, norm2);
92
+ }
93
+ if (t1 > tNear) {
94
+ tNear = t1;
95
+ nearNormal = norm1;
96
+ }
97
+ if (t2 < tFar) {
98
+ tFar = t2;
99
+ }
100
+ if (tNear > tFar || tFar < 0.0) return false;
101
+ }
102
+
103
+ // Y axis slab
104
+ if (std::abs(delta.y) < 1e-12) {
105
+ if (start.y < boxMin.y || start.y > boxMax.y) return false;
106
+ } else {
107
+ double invDy = 1.0 / delta.y;
108
+ double t1 = (boxMin.y - start.y) * invDy;
109
+ double t2 = (boxMax.y - start.y) * invDy;
110
+ PlatformerVec2 norm1{0.0, -1.0};
111
+ PlatformerVec2 norm2{0.0, 1.0};
112
+ if (t1 > t2) {
113
+ std::swap(t1, t2);
114
+ std::swap(norm1, norm2);
115
+ }
116
+ if (t1 > tNear) {
117
+ tNear = t1;
118
+ nearNormal = norm1;
119
+ }
120
+ if (t2 < tFar) {
121
+ tFar = t2;
122
+ }
123
+ if (tNear > tFar || tFar < 0.0) return false;
124
+ }
125
+
126
+ if (tNear > 1.0 || tFar < 0.0) return false;
127
+
128
+ hit.hit = true;
129
+ hit.fraction = std::max(0.0, tNear);
130
+ hit.normal = nearNormal;
131
+ hit.point = addVec2(start, scaleVec2(delta, hit.fraction));
132
+ return true;
133
+ }
134
+
135
+ bool castAgainstBody(
136
+ const PlatformerVec2& start,
137
+ const PlatformerVec2& delta,
138
+ const Platformer2DShape& charShape,
139
+ const PlatformerBody2D& body,
140
+ Platformer2DHit& hit) {
141
+ // Ladders are triggers/sensors; do not collide solidly
142
+ if (body.kind == PlatformerBodyKind2D::Ladder) return false;
143
+
144
+ // One-way platform logic
145
+ if (body.kind == PlatformerBodyKind2D::OneWay) {
146
+ // Only collide if moving downward
147
+ if (delta.y >= 0.0) return false;
148
+
149
+ const double yTop = body.y + body.halfY;
150
+ const double startBottom = start.y - charShape.halfHeight;
151
+ const double endBottom = start.y + delta.y - charShape.halfHeight;
152
+
153
+ // Must start at or above the platform surface
154
+ if (startBottom < yTop - 1e-4) return false;
155
+ if (endBottom > yTop) return false;
156
+
157
+ const double t = (yTop - startBottom) / delta.y;
158
+ if (t < 0.0 || t > 1.0) return false;
159
+
160
+ const double xAtHit = start.x + delta.x * t;
161
+ const double platMinX = body.x - body.halfX;
162
+ const double platMaxX = body.x + body.halfX;
163
+
164
+ if (xAtHit + charShape.halfWidth >= platMinX && xAtHit - charShape.halfWidth <= platMaxX) {
165
+ hit.hit = true;
166
+ hit.fraction = t;
167
+ hit.normal = {0.0, 1.0};
168
+ hit.point = {xAtHit, yTop};
169
+ hit.bodyId = body.id;
170
+ hit.bodyKind = body.kind;
171
+ return true;
172
+ }
173
+ return false;
174
+ }
175
+
176
+ // Slope shape handling
177
+ if (body.shape == PlatformerShapeKind2D::Slope) {
178
+ const double minX = body.x - body.halfX;
179
+ const double maxX = body.x + body.halfX;
180
+ const double baseY = body.y - body.halfY;
181
+ const double rise = body.slopeRisePerUnit;
182
+
183
+ const double dx = maxX - minX;
184
+ const double dy = rise * dx;
185
+ PlatformerVec2 slopeNorm = normalizeVec2({-dy, dx});
186
+ if (slopeNorm.y < 0.0) {
187
+ slopeNorm = scaleVec2(slopeNorm, -1.0);
188
+ }
189
+
190
+ // Expand surface upward by character half-extents
191
+ const double charEffectiveRadius = charShape.halfWidth * std::abs(slopeNorm.x) + charShape.halfHeight * std::abs(slopeNorm.y);
192
+ const double p0x = minX;
193
+ const double p0y = baseY + charEffectiveRadius * slopeNorm.y;
194
+
195
+ // Plane intersection
196
+ const double denom = dotVec2(delta, slopeNorm);
197
+ if (denom >= -1e-12) return false; // Moving away from or parallel to slope
198
+
199
+ PlatformerVec2 toStart = subVec2(start, {p0x, p0y});
200
+ const double dist = dotVec2(toStart, slopeNorm);
201
+ const double t = -dist / denom;
202
+
203
+ if (t >= 0.0 && t <= 1.0) {
204
+ PlatformerVec2 testPos = addVec2(start, scaleVec2(delta, t));
205
+ if (testPos.x >= minX - charShape.halfWidth && testPos.x <= maxX + charShape.halfWidth) {
206
+ hit.hit = true;
207
+ hit.fraction = t;
208
+ hit.normal = slopeNorm;
209
+ hit.point = testPos;
210
+ hit.bodyId = body.id;
211
+ hit.bodyKind = body.kind;
212
+ return true;
213
+ }
214
+ }
215
+ return false;
216
+ }
217
+
218
+ // General Box obstacle (optionally rotated)
219
+ if (std::abs(body.angle) > 1e-6) {
220
+ const double cosA = std::cos(-body.angle);
221
+ const double sinA = std::sin(-body.angle);
222
+
223
+ PlatformerVec2 relStart = subVec2(start, {body.x, body.y});
224
+ PlatformerVec2 localStart = rotateVec2(relStart, cosA, sinA);
225
+ PlatformerVec2 localDelta = rotateVec2(delta, cosA, sinA);
226
+
227
+ PlatformerVec2 boxMin{-body.halfX - charShape.halfWidth, -body.halfY - charShape.halfHeight};
228
+ PlatformerVec2 boxMax{body.halfX + charShape.halfWidth, body.halfY + charShape.halfHeight};
229
+
230
+ Platformer2DHit localHit{};
231
+ if (castAABB(localStart, localDelta, boxMin, boxMax, localHit)) {
232
+ hit.hit = true;
233
+ hit.fraction = localHit.fraction;
234
+ hit.normal = rotateVec2(localHit.normal, cosA, -sinA);
235
+ hit.point = addVec2(start, scaleVec2(delta, hit.fraction));
236
+ hit.bodyId = body.id;
237
+ hit.bodyKind = body.kind;
238
+ return true;
239
+ }
240
+ return false;
241
+ }
242
+
243
+ // Standard Axis-Aligned Box
244
+ PlatformerVec2 boxMin{body.x - body.halfX - charShape.halfWidth, body.y - body.halfY - charShape.halfHeight};
245
+ PlatformerVec2 boxMax{body.x + body.halfX + charShape.halfWidth, body.y + body.halfY + charShape.halfHeight};
246
+
247
+ if (castAABB(start, delta, boxMin, boxMax, hit)) {
248
+ hit.bodyId = body.id;
249
+ hit.bodyKind = body.kind;
250
+ return true;
251
+ }
252
+
253
+ return false;
254
+ }
255
+
256
+ bool checkOverlap(
257
+ const PlatformerVec2& pos,
258
+ const Platformer2DShape& charShape,
259
+ const PlatformerBody2D& body) {
260
+ if (std::abs(body.angle) > 1e-6) {
261
+ const double cosA = std::cos(-body.angle);
262
+ const double sinA = std::sin(-body.angle);
263
+ PlatformerVec2 relPos = subVec2(pos, {body.x, body.y});
264
+ PlatformerVec2 localPos = rotateVec2(relPos, cosA, sinA);
265
+ return (std::abs(localPos.x) <= body.halfX + charShape.halfWidth) &&
266
+ (std::abs(localPos.y) <= body.halfY + charShape.halfHeight);
267
+ }
268
+ return (std::abs(pos.x - body.x) <= body.halfX + charShape.halfWidth) &&
269
+ (std::abs(pos.y - body.y) <= body.halfY + charShape.halfHeight);
270
+ }
271
+
272
+ } // namespace
273
+
274
+ uint32_t convertLegacyWorldToBodies(
275
+ const LegacyMovementWorld2D& legacyWorld,
276
+ PlatformerBody2D* outBodies,
277
+ std::uint32_t maxBodies) {
278
+ if (outBodies == nullptr || maxBodies == 0) return 0;
279
+ std::uint32_t count = 0;
280
+ std::int32_t nextId = 1;
281
+
282
+ // 1. Boundary walls
283
+ if (count < maxBodies) {
284
+ // Floor
285
+ outBodies[count++] = PlatformerBody2D{
286
+ .id = nextId++,
287
+ .kind = PlatformerBodyKind2D::Static,
288
+ .shape = PlatformerShapeKind2D::Box,
289
+ .x = (legacyWorld.minX + legacyWorld.maxX) * 0.5,
290
+ .y = legacyWorld.minY - 5.0,
291
+ .angle = 0.0,
292
+ .vx = 0.0,
293
+ .vy = 0.0,
294
+ .halfX = (legacyWorld.maxX - legacyWorld.minX) * 0.5 + 50.0,
295
+ .halfY = 5.0,
296
+ };
297
+ }
298
+
299
+ // 2. Interior Walls
300
+ for (std::uint32_t i = 0; i < legacyWorld.wallCount && count < maxBodies; ++i) {
301
+ const double wallX = legacyWorld.walls[i];
302
+ outBodies[count++] = PlatformerBody2D{
303
+ .id = nextId++,
304
+ .kind = PlatformerBodyKind2D::Static,
305
+ .shape = PlatformerShapeKind2D::Box,
306
+ .x = wallX,
307
+ .y = (legacyWorld.minY + legacyWorld.maxY) * 0.5,
308
+ .angle = 0.0,
309
+ .vx = 0.0,
310
+ .vy = 0.0,
311
+ .halfX = 0.5,
312
+ .halfY = (legacyWorld.maxY - legacyWorld.minY) * 0.5 + 10.0,
313
+ };
314
+ }
315
+
316
+ // 3. Slopes
317
+ for (std::uint32_t i = 0; i < legacyWorld.slopeCount && count < maxBodies; ++i) {
318
+ const auto& s = legacyWorld.slopes[i];
319
+ outBodies[count++] = PlatformerBody2D{
320
+ .id = s.id > 0 ? s.id : nextId++,
321
+ .kind = PlatformerBodyKind2D::Static,
322
+ .shape = PlatformerShapeKind2D::Slope,
323
+ .x = (s.minX + s.maxX) * 0.5,
324
+ .y = s.baseY,
325
+ .angle = 0.0,
326
+ .vx = 0.0,
327
+ .vy = 0.0,
328
+ .halfX = (s.maxX - s.minX) * 0.5,
329
+ .halfY = 1.0,
330
+ .slopeRisePerUnit = s.risePerUnit,
331
+ };
332
+ }
333
+
334
+ // 4. Steps
335
+ for (std::uint32_t i = 0; i < legacyWorld.stepCount && count < maxBodies; ++i) {
336
+ const auto& st = legacyWorld.steps[i];
337
+ outBodies[count++] = PlatformerBody2D{
338
+ .id = st.id > 0 ? st.id : nextId++,
339
+ .kind = PlatformerBodyKind2D::Static,
340
+ .shape = PlatformerShapeKind2D::Box,
341
+ .x = st.x,
342
+ .y = st.height * 0.5,
343
+ .angle = 0.0,
344
+ .vx = 0.0,
345
+ .vy = 0.0,
346
+ .halfX = 0.5,
347
+ .halfY = st.height * 0.5,
348
+ };
349
+ }
350
+
351
+ // 5. Moving Platforms & Elevators
352
+ for (std::uint32_t i = 0; i < legacyWorld.movingPlatformCount && count < maxBodies; ++i) {
353
+ const auto& mp = legacyWorld.movingPlatforms[i];
354
+ const bool isElevator = (std::abs(mp.dyPerFrame) > 1e-6);
355
+ outBodies[count++] = PlatformerBody2D{
356
+ .id = mp.id > 0 ? mp.id : nextId++,
357
+ .kind = isElevator ? PlatformerBodyKind2D::Elevator : PlatformerBodyKind2D::MovingPlatform,
358
+ .shape = PlatformerShapeKind2D::Box,
359
+ .x = (mp.minX + mp.maxX) * 0.5,
360
+ .y = mp.y,
361
+ .angle = 0.0,
362
+ .vx = mp.dxPerFrame * 60.0,
363
+ .vy = mp.dyPerFrame * 60.0,
364
+ .halfX = (mp.maxX - mp.minX) * 0.5,
365
+ .halfY = 0.5,
366
+ };
367
+ }
368
+
369
+ // 6. One-Way Platforms
370
+ for (std::uint32_t i = 0; i < legacyWorld.oneWayPlatformCount && count < maxBodies; ++i) {
371
+ const auto& ow = legacyWorld.oneWayPlatforms[i];
372
+ outBodies[count++] = PlatformerBody2D{
373
+ .id = ow.id > 0 ? ow.id : nextId++,
374
+ .kind = PlatformerBodyKind2D::OneWay,
375
+ .shape = PlatformerShapeKind2D::Box,
376
+ .x = (ow.minX + ow.maxX) * 0.5,
377
+ .y = ow.y,
378
+ .angle = 0.0,
379
+ .vx = 0.0,
380
+ .vy = 0.0,
381
+ .halfX = (ow.maxX - ow.minX) * 0.5,
382
+ .halfY = 0.5,
383
+ };
384
+ }
385
+
386
+ return count;
387
+ }
388
+
389
+ namespace {
390
+
391
+ bool sweepMove(
392
+ PlatformerVec2& currentPos,
393
+ PlatformerVec2& move,
394
+ Platformer2DState& state,
395
+ const Platformer2DConfig& config,
396
+ const PlatformerBody2D* bodies,
397
+ std::uint32_t bodyCount,
398
+ bool isHorizontal,
399
+ Platformer2DEventRecord* outEvents,
400
+ std::uint32_t maxEvents,
401
+ std::uint32_t& eventCount) {
402
+ auto addEvent = [&](Platformer2DEventKind kind, double p0 = 0.0, double p1 = 0.0, std::int32_t bId = -1) {
403
+ if (outEvents != nullptr && eventCount < maxEvents) {
404
+ outEvents[eventCount++] = Platformer2DEventRecord{
405
+ .kind = kind,
406
+ .param0 = p0,
407
+ .param1 = p1,
408
+ .bodyId = bId,
409
+ };
410
+ }
411
+ };
412
+
413
+ for (std::uint32_t iter = 0; iter < config.maxSlideIterations; ++iter) {
414
+ const double dist = lengthVec2(move);
415
+ if (dist < config.minStepDistance) break;
416
+
417
+ Platformer2DHit earliestHit{};
418
+ earliestHit.fraction = 1.0;
419
+ bool foundHit = false;
420
+
421
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
422
+ const auto& body = bodies[i];
423
+ if (body.id == config.excludedBodyId) continue;
424
+ if (body.id == state.oneWayIgnoreBodyId) continue;
425
+ if ((body.layer & config.collisionMask) == 0) continue;
426
+
427
+ Platformer2DHit hit{};
428
+ if (castAgainstBody(currentPos, move, config.shape, body, hit)) {
429
+ if (hit.fraction < earliestHit.fraction) {
430
+ earliestHit = hit;
431
+ foundHit = true;
432
+ }
433
+ }
434
+ }
435
+
436
+ if (!foundHit) {
437
+ currentPos = addVec2(currentPos, move);
438
+ break;
439
+ }
440
+
441
+ state.slideCount++;
442
+
443
+ // Advance up to hit minus skin width
444
+ const double moveFraction = std::max(0.0, earliestHit.fraction - (config.skinWidth / dist));
445
+ currentPos = addVec2(currentPos, scaleVec2(move, moveFraction));
446
+
447
+ // Normal and surface classification
448
+ const PlatformerVec2 normal = earliestHit.normal;
449
+ const double remainingFrac = 1.0 - earliestHit.fraction;
450
+
451
+ if (isHorizontal) {
452
+ state.hitWall = true;
453
+ if (normal.x > 0.5) {
454
+ state.wallSlideSide = -1; // Wall is to character's left
455
+ } else if (normal.x < -0.5) {
456
+ state.wallSlideSide = 1; // Wall is to character's right
457
+ }
458
+ addEvent(Platformer2DEventKind::WallContact, normal.x, normal.y, earliestHit.bodyId);
459
+ break;
460
+ } else {
461
+ if (normal.y > std::cos(config.maxSlopeAngle)) {
462
+ // Ground landing
463
+ state.grounded = true;
464
+ state.groundBodyId = earliestHit.bodyId;
465
+ state.groundNormal = normal;
466
+ state.groundDistance = 0.0;
467
+ if (state.velocity.y < 0.0) state.velocity.y = 0.0;
468
+
469
+ if (earliestHit.bodyKind == PlatformerBodyKind2D::Elevator) {
470
+ addEvent(Platformer2DEventKind::ElevatorContact, 0.0, 0.0, earliestHit.bodyId);
471
+ } else if (earliestHit.bodyKind == PlatformerBodyKind2D::MovingPlatform) {
472
+ addEvent(Platformer2DEventKind::PlatformContact, 0.0, 0.0, earliestHit.bodyId);
473
+ } else if (earliestHit.bodyKind == PlatformerBodyKind2D::OneWay) {
474
+ addEvent(Platformer2DEventKind::OneWayLand, 0.0, 0.0, earliestHit.bodyId);
475
+ }
476
+ } else if (normal.y < -0.1) {
477
+ // Ceiling hit
478
+ state.hitCeiling = true;
479
+ if (state.velocity.y > 0.0) state.velocity.y = 0.0;
480
+ } else {
481
+ state.hitWall = true;
482
+ }
483
+ }
484
+
485
+ // Slide remaining move along surface tangent
486
+ PlatformerVec2 rem = scaleVec2(move, remainingFrac);
487
+ move = subVec2(rem, scaleVec2(normal, dotVec2(rem, normal)));
488
+ if (isHorizontal) move.y = 0.0;
489
+ }
490
+
491
+ return true;
492
+ }
493
+
494
+ bool tryStepUp2D(
495
+ PlatformerVec2& currentPos,
496
+ double horizMoveX,
497
+ Platformer2DState& state,
498
+ const Platformer2DConfig& config,
499
+ const PlatformerBody2D* bodies,
500
+ std::uint32_t bodyCount) {
501
+ if (config.maxStepHeight <= 0.0 || std::abs(horizMoveX) < config.minStepDistance) return false;
502
+
503
+ // 1. Lift character up by step height
504
+ PlatformerVec2 upPos = {currentPos.x, currentPos.y + config.maxStepHeight};
505
+
506
+ // Check if upward space is clear
507
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
508
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) continue;
509
+ if (bodies[i].kind == PlatformerBodyKind2D::OneWay) continue;
510
+ if (checkOverlap(upPos, config.shape, bodies[i])) {
511
+ return false; // Blocked above
512
+ }
513
+ }
514
+
515
+ // 2. Move forward horizontally
516
+ PlatformerVec2 fwdPos = {upPos.x + horizMoveX, upPos.y};
517
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
518
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) continue;
519
+ if (bodies[i].kind == PlatformerBodyKind2D::OneWay) continue;
520
+ if (checkOverlap(fwdPos, config.shape, bodies[i])) {
521
+ return false; // Blocked in front
522
+ }
523
+ }
524
+
525
+ // 3. Cast downward onto step surface
526
+ PlatformerVec2 downDelta = {0.0, -(config.maxStepHeight + config.skinWidth * 2.0)};
527
+ Platformer2DHit stepHit{};
528
+ stepHit.fraction = 1.0;
529
+ bool landed = false;
530
+
531
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
532
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) continue;
533
+ Platformer2DHit h{};
534
+ if (castAgainstBody(fwdPos, downDelta, config.shape, bodies[i], h)) {
535
+ if (h.fraction < stepHit.fraction && h.normal.y >= std::cos(config.maxSlopeAngle)) {
536
+ stepHit = h;
537
+ landed = true;
538
+ }
539
+ }
540
+ }
541
+
542
+ if (!landed) return false;
543
+
544
+ PlatformerVec2 landingPos = addVec2(fwdPos, scaleVec2(downDelta, std::max(0.0, stepHit.fraction - (config.skinWidth / lengthVec2(downDelta)))));
545
+ const double stepDiff = landingPos.y - currentPos.y;
546
+ if (stepDiff <= 0.005 || stepDiff > config.maxStepHeight + 1e-4) return false;
547
+
548
+ currentPos = landingPos;
549
+ state.steppedUp = true;
550
+ state.grounded = true;
551
+ state.groundNormal = stepHit.normal;
552
+ state.groundBodyId = stepHit.bodyId;
553
+ state.groundDistance = 0.0;
554
+ return true;
555
+ }
556
+
557
+ } // namespace
558
+
559
+ Platformer2DStatus stepPlatformer2D(
560
+ Platformer2DState& state,
561
+ const Platformer2DInput& input,
562
+ const Platformer2DConfig& config,
563
+ const PlatformerBody2D* bodies,
564
+ std::uint32_t bodyCount,
565
+ Platformer2DEventRecord* outEvents,
566
+ std::uint32_t maxEvents,
567
+ std::uint32_t* outEventCount) {
568
+ if (input.dt <= 0.0) return Platformer2DStatus::InvalidInput;
569
+ if (config.maxSlideIterations == 0) return Platformer2DStatus::InvalidConfig;
570
+
571
+ std::uint32_t eventCount = 0;
572
+ auto addEvent = [&](Platformer2DEventKind kind, double p0 = 0.0, double p1 = 0.0, std::int32_t bId = -1) {
573
+ if (outEvents != nullptr && eventCount < maxEvents) {
574
+ outEvents[eventCount++] = Platformer2DEventRecord{
575
+ .kind = kind,
576
+ .param0 = p0,
577
+ .param1 = p1,
578
+ .bodyId = bId,
579
+ };
580
+ }
581
+ };
582
+
583
+ // 1. Reset per-frame flags
584
+ state.steppedUp = false;
585
+ state.hitWall = false;
586
+ state.hitCeiling = false;
587
+ state.droppedOneWay = false;
588
+ state.slideCount = 0;
589
+
590
+ // Clear one-way pass-through ignore if clear
591
+ if (state.oneWayIgnoreBodyId >= 0) {
592
+ bool stillOverlaps = false;
593
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
594
+ if (bodies[i].id == state.oneWayIgnoreBodyId) {
595
+ if (checkOverlap(state.position, config.shape, bodies[i])) {
596
+ stillOverlaps = true;
597
+ }
598
+ break;
599
+ }
600
+ }
601
+ if (!stillOverlaps) {
602
+ state.oneWayIgnoreBodyId = -1;
603
+ }
604
+ }
605
+
606
+ // 2. Ladder zone query
607
+ std::int32_t touchingLadderId = -1;
608
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
609
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) {
610
+ if (checkOverlap(state.position, config.shape, bodies[i])) {
611
+ touchingLadderId = bodies[i].id;
612
+ break;
613
+ }
614
+ }
615
+ }
616
+
617
+ if (touchingLadderId >= 0) {
618
+ if (!state.ladderStance) {
619
+ // Mount ladder on vertical input
620
+ if (std::abs(input.moveY) > 0.1 || (!state.grounded && input.moveY > 0.0)) {
621
+ state.ladderStance = true;
622
+ state.ladderBodyId = touchingLadderId;
623
+ state.grounded = false;
624
+ state.groundBodyId = -1;
625
+ state.wallSlideSide = 0;
626
+ addEvent(Platformer2DEventKind::LadderEnter, 0.0, 0.0, touchingLadderId);
627
+ }
628
+ }
629
+ } else {
630
+ if (state.ladderStance) {
631
+ state.ladderStance = false;
632
+ state.ladderBodyId = -1;
633
+ addEvent(Platformer2DEventKind::LadderExit, 0.0, 0.0, -1);
634
+ }
635
+ }
636
+
637
+ // 3. Moving Platform / Elevator persistent attachment & displacement
638
+ if (state.grounded && state.groundBodyId >= 0 && !state.ladderStance) {
639
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
640
+ const auto& body = bodies[i];
641
+ if (body.id == state.groundBodyId) {
642
+ state.platformId = body.id;
643
+ const double platDx = body.vx * input.dt;
644
+ const double platDy = body.vy * input.dt;
645
+ state.position.x += platDx;
646
+ state.position.y += platDy;
647
+
648
+ if (std::abs(body.vy) > 1e-6) {
649
+ addEvent(Platformer2DEventKind::ElevatorRide, body.vx, body.vy, body.id);
650
+ } else if (std::abs(body.vx) > 1e-6) {
651
+ addEvent(Platformer2DEventKind::PlatformRide, body.vx, body.vy, body.id);
652
+ }
653
+ break;
654
+ }
655
+ }
656
+ } else {
657
+ state.platformId = -1;
658
+ }
659
+
660
+ // 4. Update Coyote Time and Jump Buffer timers
661
+ if (state.grounded) {
662
+ state.coyoteTimer = config.coyoteTime;
663
+ } else {
664
+ state.coyoteTimer = std::max(0.0, state.coyoteTimer - input.dt);
665
+ }
666
+
667
+ if (input.jumpPressed) {
668
+ state.jumpBufferTimer = config.jumpBufferTime;
669
+ } else {
670
+ state.jumpBufferTimer = std::max(0.0, state.jumpBufferTimer - input.dt);
671
+ }
672
+
673
+ // 5. Jump Resolution (Buffered Jump, Coyote Jump, Wall Jump, Ladder Jump)
674
+ bool jumpFired = false;
675
+ if (input.jumpPressed || state.jumpBufferTimer > 0.0) {
676
+ if (state.ladderStance && input.jumpPressed) {
677
+ // Dismount/jump off ladder
678
+ state.ladderStance = false;
679
+ state.ladderBodyId = -1;
680
+ state.velocity.y = config.jumpSpeed;
681
+ state.velocity.x = input.moveX * config.walkSpeed;
682
+ jumpFired = true;
683
+ addEvent(Platformer2DEventKind::Jump, state.velocity.x, state.velocity.y, -1);
684
+ addEvent(Platformer2DEventKind::LadderExit, 0.0, 0.0, -1);
685
+ state.jumpBufferTimer = 0.0;
686
+ state.coyoteTimer = 0.0;
687
+ } else if (state.wallSlideSide != 0 && input.jumpPressed && !state.grounded) {
688
+ // Wall jump away from wall
689
+ state.velocity.x = -static_cast<double>(state.wallSlideSide) * config.wallJumpSpeedX;
690
+ state.velocity.y = config.wallJumpSpeedY;
691
+ state.wallSlideSide = 0;
692
+ jumpFired = true;
693
+ addEvent(Platformer2DEventKind::WallJump, state.velocity.x, state.velocity.y, -1);
694
+ state.jumpBufferTimer = 0.0;
695
+ state.coyoteTimer = 0.0;
696
+ } else if (state.grounded) {
697
+ // Normal or buffered ground jump
698
+ state.velocity.y = config.jumpSpeed;
699
+ state.grounded = false;
700
+ state.groundBodyId = -1;
701
+ jumpFired = true;
702
+ if (state.jumpBufferTimer > 0.0 && !input.jumpPressed) {
703
+ addEvent(Platformer2DEventKind::BufferedJump, state.velocity.x, state.velocity.y, -1);
704
+ } else {
705
+ addEvent(Platformer2DEventKind::Jump, state.velocity.x, state.velocity.y, -1);
706
+ }
707
+ state.jumpBufferTimer = 0.0;
708
+ state.coyoteTimer = 0.0;
709
+ } else if (state.coyoteTimer > 0.0) {
710
+ // Coyote Jump
711
+ state.velocity.y = config.jumpSpeed;
712
+ state.grounded = false;
713
+ state.groundBodyId = -1;
714
+ jumpFired = true;
715
+ addEvent(Platformer2DEventKind::CoyoteJump, state.velocity.x, state.velocity.y, -1);
716
+ state.jumpBufferTimer = 0.0;
717
+ state.coyoteTimer = 0.0;
718
+ }
719
+ }
720
+
721
+ // 6. One-Way Platform Drop
722
+ if (input.dropPressed && state.grounded && state.groundBodyId >= 0) {
723
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
724
+ if (bodies[i].id == state.groundBodyId && bodies[i].kind == PlatformerBodyKind2D::OneWay) {
725
+ state.grounded = false;
726
+ state.groundBodyId = -1;
727
+ state.oneWayIgnoreBodyId = bodies[i].id;
728
+ state.position.y -= (config.skinWidth * 2.0 + 0.02);
729
+ state.droppedOneWay = true;
730
+ addEvent(Platformer2DEventKind::OneWayDrop, 0.0, 0.0, bodies[i].id);
731
+ break;
732
+ }
733
+ }
734
+ }
735
+
736
+ // 7. Velocity Integration & Acceleration
737
+ if (state.ladderStance) {
738
+ state.velocity.y = input.moveY * config.ladderClimbSpeed;
739
+ if (std::abs(input.moveY) > 0.01) {
740
+ addEvent(Platformer2DEventKind::LadderClimb, 0.0, input.moveY, state.ladderBodyId);
741
+ }
742
+ // Snap horizontally to ladder center
743
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
744
+ if (bodies[i].id == state.ladderBodyId) {
745
+ state.position.x += (bodies[i].x - state.position.x) * std::min(1.0, config.ladderSnapSpeed * input.dt);
746
+ break;
747
+ }
748
+ }
749
+ state.velocity.x = input.moveX * (config.walkSpeed * 0.5);
750
+ } else {
751
+ // Horizontal acceleration / deceleration
752
+ if (state.grounded) {
753
+ const double targetVx = input.moveX * config.walkSpeed;
754
+ const double rate = (std::abs(targetVx) > 0.01) ? config.acceleration : config.deceleration;
755
+ state.velocity.x = moveTowards(state.velocity.x, targetVx, rate * input.dt);
756
+ } else {
757
+ if (!jumpFired) {
758
+ const double targetVx = input.moveX * config.airSpeed;
759
+ state.velocity.x = moveTowards(state.velocity.x, targetVx, config.airAcceleration * input.dt);
760
+ state.velocity.y += config.gravity * input.dt;
761
+ }
762
+
763
+ // Variable jump height cutoff
764
+ if (!input.jumpHeld && state.velocity.y > 0.0 && !jumpFired) {
765
+ state.velocity.y *= config.variableJumpCutoff;
766
+ }
767
+ }
768
+ }
769
+
770
+ // Facing direction
771
+ if (input.moveX > 0.01) {
772
+ state.facing = 1;
773
+ } else if (input.moveX < -0.01) {
774
+ state.facing = -1;
775
+ }
776
+
777
+ // 8. Motion Decomposition and Sweeps
778
+ PlatformerVec2 horizMove{state.velocity.x * input.dt, 0.0};
779
+ PlatformerVec2 vertMove{0.0, state.velocity.y * input.dt};
780
+
781
+ // Slope projection when grounded
782
+ if (state.grounded && !state.ladderStance) {
783
+ const double cosSlope = state.groundNormal.y;
784
+ if (cosSlope >= std::cos(config.maxSlopeAngle)) {
785
+ // Walkable slope: project horizontal velocity along slope tangent
786
+ const PlatformerVec2 tangent{state.groundNormal.y, -state.groundNormal.x};
787
+ const double hSpeed = state.velocity.x;
788
+ horizMove = scaleVec2(tangent, hSpeed * input.dt);
789
+ if (std::abs(state.groundNormal.x) > 0.05) {
790
+ addEvent(Platformer2DEventKind::SlopeWalk, state.groundNormal.x, state.groundNormal.y, state.groundBodyId);
791
+ }
792
+ }
793
+ }
794
+
795
+ const bool wasGroundedBeforeMove = state.grounded;
796
+
797
+ // Horizontal sweep
798
+ if (std::abs(horizMove.x) > config.minStepDistance || std::abs(horizMove.y) > config.minStepDistance) {
799
+ const PlatformerVec2 prevPos = state.position;
800
+ PlatformerVec2 remainingMove = horizMove;
801
+ sweepMove(state.position, remainingMove, state, config, bodies, bodyCount, true, outEvents, maxEvents, eventCount);
802
+
803
+ // Try Step-Up if wall was hit while grounded
804
+ if (state.hitWall && wasGroundedBeforeMove && config.maxStepHeight > 0.0) {
805
+ PlatformerVec2 stepPos = prevPos;
806
+ if (tryStepUp2D(stepPos, horizMove.x, state, config, bodies, bodyCount)) {
807
+ state.position = stepPos;
808
+ state.hitWall = false;
809
+ addEvent(Platformer2DEventKind::StepUp, stepPos.x, stepPos.y, -1);
810
+ }
811
+ }
812
+ }
813
+
814
+ // Vertical sweep
815
+ if (std::abs(vertMove.y) > 1e-12) {
816
+ PlatformerVec2 remainingMove = vertMove;
817
+ sweepMove(state.position, remainingMove, state, config, bodies, bodyCount, false, outEvents, maxEvents, eventCount);
818
+ }
819
+
820
+ // Check buffered jump on landing
821
+ if (state.grounded && state.jumpBufferTimer > 0.0) {
822
+ state.velocity.y = config.jumpSpeed;
823
+ state.grounded = false;
824
+ state.groundBodyId = -1;
825
+ state.jumpBufferTimer = 0.0;
826
+ state.coyoteTimer = 0.0;
827
+ addEvent(Platformer2DEventKind::BufferedJump, state.velocity.x, state.velocity.y, -1);
828
+ }
829
+
830
+ // 9. Ground Support Check & Snap
831
+ if (state.velocity.y <= 0.01 && !state.ladderStance) {
832
+ // Probe ground under feet
833
+ const double probeDist = config.groundProbeDistance + config.skinWidth * 2.0;
834
+ PlatformerVec2 probeDelta{0.0, -probeDist};
835
+ Platformer2DHit groundHit{};
836
+ groundHit.fraction = 1.0;
837
+ bool foundGround = false;
838
+
839
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
840
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) continue;
841
+ if (bodies[i].id == state.oneWayIgnoreBodyId) continue;
842
+ PlatformerBody2D probeBody = bodies[i];
843
+ if (probeBody.id == state.groundBodyId && (std::abs(probeBody.vx) > 1e-6 || std::abs(probeBody.vy) > 1e-6)) {
844
+ probeBody.x += probeBody.vx * input.dt;
845
+ probeBody.y += probeBody.vy * input.dt;
846
+ }
847
+ Platformer2DHit h{};
848
+ if (castAgainstBody(state.position, probeDelta, config.shape, probeBody, h)) {
849
+ if (h.fraction < groundHit.fraction && h.normal.y >= std::cos(config.maxSlopeAngle)) {
850
+ groundHit = h;
851
+ foundGround = true;
852
+ }
853
+ }
854
+ }
855
+
856
+ if (foundGround) {
857
+ state.grounded = true;
858
+ state.groundBodyId = groundHit.bodyId;
859
+ state.groundNormal = groundHit.normal;
860
+ state.groundDistance = groundHit.fraction * probeDist;
861
+
862
+ // Ground snap
863
+ if (wasGroundedBeforeMove && config.snapToGround && state.groundDistance > config.skinWidth) {
864
+ const double snapDist = state.groundDistance - config.skinWidth;
865
+ if (snapDist <= config.maxStepHeight + config.groundProbeDistance) {
866
+ state.position.y -= snapDist;
867
+ }
868
+ }
869
+
870
+ if (state.velocity.y < 0.0) {
871
+ state.velocity.y = 0.0;
872
+ }
873
+
874
+ if (state.jumpBufferTimer > 0.0) {
875
+ state.velocity.y = config.jumpSpeed;
876
+ state.grounded = false;
877
+ state.groundBodyId = -1;
878
+ state.jumpBufferTimer = 0.0;
879
+ state.coyoteTimer = 0.0;
880
+ addEvent(Platformer2DEventKind::BufferedJump, state.velocity.x, state.velocity.y, -1);
881
+ }
882
+ } else {
883
+ state.grounded = false;
884
+ state.groundBodyId = -1;
885
+ }
886
+ } else if (!state.ladderStance) {
887
+ state.grounded = false;
888
+ state.groundBodyId = -1;
889
+ }
890
+
891
+ // 10. Wall Slide state evaluation
892
+ if (!state.grounded && state.velocity.y < 0.0 && !state.ladderStance) {
893
+ // Check if player is falling along a wall
894
+ PlatformerVec2 wallCheckDelta{state.facing > 0 ? (config.skinWidth * 4.0) : -(config.skinWidth * 4.0), 0.0};
895
+ Platformer2DHit wallHit{};
896
+ wallHit.fraction = 1.0;
897
+ bool againstWall = false;
898
+
899
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
900
+ if (bodies[i].kind == PlatformerBodyKind2D::Ladder) continue;
901
+ if (bodies[i].kind == PlatformerBodyKind2D::OneWay) continue;
902
+ Platformer2DHit h{};
903
+ if (castAgainstBody(state.position, wallCheckDelta, config.shape, bodies[i], h)) {
904
+ if (std::abs(h.normal.x) > 0.7) {
905
+ againstWall = true;
906
+ wallHit = h;
907
+ break;
908
+ }
909
+ }
910
+ }
911
+
912
+ if (againstWall) {
913
+ const std::int32_t side = wallHit.normal.x < 0.0 ? 1 : -1;
914
+ if (input.moveX * static_cast<double>(side) >= 0.0) {
915
+ state.wallSlideSide = side;
916
+ if (state.velocity.y < -config.wallSlideSpeed) {
917
+ state.velocity.y = -config.wallSlideSpeed;
918
+ }
919
+ addEvent(Platformer2DEventKind::WallSlide, state.velocity.x, state.velocity.y, wallHit.bodyId);
920
+ } else {
921
+ state.wallSlideSide = 0;
922
+ }
923
+ } else {
924
+ state.wallSlideSide = 0;
925
+ }
926
+ } else {
927
+ state.wallSlideSide = 0;
928
+ }
929
+
930
+ if (outEventCount != nullptr) {
931
+ *outEventCount = eventCount;
932
+ }
933
+
934
+ return Platformer2DStatus::Success;
935
+ }
936
+
937
+ Platformer2DStatus stepPlatformer2DWithPhysicsBodies(
938
+ Platformer2DState& state,
939
+ const Platformer2DInput& input,
940
+ const Platformer2DConfig& config,
941
+ const rundot::kinetix::physics::Body* bodies,
942
+ std::uint32_t bodyCount,
943
+ Platformer2DEventRecord* outEvents,
944
+ std::uint32_t maxEvents,
945
+ std::uint32_t* outEventCount) {
946
+ if (bodies == nullptr && bodyCount > 0) return Platformer2DStatus::InvalidInput;
947
+
948
+ std::array<PlatformerBody2D, kPlatformer2DMaxBodies> convBodies{};
949
+ const std::uint32_t n = std::min(bodyCount, kPlatformer2DMaxBodies);
950
+
951
+ for (std::uint32_t i = 0; i < n; ++i) {
952
+ const auto& b = bodies[i];
953
+ PlatformerBodyKind2D kind = PlatformerBodyKind2D::Static;
954
+ if (b.sensor || b.trigger) {
955
+ kind = PlatformerBodyKind2D::Ladder;
956
+ } else if (b.kinematic) {
957
+ if (std::abs(b.vy) > 1e-6) {
958
+ kind = PlatformerBodyKind2D::Elevator;
959
+ } else {
960
+ kind = PlatformerBodyKind2D::MovingPlatform;
961
+ }
962
+ } else if (b.dynamic) {
963
+ kind = PlatformerBodyKind2D::Dynamic;
964
+ }
965
+
966
+ PlatformerShapeKind2D shape = PlatformerShapeKind2D::Box;
967
+ if (b.shape == ShapeKind::Sphere) {
968
+ shape = PlatformerShapeKind2D::Circle;
969
+ } else if (b.shape == ShapeKind::Capsule) {
970
+ shape = PlatformerShapeKind2D::Capsule;
971
+ }
972
+
973
+ convBodies[i] = PlatformerBody2D{
974
+ .id = static_cast<std::int32_t>(i + 1),
975
+ .kind = kind,
976
+ .shape = shape,
977
+ .x = b.x,
978
+ .y = b.y,
979
+ .angle = b.angularVelZ,
980
+ .vx = b.vx,
981
+ .vy = b.vy,
982
+ .halfX = b.shape0 > 0.0 ? b.shape0 : 0.5,
983
+ .halfY = b.shape1 > 0.0 ? b.shape1 : 0.5,
984
+ .friction = b.friction,
985
+ .restitution = b.restitution,
986
+ .layer = b.layer,
987
+ .mask = b.mask,
988
+ .slopeRisePerUnit = 0.0,
989
+ };
990
+ }
991
+
992
+ return stepPlatformer2D(state, input, config, convBodies.data(), n, outEvents, maxEvents, outEventCount);
993
+ }
994
+
995
+ Platformer2DStatus stepPlatformer2DWithCallbacks(
996
+ Platformer2DState& state,
997
+ const Platformer2DInput& input,
998
+ const Platformer2DConfig& config,
999
+ const Platformer2DQueryCallbacks& queries,
1000
+ Platformer2DEventRecord* outEvents,
1001
+ std::uint32_t maxEvents,
1002
+ std::uint32_t* outEventCount) {
1003
+ if (queries.shapeCast == nullptr) return Platformer2DStatus::InvalidInput;
1004
+ (void)outEvents;
1005
+ (void)maxEvents;
1006
+
1007
+ // Sweep using shapeCast callback
1008
+ std::uint32_t eventCount = 0;
1009
+ state.steppedUp = false;
1010
+ state.hitWall = false;
1011
+ state.hitCeiling = false;
1012
+
1013
+ PlatformerVec2 delta{state.velocity.x * input.dt, state.velocity.y * input.dt};
1014
+ Platformer2DHit hit{};
1015
+ if (queries.shapeCast(queries.userData, config.shape, state.position, delta, lengthVec2(delta), config.collisionMask, config.excludedBodyId, hit)) {
1016
+ state.position = hit.point;
1017
+ if (hit.normal.y > 0.7) {
1018
+ state.grounded = true;
1019
+ state.groundNormal = hit.normal;
1020
+ }
1021
+ } else {
1022
+ state.position = addVec2(state.position, delta);
1023
+ }
1024
+
1025
+ if (outEventCount != nullptr) *outEventCount = eventCount;
1026
+ return Platformer2DStatus::Success;
1027
+ }
1028
+
1029
+ void unpackPlatformer2DState(const double* src, Platformer2DState& state) {
1030
+ if (src == nullptr) return;
1031
+ state.position.x = src[0];
1032
+ state.position.y = src[1];
1033
+ state.velocity.x = src[2];
1034
+ state.velocity.y = src[3];
1035
+ state.grounded = (src[4] != 0.0);
1036
+ state.groundBodyId = static_cast<std::int32_t>(src[5]);
1037
+ state.groundNormal.x = src[6];
1038
+ state.groundNormal.y = src[7];
1039
+ state.coyoteTimer = src[8];
1040
+ state.jumpBufferTimer = src[9];
1041
+ state.wallSlideSide = static_cast<std::int32_t>(src[10]);
1042
+ state.ladderStance = (src[11] != 0.0);
1043
+ state.ladderBodyId = static_cast<std::int32_t>(src[12]);
1044
+ state.platformId = static_cast<std::int32_t>(src[13]);
1045
+ state.facing = static_cast<std::int32_t>(src[14]);
1046
+ state.steppedUp = (src[15] != 0.0);
1047
+ state.hitWall = (src[16] != 0.0);
1048
+ state.hitCeiling = (src[17] != 0.0);
1049
+ state.droppedOneWay = (src[18] != 0.0);
1050
+ state.oneWayIgnoreBodyId = static_cast<std::int32_t>(src[19]);
1051
+ state.slideCount = static_cast<std::uint32_t>(src[20]);
1052
+ }
1053
+
1054
+ void packPlatformer2DState(const Platformer2DState& state, double* dst) {
1055
+ if (dst == nullptr) return;
1056
+ dst[0] = state.position.x;
1057
+ dst[1] = state.position.y;
1058
+ dst[2] = state.velocity.x;
1059
+ dst[3] = state.velocity.y;
1060
+ dst[4] = state.grounded ? 1.0 : 0.0;
1061
+ dst[5] = static_cast<double>(state.groundBodyId);
1062
+ dst[6] = state.groundNormal.x;
1063
+ dst[7] = state.groundNormal.y;
1064
+ dst[8] = state.coyoteTimer;
1065
+ dst[9] = state.jumpBufferTimer;
1066
+ dst[10] = static_cast<double>(state.wallSlideSide);
1067
+ dst[11] = state.ladderStance ? 1.0 : 0.0;
1068
+ dst[12] = static_cast<double>(state.ladderBodyId);
1069
+ dst[13] = static_cast<double>(state.platformId);
1070
+ dst[14] = static_cast<double>(state.facing);
1071
+ dst[15] = state.steppedUp ? 1.0 : 0.0;
1072
+ dst[16] = state.hitWall ? 1.0 : 0.0;
1073
+ dst[17] = state.hitCeiling ? 1.0 : 0.0;
1074
+ dst[18] = state.droppedOneWay ? 1.0 : 0.0;
1075
+ dst[19] = static_cast<double>(state.oneWayIgnoreBodyId);
1076
+ dst[20] = static_cast<double>(state.slideCount);
1077
+ dst[21] = 0.0;
1078
+ dst[22] = 0.0;
1079
+ dst[23] = 0.0;
1080
+ }
1081
+
1082
+ void unpackPlatformer2DInput(const double* src, Platformer2DInput& input) {
1083
+ if (src == nullptr) return;
1084
+ input.moveX = src[0];
1085
+ input.moveY = src[1];
1086
+ input.jumpPressed = (src[2] != 0.0);
1087
+ input.jumpHeld = (src[3] != 0.0);
1088
+ input.dropPressed = (src[4] != 0.0);
1089
+ input.dropHeld = (src[5] != 0.0);
1090
+ input.dt = src[6] > 0.0 ? src[6] : (1.0 / 60.0);
1091
+ }
1092
+
1093
+ void unpackPlatformer2DConfig(const double* src, Platformer2DConfig& config) {
1094
+ if (src == nullptr) return;
1095
+ config.gravity = src[0];
1096
+ config.walkSpeed = src[1];
1097
+ config.airSpeed = src[2];
1098
+ config.acceleration = src[3];
1099
+ config.deceleration = src[4];
1100
+ config.airAcceleration = src[5];
1101
+ config.jumpSpeed = src[6];
1102
+ config.coyoteTime = src[7];
1103
+ config.jumpBufferTime = src[8];
1104
+ config.maxSlopeAngle = src[9];
1105
+ config.maxStepHeight = src[10];
1106
+ config.skinWidth = src[11];
1107
+ config.maxSlideIterations = static_cast<std::uint32_t>(src[12]);
1108
+ config.wallSlideSpeed = src[13];
1109
+ config.wallJumpSpeedX = src[14];
1110
+ config.wallJumpSpeedY = src[15];
1111
+ config.ladderClimbSpeed = src[16];
1112
+ config.ladderSnapSpeed = src[17];
1113
+ config.shape.halfWidth = src[18];
1114
+ config.shape.halfHeight = src[19];
1115
+ config.collisionLayer = static_cast<std::uint32_t>(src[20]);
1116
+ config.collisionMask = static_cast<std::uint32_t>(src[21]);
1117
+ config.variableJumpCutoff = src[22];
1118
+ config.snapToGround = (src[23] != 0.0);
1119
+ config.minStepDistance = src[24];
1120
+ config.groundProbeDistance = src[25];
1121
+ }
1122
+
1123
+ void unpackPlatformer2DBody(const double* src, PlatformerBody2D& body) {
1124
+ if (src == nullptr) return;
1125
+ body.id = static_cast<std::int32_t>(src[0]);
1126
+ body.kind = static_cast<PlatformerBodyKind2D>(static_cast<std::uint32_t>(src[1]));
1127
+ body.shape = static_cast<PlatformerShapeKind2D>(static_cast<std::uint32_t>(src[2]));
1128
+ body.x = src[3];
1129
+ body.y = src[4];
1130
+ body.angle = src[5];
1131
+ body.vx = src[6];
1132
+ body.vy = src[7];
1133
+ body.halfX = src[8];
1134
+ body.halfY = src[9];
1135
+ body.friction = src[10];
1136
+ body.restitution = src[11];
1137
+ body.layer = static_cast<std::uint32_t>(src[12]);
1138
+ body.mask = static_cast<std::uint32_t>(src[13]);
1139
+ body.slopeRisePerUnit = src[14];
1140
+ body.reserved = src[15];
1141
+ }
1142
+
1143
+ void packPlatformer2DEvent(const Platformer2DEventRecord& event, double* dst) {
1144
+ if (dst == nullptr) return;
1145
+ dst[0] = static_cast<double>(static_cast<std::uint32_t>(event.kind));
1146
+ dst[1] = event.param0;
1147
+ dst[2] = event.param1;
1148
+ dst[3] = static_cast<double>(event.bodyId);
1149
+ }
1150
+
1151
+ } // namespace rundot::kinetix::physics::platformer2d
1152
+
1153
+ extern "C" {
1154
+
1155
+ using namespace rundot::kinetix::physics::platformer2d;
1156
+
1157
+ std::uint32_t rundot_kinetix_ext_platformer2d_abi_version() {
1158
+ return kPlatformer2DAbiVersion;
1159
+ }
1160
+
1161
+ std::uint32_t rundot_kinetix_ext_platformer2d_state_stride() {
1162
+ return kPlatformer2DStateStride;
1163
+ }
1164
+
1165
+ std::uint32_t rundot_kinetix_ext_platformer2d_input_stride() {
1166
+ return kPlatformer2DInputStride;
1167
+ }
1168
+
1169
+ std::uint32_t rundot_kinetix_ext_platformer2d_config_stride() {
1170
+ return kPlatformer2DConfigStride;
1171
+ }
1172
+
1173
+ std::uint32_t rundot_kinetix_ext_platformer2d_body_stride() {
1174
+ return kPlatformer2DBodyStride;
1175
+ }
1176
+
1177
+ std::uint32_t rundot_kinetix_ext_platformer2d_event_stride() {
1178
+ return kPlatformer2DEventStride;
1179
+ }
1180
+
1181
+ std::uint32_t rundot_kinetix_ext_platformer2d_max_events() {
1182
+ return kPlatformer2DMaxEvents;
1183
+ }
1184
+
1185
+ std::uint32_t rundot_kinetix_ext_platformer2d_max_bodies() {
1186
+ return kPlatformer2DMaxBodies;
1187
+ }
1188
+
1189
+ std::int32_t rundot_kinetix_ext_platformer2d_step(
1190
+ double* stateData,
1191
+ const double* inputData,
1192
+ const double* configData,
1193
+ const double* bodyData,
1194
+ std::uint32_t bodyCount,
1195
+ double* outEventsData,
1196
+ std::uint32_t maxEvents,
1197
+ std::uint32_t* outEventCount) {
1198
+ if (stateData == nullptr || inputData == nullptr || configData == nullptr) {
1199
+ return static_cast<std::int32_t>(Platformer2DStatus::InvalidInput);
1200
+ }
1201
+
1202
+ Platformer2DState state{};
1203
+ unpackPlatformer2DState(stateData, state);
1204
+
1205
+ Platformer2DInput input{};
1206
+ unpackPlatformer2DInput(inputData, input);
1207
+
1208
+ Platformer2DConfig config{};
1209
+ unpackPlatformer2DConfig(configData, config);
1210
+
1211
+ std::array<PlatformerBody2D, kPlatformer2DMaxBodies> bodies{};
1212
+ const std::uint32_t count = std::min(bodyCount, kPlatformer2DMaxBodies);
1213
+ for (std::uint32_t i = 0; i < count; ++i) {
1214
+ unpackPlatformer2DBody(bodyData + i * kPlatformer2DBodyStride, bodies[i]);
1215
+ }
1216
+
1217
+ std::array<Platformer2DEventRecord, kPlatformer2DMaxEvents> events{};
1218
+ const std::uint32_t eventCap = std::min(maxEvents, kPlatformer2DMaxEvents);
1219
+ std::uint32_t producedEvents = 0;
1220
+
1221
+ Platformer2DStatus status = stepPlatformer2D(
1222
+ state, input, config, bodies.data(), count, events.data(), eventCap, &producedEvents);
1223
+
1224
+ if (status != Platformer2DStatus::Success) {
1225
+ return static_cast<std::int32_t>(status);
1226
+ }
1227
+
1228
+ packPlatformer2DState(state, stateData);
1229
+
1230
+ if (outEventsData != nullptr && producedEvents > 0) {
1231
+ for (std::uint32_t i = 0; i < producedEvents; ++i) {
1232
+ packPlatformer2DEvent(events[i], outEventsData + i * kPlatformer2DEventStride);
1233
+ }
1234
+ }
1235
+
1236
+ if (outEventCount != nullptr) {
1237
+ *outEventCount = producedEvents;
1238
+ }
1239
+
1240
+ return 0;
1241
+ }
1242
+
1243
+ std::int32_t rundot_kinetix_ext_platformer2d_step_legacy(
1244
+ double* stateData,
1245
+ const double* inputData,
1246
+ const double* configData,
1247
+ double worldMinX, double worldMaxX,
1248
+ double worldMinY, double worldMaxY,
1249
+ const double* walls, std::uint32_t wallCount,
1250
+ const double* slopesData, std::uint32_t slopeCount,
1251
+ const double* stepsData, std::uint32_t stepCount,
1252
+ const double* movingPlatformsData, std::uint32_t movingPlatformCount,
1253
+ const double* oneWayPlatformsData, std::uint32_t oneWayPlatformCount,
1254
+ double* outEventsData,
1255
+ std::uint32_t maxEvents,
1256
+ std::uint32_t* outEventCount) {
1257
+ if (stateData == nullptr || inputData == nullptr || configData == nullptr) {
1258
+ return static_cast<std::int32_t>(Platformer2DStatus::InvalidInput);
1259
+ }
1260
+
1261
+ std::array<LegacySlope2D, 32> slopes{};
1262
+ const std::uint32_t nSlopes = std::min(slopeCount, 32u);
1263
+ for (std::uint32_t i = 0; i < nSlopes; ++i) {
1264
+ slopes[i] = LegacySlope2D{
1265
+ .id = static_cast<std::int32_t>(slopesData[i * 5 + 0]),
1266
+ .minX = slopesData[i * 5 + 1],
1267
+ .maxX = slopesData[i * 5 + 2],
1268
+ .baseY = slopesData[i * 5 + 3],
1269
+ .risePerUnit = slopesData[i * 5 + 4],
1270
+ };
1271
+ }
1272
+
1273
+ std::array<LegacyStep2D, 32> steps{};
1274
+ const std::uint32_t nSteps = std::min(stepCount, 32u);
1275
+ for (std::uint32_t i = 0; i < nSteps; ++i) {
1276
+ steps[i] = LegacyStep2D{
1277
+ .id = static_cast<std::int32_t>(stepsData[i * 3 + 0]),
1278
+ .x = stepsData[i * 3 + 1],
1279
+ .height = stepsData[i * 3 + 2],
1280
+ };
1281
+ }
1282
+
1283
+ std::array<LegacyMovingPlatform2D, 32> movingPlats{};
1284
+ const std::uint32_t nMoving = std::min(movingPlatformCount, 32u);
1285
+ for (std::uint32_t i = 0; i < nMoving; ++i) {
1286
+ movingPlats[i] = LegacyMovingPlatform2D{
1287
+ .id = static_cast<std::int32_t>(movingPlatformsData[i * 5 + 0]),
1288
+ .minX = movingPlatformsData[i * 5 + 1],
1289
+ .maxX = movingPlatformsData[i * 5 + 2],
1290
+ .y = movingPlatformsData[i * 5 + 3],
1291
+ .dxPerFrame = movingPlatformsData[i * 5 + 4],
1292
+ .dyPerFrame = 0.0,
1293
+ };
1294
+ }
1295
+
1296
+ std::array<LegacyOneWayPlatform2D, 32> oneWays{};
1297
+ const std::uint32_t nOneWay = std::min(oneWayPlatformCount, 32u);
1298
+ for (std::uint32_t i = 0; i < nOneWay; ++i) {
1299
+ oneWays[i] = LegacyOneWayPlatform2D{
1300
+ .id = static_cast<std::int32_t>(oneWayPlatformsData[i * 4 + 0]),
1301
+ .minX = oneWayPlatformsData[i * 4 + 1],
1302
+ .maxX = oneWayPlatformsData[i * 4 + 2],
1303
+ .y = oneWayPlatformsData[i * 4 + 3],
1304
+ };
1305
+ }
1306
+
1307
+ LegacyMovementWorld2D legacyWorld{
1308
+ .minX = worldMinX,
1309
+ .maxX = worldMaxX,
1310
+ .minY = worldMinY,
1311
+ .maxY = worldMaxY,
1312
+ .walls = walls,
1313
+ .wallCount = wallCount,
1314
+ .slopes = slopes.data(),
1315
+ .slopeCount = nSlopes,
1316
+ .steps = steps.data(),
1317
+ .stepCount = nSteps,
1318
+ .movingPlatforms = movingPlats.data(),
1319
+ .movingPlatformCount = nMoving,
1320
+ .oneWayPlatforms = oneWays.data(),
1321
+ .oneWayPlatformCount = nOneWay,
1322
+ };
1323
+
1324
+ std::array<PlatformerBody2D, kPlatformer2DMaxBodies> bodies{};
1325
+ const std::uint32_t bodyCount = convertLegacyWorldToBodies(legacyWorld, bodies.data(), kPlatformer2DMaxBodies);
1326
+
1327
+ std::array<double, kPlatformer2DMaxBodies * kPlatformer2DBodyStride> packedBodies{};
1328
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
1329
+ const auto& b = bodies[i];
1330
+ double* d = packedBodies.data() + i * kPlatformer2DBodyStride;
1331
+ d[0] = static_cast<double>(b.id);
1332
+ d[1] = static_cast<double>(static_cast<std::uint32_t>(b.kind));
1333
+ d[2] = static_cast<double>(static_cast<std::uint32_t>(b.shape));
1334
+ d[3] = b.x;
1335
+ d[4] = b.y;
1336
+ d[5] = b.angle;
1337
+ d[6] = b.vx;
1338
+ d[7] = b.vy;
1339
+ d[8] = b.halfX;
1340
+ d[9] = b.halfY;
1341
+ d[10] = b.friction;
1342
+ d[11] = b.restitution;
1343
+ d[12] = static_cast<double>(b.layer);
1344
+ d[13] = static_cast<double>(b.mask);
1345
+ d[14] = b.slopeRisePerUnit;
1346
+ d[15] = 0.0;
1347
+ }
1348
+
1349
+ return rundot_kinetix_ext_platformer2d_step(
1350
+ stateData, inputData, configData, packedBodies.data(), bodyCount, outEventsData, maxEvents, outEventCount);
1351
+ }
1352
+
1353
+ } // extern "C"