@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
@@ -2,6 +2,10 @@
2
2
 
3
3
  #include "fluid.hpp"
4
4
  #include "physics2d.hpp"
5
+ #include "ccd.hpp"
6
+ #include "joint_phases.hpp"
7
+ #include "phase_test_hooks.hpp"
8
+ #include "memory_private.hpp"
5
9
 
6
10
  #include "../deterministic_math.hpp"
7
11
 
@@ -12,11 +16,7 @@
12
16
  #include <cstring>
13
17
  #include <limits>
14
18
  #include <new>
15
-
16
- #if defined(RUNDOT_PHYSICS2D_TEST_ALLOCATOR)
17
- extern "C" void* rundot_physics2d_test_allocate(std::size_t size);
18
- extern "C" void rundot_physics2d_test_free(void* value);
19
- #endif
19
+ #include <utility>
20
20
 
21
21
  namespace rundot::kinetix::physics2d {
22
22
  namespace {
@@ -26,27 +26,23 @@ constexpr double kPi = 3.141592653589793238462643383279502884;
26
26
  constexpr double kBiasFactor = 0.2;
27
27
  constexpr double kInfiniteImpulse = std::numeric_limits<double>::infinity();
28
28
  constexpr std::uint32_t kNoBody = std::numeric_limits<std::uint32_t>::max();
29
+ enum class GeometryPhase { BeginFixed, RefreshAtPose };
30
+ using joint_phases_private::JointEventLedger2D;
31
+ using joint_phases_private::GearEventLedger2D;
29
32
 
30
33
  void* solverAllocate(std::size_t size) {
31
- #if defined(RUNDOT_PHYSICS2D_TEST_ALLOCATOR)
32
- return rundot_physics2d_test_allocate(size);
33
- #else
34
- return std::malloc(size);
35
- #endif
34
+ return memory_private::allocate(memory_private::LedgerClass::Storage,
35
+ size, alignof(std::max_align_t));
36
36
  }
37
37
 
38
38
  void solverFree(void* value) {
39
- #if defined(RUNDOT_PHYSICS2D_TEST_ALLOCATOR)
40
- rundot_physics2d_test_free(value);
41
- #else
42
- std::free(value);
43
- #endif
39
+ memory_private::deallocate(value);
44
40
  }
45
41
 
46
42
  template <typename T>
47
43
  T* allocateSolverArray(std::uint32_t count) {
48
44
  if (count == 0) return nullptr;
49
- return static_cast<T*>(solverAllocate(static_cast<std::size_t>(count) * sizeof(T)));
45
+ return memory_private::allocateArray<T>(memory_private::LedgerClass::Storage, count, false);
50
46
  }
51
47
 
52
48
  struct SolverVec2 {
@@ -821,7 +817,10 @@ double rowVelocity(const SolverRow& row, const Body* bodies) {
821
817
  return value;
822
818
  }
823
819
 
824
- void applyRowImpulse(SolverRow& row, Body* bodies, PreparedJoint& prepared, double delta) {
820
+ bool scaledPulleyProduct(double, double, int, double&);
821
+
822
+ void applyRowImpulse(SolverRow& row, Body* bodies, PreparedJoint& prepared, double delta,
823
+ JointEventLedger2D* ledger = nullptr, bool pulley = false, int pulleyExponent = 0) {
825
824
  Body* bodyA = bodyAt(bodies, row.bodyA);
826
825
  Body* bodyB = bodyAt(bodies, row.bodyB);
827
826
  if (bodyA != nullptr && bodyA->type == BodyType::Dynamic) {
@@ -836,9 +835,30 @@ void applyRowImpulse(SolverRow& row, Body* bodies, PreparedJoint& prepared, doub
836
835
  bodyB->vy += row.linearB.y * massScale;
837
836
  bodyB->angularVelocity += row.angularB * inverseInertia(bodyB) * delta;
838
837
  }
839
- const SolverVec2 impulseB = scale(row.linearB, delta);
840
- prepared.substepImpulseB = add(prepared.substepImpulseB, impulseB);
838
+ SolverVec2 impulseB = scale(row.linearB, delta);
839
+ const SolverVec2 solverImpulseB = impulseB;
840
+ if (ledger != nullptr && pulley) {
841
+ double physicalMagnitude = 0.0;
842
+ if (!scaledPulleyProduct(row.reactionCoefficientB, delta,
843
+ pulleyExponent, physicalMagnitude)) {
844
+ ledger->reactionFailure = true;
845
+ return;
846
+ }
847
+ impulseB = scale(row.reactionLinearB, physicalMagnitude);
848
+ if (!finite(impulseB.x) || !finite(impulseB.y)) {
849
+ ledger->reactionFailure = true;
850
+ return;
851
+ }
852
+ }
853
+ prepared.substepImpulseB = add(prepared.substepImpulseB, solverImpulseB);
841
854
  prepared.substepCoupleB += row.directCoupleB * delta;
855
+ if (ledger != nullptr) {
856
+ ledger->substepImpulseB.x += impulseB.x;
857
+ ledger->substepImpulseB.y += impulseB.y;
858
+ ledger->substepCoupleB += row.directCoupleB * delta;
859
+ if (!finite(ledger->substepImpulseB.x) || !finite(ledger->substepImpulseB.y)
860
+ || !finite(ledger->substepCoupleB)) ledger->reactionFailure = true;
861
+ }
842
862
  if (row.motor) prepared.appliedMotorImpulse += delta;
843
863
  }
844
864
 
@@ -887,17 +907,21 @@ void configureMotor(SolverRow& row, double speed, double maximum, double substep
887
907
  row.motor = true;
888
908
  }
889
909
 
890
- bool addRow(PreparedJoint& prepared, JointState2D& state, SolverRow row) {
910
+ bool addRow(PreparedJoint& prepared, JointState2D& state, SolverRow row,
911
+ const JointEventLedger2D* retained = nullptr) {
891
912
  if (prepared.rowCount >= kMaxJointWarmRows || row.slot >= kMaxJointWarmRows) return false;
892
- row.impulse = state.accumulatedImpulse[row.slot];
913
+ row.impulse = retained == nullptr
914
+ ? state.accumulatedImpulse[row.slot] : retained->retainedSlotImpulse[row.slot];
893
915
  prepared.rows[prepared.rowCount++] = row;
894
916
  prepared.activeMask |= 1u << row.slot;
895
917
  return true;
896
918
  }
897
919
 
898
- bool addPulleyRow(PreparedJoint& prepared, const JointState2D& state, SolverRow row) {
920
+ bool addPulleyRow(PreparedJoint& prepared, const JointState2D& state, SolverRow row,
921
+ const JointEventLedger2D* retained = nullptr) {
899
922
  if (prepared.rowCount >= kMaxJointWarmRows || row.slot >= kMaxJointWarmRows) return false;
900
- const double stateImpulse = state.accumulatedImpulse[row.slot];
923
+ const double stateImpulse = retained == nullptr
924
+ ? state.accumulatedImpulse[row.slot] : retained->retainedSlotImpulse[row.slot];
901
925
  const int scaleExponent = prepared.pulleyMobilityScaleExponent;
902
926
  row.impulse = scaleExponent == 0
903
927
  ? stateImpulse : std::scalbn(stateImpulse, -scaleExponent);
@@ -916,7 +940,7 @@ void addAnchorBlock(
916
940
  double substepDuration,
917
941
  double frequencyHz,
918
942
  double dampingRatio,
919
- bool angularRow) {
943
+ bool angularRow, const JointEventLedger2D* retained = nullptr) {
920
944
  const SolverVec2 error = subtract(endpointB.point, endpointA.point);
921
945
  SolverRow x = makeRow(0, endpointA, endpointB);
922
946
  setLinearRow(x, {1.0, 0.0}, endpointA.lever, endpointB.lever);
@@ -926,14 +950,14 @@ void addAnchorBlock(
926
950
  setLinearRow(y, {0.0, 1.0}, endpointA.lever, endpointB.lever);
927
951
  y.block = 1;
928
952
  configureSoft(y, bodies, error.y, frequencyHz, dampingRatio, substepDuration);
929
- addRow(prepared, state, x);
930
- addRow(prepared, state, y);
953
+ addRow(prepared, state, x, retained);
954
+ addRow(prepared, state, y, retained);
931
955
  if (angularRow) {
932
956
  SolverRow angular = makeRow(2, endpointA, endpointB);
933
957
  setAngularRow(angular);
934
958
  angular.block = 1;
935
959
  configureSoft(angular, bodies, wrappedAngle(endpointB.angle - endpointA.angle), 0.0, 0.0, substepDuration);
936
- addRow(prepared, state, angular);
960
+ addRow(prepared, state, angular, retained);
937
961
  }
938
962
  }
939
963
 
@@ -1100,7 +1124,8 @@ double gearVelocity(const PreparedGear& prepared, const Body* bodies) {
1100
1124
  return value;
1101
1125
  }
1102
1126
 
1103
- void applyGearImpulse(const PreparedGear& prepared, Body* bodies, double impulse) {
1127
+ void applyGearImpulse(const PreparedGear& prepared, Body* bodies, double impulse,
1128
+ GearEventLedger2D* ledger = nullptr) {
1104
1129
  for (std::uint32_t index = 0; index < prepared.bodyTermCount; index += 1) {
1105
1130
  const GearBodyTerm& term = prepared.bodyTerms[index];
1106
1131
  Body* body = bodyAt(bodies, term.bodyIndex);
@@ -1110,6 +1135,41 @@ void applyGearImpulse(const PreparedGear& prepared, Body* bodies, double impulse
1110
1135
  body->vy += term.linear.y * massScale;
1111
1136
  body->angularVelocity += term.angular * inverseInertia(body) * impulse;
1112
1137
  }
1138
+ if (ledger != nullptr) {
1139
+ const double physicalDelta = std::scalbn(impulse, prepared.mobilityScaleExponent);
1140
+ if (!finite(physicalDelta) || (impulse != 0.0 && physicalDelta == 0.0)) {
1141
+ ledger->reactionFailure = true;
1142
+ return;
1143
+ }
1144
+ for (std::uint32_t endpoint = 0; endpoint < 4; endpoint += 1) {
1145
+ const SolverVec2 delta = scale(prepared.endpoints[endpoint].linear, physicalDelta);
1146
+ const double couple = prepared.endpoints[endpoint].directCouple * physicalDelta;
1147
+ ledger->substepImpulse[endpoint].x += delta.x;
1148
+ ledger->substepImpulse[endpoint].y += delta.y;
1149
+ ledger->substepCouple[endpoint] += couple;
1150
+ if (!finite(delta.x) || !finite(delta.y) || !finite(couple)
1151
+ || !finite(ledger->substepImpulse[endpoint].x)
1152
+ || !finite(ledger->substepImpulse[endpoint].y)
1153
+ || !finite(ledger->substepCouple[endpoint])) ledger->reactionFailure = true;
1154
+ }
1155
+ }
1156
+ }
1157
+
1158
+ bool hasAppliedHistory(const GearEventLedger2D& ledger) {
1159
+ if (ledger.reactionFailure) return true;
1160
+ for (std::uint32_t endpoint = 0; endpoint < 4; endpoint += 1) {
1161
+ if (ledger.substepImpulse[endpoint].x != 0.0
1162
+ || ledger.substepImpulse[endpoint].y != 0.0
1163
+ || ledger.substepCouple[endpoint] != 0.0) return true;
1164
+ }
1165
+ return false;
1166
+ }
1167
+
1168
+ bool hasCarriedHistory(const JointEventLedger2D& ledger) {
1169
+ if (ledger.reactionFailure || ledger.substepImpulseB.x != 0.0
1170
+ || ledger.substepImpulseB.y != 0.0 || ledger.substepCoupleB != 0.0) return true;
1171
+ for (double value : ledger.retainedSlotImpulse) if (value != 0.0) return true;
1172
+ return false;
1113
1173
  }
1114
1174
 
1115
1175
  bool prepareGear(
@@ -1122,9 +1182,18 @@ bool prepareGear(
1122
1182
  const BodyLookupEntry* lookup,
1123
1183
  std::uint32_t bodyCount,
1124
1184
  std::uint32_t jointCount,
1125
- double substepDuration) {
1185
+ double substepDuration,
1186
+ GeometryPhase phase = GeometryPhase::BeginFixed,
1187
+ const GearEventLedger2D* ledger = nullptr) {
1126
1188
  const PreparedGear prior = prepared;
1127
1189
  prepared = {};
1190
+ if (phase == GeometryPhase::RefreshAtPose) {
1191
+ if (ledger == nullptr || ledger->reactionFailure) return false;
1192
+ const double physicalImpulse = std::scalbn(prior.solverImpulse, prior.mobilityScaleExponent);
1193
+ if (!finite(physicalImpulse)
1194
+ || (prior.solverImpulse != 0.0 && physicalImpulse == 0.0)) return false;
1195
+ states[findJointIndex(joints, jointCount, gear.jointId)].accumulatedImpulse[0] = physicalImpulse;
1196
+ }
1128
1197
  for (std::uint32_t endpoint = 0; endpoint < 4; endpoint += 1) {
1129
1198
  prepared.tickImpulse[endpoint] = prior.tickImpulse[endpoint];
1130
1199
  prepared.tickCouple[endpoint] = prior.tickCouple[endpoint];
@@ -1179,6 +1248,8 @@ bool prepareGear(
1179
1248
  && (states[prepared.sourceIndexB].stateFlags & kJointBroken) == 0;
1180
1249
  if ((gear.flags & kJointEnabled) == 0 || (state.stateFlags & kJointBroken) != 0
1181
1250
  || !dependenciesActive) {
1251
+ if (phase == GeometryPhase::RefreshAtPose
1252
+ && (state.accumulatedImpulse[0] != 0.0 || hasAppliedHistory(*ledger))) return false;
1182
1253
  state.activeRowMask = 0;
1183
1254
  for (double& impulse : state.accumulatedImpulse) impulse = 0.0;
1184
1255
  return true;
@@ -1188,10 +1259,10 @@ bool prepareGear(
1188
1259
  context,
1189
1260
  gear, sourceA, states[prepared.sourceIndexA], sourceB, states[prepared.sourceIndexB],
1190
1261
  bodies, lookup, bodyCount);
1191
- if (state.warmSignature != signature) {
1262
+ if (phase == GeometryPhase::BeginFixed && state.warmSignature != signature) {
1192
1263
  state.activeRowMask = 0;
1193
1264
  for (double& impulse : state.accumulatedImpulse) impulse = 0.0;
1194
- } else if (state.previousSubstepDuration > 0.0) {
1265
+ } else if (phase == GeometryPhase::BeginFixed && state.previousSubstepDuration > 0.0) {
1195
1266
  const double durationRatio = substepDuration / state.previousSubstepDuration;
1196
1267
  state.accumulatedImpulse[0] *= durationRatio;
1197
1268
  if (!finite(state.accumulatedImpulse[0])) return false;
@@ -1225,6 +1296,7 @@ bool prepareGear(
1225
1296
  includeGearMobilityExponent(term.angular, inverseInertia(body), maximumExponent);
1226
1297
  }
1227
1298
  if (maximumExponent == std::numeric_limits<int>::min()) {
1299
+ if (phase == GeometryPhase::RefreshAtPose && state.accumulatedImpulse[0] != 0.0) return false;
1228
1300
  state.activeRowMask = 0;
1229
1301
  state.accumulatedImpulse[0] = 0.0;
1230
1302
  return true;
@@ -1254,18 +1326,19 @@ bool prepareGear(
1254
1326
  return true;
1255
1327
  }
1256
1328
 
1257
- void warmStartGear(const PreparedGear& prepared, Body* bodies) {
1258
- if (prepared.active) applyGearImpulse(prepared, bodies, prepared.solverImpulse);
1329
+ void warmStartGear(const PreparedGear& prepared, Body* bodies, GearEventLedger2D* ledger = nullptr) {
1330
+ if (prepared.active) applyGearImpulse(prepared, bodies, prepared.solverImpulse, ledger);
1259
1331
  }
1260
1332
 
1261
- bool solveGearIteration(PreparedGear& prepared, Body* bodies) {
1333
+ bool solveGearIteration(PreparedGear& prepared, Body* bodies, GearEventLedger2D* ledger = nullptr) {
1334
+ if (ledger != nullptr && ledger->reactionFailure) return false;
1262
1335
  if (!prepared.active) return true;
1263
1336
  const double delta = -(gearVelocity(prepared, bodies) + prepared.bias)
1264
1337
  / prepared.effectiveMassDenominator;
1265
1338
  if (!finite(delta) || !finite(prepared.solverImpulse + delta)) return false;
1266
1339
  prepared.solverImpulse += delta;
1267
- applyGearImpulse(prepared, bodies, delta);
1268
- return true;
1340
+ applyGearImpulse(prepared, bodies, delta, ledger);
1341
+ return ledger == nullptr || !ledger->reactionFailure;
1269
1342
  }
1270
1343
 
1271
1344
  bool prepareJoint(
@@ -1277,7 +1350,12 @@ bool prepareJoint(
1277
1350
  const BodyLookupEntry* lookup,
1278
1351
  std::uint32_t bodyCount,
1279
1352
  double substepDuration,
1280
- std::int32_t& failureStatus) {
1353
+ std::int32_t& failureStatus,
1354
+ GeometryPhase phase = GeometryPhase::BeginFixed,
1355
+ JointEventLedger2D* ledger = nullptr) {
1356
+ const JointEventLedger2D* retained = phase == GeometryPhase::RefreshAtPose ? ledger : nullptr;
1357
+ const SolverVec2 priorSubstepImpulse = prepared.substepImpulseB;
1358
+ const double priorSubstepCouple = prepared.substepCoupleB;
1281
1359
  const SolverVec2 priorTickImpulse = prepared.tickImpulseB;
1282
1360
  const double priorTickCouple = prepared.tickCoupleB;
1283
1361
  const double priorMaximumForce = prepared.maximumSubstepForce;
@@ -1287,7 +1365,21 @@ bool prepareJoint(
1287
1365
  const double priorAppliedMotorImpulse = prepared.appliedMotorImpulse;
1288
1366
  const std::uint32_t priorTelemetryFlags = prepared.telemetryFlags;
1289
1367
  const JointLimitState2D priorLimitState = prepared.limitState;
1368
+ if (phase == GeometryPhase::RefreshAtPose) {
1369
+ if (ledger == nullptr || ledger->reactionFailure) return false;
1370
+ for (std::uint32_t index = 0; index < prepared.rowCount; index += 1) {
1371
+ const SolverRow& row = prepared.rows[index];
1372
+ const double physicalImpulse = joint.type == JointType2D::Pulley
1373
+ ? std::scalbn(row.impulse, prepared.pulleyMobilityScaleExponent) : row.impulse;
1374
+ if (!finite(physicalImpulse) || (row.impulse != 0.0 && physicalImpulse == 0.0)) return false;
1375
+ ledger->retainedSlotImpulse[row.slot] = physicalImpulse;
1376
+ }
1377
+ }
1290
1378
  prepared = {};
1379
+ if (phase == GeometryPhase::RefreshAtPose) {
1380
+ prepared.substepImpulseB = priorSubstepImpulse;
1381
+ prepared.substepCoupleB = priorSubstepCouple;
1382
+ }
1291
1383
  prepared.tickImpulseB = priorTickImpulse;
1292
1384
  prepared.tickCoupleB = priorTickCouple;
1293
1385
  prepared.maximumSubstepForce = priorMaximumForce;
@@ -1301,6 +1393,7 @@ bool prepareJoint(
1301
1393
  state.type = joint.type;
1302
1394
  state.reserved = 0;
1303
1395
  if ((joint.flags & kJointEnabled) == 0 || (state.stateFlags & kJointBroken) != 0) {
1396
+ if (phase == GeometryPhase::RefreshAtPose && hasCarriedHistory(*ledger)) return false;
1304
1397
  if ((state.stateFlags & kJointBroken) != 0) prepared.limitState = priorLimitState;
1305
1398
  state.activeRowMask = 0;
1306
1399
  state.limitState = JointLimitState2D::Inactive;
@@ -1309,15 +1402,18 @@ bool prepareJoint(
1309
1402
  }
1310
1403
 
1311
1404
  const std::uint64_t signature = warmSignature(context, joint, bodies, lookup, bodyCount);
1312
- if (state.warmSignature != signature) {
1405
+ if (phase == GeometryPhase::BeginFixed && state.warmSignature != signature) {
1313
1406
  state.activeRowMask = 0;
1314
1407
  for (double& impulse : state.accumulatedImpulse) impulse = 0.0;
1315
- } else if (state.previousSubstepDuration > 0.0) {
1408
+ } else if (phase == GeometryPhase::BeginFixed && state.previousSubstepDuration > 0.0) {
1316
1409
  const double ratio = substepDuration / state.previousSubstepDuration;
1317
1410
  for (double& impulse : state.accumulatedImpulse) impulse *= ratio;
1318
1411
  }
1319
1412
  state.warmSignature = signature;
1320
1413
  state.previousSubstepDuration = substepDuration;
1414
+ if (phase == GeometryPhase::BeginFixed && ledger != nullptr) {
1415
+ std::copy_n(state.accumulatedImpulse, kMaxJointWarmRows, ledger->retainedSlotImpulse);
1416
+ }
1321
1417
 
1322
1418
  const PrimitiveJointHeader2D& header = *primitiveHeader(joint);
1323
1419
  const EndpointRuntime endpointA = endpointRuntime(context, header.endpointA, bodies, lookup, bodyCount);
@@ -1348,7 +1444,7 @@ bool prepareJoint(
1348
1444
  configureSoft(axial, bodies, distance - value.restLength,
1349
1445
  (joint.flags & kJointSpringEnabled) != 0 ? value.frequencyHz : 0.0,
1350
1446
  value.dampingRatio, substepDuration);
1351
- if (!addRow(prepared, state, axial)) return false;
1447
+ if (!addRow(prepared, state, axial, retained)) return false;
1352
1448
  }
1353
1449
  if ((joint.flags & kJointLimitEnabled) != 0) {
1354
1450
  if (equalLimit) {
@@ -1356,28 +1452,28 @@ bool prepareJoint(
1356
1452
  SolverRow row = makeRow(1, endpointA, endpointB);
1357
1453
  setLinearRow(row, normal, endpointA.lever, endpointB.lever);
1358
1454
  row.bias = kBiasFactor * (distance - value.minLength) / substepDuration;
1359
- if (!addRow(prepared, state, row)) return false;
1455
+ if (!addRow(prepared, state, row, retained)) return false;
1360
1456
  } else if (distance <= value.minLength
1361
1457
  || distance + prepared.speed * substepDuration < value.minLength) {
1362
1458
  prepared.limitState = JointLimitState2D::AtLower;
1363
1459
  SolverRow row = makeRow(1, endpointA, endpointB);
1364
1460
  setLinearRow(row, normal, endpointA.lever, endpointB.lever);
1365
1461
  configureLower(row, distance - value.minLength, substepDuration);
1366
- if (!addRow(prepared, state, row)) return false;
1462
+ if (!addRow(prepared, state, row, retained)) return false;
1367
1463
  } else if (distance >= value.maxLength
1368
1464
  || distance + prepared.speed * substepDuration > value.maxLength) {
1369
1465
  prepared.limitState = JointLimitState2D::AtUpper;
1370
1466
  SolverRow row = makeRow(2, endpointA, endpointB);
1371
1467
  setLinearRow(row, normal, endpointA.lever, endpointB.lever);
1372
1468
  configureUpper(row, distance - value.maxLength, substepDuration);
1373
- if (!addRow(prepared, state, row)) return false;
1469
+ if (!addRow(prepared, state, row, retained)) return false;
1374
1470
  }
1375
1471
  }
1376
1472
  if ((joint.flags & kJointVelocityMotorEnabled) != 0) {
1377
1473
  SolverRow row = makeRow(3, endpointA, endpointB);
1378
1474
  setLinearRow(row, normal, endpointA.lever, endpointB.lever);
1379
1475
  configureMotor(row, value.motorSpeed, value.maxMotorForce, substepDuration);
1380
- if (!addRow(prepared, state, row)) return false;
1476
+ if (!addRow(prepared, state, row, retained)) return false;
1381
1477
  }
1382
1478
  break;
1383
1479
  }
@@ -1386,28 +1482,28 @@ bool prepareJoint(
1386
1482
  updateCoordinateState(state, coordinateKey, angle, true);
1387
1483
  prepared.coordinate = state.unwrappedCoordinate;
1388
1484
  prepared.speed = relativeAngularSpeed;
1389
- addAnchorBlock(prepared, state, endpointA, endpointB, bodies, substepDuration, 0.0, 0.0, false);
1485
+ addAnchorBlock(prepared, state, endpointA, endpointB, bodies, substepDuration, 0.0, 0.0, false, retained);
1390
1486
  if ((joint.flags & kJointLimitEnabled) != 0) {
1391
1487
  if (value.lowerAngle == value.upperAngle) {
1392
1488
  prepared.limitState = JointLimitState2D::Equal;
1393
1489
  SolverRow row = makeRow(2, endpointA, endpointB);
1394
1490
  setAngularRow(row);
1395
1491
  row.bias = kBiasFactor * (prepared.coordinate - value.lowerAngle) / substepDuration;
1396
- if (!addRow(prepared, state, row)) return false;
1492
+ if (!addRow(prepared, state, row, retained)) return false;
1397
1493
  } else if (prepared.coordinate <= value.lowerAngle
1398
1494
  || prepared.coordinate + prepared.speed * substepDuration < value.lowerAngle) {
1399
1495
  prepared.limitState = JointLimitState2D::AtLower;
1400
1496
  SolverRow row = makeRow(2, endpointA, endpointB);
1401
1497
  setAngularRow(row);
1402
1498
  configureLower(row, prepared.coordinate - value.lowerAngle, substepDuration);
1403
- if (!addRow(prepared, state, row)) return false;
1499
+ if (!addRow(prepared, state, row, retained)) return false;
1404
1500
  } else if (prepared.coordinate >= value.upperAngle
1405
1501
  || prepared.coordinate + prepared.speed * substepDuration > value.upperAngle) {
1406
1502
  prepared.limitState = JointLimitState2D::AtUpper;
1407
1503
  SolverRow row = makeRow(3, endpointA, endpointB);
1408
1504
  setAngularRow(row);
1409
1505
  configureUpper(row, prepared.coordinate - value.upperAngle, substepDuration);
1410
- if (!addRow(prepared, state, row)) return false;
1506
+ if (!addRow(prepared, state, row, retained)) return false;
1411
1507
  }
1412
1508
  }
1413
1509
  if ((joint.flags & kJointSpringEnabled) != 0) {
@@ -1415,7 +1511,7 @@ bool prepareJoint(
1415
1511
  setAngularRow(row);
1416
1512
  configureSoft(row, bodies, prepared.coordinate - value.targetAngle, value.frequencyHz,
1417
1513
  value.dampingRatio, substepDuration);
1418
- if (!addRow(prepared, state, row)) return false;
1514
+ if (!addRow(prepared, state, row, retained)) return false;
1419
1515
  }
1420
1516
  if ((joint.flags & (kJointVelocityMotorEnabled | kJointServoEnabled)) != 0) {
1421
1517
  SolverRow row = makeRow(5, endpointA, endpointB);
@@ -1429,13 +1525,13 @@ bool prepareJoint(
1429
1525
  } else {
1430
1526
  configureMotor(row, value.motorSpeed, value.maxMotorTorque, substepDuration);
1431
1527
  }
1432
- if (!addRow(prepared, state, row)) return false;
1528
+ if (!addRow(prepared, state, row, retained)) return false;
1433
1529
  }
1434
1530
  if ((joint.flags & kJointFrictionOrBrakeEnabled) != 0) {
1435
1531
  SolverRow row = makeRow(6, endpointA, endpointB);
1436
1532
  setAngularRow(row);
1437
1533
  configureMotor(row, 0.0, value.maxFrictionTorque, substepDuration);
1438
- if (!addRow(prepared, state, row)) return false;
1534
+ if (!addRow(prepared, state, row, retained)) return false;
1439
1535
  }
1440
1536
  break;
1441
1537
  }
@@ -1449,33 +1545,33 @@ bool prepareJoint(
1449
1545
  endpointA.lever, endpointB.lever);
1450
1546
  perpendicularRow.block = 1;
1451
1547
  perpendicularRow.bias = kBiasFactor * dot(delta, tangent) / substepDuration;
1452
- if (!addRow(prepared, state, perpendicularRow)) return false;
1548
+ if (!addRow(prepared, state, perpendicularRow, retained)) return false;
1453
1549
  SolverRow angularLock = makeRow(1, endpointA, endpointB);
1454
1550
  setAngularRow(angularLock);
1455
1551
  angularLock.block = 1;
1456
1552
  angularLock.bias = kBiasFactor * angle / substepDuration;
1457
- if (!addRow(prepared, state, angularLock)) return false;
1553
+ if (!addRow(prepared, state, angularLock, retained)) return false;
1458
1554
  if ((joint.flags & kJointLimitEnabled) != 0) {
1459
1555
  if (value.lowerTranslation == value.upperTranslation) {
1460
1556
  prepared.limitState = JointLimitState2D::Equal;
1461
1557
  SolverRow row = makeRow(2, endpointA, endpointB);
1462
1558
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1463
1559
  row.bias = kBiasFactor * (translation - value.lowerTranslation) / substepDuration;
1464
- if (!addRow(prepared, state, row)) return false;
1560
+ if (!addRow(prepared, state, row, retained)) return false;
1465
1561
  } else if (translation <= value.lowerTranslation
1466
1562
  || translation + prepared.speed * substepDuration < value.lowerTranslation) {
1467
1563
  prepared.limitState = JointLimitState2D::AtLower;
1468
1564
  SolverRow row = makeRow(2, endpointA, endpointB);
1469
1565
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1470
1566
  configureLower(row, translation - value.lowerTranslation, substepDuration);
1471
- if (!addRow(prepared, state, row)) return false;
1567
+ if (!addRow(prepared, state, row, retained)) return false;
1472
1568
  } else if (translation >= value.upperTranslation
1473
1569
  || translation + prepared.speed * substepDuration > value.upperTranslation) {
1474
1570
  prepared.limitState = JointLimitState2D::AtUpper;
1475
1571
  SolverRow row = makeRow(3, endpointA, endpointB);
1476
1572
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1477
1573
  configureUpper(row, translation - value.upperTranslation, substepDuration);
1478
- if (!addRow(prepared, state, row)) return false;
1574
+ if (!addRow(prepared, state, row, retained)) return false;
1479
1575
  }
1480
1576
  }
1481
1577
  if ((joint.flags & kJointSpringEnabled) != 0) {
@@ -1483,7 +1579,7 @@ bool prepareJoint(
1483
1579
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1484
1580
  configureSoft(row, bodies, translation - value.targetTranslation, value.frequencyHz,
1485
1581
  value.dampingRatio, substepDuration);
1486
- if (!addRow(prepared, state, row)) return false;
1582
+ if (!addRow(prepared, state, row, retained)) return false;
1487
1583
  }
1488
1584
  if ((joint.flags & (kJointVelocityMotorEnabled | kJointServoEnabled)) != 0) {
1489
1585
  SolverRow row = makeRow(5, endpointA, endpointB);
@@ -1497,13 +1593,13 @@ bool prepareJoint(
1497
1593
  } else {
1498
1594
  configureMotor(row, value.motorSpeed, value.maxMotorForce, substepDuration);
1499
1595
  }
1500
- if (!addRow(prepared, state, row)) return false;
1596
+ if (!addRow(prepared, state, row, retained)) return false;
1501
1597
  }
1502
1598
  if ((joint.flags & kJointFrictionOrBrakeEnabled) != 0) {
1503
1599
  SolverRow row = makeRow(6, endpointA, endpointB);
1504
1600
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1505
1601
  configureMotor(row, 0.0, value.maxFrictionForce, substepDuration);
1506
- if (!addRow(prepared, state, row)) return false;
1602
+ if (!addRow(prepared, state, row, retained)) return false;
1507
1603
  }
1508
1604
  break;
1509
1605
  }
@@ -1514,7 +1610,7 @@ bool prepareJoint(
1514
1610
  prepared.speed = relativeAngularSpeed;
1515
1611
  const bool spring = (joint.flags & kJointSpringEnabled) != 0;
1516
1612
  addAnchorBlock(prepared, state, endpointA, endpointB, bodies, substepDuration,
1517
- spring ? value.linearFrequencyHz : 0.0, value.linearDampingRatio, true);
1613
+ spring ? value.linearFrequencyHz : 0.0, value.linearDampingRatio, true, retained);
1518
1614
  SolverRow& angularRow = prepared.rows[2];
1519
1615
  angularRow.bias = 0.0;
1520
1616
  configureSoft(angularRow, bodies, prepared.coordinate,
@@ -1531,28 +1627,28 @@ bool prepareJoint(
1531
1627
  setFrameLinearRow(perpendicularRow, tangent, scale(axis, -1.0), delta,
1532
1628
  endpointA.lever, endpointB.lever);
1533
1629
  perpendicularRow.bias = kBiasFactor * dot(delta, tangent) / substepDuration;
1534
- if (!addRow(prepared, state, perpendicularRow)) return false;
1630
+ if (!addRow(prepared, state, perpendicularRow, retained)) return false;
1535
1631
  if ((joint.flags & kJointLimitEnabled) != 0) {
1536
1632
  if (value.lowerTranslation == value.upperTranslation) {
1537
1633
  prepared.limitState = JointLimitState2D::Equal;
1538
1634
  SolverRow row = makeRow(1, endpointA, endpointB);
1539
1635
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1540
1636
  row.bias = kBiasFactor * (translation - value.lowerTranslation) / substepDuration;
1541
- if (!addRow(prepared, state, row)) return false;
1637
+ if (!addRow(prepared, state, row, retained)) return false;
1542
1638
  } else if (translation <= value.lowerTranslation
1543
1639
  || translation + prepared.speed * substepDuration < value.lowerTranslation) {
1544
1640
  prepared.limitState = JointLimitState2D::AtLower;
1545
1641
  SolverRow row = makeRow(1, endpointA, endpointB);
1546
1642
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1547
1643
  configureLower(row, translation - value.lowerTranslation, substepDuration);
1548
- if (!addRow(prepared, state, row)) return false;
1644
+ if (!addRow(prepared, state, row, retained)) return false;
1549
1645
  } else if (translation >= value.upperTranslation
1550
1646
  || translation + prepared.speed * substepDuration > value.upperTranslation) {
1551
1647
  prepared.limitState = JointLimitState2D::AtUpper;
1552
1648
  SolverRow row = makeRow(2, endpointA, endpointB);
1553
1649
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1554
1650
  configureUpper(row, translation - value.upperTranslation, substepDuration);
1555
- if (!addRow(prepared, state, row)) return false;
1651
+ if (!addRow(prepared, state, row, retained)) return false;
1556
1652
  }
1557
1653
  }
1558
1654
  if ((joint.flags & kJointSpringEnabled) != 0) {
@@ -1560,19 +1656,19 @@ bool prepareJoint(
1560
1656
  setFrameLinearRow(row, axis, tangent, delta, endpointA.lever, endpointB.lever);
1561
1657
  configureSoft(row, bodies, translation - value.restTranslation, value.frequencyHz,
1562
1658
  value.dampingRatio, substepDuration);
1563
- if (!addRow(prepared, state, row)) return false;
1659
+ if (!addRow(prepared, state, row, retained)) return false;
1564
1660
  }
1565
1661
  if ((joint.flags & kJointVelocityMotorEnabled) != 0) {
1566
1662
  SolverRow row = makeRow(4, endpointA, endpointB);
1567
1663
  setAngularRow(row);
1568
1664
  configureMotor(row, value.motorSpeed, value.maxMotorTorque, substepDuration);
1569
- if (!addRow(prepared, state, row)) return false;
1665
+ if (!addRow(prepared, state, row, retained)) return false;
1570
1666
  }
1571
1667
  if ((joint.flags & kJointFrictionOrBrakeEnabled) != 0) {
1572
1668
  SolverRow row = makeRow(5, endpointA, endpointB);
1573
1669
  setAngularRow(row);
1574
1670
  configureMotor(row, 0.0, value.maxBrakeTorque, substepDuration);
1575
- if (!addRow(prepared, state, row)) return false;
1671
+ if (!addRow(prepared, state, row, retained)) return false;
1576
1672
  }
1577
1673
  break;
1578
1674
  }
@@ -1590,21 +1686,21 @@ bool prepareJoint(
1590
1686
  x.bias = value.correctionFactor * error.x / substepDuration;
1591
1687
  x.circularLimit = value.maxForce * substepDuration;
1592
1688
  x.motor = true;
1593
- if (!addRow(prepared, state, x)) return false;
1689
+ if (!addRow(prepared, state, x, retained)) return false;
1594
1690
  SolverRow y = makeRow(1, endpointA, endpointB);
1595
1691
  setLinearRow(y, {0.0, 1.0}, add(endpointA.lever, targetOffset), endpointB.lever);
1596
1692
  y.block = 1;
1597
1693
  y.bias = value.correctionFactor * error.y / substepDuration;
1598
1694
  y.circularLimit = value.maxForce * substepDuration;
1599
1695
  y.motor = true;
1600
- if (!addRow(prepared, state, y)) return false;
1696
+ if (!addRow(prepared, state, y, retained)) return false;
1601
1697
  SolverRow angular = makeRow(2, endpointA, endpointB);
1602
1698
  setAngularRow(angular);
1603
1699
  angular.bias = value.correctionFactor * (prepared.coordinate - value.targetAngle) / substepDuration;
1604
1700
  angular.lowerImpulse = -value.maxTorque * substepDuration;
1605
1701
  angular.upperImpulse = value.maxTorque * substepDuration;
1606
1702
  angular.motor = true;
1607
- if (!addRow(prepared, state, angular)) return false;
1703
+ if (!addRow(prepared, state, angular, retained)) return false;
1608
1704
  break;
1609
1705
  }
1610
1706
  case JointType2D::Mouse: {
@@ -1620,7 +1716,7 @@ bool prepareJoint(
1620
1716
  (joint.flags & kJointSpringEnabled) != 0 ? value.frequencyHz : 0.0,
1621
1717
  value.dampingRatio, substepDuration);
1622
1718
  x.circularLimit = value.maxForce * substepDuration;
1623
- if (!addRow(prepared, state, x)) return false;
1719
+ if (!addRow(prepared, state, x, retained)) return false;
1624
1720
  SolverRow y = makeRow(1, endpointA, endpointB);
1625
1721
  setLinearRow(y, {0.0, 1.0}, endpointA.lever, endpointB.lever);
1626
1722
  y.block = 1;
@@ -1628,7 +1724,7 @@ bool prepareJoint(
1628
1724
  (joint.flags & kJointSpringEnabled) != 0 ? value.frequencyHz : 0.0,
1629
1725
  value.dampingRatio, substepDuration);
1630
1726
  y.circularLimit = value.maxForce * substepDuration;
1631
- if (!addRow(prepared, state, y)) return false;
1727
+ if (!addRow(prepared, state, y, retained)) return false;
1632
1728
  break;
1633
1729
  }
1634
1730
  case JointType2D::Rope: {
@@ -1642,7 +1738,7 @@ bool prepareJoint(
1642
1738
  SolverRow row = makeRow(0, endpointA, endpointB);
1643
1739
  setLinearRow(row, normal, endpointA.lever, endpointB.lever);
1644
1740
  configureUpper(row, distance - value.maxLength, substepDuration);
1645
- if (!addRow(prepared, state, row)) return false;
1741
+ if (!addRow(prepared, state, row, retained)) return false;
1646
1742
  }
1647
1743
  break;
1648
1744
  }
@@ -1742,7 +1838,7 @@ bool prepareJoint(
1742
1838
  row.unilateral = true;
1743
1839
  }
1744
1840
  row.bias += prescribedSpeed;
1745
- if (!finite(row.bias) || !addPulleyRow(prepared, state, row)) return false;
1841
+ if (!finite(row.bias) || !addPulleyRow(prepared, state, row, retained)) return false;
1746
1842
  if (dormant) prepared.activeMask &= ~1u;
1747
1843
  break;
1748
1844
  }
@@ -1751,28 +1847,35 @@ bool prepareJoint(
1751
1847
  }
1752
1848
 
1753
1849
  for (std::uint32_t slot = 0; slot < kMaxJointWarmRows; slot += 1) {
1754
- if ((prepared.activeMask & (1u << slot)) == 0) state.accumulatedImpulse[slot] = 0.0;
1850
+ if (phase == GeometryPhase::BeginFixed && (prepared.activeMask & (1u << slot)) == 0) {
1851
+ state.accumulatedImpulse[slot] = 0.0;
1852
+ if (ledger != nullptr) ledger->retainedSlotImpulse[slot] = 0.0;
1853
+ }
1755
1854
  }
1756
1855
  state.activeRowMask = prepared.activeMask;
1757
1856
  state.limitState = prepared.limitState;
1758
1857
  return true;
1759
1858
  }
1760
1859
 
1761
- void warmStartJoint(PreparedJoint& prepared, Body* bodies) {
1860
+ void warmStartJoint(PreparedJoint& prepared, Body* bodies, JointEventLedger2D* ledger = nullptr,
1861
+ bool pulley = false) {
1762
1862
  for (std::uint32_t index = 0; index < prepared.rowCount; index += 1) {
1763
1863
  SolverRow& row = prepared.rows[index];
1764
- applyRowImpulse(row, bodies, prepared, row.impulse);
1864
+ applyRowImpulse(row, bodies, prepared, row.impulse, ledger, pulley,
1865
+ prepared.pulleyMobilityScaleExponent);
1765
1866
  }
1766
1867
  }
1767
1868
 
1768
- bool solveScalar(SolverRow& row, Body* bodies, PreparedJoint& prepared) {
1869
+ bool solveScalar(SolverRow& row, Body* bodies, PreparedJoint& prepared,
1870
+ JointEventLedger2D* ledger = nullptr, bool pulley = false) {
1769
1871
  const double denominator = rowEffectiveMass(row, bodies);
1770
1872
  if (!(denominator > 0.0)) return finite(denominator);
1771
1873
  const double delta = -(rowVelocity(row, bodies) + row.bias + row.gamma * row.impulse) / denominator;
1772
1874
  if (!finite(delta)) return false;
1773
1875
  const double oldImpulse = row.impulse;
1774
1876
  row.impulse = std::clamp(oldImpulse + delta, row.lowerImpulse, row.upperImpulse);
1775
- applyRowImpulse(row, bodies, prepared, row.impulse - oldImpulse);
1877
+ applyRowImpulse(row, bodies, prepared, row.impulse - oldImpulse, ledger, pulley,
1878
+ prepared.pulleyMobilityScaleExponent);
1776
1879
  if (row.motor && (row.impulse == row.lowerImpulse || row.impulse == row.upperImpulse)) {
1777
1880
  prepared.telemetryFlags |= kJointMotorSaturated;
1778
1881
  }
@@ -1783,7 +1886,7 @@ bool solveBlock(
1783
1886
  PreparedJoint& prepared,
1784
1887
  Body* bodies,
1785
1888
  std::uint32_t first,
1786
- std::uint32_t count) {
1889
+ std::uint32_t count, JointEventLedger2D* ledger = nullptr, bool pulley = false) {
1787
1890
  double matrix[3][3]{};
1788
1891
  double rhs[3]{};
1789
1892
  for (std::uint32_t rowIndex = 0; rowIndex < count; rowIndex += 1) {
@@ -1844,16 +1947,19 @@ bool solveBlock(
1844
1947
  SolverRow& row = prepared.rows[first + index];
1845
1948
  const double oldImpulse = row.impulse;
1846
1949
  row.impulse = next[index];
1847
- applyRowImpulse(row, bodies, prepared, row.impulse - oldImpulse);
1950
+ applyRowImpulse(row, bodies, prepared, row.impulse - oldImpulse, ledger, pulley,
1951
+ prepared.pulleyMobilityScaleExponent);
1848
1952
  }
1849
1953
  return true;
1850
1954
  }
1851
1955
 
1852
- bool solveJointIteration(PreparedJoint& prepared, Body* bodies) {
1956
+ bool solveJointIteration(PreparedJoint& prepared, Body* bodies,
1957
+ JointEventLedger2D* ledger = nullptr, bool pulley = false) {
1958
+ if (ledger != nullptr && ledger->reactionFailure) return false;
1853
1959
  std::uint32_t index = 0;
1854
1960
  while (index < prepared.rowCount) {
1855
1961
  if (prepared.rows[index].block == 0) {
1856
- if (!solveScalar(prepared.rows[index], bodies, prepared)) return false;
1962
+ if (!solveScalar(prepared.rows[index], bodies, prepared, ledger, pulley)) return false;
1857
1963
  index += 1;
1858
1964
  continue;
1859
1965
  }
@@ -1861,13 +1967,13 @@ bool solveJointIteration(PreparedJoint& prepared, Body* bodies) {
1861
1967
  std::uint32_t count = 1;
1862
1968
  while (index + count < prepared.rowCount
1863
1969
  && prepared.rows[index + count].block == block && count < 3) count += 1;
1864
- if (!solveBlock(prepared, bodies, index, count)) return false;
1970
+ if (!solveBlock(prepared, bodies, index, count, ledger, pulley)) return false;
1865
1971
  index += count;
1866
1972
  }
1867
1973
  for (std::uint32_t row = 0; row < prepared.rowCount; row += 1) {
1868
- if (prepared.rows[row].unilateral && !solveScalar(prepared.rows[row], bodies, prepared)) return false;
1974
+ if (prepared.rows[row].unilateral && !solveScalar(prepared.rows[row], bodies, prepared, ledger, pulley)) return false;
1869
1975
  }
1870
- return true;
1976
+ return ledger == nullptr || !ledger->reactionFailure;
1871
1977
  }
1872
1978
 
1873
1979
  bool finishJointSubstep(
@@ -1877,7 +1983,15 @@ bool finishJointSubstep(
1877
1983
  JointBreakEvent2D* events,
1878
1984
  std::uint32_t& eventCount,
1879
1985
  std::uint32_t substepIndex,
1880
- double substepDuration) {
1986
+ double substepDuration, const JointEventLedger2D* ledger = nullptr) {
1987
+ if (ledger != nullptr && ledger->reactionFailure) return false;
1988
+ if (ledger != nullptr) {
1989
+ prepared.substepImpulseB = {ledger->substepImpulseB.x, ledger->substepImpulseB.y};
1990
+ prepared.substepCoupleB = ledger->substepCoupleB;
1991
+ for (std::uint32_t slot = 0; slot < kMaxJointWarmRows; slot += 1) {
1992
+ if ((prepared.activeMask & (1u << slot)) == 0) state.accumulatedImpulse[slot] = 0.0;
1993
+ }
1994
+ }
1881
1995
  if (joint.type == JointType2D::Pulley) {
1882
1996
  const int scaleExponent = prepared.pulleyMobilityScaleExponent;
1883
1997
  for (std::uint32_t index = 0; index < prepared.rowCount; index += 1) {
@@ -1895,7 +2009,7 @@ bool finishJointSubstep(
1895
2009
  state.accumulatedImpulse[row.slot] = row.impulse;
1896
2010
  }
1897
2011
  }
1898
- if (joint.type == JointType2D::Pulley && prepared.rowCount > 0) {
2012
+ if (joint.type == JointType2D::Pulley && prepared.rowCount > 0 && ledger == nullptr) {
1899
2013
  const SolverRow& row = prepared.rows[0];
1900
2014
  double reactionMagnitudeB = 0.0;
1901
2015
  if (!scaledPulleyProduct(row.reactionCoefficientB,
@@ -1951,16 +2065,24 @@ bool finishGearSubstep(
1951
2065
  JointBreakEvent2D* events,
1952
2066
  std::uint32_t& eventCount,
1953
2067
  std::uint32_t substepIndex,
1954
- double substepDuration) {
1955
- if (!prepared.active) return true;
2068
+ double substepDuration, const GearEventLedger2D* ledger = nullptr) {
2069
+ if (ledger != nullptr && ledger->reactionFailure) return false;
2070
+ if (!prepared.active) {
2071
+ if (ledger == nullptr) return true;
2072
+ if (prepared.solverImpulse != 0.0 || state.accumulatedImpulse[0] != 0.0) return false;
2073
+ state.activeRowMask = 0;
2074
+ state.limitState = JointLimitState2D::Inactive;
2075
+ }
1956
2076
  const double baseImpulse = std::scalbn(
1957
2077
  prepared.solverImpulse, prepared.mobilityScaleExponent);
1958
2078
  if (!finite(baseImpulse)
1959
2079
  || (prepared.solverImpulse != 0.0 && baseImpulse == 0.0)) return false;
1960
2080
  state.accumulatedImpulse[0] = baseImpulse;
1961
2081
  for (std::uint32_t endpoint = 0; endpoint < 4; endpoint += 1) {
1962
- const SolverVec2 impulse = scale(prepared.endpoints[endpoint].linear, baseImpulse);
1963
- const double couple = prepared.endpoints[endpoint].directCouple * baseImpulse;
2082
+ const SolverVec2 impulse = ledger != nullptr
2083
+ ? SolverVec2{ledger->substepImpulse[endpoint].x, ledger->substepImpulse[endpoint].y} : scale(prepared.endpoints[endpoint].linear, baseImpulse);
2084
+ const double couple = ledger != nullptr
2085
+ ? ledger->substepCouple[endpoint] : prepared.endpoints[endpoint].directCouple * baseImpulse;
1964
2086
  const double force = length(impulse) / substepDuration;
1965
2087
  const double torque = absolute(couple) / substepDuration;
1966
2088
  if (!finite(impulse.x) || !finite(impulse.y) || !finite(couple)
@@ -1979,8 +2101,10 @@ bool finishGearSubstep(
1979
2101
  const GearJointPayload2D& value = joint.payload.gear;
1980
2102
  const std::uint32_t selected = 2 * static_cast<std::uint32_t>(value.breakSourceSlot)
1981
2103
  + static_cast<std::uint32_t>(value.breakSourceEndpoint);
1982
- const SolverVec2 selectedImpulse = scale(prepared.endpoints[selected].linear, baseImpulse);
1983
- const double selectedCouple = prepared.endpoints[selected].directCouple * baseImpulse;
2104
+ const SolverVec2 selectedImpulse = ledger != nullptr
2105
+ ? SolverVec2{ledger->substepImpulse[selected].x, ledger->substepImpulse[selected].y} : scale(prepared.endpoints[selected].linear, baseImpulse);
2106
+ const double selectedCouple = ledger != nullptr
2107
+ ? ledger->substepCouple[selected] : prepared.endpoints[selected].directCouple * baseImpulse;
1984
2108
  std::uint32_t reasons = 0;
1985
2109
  if (value.breakForce > 0.0
1986
2110
  && length(selectedImpulse) > value.breakForce * substepDuration) {
@@ -2052,6 +2176,7 @@ std::uint32_t buildDisabledContactPairs(
2052
2176
  } // namespace
2053
2177
 
2054
2178
  struct SolverContext2D::Storage {
2179
+ clock_private::LifetimeAnchor* frame = nullptr;
2055
2180
  Body* bodies = nullptr;
2056
2181
  JointState2D* states = nullptr;
2057
2182
  JointTelemetry2D* telemetry = nullptr;
@@ -2063,6 +2188,321 @@ struct SolverContext2D::Storage {
2063
2188
  std::uint32_t jointCapacity = 0;
2064
2189
  };
2065
2190
 
2191
+ SolverContext2D::Storage& JointPhaseAccess2D::storage(
2192
+ const joint_phases_private::BorrowedJointPhases2D& phases) {
2193
+ return *phases.solver_->storage_;
2194
+ }
2195
+ bool JointPhaseAccess2D::frameBusy(const SolverContext2D& solver,
2196
+ const PhysicsContext2D& physics) noexcept {
2197
+ return (solver.ready() && solver.storage_->frame != nullptr)
2198
+ || clock_private::Access::frameOwner(physics) != nullptr;
2199
+ }
2200
+ bool JointPhaseAccess2D::intervalLive(
2201
+ const joint_phases_private::BorrowedJointPhases2D& phases) noexcept {
2202
+ return clock_private::Access::bound(phases.anchor_, phases.frameIdentity_);
2203
+ }
2204
+ joint_phases_private::BorrowedJointPhases2D JointPhaseAccess2D::bindEventStage(
2205
+ clock_private::EventInterval& interval, SolverContext2D& solver, PhysicsContext2D& physics,
2206
+ const JointRecord2D* joints, std::uint32_t jointCount, std::uint32_t& eventCount,
2207
+ std::span<joint_phases_private::EventConstraintLedger2D> reactions) {
2208
+ std::int32_t status = kStepJointInvalidArguments;
2209
+ std::uint64_t generation = 0;
2210
+ const bool liveInterval = interval.live() && interval.anchor_->owner == &physics;
2211
+ const auto* frame = liveInterval ? clock_private::Access::frameOwner(physics) : nullptr;
2212
+ if (liveInterval && !interval.jointBound_ && solver.ready()
2213
+ && frame != nullptr && frame->ready() && frame->solver_ == &solver
2214
+ && solver.storage_->frame == interval.anchor_
2215
+ && solver.storage_->bodies == interval.bodies_ && jointCount > 0
2216
+ && jointCount <= solver.storage_->jointCapacity && reactions.size() >= jointCount) {
2217
+ status = validateWorldJoints2D(solver, physics, interval.bodies_, interval.bodyCount_,
2218
+ joints, jointCount, solver.storage_->states, jointCount);
2219
+ if (status == kStepOk) { interval.jointBound_ = true; generation = interval.generation_; }
2220
+ }
2221
+ // Failed views keep no authority over the accepted interval.
2222
+ return joint_phases_private::BorrowedJointPhases2D(solver, physics, joints,
2223
+ jointCount, interval.bodyCount_, interval.h_, interval.ordinal_, eventCount, reactions,
2224
+ interval.bodies_, generation, status, generation == 0 ? nullptr : &interval);
2225
+ }
2226
+
2227
+ joint_phases_private::BorrowedJointPhases2D JointPhaseAccess2D::bindValidatedStage(
2228
+ SolverContext2D& solver, PhysicsContext2D& physics,
2229
+ const JointRecord2D* joints, std::uint32_t jointCount, std::uint32_t bodyCount,
2230
+ double duration, std::uint32_t substep, std::uint32_t& eventCount,
2231
+ joint_phases_private::ReactionBinding2D reactions) {
2232
+ Body* bodies = solver.ready() ? solver.storage_->bodies : nullptr;
2233
+ std::int32_t status = kStepJointInvalidArguments;
2234
+ std::uint64_t generation = 0;
2235
+ const auto* frame = clock_private::Access::frameOwner(physics);
2236
+ const bool frameMatches = frame == nullptr || (solver.ready() && frame->ready() && frame->solver_ == &solver
2237
+ && solver.storage_->frame == frame->anchor_ && frame->candidate_ == bodies && frame->bodyCount_ == bodyCount);
2238
+ if (solver.ready() && physics.ready() && frameMatches && jointCount > 0 && bodyCount <= solver.storage_->bodyCapacity
2239
+ && jointCount <= solver.storage_->jointCapacity) {
2240
+ status = validateWorldJoints2D(solver, physics, bodies, bodyCount, joints, jointCount,
2241
+ solver.storage_->states, jointCount);
2242
+ if (status == kStepOk) {
2243
+ auto receipt = clock_private::Access::claim(physics, bodies, bodyCount, duration, substep);
2244
+ if (auto* value = std::get_if<clock_private::PreparationReceipt>(&receipt)) {
2245
+ const auto consumed = clock_private::Access::consume(std::move(*value), physics,
2246
+ bodies, bodyCount, duration, substep);
2247
+ if (const auto* accepted = std::get_if<std::uint64_t>(&consumed)) generation = *accepted;
2248
+ else status = kStepJointInvalidArguments;
2249
+ } else status = kStepJointInvalidArguments;
2250
+ }
2251
+ }
2252
+ return joint_phases_private::BorrowedJointPhases2D(solver, physics, joints,
2253
+ jointCount, bodyCount, duration, substep, eventCount, reactions, bodies, generation, status);
2254
+ }
2255
+
2256
+ namespace joint_phases_private {
2257
+ BorrowedJointPhases2D::BorrowedJointPhases2D(
2258
+ SolverContext2D& solver, PhysicsContext2D& physics,
2259
+ const JointRecord2D* joints, std::uint32_t jointCount, std::uint32_t bodyCount,
2260
+ double duration, std::uint32_t substep, std::uint32_t& eventCount,
2261
+ ReactionBinding2D reactions, Body* bodies, std::uint64_t generation, std::int32_t initialStatus,
2262
+ clock_private::EventInterval* interval)
2263
+ : solver_(&solver), physics_(&physics), joints_(joints), jointCount_(jointCount),
2264
+ bodyCount_(bodyCount), duration_(duration), substep_(substep),
2265
+ eventCount_(&eventCount), reactions_(reactions), bodies_(bodies),
2266
+ generation_(generation), initialStatus_(initialStatus), eventBinding_(interval != nullptr) {
2267
+ if (generation_ == 0 || initialStatus_ != kStepOk) { state_ = State::Failed; return; }
2268
+ anchor_ = clock_private::Access::anchor(physics);
2269
+ if (anchor_ == nullptr || std::holds_alternative<clock_private::Failure>(clock_private::Access::retain(*anchor_))) {
2270
+ anchor_ = nullptr; state_ = State::Failed; initialStatus_ = kStepJointNumericFailure;
2271
+ clock_private::Access::failed(physics, generation_); return;
2272
+ }
2273
+ frameIdentity_ = anchor_->activeFrameIdentity;
2274
+ if (frameIdentity_ == 0) {
2275
+ auto& stage = JointPhaseAccess2D::storage(*this);
2276
+ if (stage.frame != nullptr || anchor_->solverLease != nullptr) {
2277
+ state_ = State::Failed; initialStatus_ = kStepJointInvalidArguments;
2278
+ clock_private::Access::failed(physics, generation_);
2279
+ clock_private::Access::release(anchor_); anchor_ = nullptr; return;
2280
+ }
2281
+ stage.frame = anchor_; anchor_->solverLease = &stage.frame; ordinaryLease_ = &stage.frame;
2282
+ }
2283
+ }
2284
+
2285
+ BorrowedJointPhases2D::BorrowedJointPhases2D(BorrowedJointPhases2D&& source) noexcept
2286
+ : solver_(source.solver_), physics_(source.physics_), anchor_(source.anchor_),
2287
+ frameIdentity_(source.frameIdentity_), joints_(source.joints_),
2288
+ jointCount_(source.jointCount_), bodyCount_(source.bodyCount_), duration_(source.duration_),
2289
+ substep_(source.substep_), eventCount_(source.eventCount_), reactions_(source.reactions_),
2290
+ state_(source.state_), disabledCount_(source.disabledCount_), bodies_(source.bodies_),
2291
+ generation_(source.generation_), initialStatus_(source.initialStatus_), eventBinding_(source.eventBinding_),
2292
+ ordinaryLease_(source.ordinaryLease_) {
2293
+ source.solver_ = nullptr; source.physics_ = nullptr; source.bodies_ = nullptr;
2294
+ source.eventCount_ = nullptr; source.state_ = State::MovedFrom;
2295
+ source.anchor_ = nullptr; source.frameIdentity_ = 0;
2296
+ source.eventBinding_ = false; source.ordinaryLease_ = nullptr;
2297
+ }
2298
+
2299
+ BorrowedJointPhases2D::~BorrowedJointPhases2D() {
2300
+ if (clock_private::Access::bound(anchor_, frameIdentity_) && generation_ != 0
2301
+ && state_ != State::Finished && state_ != State::MovedFrom)
2302
+ clock_private::Access::failed(*anchor_->owner, generation_);
2303
+ if (ownsOrdinaryLease()) {
2304
+ *ordinaryLease_ = nullptr; anchor_->solverLease = nullptr;
2305
+ }
2306
+ clock_private::Access::release(anchor_);
2307
+ }
2308
+
2309
+ bool BorrowedJointPhases2D::ownsOrdinaryLease() const noexcept {
2310
+ return ordinaryLease_ != nullptr && clock_private::Access::bound(anchor_, frameIdentity_)
2311
+ && clock_private::Access::generation(*anchor_->owner) == generation_
2312
+ && anchor_->solverLease == ordinaryLease_ && *ordinaryLease_ == anchor_;
2313
+ }
2314
+
2315
+ bool BorrowedJointPhases2D::validFor(State expected) const noexcept {
2316
+ return state_ == expected && JointPhaseAccess2D::intervalLive(*this)
2317
+ && physics_ == anchor_->owner && (ordinaryLease_ == nullptr || ownsOrdinaryLease())
2318
+ && clock_private::Access::live(*anchor_->owner, generation_, bodies_,
2319
+ bodyCount_, duration_, substep_) && solver_ != nullptr && solver_->ready()
2320
+ && JointPhaseAccess2D::storage(*this).bodies == bodies_;
2321
+ }
2322
+
2323
+ EventConstraintLedger2D* BorrowedJointPhases2D::eventLedger(std::uint32_t index) const noexcept {
2324
+ const auto* records = std::get_if<std::span<EventConstraintLedger2D>>(&reactions_);
2325
+ return records == nullptr ? nullptr : &(*records)[index];
2326
+ }
2327
+
2328
+ std::int32_t BorrowedJointPhases2D::fail(std::int32_t status) noexcept {
2329
+ if (clock_private::Access::bound(anchor_, frameIdentity_) && generation_ != 0)
2330
+ clock_private::Access::failed(*anchor_->owner, generation_);
2331
+ state_ = State::Failed;
2332
+ return status;
2333
+ }
2334
+
2335
+ std::int32_t BorrowedJointPhases2D::beginFixed() {
2336
+ if (!validFor(State::Bound)) return fail(initialStatus_ != kStepOk ? initialStatus_ : kStepJointInvalidArguments);
2337
+ auto& stage = JointPhaseAccess2D::storage(*this);
2338
+ if (const auto* records = std::get_if<std::span<EventConstraintLedger2D>>(&reactions_)) {
2339
+ if (records->size() < jointCount_) return fail(kStepJointInvalidArguments);
2340
+ }
2341
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2342
+ if (joints_[index].type == JointType2D::Gear) continue;
2343
+ auto* record = eventLedger(index);
2344
+ if (record != nullptr) ::new (static_cast<void*>(&record->primitive)) JointEventLedger2D{};
2345
+ auto* ledger = record == nullptr ? nullptr : &record->primitive;
2346
+ phase_test_private::record(phase_test_private::Code::PrimitivePrepare, index);
2347
+ std::int32_t failureStatus = kStepJointNumericFailure;
2348
+ if (!prepareJoint(*physics_, joints_[index], stage.states[index],
2349
+ stage.prepared[index].primitive, stage.bodies, stage.bodyLookup, bodyCount_,
2350
+ duration_, failureStatus, GeometryPhase::BeginFixed, ledger)) return fail(failureStatus);
2351
+ phase_test_private::record(phase_test_private::Code::PrimitiveWarm, index);
2352
+ warmStartJoint(stage.prepared[index].primitive, stage.bodies, ledger,
2353
+ joints_[index].type == JointType2D::Pulley);
2354
+ if (ledger != nullptr && ledger->reactionFailure) return fail(kStepJointNumericFailure);
2355
+ }
2356
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2357
+ if (joints_[index].type != JointType2D::Gear) continue;
2358
+ auto* record = eventLedger(index);
2359
+ if (record != nullptr) ::new (static_cast<void*>(&record->gear)) GearEventLedger2D{};
2360
+ auto* ledger = record == nullptr ? nullptr : &record->gear;
2361
+ phase_test_private::record(phase_test_private::Code::GearPrepare, index);
2362
+ if (!prepareGear(*physics_, joints_[index], joints_, stage.states,
2363
+ stage.prepared[index].gear, stage.bodies, stage.bodyLookup, bodyCount_,
2364
+ jointCount_, duration_)) return fail(kStepJointNumericFailure);
2365
+ phase_test_private::record(phase_test_private::Code::GearWarm, index);
2366
+ warmStartGear(stage.prepared[index].gear, stage.bodies, ledger);
2367
+ if (ledger != nullptr && ledger->reactionFailure) return fail(kStepJointNumericFailure);
2368
+ }
2369
+ disabledCount_ = buildDisabledContactPairs(joints_, stage.states, jointCount_, stage.disabledPairs);
2370
+ state_ = State::Begun;
2371
+ return kStepOk;
2372
+ }
2373
+
2374
+ std::int32_t BorrowedJointPhases2D::refreshAtPose() {
2375
+ if (!validFor(State::Begun) || std::holds_alternative<OrdinaryFinish2D>(reactions_)) {
2376
+ return fail(kStepJointInvalidArguments);
2377
+ }
2378
+ auto& stage = JointPhaseAccess2D::storage(*this);
2379
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2380
+ if (joints_[index].type == JointType2D::Gear) continue;
2381
+ phase_test_private::record(phase_test_private::Code::PrimitiveRefresh, index);
2382
+ JointState2D state = stage.states[index];
2383
+ PreparedJoint prepared = stage.prepared[index].primitive;
2384
+ JointEventLedger2D ledger = eventLedger(index)->primitive;
2385
+ std::int32_t failureStatus = kStepJointNumericFailure;
2386
+ if (!prepareJoint(*physics_, joints_[index], state, prepared, stage.bodies,
2387
+ stage.bodyLookup, bodyCount_, duration_, failureStatus, GeometryPhase::RefreshAtPose, &ledger)) {
2388
+ return fail(failureStatus);
2389
+ }
2390
+ stage.states[index] = state;
2391
+ stage.prepared[index].primitive = prepared;
2392
+ eventLedger(index)->primitive = ledger;
2393
+ }
2394
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2395
+ if (joints_[index].type != JointType2D::Gear) continue;
2396
+ // Source coordinate state already belongs to this private candidate stage.
2397
+ phase_test_private::record(phase_test_private::Code::GearRefresh, index);
2398
+ PreparedGear prepared = stage.prepared[index].gear;
2399
+ JointState2D state = stage.states[index];
2400
+ if (!prepareGear(*physics_, joints_[index], joints_, stage.states, prepared,
2401
+ stage.bodies, stage.bodyLookup, bodyCount_, jointCount_, duration_, GeometryPhase::RefreshAtPose,
2402
+ &eventLedger(index)->gear)) {
2403
+ stage.states[index] = state;
2404
+ return fail(kStepJointNumericFailure);
2405
+ }
2406
+ stage.prepared[index].gear = prepared;
2407
+ }
2408
+ return kStepOk;
2409
+ }
2410
+
2411
+ std::int32_t BorrowedJointPhases2D::solvePrimitiveIteration() {
2412
+ if (!validFor(State::Begun)) return fail(kStepJointInvalidArguments);
2413
+ phase_test_private::record(phase_test_private::Code::PrimitiveSolve);
2414
+ auto& stage = JointPhaseAccess2D::storage(*this);
2415
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2416
+ if ((stage.states[index].stateFlags & kJointBroken) != 0) continue;
2417
+ if (joints_[index].type == JointType2D::Gear) continue;
2418
+ auto* record = eventLedger(index);
2419
+ if (!solveJointIteration(stage.prepared[index].primitive, stage.bodies,
2420
+ record == nullptr ? nullptr : &record->primitive, joints_[index].type == JointType2D::Pulley)) {
2421
+ return fail(kStepJointNumericFailure);
2422
+ }
2423
+ }
2424
+ return kStepOk;
2425
+ }
2426
+
2427
+ std::int32_t BorrowedJointPhases2D::solveGearIteration() {
2428
+ if (!validFor(State::Begun)) return fail(kStepJointInvalidArguments);
2429
+ phase_test_private::record(phase_test_private::Code::GearSolve);
2430
+ auto& stage = JointPhaseAccess2D::storage(*this);
2431
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2432
+ if (joints_[index].type != JointType2D::Gear) continue;
2433
+ auto* record = eventLedger(index);
2434
+ if (!::rundot::kinetix::physics2d::solveGearIteration(stage.prepared[index].gear, stage.bodies,
2435
+ record == nullptr ? nullptr : &record->gear)) {
2436
+ return fail(kStepJointNumericFailure);
2437
+ }
2438
+ }
2439
+ return kStepOk;
2440
+ }
2441
+
2442
+ std::int32_t BorrowedJointPhases2D::finishFixed() {
2443
+ if (!validFor(State::Begun)) return fail(kStepJointInvalidArguments);
2444
+ if (eventBinding_) return fail(kStepJointInvalidArguments);
2445
+ const auto status = finishReactions();
2446
+ if (status != kStepOk) return status;
2447
+ if (std::holds_alternative<clock_private::Failure>(clock_private::Access::finish(*physics_, generation_)))
2448
+ return fail(kStepJointInvalidArguments);
2449
+ state_ = State::Finished;
2450
+ return kStepOk;
2451
+ }
2452
+
2453
+ std::variant<std::int32_t, clock_private::Failure> BorrowedJointPhases2D::finishEvent(
2454
+ clock_private::EventInterval& interval) {
2455
+ if (!eventBinding_ || !validFor(State::Begun) || !interval.live() || !interval.jointBound_
2456
+ || interval.anchor_ != anchor_ || interval.frameIdentity_ != frameIdentity_
2457
+ || interval.generation_ != generation_ || interval.bodies_ != bodies_
2458
+ || interval.bodyCount_ != bodyCount_ || interval.ordinal_ != substep_)
2459
+ return kStepJointInvalidArguments;
2460
+ const auto admitted = interval.admitCompletion();
2461
+ if (const auto* failure = std::get_if<clock_private::Failure>(&admitted)) return *failure;
2462
+ const auto work = clock_private::Access::charge(*physics_, clock_private::Work::EventIteration, jointCount_);
2463
+ if (const auto* failure = std::get_if<clock_private::Failure>(&work)) return *failure;
2464
+ const auto status = finishReactions();
2465
+ if (status != kStepOk) return status;
2466
+ interval.publishCompletion();
2467
+ state_ = State::Finished;
2468
+ return kStepOk;
2469
+ }
2470
+
2471
+ std::int32_t BorrowedJointPhases2D::finishReactions() {
2472
+ phase_test_private::record(phase_test_private::Code::JointFinish);
2473
+ auto& stage = JointPhaseAccess2D::storage(*this);
2474
+ for (std::uint32_t index = 0; index < jointCount_; index += 1) {
2475
+ if (joints_[index].type == JointType2D::Gear) {
2476
+ auto* record = eventLedger(index);
2477
+ if (!finishGearSubstep(joints_[index], stage.states[index], stage.prepared[index].gear,
2478
+ stage.events, *eventCount_, substep_, duration_, record == nullptr ? nullptr : &record->gear)) {
2479
+ return fail(kStepJointNumericFailure);
2480
+ }
2481
+ continue;
2482
+ }
2483
+ if ((stage.states[index].stateFlags & kJointBroken) != 0
2484
+ && stage.prepared[index].primitive.rowCount == 0) continue;
2485
+ auto* record = eventLedger(index);
2486
+ if (!finishJointSubstep(joints_[index], stage.states[index], stage.prepared[index].primitive,
2487
+ stage.events, *eventCount_, substep_, duration_, record == nullptr ? nullptr : &record->primitive)) {
2488
+ return fail(kStepJointNumericFailure);
2489
+ }
2490
+ }
2491
+ return kStepOk;
2492
+ }
2493
+
2494
+ std::variant<std::span<const DisabledContactPair2D>, std::int32_t>
2495
+ BorrowedJointPhases2D::disabledPairs() const noexcept {
2496
+ if (!validFor(State::Begun)) return kStepJointInvalidArguments;
2497
+ return std::span<const DisabledContactPair2D>(JointPhaseAccess2D::storage(*this).disabledPairs, disabledCount_);
2498
+ }
2499
+
2500
+ std::variant<std::uint32_t, std::int32_t> BorrowedJointPhases2D::disabledPairCount() const noexcept {
2501
+ if (!validFor(State::Begun)) return kStepJointInvalidArguments;
2502
+ return disabledCount_;
2503
+ }
2504
+ }
2505
+
2066
2506
  SolverContext2D::SolverContext2D()
2067
2507
  : SolverContext2D(kMaxBodies, kMaxJoints) {}
2068
2508
 
@@ -2098,6 +2538,7 @@ SolverContext2D::SolverContext2D(
2098
2538
 
2099
2539
  SolverContext2D::~SolverContext2D() {
2100
2540
  if (storage_ == nullptr) return;
2541
+ if (storage_->frame != nullptr) clock_private::Access::solverReleased(*storage_->frame);
2101
2542
  solverFree(storage_->bodies);
2102
2543
  solverFree(storage_->states);
2103
2544
  solverFree(storage_->telemetry);
@@ -2130,13 +2571,15 @@ std::uint32_t SolverContext2D::jointCapacity() const {
2130
2571
 
2131
2572
  std::size_t SolverContext2D::retainedBytes() const {
2132
2573
  if (!ready()) return 0;
2133
- return sizeof(Storage)
2134
- + static_cast<std::size_t>(storage_->bodyCapacity)
2135
- * (sizeof(Body) + sizeof(BodyLookupEntry))
2136
- + static_cast<std::size_t>(storage_->jointCapacity)
2137
- * (sizeof(JointState2D) + sizeof(JointTelemetry2D)
2138
- + sizeof(PreparedConstraint) + sizeof(JointBreakEvent2D)
2139
- + sizeof(DisabledContactPair2D));
2574
+ return memory_private::chargedBytes(this)
2575
+ + memory_private::chargedBytes(storage_)
2576
+ + memory_private::chargedBytes(storage_->bodies)
2577
+ + memory_private::chargedBytes(storage_->states)
2578
+ + memory_private::chargedBytes(storage_->telemetry)
2579
+ + memory_private::chargedBytes(storage_->prepared)
2580
+ + memory_private::chargedBytes(storage_->events)
2581
+ + memory_private::chargedBytes(storage_->disabledPairs)
2582
+ + memory_private::chargedBytes(storage_->bodyLookup);
2140
2583
  }
2141
2584
 
2142
2585
  std::int32_t validateWorldJoints2D(
@@ -2205,6 +2648,7 @@ std::int32_t stepWorld2D(
2205
2648
  std::uint32_t* gearReactionCount,
2206
2649
  const FluidState2D* fluidOrNull,
2207
2650
  const StepOptions& options) {
2651
+ if (JointPhaseAccess2D::frameBusy(context, physicsContext)) return kStepJointInvalidArguments;
2208
2652
  if (jointCount > kMaxJoints || telemetryCapacity > kMaxJoints
2209
2653
  || telemetryCapacity < jointCount || eventCapacity < jointCount
2210
2654
  || eventCapacity > kMaxJointEvents || gearReactionCapacity > kMaxGearReactionEntries) return kStepJointCapacity;
@@ -2260,6 +2704,9 @@ std::int32_t stepWorld2D(
2260
2704
  }
2261
2705
  if (gearReactionCapacity < candidateGearReactionCount) return kStepJointCapacity;
2262
2706
  SolverContext2D::Storage& storage = *context.storage_;
2707
+ clock_private::FrameScope frame(physicsContext, bodies, storage.bodies, nullptr,
2708
+ bodyCount, options, &context, &storage.frame);
2709
+ if (!frame.ready()) return kStepJointInvalidArguments;
2263
2710
  if (bodyCount > 0) {
2264
2711
  std::memcpy(storage.bodies, bodies, sizeof(Body) * bodyCount);
2265
2712
  }
@@ -2290,64 +2737,49 @@ std::int32_t stepWorld2D(
2290
2737
  options,
2291
2738
  substepDuration);
2292
2739
  if (status != kStepOk) return status;
2293
- for (std::uint32_t index = 0; index < jointCount; index += 1) {
2294
- if (joints[index].type == JointType2D::Gear) continue;
2295
- std::int32_t failureStatus = kStepJointNumericFailure;
2296
- if (!prepareJoint(physicsContext, joints[index], storage.states[index], storage.prepared[index].primitive,
2297
- storage.bodies, storage.bodyLookup, bodyCount, substepDuration, failureStatus)) {
2298
- return failureStatus;
2299
- }
2300
- warmStartJoint(storage.prepared[index].primitive, storage.bodies);
2301
- }
2302
- for (std::uint32_t index = 0; index < jointCount; index += 1) {
2303
- if (joints[index].type != JointType2D::Gear) continue;
2304
- if (!prepareGear(physicsContext, joints[index], joints, storage.states, storage.prepared[index].gear,
2305
- storage.bodies, storage.bodyLookup, bodyCount, jointCount, substepDuration)) return kStepJointNumericFailure;
2306
- warmStartGear(storage.prepared[index].gear, storage.bodies);
2307
- }
2308
- const std::uint32_t disabledPairCount = buildDisabledContactPairs(
2309
- joints,
2310
- storage.states,
2311
- jointCount,
2312
- storage.disabledPairs);
2740
+ auto phases = JointPhaseAccess2D::bindValidatedStage(context, physicsContext,
2741
+ joints, jointCount, bodyCount, substepDuration, substep, candidateEventCount,
2742
+ joint_phases_private::OrdinaryFinish2D{});
2743
+ status = phases.beginFixed();
2744
+ if (status != kStepOk) return status;
2313
2745
  for (std::uint32_t iteration = 0; iteration < options.velocityIterations; iteration += 1) {
2746
+ const auto disabled = phases.disabledPairs();
2747
+ if (const auto* error = std::get_if<std::int32_t>(&disabled)) return *error;
2748
+ const auto pairs = std::get<std::span<const DisabledContactPair2D>>(disabled);
2314
2749
  status = solveJointContactIteration(
2315
2750
  physicsContext,
2316
2751
  storage.bodies,
2317
2752
  bodyCount,
2318
- storage.disabledPairs,
2319
- disabledPairCount,
2753
+ pairs.data(),
2754
+ static_cast<std::uint32_t>(pairs.size()),
2320
2755
  iteration == 0);
2321
2756
  if (status != kStepOk) return status;
2322
- for (std::uint32_t index = 0; index < jointCount; index += 1) {
2323
- if ((storage.states[index].stateFlags & kJointBroken) != 0) continue;
2324
- if (joints[index].type == JointType2D::Gear) continue;
2325
- if (!solveJointIteration(storage.prepared[index].primitive, storage.bodies)) return kStepJointNumericFailure;
2326
- }
2327
- for (std::uint32_t index = 0; index < jointCount; index += 1) {
2328
- if (joints[index].type != JointType2D::Gear) continue;
2329
- if (!solveGearIteration(storage.prepared[index].gear, storage.bodies)) return kStepJointNumericFailure;
2330
- }
2757
+ status = phases.solvePrimitiveIteration();
2758
+ if (status != kStepOk) return status;
2759
+ status = phases.solveGearIteration();
2760
+ if (status != kStepOk) return status;
2331
2761
  }
2332
- status = finishJointContactSubstep(physicsContext, storage.bodies, bodyCount, substepDuration);
2333
- if (status != kStepOk) return status;
2334
- for (std::uint32_t index = 0; index < jointCount; index += 1) {
2335
- if (joints[index].type == JointType2D::Gear) {
2336
- if (!finishGearSubstep(joints[index], storage.states[index], storage.prepared[index].gear,
2337
- storage.events, candidateEventCount, substep, substepDuration)) return kStepJointNumericFailure;
2338
- continue;
2339
- }
2340
- if ((storage.states[index].stateFlags & kJointBroken) != 0
2341
- && storage.prepared[index].primitive.rowCount == 0) continue;
2342
- if (!finishJointSubstep(
2343
- joints[index],
2344
- storage.states[index],
2345
- storage.prepared[index].primitive,
2346
- storage.events,
2347
- candidateEventCount,
2348
- substep,
2349
- substepDuration)) return kStepJointNumericFailure;
2762
+ bool hasCcd = false;
2763
+ for (std::uint32_t index = 0; index < bodyCount; ++index) {
2764
+ hasCcd = hasCcd || (storage.bodies[index].type == BodyType::Dynamic
2765
+ && storage.bodies[index].ccd != 0);
2766
+ }
2767
+ if (hasCcd) {
2768
+ const auto disabled = phases.disabledPairs();
2769
+ if (const auto* error = std::get_if<std::int32_t>(&disabled)) return *error;
2770
+ const auto pairs = std::get<std::span<const DisabledContactPair2D>>(disabled);
2771
+ phase_test_private::record(phase_test_private::Code::Pose);
2772
+ StepOptions ccdOptions = options;
2773
+ ccdOptions.dt = substepDuration;
2774
+ ccdOptions.substeps = 1;
2775
+ status = resolveContinuousContacts2D(physicsContext, storage.bodies, bodyCount,
2776
+ ccdOptions, pairs.data(), static_cast<std::uint32_t>(pairs.size()), nullptr, nullptr);
2777
+ } else {
2778
+ status = finishJointContactSubstep(physicsContext, storage.bodies, bodyCount, substepDuration);
2350
2779
  }
2780
+ if (status != kStepOk) return status;
2781
+ status = phases.finishFixed();
2782
+ if (status != kStepOk) return status;
2351
2783
  for (std::uint32_t index = 0; index < bodyCount; index += 1) {
2352
2784
  if (!validBody(physicsContext, storage.bodies[index])) return kStepJointNumericFailure;
2353
2785
  }
@@ -2391,6 +2823,11 @@ std::int32_t stepWorld2D(
2391
2823
  if (!allFinite(values, 8)) return kStepJointNumericFailure;
2392
2824
  }
2393
2825
 
2826
+ auto output = frame.validatedOutput();
2827
+ auto* validated = std::get_if<clock_private::FrameOutputReceipt>(&output);
2828
+ if (validated == nullptr) return kStepJointNumericFailure;
2829
+ if (std::holds_alternative<clock_private::Failure>(frame.commit(std::move(*validated))))
2830
+ return kStepJointInvalidArguments;
2394
2831
  std::memcpy(bodies, storage.bodies, sizeof(Body) * bodyCount);
2395
2832
  std::memcpy(jointState, storage.states, sizeof(JointState2D) * jointCount);
2396
2833
  std::memcpy(telemetry, storage.telemetry, sizeof(JointTelemetry2D) * jointCount);