@woosh/meep-engine 2.139.0 → 2.141.0

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 (199) hide show
  1. package/package.json +1 -1
  2. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.d.ts +3 -3
  3. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.d.ts.map +1 -1
  4. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.js +4 -4
  5. package/src/{engine/physics/broadphase/aabb_transform_oriented.d.ts → core/geom/3d/aabb/aabb3_transform_oriented.d.ts} +2 -2
  6. package/src/core/geom/3d/aabb/aabb3_transform_oriented.d.ts.map +1 -0
  7. package/src/{engine/physics/broadphase/aabb_transform_oriented.js → core/geom/3d/aabb/aabb3_transform_oriented.js} +1 -1
  8. package/src/core/geom/3d/quaternion/quat3_multiply.d.ts +21 -0
  9. package/src/core/geom/3d/quaternion/quat3_multiply.d.ts.map +1 -0
  10. package/src/core/geom/3d/quaternion/quat3_multiply.js +25 -0
  11. package/src/core/geom/3d/quaternion/quat3_to_matrix3.d.ts +54 -0
  12. package/src/core/geom/3d/quaternion/quat3_to_matrix3.d.ts.map +1 -0
  13. package/src/core/geom/3d/quaternion/quat3_to_matrix3.js +69 -0
  14. package/src/core/geom/3d/shape/AbstractShape3D.d.ts +24 -2
  15. package/src/core/geom/3d/shape/AbstractShape3D.d.ts.map +1 -1
  16. package/src/core/geom/3d/shape/AbstractShape3D.js +24 -1
  17. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts +148 -0
  18. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts.map +1 -0
  19. package/src/core/geom/3d/shape/HeightMapShape3D.js +451 -0
  20. package/src/core/geom/3d/shape/MeshShape3D.d.ts +210 -0
  21. package/src/core/geom/3d/shape/MeshShape3D.d.ts.map +1 -0
  22. package/src/core/geom/3d/shape/MeshShape3D.js +593 -0
  23. package/src/core/geom/3d/shape/TransformedShape3D.d.ts.map +1 -1
  24. package/src/core/geom/3d/shape/TransformedShape3D.js +46 -2
  25. package/src/core/geom/3d/shape/Triangle3D.d.ts +95 -0
  26. package/src/core/geom/3d/shape/Triangle3D.d.ts.map +1 -0
  27. package/src/core/geom/3d/shape/Triangle3D.js +318 -0
  28. package/src/core/geom/3d/shape/UnionShape3D.js +13 -0
  29. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts +30 -0
  30. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts.map +1 -0
  31. package/src/core/geom/3d/shape/shape_mesh_from_geometry.js +64 -0
  32. package/src/core/geom/3d/tetrahedra/prototype_tetrahedrize_mesh.js +9 -11
  33. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts +28 -0
  34. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts.map +1 -0
  35. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.js +48 -0
  36. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.d.ts.map +1 -1
  37. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.js +40 -18
  38. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts +9 -5
  39. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts.map +1 -1
  40. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.js +38 -10
  41. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts +14 -5
  42. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts.map +1 -1
  43. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.js +47 -5
  44. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts +19 -0
  45. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts.map +1 -1
  46. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.js +75 -13
  47. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts +2 -2
  48. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts.map +1 -1
  49. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.js +1 -1
  50. package/src/core/geom/vec3/v3_dot_array_array.d.ts +3 -3
  51. package/src/core/geom/vec3/v3_dot_array_array.d.ts.map +1 -1
  52. package/src/core/geom/vec3/v3_dot_array_array.js +2 -2
  53. package/src/core/geom/vec3/v3_negate_array.d.ts +3 -3
  54. package/src/core/geom/vec3/v3_negate_array.d.ts.map +1 -1
  55. package/src/core/geom/vec3/v3_negate_array.js +2 -2
  56. package/src/core/geom/vec3/v3_quat3_apply.d.ts +29 -0
  57. package/src/core/geom/vec3/v3_quat3_apply.d.ts.map +1 -0
  58. package/src/core/geom/vec3/v3_quat3_apply.js +39 -0
  59. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts +30 -0
  60. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts.map +1 -0
  61. package/src/core/geom/vec3/v3_quat3_apply_inverse.js +41 -0
  62. package/src/core/geom/vec3/v3_triple_cross_product.d.ts +32 -0
  63. package/src/core/geom/vec3/v3_triple_cross_product.d.ts.map +1 -0
  64. package/src/core/geom/vec3/v3_triple_cross_product.js +45 -0
  65. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts +16 -3
  66. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts.map +1 -1
  67. package/src/engine/control/first-person/FirstPersonPlayerController.js +211 -211
  68. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts +72 -8
  69. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts.map +1 -1
  70. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.js +37 -5
  71. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts +101 -3
  72. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts.map +1 -1
  73. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.js +1789 -1416
  74. package/src/engine/control/first-person/TODO.md +173 -127
  75. package/src/engine/control/first-person/abilities/Slide.d.ts.map +1 -1
  76. package/src/engine/control/first-person/abilities/Slide.js +9 -1
  77. package/src/engine/control/first-person/prototype_first_person_controller.js +88 -2
  78. package/src/engine/control/first-person/test/buildTestPlayer.d.ts.map +1 -1
  79. package/src/engine/control/first-person/test/buildTestPlayer.js +9 -1
  80. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts +42 -0
  81. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts.map +1 -0
  82. package/src/engine/graphics/geometry/CapsuleGeometry.js +171 -0
  83. package/src/engine/physics/BULLET_REVIEW.md +945 -0
  84. package/src/engine/physics/CANNON_REVIEW.md +1300 -0
  85. package/src/engine/physics/JOLT_REVIEW.md +913 -0
  86. package/src/engine/physics/PLAN.md +578 -236
  87. package/src/engine/physics/RAPIER_REVIEW.md +934 -0
  88. package/src/engine/physics/REVIEW_001_ACTION_PLAN.md +642 -0
  89. package/src/engine/physics/REVIEW_002.md +151 -0
  90. package/src/engine/physics/broadphase/compute_fat_world_aabb.js +2 -2
  91. package/src/engine/physics/constraint/DofMode.d.ts +28 -0
  92. package/src/engine/physics/constraint/DofMode.d.ts.map +1 -0
  93. package/src/engine/physics/constraint/DofMode.js +35 -0
  94. package/src/engine/physics/constraint/solve_constraints.d.ts +16 -0
  95. package/src/engine/physics/constraint/solve_constraints.d.ts.map +1 -0
  96. package/src/engine/physics/constraint/solve_constraints.js +436 -0
  97. package/src/engine/physics/contact/ManifoldStore.d.ts +83 -10
  98. package/src/engine/physics/contact/ManifoldStore.d.ts.map +1 -1
  99. package/src/engine/physics/contact/ManifoldStore.js +608 -499
  100. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts +2 -2
  101. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts.map +1 -1
  102. package/src/engine/physics/ecs/Joint.d.ts +179 -0
  103. package/src/engine/physics/ecs/Joint.d.ts.map +1 -0
  104. package/src/engine/physics/ecs/Joint.js +234 -0
  105. package/src/engine/physics/ecs/PhysicsSystem.d.ts +180 -20
  106. package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
  107. package/src/engine/physics/ecs/PhysicsSystem.js +1423 -1159
  108. package/src/engine/physics/fluid/FluidField.d.ts +14 -10
  109. package/src/engine/physics/fluid/FluidField.d.ts.map +1 -1
  110. package/src/engine/physics/fluid/FluidField.js +14 -10
  111. package/src/engine/physics/fluid/FluidSimulator.js +1 -1
  112. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.d.ts +17 -10
  113. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.d.ts.map +1 -1
  114. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.js +18 -11
  115. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.d.ts +13 -10
  116. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.d.ts.map +1 -1
  117. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.js +18 -13
  118. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.d.ts +4 -3
  119. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.d.ts.map +1 -1
  120. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.js +15 -11
  121. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts +30 -6
  122. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts.map +1 -1
  123. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.js +44 -18
  124. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts +6 -6
  125. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts.map +1 -1
  126. package/src/engine/physics/gjk/expanding_polytope_algorithm.js +68 -22
  127. package/src/engine/physics/gjk/gjk.d.ts +28 -2
  128. package/src/engine/physics/gjk/gjk.d.ts.map +1 -1
  129. package/src/engine/physics/gjk/gjk.js +421 -378
  130. package/src/engine/physics/gjk/minkowski_support.d.ts +37 -0
  131. package/src/engine/physics/gjk/minkowski_support.d.ts.map +1 -0
  132. package/src/engine/physics/gjk/minkowski_support.js +75 -0
  133. package/src/engine/physics/gjk/mpr.d.ts +56 -0
  134. package/src/engine/physics/gjk/mpr.d.ts.map +1 -0
  135. package/src/engine/physics/gjk/mpr.js +344 -0
  136. package/src/engine/physics/inertia/world_inverse_inertia.d.ts +20 -5
  137. package/src/engine/physics/inertia/world_inverse_inertia.d.ts.map +1 -1
  138. package/src/engine/physics/inertia/world_inverse_inertia.js +36 -38
  139. package/src/engine/physics/integration/integrate_position.d.ts +25 -7
  140. package/src/engine/physics/integration/integrate_position.d.ts.map +1 -1
  141. package/src/engine/physics/integration/integrate_position.js +43 -12
  142. package/src/engine/physics/integration/integrate_velocity.d.ts +30 -0
  143. package/src/engine/physics/integration/integrate_velocity.d.ts.map +1 -1
  144. package/src/engine/physics/integration/integrate_velocity.js +82 -1
  145. package/src/engine/physics/island/IslandBuilder.d.ts +4 -1
  146. package/src/engine/physics/island/IslandBuilder.d.ts.map +1 -1
  147. package/src/engine/physics/island/IslandBuilder.js +33 -16
  148. package/src/engine/physics/narrowphase/PosedShape.d.ts +0 -8
  149. package/src/engine/physics/narrowphase/PosedShape.d.ts.map +1 -1
  150. package/src/engine/physics/narrowphase/PosedShape.js +28 -30
  151. package/src/engine/physics/narrowphase/box_box_manifold.d.ts.map +1 -1
  152. package/src/engine/physics/narrowphase/box_box_manifold.js +140 -18
  153. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts +30 -0
  154. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts.map +1 -0
  155. package/src/engine/physics/narrowphase/box_triangle_contact.js +811 -0
  156. package/src/engine/physics/narrowphase/capsule_contacts.d.ts.map +1 -1
  157. package/src/engine/physics/narrowphase/capsule_contacts.js +10 -56
  158. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts +71 -0
  159. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts.map +1 -0
  160. package/src/engine/physics/narrowphase/capsule_triangle_contact.js +375 -0
  161. package/src/engine/physics/narrowphase/compute_penetration.d.ts +91 -0
  162. package/src/engine/physics/narrowphase/compute_penetration.d.ts.map +1 -0
  163. package/src/engine/physics/narrowphase/compute_penetration.js +396 -0
  164. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts +35 -0
  165. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts.map +1 -0
  166. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.js +80 -0
  167. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts +31 -0
  168. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts.map +1 -0
  169. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.js +55 -0
  170. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts +42 -0
  171. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts.map +1 -0
  172. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.js +204 -0
  173. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts +42 -0
  174. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts.map +1 -0
  175. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.js +94 -0
  176. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts +37 -0
  177. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts.map +1 -0
  178. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.js +37 -0
  179. package/src/engine/physics/narrowphase/narrowphase_step.d.ts +41 -2
  180. package/src/engine/physics/narrowphase/narrowphase_step.d.ts.map +1 -1
  181. package/src/engine/physics/narrowphase/narrowphase_step.js +1497 -382
  182. package/src/engine/physics/narrowphase/sphere_box_contact.d.ts.map +1 -1
  183. package/src/engine/physics/narrowphase/sphere_box_contact.js +16 -23
  184. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts +48 -0
  185. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts.map +1 -0
  186. package/src/engine/physics/narrowphase/sphere_triangle_contact.js +143 -0
  187. package/src/engine/physics/queries/overlap_shape.d.ts +51 -0
  188. package/src/engine/physics/queries/overlap_shape.d.ts.map +1 -0
  189. package/src/engine/physics/queries/overlap_shape.js +183 -0
  190. package/src/engine/physics/queries/shape_cast.d.ts +56 -0
  191. package/src/engine/physics/queries/shape_cast.d.ts.map +1 -0
  192. package/src/engine/physics/queries/shape_cast.js +387 -0
  193. package/src/engine/physics/solver/solve_contacts.d.ts +146 -32
  194. package/src/engine/physics/solver/solve_contacts.d.ts.map +1 -1
  195. package/src/engine/physics/solver/solve_contacts.js +809 -223
  196. package/src/engine/physics/broadphase/aabb_transform_oriented.d.ts.map +0 -1
  197. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts +0 -20
  198. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts.map +0 -1
  199. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.js +0 -83
@@ -0,0 +1,913 @@
1
+ # Physics Engine Review: meep vs. Jolt
2
+
3
+ A deep technical comparison of the meep in-house rigid-body engine
4
+ (`H:/git/moh/app/src/mir-engine/meep/src/engine/physics/`) against
5
+ **Jolt Physics** (https://github.com/jrouwe/JoltPhysics).
6
+
7
+ This review respects PLAN.md's documented out-of-scope decisions (pure JS,
8
+ no SIMD, no SAB, no multi-threaded solver, action-log netcode). Citations
9
+ on our side use `path:line` relative to the package root; Jolt citations
10
+ use file + class/function name.
11
+
12
+ ---
13
+
14
+ ## 1. Overall Architecture
15
+
16
+ ### Pipeline shape
17
+
18
+ | Stage | meep | Jolt |
19
+ |---|---|---|
20
+ | Integrate velocity (gravity, forces) | `PhysicsSystem.fixedUpdate` stage 1 | `JobApplyGravity` |
21
+ | Broadphase refit | Stage 2 — per-leaf `node_move_aabb` with velocity-padded fat AABB | `BroadPhaseQuadTree::UpdatePrepare` + `UpdateFinalize` (background tree rebuild w/ atomic swap) |
22
+ | Pair generation | Stage 3 — `generate_pairs.js`, awake-list driven, leaf query into both BVHs | `JobFindCollisions` over active bodies |
23
+ | Wake propagation | Stage 4 — explicit `__wake_pairs` | implicit via island building / `BodyManager::ActivateBodies` |
24
+ | Narrowphase | Stage 5 — `narrowphase_step.js` per body-pair | `CollisionDispatch::sCollideShapeVsShape` table dispatch |
25
+ | Island build | Stage 6 — `IslandBuilder.build()` | `IslandBuilder::Build()` + `LargeIslandSplitter` |
26
+ | Solver | Stage 7 — `solve_contacts.js`, 10 velocity iterations, Baumgarte folded in | `SolveVelocityConstraints` (10×) + **separate** `SolvePositionConstraints` (2×) |
27
+ | Position integration | Stage 8 — `integrate_position.js` | `JobIntegrateVelocity` |
28
+ | Sleep test | Stage 9 — `__sleep_test`, per-island atomic | `Body::UpdateSleepStateInternal`, per-body sleep-point displacement, island-level dispatch |
29
+ | Event diff / advance | Stage 10–11 — `diff_manifolds`, `manifolds.advance_frame()` | `ContactConstraintManager::FinalizeContactCacheAndCallContactPointRemovedCallbacks` |
30
+
31
+ The single most consequential structural divergence is **stage 7**: we
32
+ fold position correction (Baumgarte) into the same velocity iterations
33
+ that handle restitution + friction. Jolt runs a **second outer pass**
34
+ (`SolvePositionConstraints`, default `mNumPositionSteps = 2`) that
35
+ operates as split-impulse — see §3 "Split impulse".
36
+
37
+ ### Data layout — body pool
38
+
39
+ Both engines mirror the same conceptual model:
40
+
41
+ | Aspect | meep `BodyStorage` | Jolt `BodyManager` |
42
+ |---|---|---|
43
+ | ID encoding | 24-bit index ǁ 8-bit generation (`body/BodyStorage.js:11-19`) | 23-bit index ǁ 8-bit sequence ǁ 1 reserved bit (`Body/BodyID.h`, `cMaxBodyIndex = 0x7fffff`, `cSequenceNumberShift = 23`) |
44
+ | Slot reuse policy | Min-heap of free indices for deterministic reuse (`body/BodyStorage.js:154-176`) | First-free; sequence number bumped on destroy |
45
+ | Active list | Dense `Uint32Array __awake_list` + reverse map `__awake_pos` (`body/BodyStorage.js:101-103`) | `BodyID * mActiveBodies[cBodyTypeCount]` + `atomic<uint32> mNumActiveBodies[cBodyTypeCount]` |
46
+ | Body storage | SoA — `__entities`, `__generations`, `__kinds`, `__flags` as parallel typed arrays (`body/BodyStorage.js:92-95`) | AoS — `Array<Body *>` of pointers; bodies allocated individually on heap |
47
+ | Iteration | `for (let i = 0; i < awake_count; i++) { const idx = awake_at(i); ... }` (`generate_pairs.js:50-51`) | `BodyID *ab = GetActiveBodiesUnsafe(EBodyType::RigidBody); for (uint32 i = 0; i < n; ++i) ...` |
48
+
49
+ **Where we diverge in our favour:** SoA in `BodyStorage` is structurally
50
+ better-suited to streaming reads of cold flags / kinds / generations
51
+ than Jolt's pointer-chase. The price: per-body mutable state
52
+ (`linearVelocity`, `angularVelocity`, accumulators) still lives on
53
+ `RigidBody` objects, so the SoA wins are concentrated at the body-identity
54
+ side (allocate, wake, sleep, generation check), not the hot solver path.
55
+ Jolt's pointer-chase is mitigated by `Body` being contiguous-allocated
56
+ in chunks via a fixed-block pool.
57
+
58
+ **Where Jolt diverges:** the 1 reserved bit in `BodyID` is for the
59
+ broadphase to mark sleeping bodies in-tree without touching the body
60
+ object. We don't need it because sleeping bodies are evicted from the
61
+ awake list, and the manifold cache decides whether a slot deserves a
62
+ solver visit independently.
63
+
64
+ ### Broadphase
65
+
66
+ | | meep | Jolt |
67
+ |---|---|---|
68
+ | Tree type | Two BVH2 (binary) | `BroadPhaseQuadTree` (4-ary), one per object layer |
69
+ | Layer model | One static + one dynamic BVH | Multiple `BroadPhaseLayer` → multiple quad trees; `ObjectLayerPairFilter` for per-pair allow/deny |
70
+ | Refit on motion | Velocity-padded fat AABB (`broadphase/compute_fat_world_aabb.js:14-18`: `FAT_LINEAR = 0.05 m`, `FAT_VELOCITY_MULTIPLIER = 2 × |v| × dt`) | Bounding boxes only widen; no velocity pad; full tree rebuild in background each step |
71
+ | Update strategy | In-place `node_move_aabb`; tree mutated immediately | Double-buffered atomic tree swap ("one-way tree swap" per Jolt GDC 2022) |
72
+ | Query | Per-leaf `bvh_query_user_data_overlaps_aabb` against both trees | `CastAABoxNoLock` etc. via collector pattern |
73
+
74
+ The two-tree split is sound — both engines exploit it to skip
75
+ static-vs-static. Where Jolt goes further: `BroadPhaseLayer` is a
76
+ **multi-tree partition** (typically static, dynamic, kinematic,
77
+ debris) with a `ObjectVsBroadPhaseLayerFilter` short-circuiting whole
78
+ trees that can't possibly collide. Our layer/mask check happens
79
+ later, in the pair filter callback. Cost: every static body is paid
80
+ for at broadphase query time even if no dynamic could ever collide
81
+ with it.
82
+
83
+ ### Solver architecture
84
+
85
+ | | meep | Jolt |
86
+ |---|---|---|
87
+ | Solver type | PGS (Projected Gauss-Seidel) with sequential impulse + warm-start | PGS with warm-start, **two-pass** velocity + position |
88
+ | Velocity iterations | 10 (`solver/solve_contacts.js:33`) | 10 (`mNumVelocitySteps`) |
89
+ | Position iterations | 0 (folded into velocity loop via Baumgarte bias) | 2 (`mNumPositionSteps`) — separate `SolvePositionConstraints` pass |
90
+ | Sub-stepping (TGS) | No (PLAN.md documents failed attempt) | No — but `inCollisionSteps` parameter to `Update()` is a coarser collision-detection sub-step, not constraint substepping |
91
+ | Baumgarte | β = 0.2 inside velocity solve, capped at `MAX_BAUMGARTE_BIAS = 3 m/s` (`solver/solve_contacts.js:62`) | β = 0.2 in `SolvePositionConstraint`, applied as pseudo-velocity to positions then discarded |
92
+ | Restitution | Velocity bias inside velocity iterations, suppressed below 1 m/s (`solver/solve_contacts.js:69`) | One-shot velocity bias, `mMinVelocityForRestitution = 1.0` |
93
+ | Penetration slop | 0.005 m (`solver/solve_contacts.js:48`) | 0.02 m (`mPenetrationSlop`) |
94
+ | Friction | Coulomb disk-clamp in 2-D tangent plane (`friction_cone.js`) | Two scalar friction constraints (t1, t2) coupled via a single friction limit `μ * j_n` |
95
+
96
+ Jolt is **not** TGS-based — PLAN.md is correct to consider TGS deferred
97
+ work, but the way to do it well is what Jolt **does have**:
98
+ split-impulse, where positions get a separate solver pass driven by a
99
+ pseudo-velocity, decoupled from `mLinearVelocity`/`mAngularVelocity`.
100
+ This is the missing primitive on the path to TGS. Details in §3.
101
+
102
+ ### Sleep system
103
+
104
+ | | meep | Jolt |
105
+ |---|---|---|
106
+ | Granularity | Per-island atomic (`PhysicsSystem.js:942-1006`) | Per-body decision aggregated to per-island |
107
+ | Test signal | `max(|v|² + |ω|²)` across island, accumulated dt below threshold (`PhysicsSystem.js:967-995`) | Sphere-fit accumulated displacement of 3 body-frame "sleep points" (`Body::UpdateSleepStateInternal` — measures actual movement across bbox corners, captures pure rotation too) |
108
+ | Wake propagation | Explicit walk over `sleep_group_next` chain in `__wake_body` (`PhysicsSystem.js:707-735`) — circular doubly-linked list | Activation listener pattern + bodies in same island activate together implicitly through `ActivateBodies` |
109
+ | Sleep timer field | `RigidBody.sleep_timer` (per body) | `MotionProperties::AccumulateSleepTime` |
110
+
111
+ **Notable divergence:** Jolt's sleep signal is **point displacement**
112
+ across the body's bounding-box corners, not velocity². This is
113
+ materially more robust on pure-rotation cases: a thin rod spinning
114
+ about its long axis has tiny linear velocity but nonzero `|ω|`; our
115
+ test catches that (we include `+ av·av`), but Jolt's catches the case
116
+ where rotation alone moves a corner — even if neither `|v|` nor `|ω|`
117
+ alone is large. Practically, our test is a close approximation; the
118
+ question is whether the failure modes Jolt's resolves (spinning
119
+ elongated bodies that we'd sleep too early) actually arise in
120
+ gameplay. For most game scenarios the equivalence is good enough.
121
+
122
+ ### Threading model
123
+
124
+ Jolt is fundamentally a job-system engine — `JobApplyGravity`,
125
+ `JobFindCollisions`, `JobBuildIslandsFromConstraints`,
126
+ `JobSolveVelocityConstraints` × N, `JobSolvePositionConstraints` × N
127
+ are all parallel jobs, and `LargeIslandSplitter` exists specifically
128
+ to subdivide big islands so multiple workers can solve a single island
129
+ in parallel. PLAN.md explicitly excludes worker-based solving until
130
+ `SharedArrayBuffer` is universally usable; this is a deliberate
131
+ tradeoff and not a gap.
132
+
133
+ What is **not** a deliberate tradeoff: even single-threaded, the
134
+ inability to parallelise within a tall stack means the largest pile
135
+ in a scene is the latency floor for the whole frame. The island data
136
+ layout we have (sorted CSR, deterministic by min root) is **ready** to
137
+ support split-island workers later — that's an underlying
138
+ architectural good.
139
+
140
+ ---
141
+
142
+ ## 2. Specific Algorithms and Tradeoffs
143
+
144
+ ### Body storage / ID stability
145
+
146
+ | Aspect | meep | Jolt |
147
+ |---|---|---|
148
+ | Index bits | 24 → 16M live bodies | 23 → 8M live bodies |
149
+ | Generation/sequence bits | 8 (wraps mod 256) | 8 (wraps mod 256) |
150
+ | Generation bump on free | `body/BodyStorage.js:200` — `(generations[i] + 1) & 0xFF` | `BodyManager::GetNextSequenceNumber` |
151
+ | Reuse policy | **Min-heap** of free indices (`body/BodyStorage.js:405-426`) | First-free / lowest-index-wins via simple iteration |
152
+
153
+ The min-heap is genuinely interesting — Jolt doesn't formalise this.
154
+ Determinism for `BodyID` reuse in Jolt depends on call order; ours
155
+ depends only on the allocation/free sequence, which is the right
156
+ property for action-log replication.
157
+
158
+ ### Broadphase
159
+
160
+ | Aspect | meep | Jolt |
161
+ |---|---|---|
162
+ | File | `broadphase/generate_pairs.js`, `broadphase/compute_fat_world_aabb.js` | `BroadPhase/BroadPhaseQuadTree.cpp`, `BroadPhase/QuadTree.cpp` |
163
+ | Tree arity | Binary BVH | 4-ary quad tree (better SIMD-pack on Jolt; for us 2-ary is fine) |
164
+ | Update model | Mutate-in-place | Background rebuild + atomic swap |
165
+ | Fat AABB philosophy | Pad at insert/refit time; cheap O(1) test against fattened envelope on each move | No pad; tree-widen + periodic full rebuild |
166
+
167
+ Tradeoff summary: our model is simpler and lower-latency per body
168
+ move. Jolt's model is friendlier to multithreaded readers (queries
169
+ can run against the stable side of the swap while writers update the
170
+ other side). Pure-JS, single-threaded, our model wins on simplicity
171
+ and zero-allocation refit cost.
172
+
173
+ ### Narrowphase pair dispatch
174
+
175
+ | | meep | Jolt |
176
+ |---|---|---|
177
+ | Dispatch shape | Cascading `if (isSphereA && isBoxB) ...` branches inside `dispatch_pair` (`narrowphase/narrowphase_step.js:192-353`) | 2-D function-pointer table `sCollideShape[NumSubShapeTypes][NumSubShapeTypes]` (`Collision/CollisionDispatch.cpp`) |
178
+ | Symmetry handling | Explicit `(isSphereA && isBoxB) || (isBoxA && isSphereB)` blocks with swap on each handler | `sReversedCollideShape` wraps the registered direction's handler with a `ReversedCollector` swap |
179
+ | Adding new shape pair | Modify the cascade in `narrowphase_step.js` + write a new module like `sphere_box_contact.js` | Register a function pointer for the pair on each side (or one + reversed) at init |
180
+ | Concave dispatch | Inline branch on `shape.is_convex === false` (`narrowphase/narrowphase_step.js:368-501`) | `CollideShape` for `MeshShape` / `HeightFieldShape` calls into `CollideConvexVsTriangles` per filtered triangle from the shape's internal BVH |
181
+
182
+ Both engines specialise primitive pair handlers (sphere-sphere,
183
+ sphere-box, capsule-capsule, capsule-sphere, capsule-box, box-box for
184
+ us; equivalent for Jolt). The structural difference is the dispatch
185
+ table — Jolt is genuinely cleaner here. See §4 "Simplicity & uniformity".
186
+
187
+ ### GJK / EPA
188
+
189
+ | | meep `gjk/gjk.js` | Jolt `Geometry/GJKClosestPoint.h` |
190
+ |---|---|---|
191
+ | Reference | Kevin Moran's `GJK.h` (cited at file top) | Gino van den Bergen "Fast and Robust GJK" |
192
+ | Simplex update | Edge-based Voronoi check using cross products (`gjk/gjk.js:163-265`) — explicit cases for triangle and tetrahedron | Christer Ericson-style sub-simplex closest-point with bitset of surviving vertices (`uint32 set`) |
193
+ | Iteration cap | 64 (`gjk/gjk.js:4`) | None visible — terminates on tolerance only |
194
+ | Separating-axis cache between calls | **No** — initial dir always `(1, 0, 0)` (`gjk/gjk.js:42`) | **Yes** — `ioV` parameter carries the last separating axis in/out, providing frame coherence |
195
+ | Tolerance | Exact zero on degenerate case + 64-iter cap; no relative tolerance | `inTolerance` parameter, default `cDefaultCollisionTolerance = 1e-4` |
196
+ | Convergence criteria | Iteration cap + dir-becomes-zero | `v_len_sq <= tolerance_sq`, `v_len_sq <= FLT_EPSILON * GetMaxYLengthSq()`, `prev_v_len_sq - v_len_sq <= FLT_EPSILON * prev_v_len_sq` |
197
+
198
+ **The missing separating-axis cache is a real opportunity.** See §3
199
+ for the detailed comparison.
200
+
201
+ | EPA | meep `gjk/expanding_polytope_algorithm.js` | Jolt `Geometry/EPAPenetrationDepth.h` |
202
+ |---|---|---|
203
+ | Initial polytope | 4-vertex tetrahedron from GJK final simplex (`expanding_polytope_algorithm.js:103-106`) | Variable — 1/2/3/4-point start; manufactures extra supports if simplex incomplete |
204
+ | Closest-face search | Linear scan over `num_faces` every iteration (`expanding_polytope_algorithm.js:118-126`) | Priority queue (`PeekClosestTriangleInQueue`, `PopClosestTriangleFromQueue`) |
205
+ | Tolerance | Absolute `EPA_TOLERANCE = 0.0001` (`expanding_polytope_algorithm.js:10`) | Relative: `dist_sq - t->mClosestLenSq < t->mClosestLenSq * inTolerance` |
206
+ | Max faces / edges | `EPA_MAX_NUM_FACES = 64`, `EPA_MAX_NUM_LOOSE_EDGES = 32` (hard cap, returns degenerate result) (`expanding_polytope_algorithm.js:11-12`) | `EPAConvexHullBuilder` with `cMaxPoints` |
207
+ | Behaviour at cap | Returns closest-face approximation (`expanding_polytope_algorithm.js:329-346`) | Same; also detects "hull defects" and bails |
208
+
209
+ ### Solver
210
+
211
+ Covered in §1 architecturally. Key per-algorithm tradeoffs:
212
+
213
+ | | meep | Jolt |
214
+ |---|---|---|
215
+ | Lambda accumulation pattern | Clamp `sum = j_n + λ; new_j_n = max(sum, 0); delta = new_j_n - j_n` (`solver/solve_contacts.js:530-535`) | Same: `Clamp(mTotalLambda + lambda, inMinLambda, inMaxLambda)`, apply delta |
216
+ | Tangent basis | Least-aligned world axis trick (`solver/solve_contacts.js:115-143`) | `Vec3::sGetNormalizedPerpendicular(inWorldSpaceAxis)` similar |
217
+ | Friction coupling | Disk clamp in (t1, t2) plane via `friction_cone_clamp` | Effectively same — friction limit `μ * j_n` applied to combined `(t1, t2)` magnitude |
218
+ | Friction combine | Geometric mean `sqrt(μA * μB)` (`solver/solve_contacts.js:161-163`) | Configurable; default is the same |
219
+ | Restitution combine | `max(eA, eB)` (`solver/solve_contacts.js:171-174`) | Configurable; default is `max` or via `ContactListener::OnContactAdded` |
220
+
221
+ ### Manifold caching
222
+
223
+ | | meep `contact/ManifoldStore.js` | Jolt `ContactConstraintManager.h/cpp` |
224
+ |---|---|---|
225
+ | Max contacts per slot | 4 (`MAX_CONTACTS_PER_MANIFOLD`) | 4 (`MaxContactPoints`) |
226
+ | Per-contact data | 13 doubles: 2× world point, 3 normal, depth, 3 accumulated impulses (`contact/ManifoldStore.js:30`) | `CachedContactPoint` = 36 bytes: 2× `Float3` local position, 1 float non-pen impulse, 2 floats friction impulse (Jolt stores **local** positions, recomputed each frame) |
227
+ | Persistence key | Canonical `(idA, idB)` via `PairUint32Map` (Robin Hood + Fibonacci hash) | `BodyPairMap::KeyValue` with `Body *` pair |
228
+ | Cross-frame matching | Slot persists; new contact set overwrites in-place by index | Local-position-matching: `ccp->mPosition1.IsClose(p1_ls, mContactPointPreserveLambdaMaxDistSq)` — impulses transferred to matching points only |
229
+ | Eviction | Touched-flag grace counter (2 frames untouched → release) (`contact/ManifoldStore.js:344-373`) | Body-pair grace via `mBodyPairCacheMaxDeltaPositionSq` |
230
+
231
+ **Important divergence:** Jolt stores contact points in **local** body
232
+ space and matches **points** across frames; we store **world** space
233
+ and inherit warm-start by **index** in the manifold. Index-based
234
+ matching is faster but breaks when contact-point identity drifts (e.g.
235
+ a box sliding across another box — contact index 0 may be a different
236
+ geometric corner this frame than last). Real-world impact: warm-start
237
+ quality degrades on sliding contacts. For a quasi-static stack
238
+ (corners stay put across frames) the two are equivalent. The bench's
239
+ 4-cube stack working cleanly is consistent with this.
240
+
241
+ ### Sleep semantics
242
+
243
+ Covered in §1. Worth restating: Jolt's per-body sleep test is
244
+ **movement of body-frame sleep points** integrated over a window. Our
245
+ test is `max(|v|² + |ω|²)` over an island. Both have an island-atomic
246
+ sleep / wake structure. Ours has the explicit `sleep_group_next`
247
+ linked list for instant wake propagation — that's a genuine engineering
248
+ investment for the "100-block tower base-knock" scenario described in
249
+ PLAN.md. Jolt achieves equivalent semantics by re-running island
250
+ detection each frame (cheap because the broadphase tree is already
251
+ maintained).
252
+
253
+ ### Islands
254
+
255
+ | | meep `island/IslandBuilder.js` + `union_find.js` | Jolt `IslandBuilder.cpp` |
256
+ |---|---|---|
257
+ | Build algorithm | Union-find over awake bodies + touched non-sensor manifolds; path halving + union by min-index (`island/union_find.js:45-76`) | Union-find via `BodyLink::mLinkedTo` atomic; linked-to-lowest-index |
258
+ | Output | CSR-style: `body_data` + `body_offsets`, `contact_data` + `contact_offsets`; both sorted ascending within islands | CSR-style: `BodyID *mBodyIslands` + `mBodyIslandEnds`, similar for constraints |
259
+ | Anchors | Static/Kinematic bodies are anchors only — do not merge islands (`island/IslandBuilder.js:172`) | Same |
260
+ | Determinism | Union by min-index + sort guarantees stable output across runs (PLAN.md determinism contract) | Atomic union may have ordering races in MT mode |
261
+ | Multi-threaded split | No (out of scope) | `LargeIslandSplitter` divides islands >128 constraints into ≤32 parallel groups by body-disjoint sets |
262
+
263
+ ### CCD
264
+
265
+ | | meep | Jolt |
266
+ |---|---|---|
267
+ | Default | Speculative margin via velocity-padded fat AABB | Discrete |
268
+ | Optional | None | `EMotionQuality::LinearCast` per body — linear shape sweep, "time steals" body to first impact |
269
+ | Threshold to activate | N/A | `mLinearCastThreshold = 0.75` (fraction of inner radius/step) |
270
+ | Max penetration on cast hit | N/A | `mLinearCastMaxPenetration = 0.25` |
271
+
272
+ PLAN.md documents the falling-tower reproducer (180/1000 bodies tunnel
273
+ through a 1cm floor) and proposes a per-body linear shape-cast as the
274
+ fix. That maps almost identically to Jolt's `LinearCast` motion
275
+ quality. Worth scoping the same opt-in field on `RigidBodyFlags` if
276
+ the gameplay budget needs it.
277
+
278
+ ### Concave / triangle mesh
279
+
280
+ | | meep | Jolt |
281
+ |---|---|---|
282
+ | Mesh shape | `MeshShape3D` — face index list + `Vector3` vertex buffer; `is_convex = false` | `MeshShape` — compressed BVH of triangles, leaf triangles with materials + active-edge flags |
283
+ | Heightmap | `HeightMapShape3D` — Catmull-Rom sampled grid (`narrowphase/decomposition/heightmap_enumerate_triangles.js`) | `HeightFieldShape` |
284
+ | Enumeration | Linear O(N) scan with per-tri AABB overlap (`mesh_enumerate_triangles`) — no internal BVH on `MeshShape3D` | Internal compressed BVH on `MeshShape`, descended for the query AABB |
285
+ | Per-triangle narrowphase | GJK + EPA every triangle (`narrowphase/narrowphase_step.js:476-481`) — hits `Triangle3D`'s degenerate face-normal support | Specialised: `CollideSphereVsTriangles` uses `ClosestPoint::GetClosestPointOnTriangle` (closed-form). Other primitives go through `CollideConvexVsTriangles` which is GJK+EPA but with **active-edge fix-normal** to suppress ghost contacts at internal edges |
286
+ | Active-edge handling | None | `EActiveEdgeMode::CollideOnlyWithActive`, `mActiveEdgeCosThresholdAngle` (~5°), `ActiveEdges::FixNormal` rewrites the contact normal to the triangle face normal when GJK lands on an inactive edge |
287
+
288
+ This is the largest correctness gap and PLAN.md correctly flags it as
289
+ the biggest accuracy opportunity. See §3 for the deep dive.
290
+
291
+ ### Queries (raycast / shape-cast / overlap)
292
+
293
+ | | meep | Jolt |
294
+ |---|---|---|
295
+ | Raycast | `queries/raycast.js` — single nearest hit across both BVHs | `NarrowPhaseQuery::CastRay` + `RayCastCollector` callback pattern |
296
+ | Shape cast | `queries/shape_cast.js` — broadphase swept AABB, per-candidate AABB-slab narrowing, coarse step, GJK bisection, MPR for normal recovery (`shape_cast.js`) | `NarrowPhaseQuery::CastShape` — dispatch table `sCastShape[NumSubShapeTypes][NumSubShapeTypes]`, specialised per shape-pair |
297
+ | Overlap | `queries/overlap_shape.js` — broadphase + GJK overlap, fills Uint32Array of body ids | `NarrowPhaseQuery::CollideShape` — collector-based |
298
+ | Filter model | Single callback `pair_filter(idA, idB)` + optional `filter?` per query | Four-stage: `BroadPhaseLayerFilter`, `ObjectLayerFilter`, `BodyFilter` (`ShouldCollide` + `ShouldCollideLocked`), `ShapeFilter` |
299
+
300
+ The collector pattern in Jolt is the cleanest extension point: a
301
+ caller asks "give me all hits and let me decide" or "give me the
302
+ closest" or "stop after the first" by choosing a collector
303
+ implementation. We expose nearest-only for raycast. Worth noting
304
+ this as a Section 4 simplicity tension: our API is narrower but
305
+ forces a separate query function per use case, vs. one query × N
306
+ collectors.
307
+
308
+ ---
309
+
310
+ ## 3. In-depth comparison — correctness and improvement opportunities
311
+
312
+ This section picks 8 algorithmic touchpoints, reads both engines'
313
+ actual code, and identifies bugs / divergences / opportunities.
314
+
315
+ ### 3.1 GJK separating-axis cache (frame coherence)
316
+
317
+ **Files:** `gjk/gjk.js`, Jolt's `Geometry/GJKClosestPoint.h`.
318
+
319
+ Jolt's `Intersects` signature:
320
+ ```cpp
321
+ template <typename A, typename B>
322
+ bool Intersects(const A &inA, const B &inB, float inTolerance, Vec3 &ioV)
323
+ ```
324
+
325
+ The `ioV` is the **in/out** separating axis. The caller passes in the
326
+ last known direction (typically cached on the contact pair), GJK refines
327
+ it, and writes the result back. Two consequences:
328
+
329
+ 1. **First-iteration quality:** with a good `ioV` from last frame, GJK
330
+ often converges in 1–2 iterations because the seed direction is
331
+ already close to the true closest-point direction.
332
+ 2. **Early miss:** if `ioV` from last frame still gives a negative dot
333
+ on the very first support point, the call returns `false` after **one**
334
+ support evaluation.
335
+
336
+ Our `gjk/gjk.js:42` starts every call with the constant `(1, 0, 0)`:
337
+
338
+ ```js
339
+ minkowski_support(
340
+ simplex, 6,
341
+ shape_a, shape_b,
342
+ 1, 0, 0
343
+ );
344
+ ```
345
+
346
+ For a single physics step the broadphase already pruned non-overlapping
347
+ pairs, so most GJK calls happen on pairs that already are or have just
348
+ been overlapping — and most of those will overlap again next frame.
349
+ We do nothing with that information. The `ManifoldStore` already has a
350
+ slot per pair; it's the natural place to cache the last separating axis.
351
+
352
+ **Concrete improvement:** add 3 doubles (`axis_x/y/z`) to the manifold
353
+ slot's per-slot data, write the EPA-output normal there at end of
354
+ narrowphase, and seed GJK's initial direction with it next frame.
355
+ Cost: 24 bytes/slot. Benefit: ~5–10× iteration-count reduction on
356
+ established contacts (Jolt's documented experience and standard GJK
357
+ practice).
358
+
359
+ This is a particularly high-value change because EPA's hot loop is
360
+ the dominant cost on non-trivial shapes — fewer GJK iterations means
361
+ fewer simplex updates, but more importantly, **better seed for EPA**
362
+ when GJK terminates with `intersecting = true`. EPA's iteration count
363
+ is also sensitive to where the initial polytope starts.
364
+
365
+ ### 3.2 Split-impulse architecture (blocker for TGS)
366
+
367
+ **Files:** `solver/solve_contacts.js:441-450`, Jolt's
368
+ `ContactConstraintManager.cpp`, `ConstraintPart/AxisConstraintPart.h`.
369
+
370
+ PLAN.md is right: this is the architectural blocker. Worth walking
371
+ through Jolt's setup so the shape of the change is concrete.
372
+
373
+ Jolt's `SolvePositionConstraints` runs **after** the velocity loop:
374
+
375
+ ```cpp
376
+ // pseudo-code, paraphrased from AxisConstraintPart::SolvePositionConstraint
377
+ separation = max(Vec3(p2 - p1).Dot(ws_normal) + inSettings.mPenetrationSlop,
378
+ -inSettings.mMaxPenetrationDistance);
379
+ lambda = -mEffectiveMass * inBaumgarte * separation;
380
+ // "Directly integrate velocity change for one time step,
381
+ // then integrate position, and discard the velocity change."
382
+ // — comment from Jolt
383
+ ```
384
+
385
+ The key sentence is the comment Jolt's author embedded. Position
386
+ correction uses a **pseudo-velocity** computed from the geometric
387
+ separation, applies it to positions for one frame, and discards.
388
+ The body's real `mLinearVelocity` / `mAngularVelocity` are
389
+ **untouched** by position correction.
390
+
391
+ Compare ours at `solver/solve_contacts.js:441-450`:
392
+
393
+ ```js
394
+ if (depth > PENETRATION_SLOP) {
395
+ bias = -BAUMGARTE_BETA / dt * (depth - PENETRATION_SLOP);
396
+ ...
397
+ if (bias < -MAX_BAUMGARTE_BIAS) bias = -MAX_BAUMGARTE_BIAS;
398
+ }
399
+ // Restitution
400
+ if (vn_pre < -RESTITUTION_VELOCITY_THRESHOLD) {
401
+ bias += restitution_combined * vn_pre;
402
+ }
403
+ pre[pre_off + 15] = bias;
404
+ ```
405
+
406
+ We bake position correction into `bias_n` which is then used inside
407
+ the velocity solve at lines 530-535:
408
+
409
+ ```js
410
+ const lambda_n = -m_eff_n * (vn + bias_n);
411
+ const sum_n = j_n_accum + lambda_n;
412
+ const new_j_n = sum_n > 0 ? sum_n : 0;
413
+ ```
414
+
415
+ Now consider TGS substeps: with this design, substep 0's `j_n` cancels
416
+ the inbound vn. Substep 1 sees `vn ≈ 0` separating, the `sum > 0`
417
+ clamp shrinks `j_n` (the restitution × warm-start interaction PLAN.md
418
+ documents). Restitution and Baumgarte are both inside the same
419
+ clamp-against-zero pipeline that's fundamentally about non-penetration
420
+ impulse direction.
421
+
422
+ **The split-impulse work has three sub-pieces** (none of which is on
423
+ the velocity hot path, so they cost a comparable amount per tick):
424
+
425
+ 1. **Per-body pseudo-velocity buffers.** Add `pos_lv[3]`, `pos_av[3]`
426
+ parallel to `linearVelocity`, `angularVelocity`. Zero at start of
427
+ position pass.
428
+ 2. **Position-only constraint computation.** Walks the same contact
429
+ list, computes separation from current world contact points
430
+ (re-evaluate from `r_a + p_a`, `r_b + p_b` to capture the latest
431
+ pose), applies the Baumgarte impulse into `pos_lv`/`pos_av`.
432
+ 3. **Position integration uses `lv + pos_lv`.** The pseudo-velocity
433
+ is consumed in `integrate_position`. After integration, pseudo
434
+ velocity is zeroed for next step.
435
+
436
+ Then in stage 7's velocity pass, **drop the `depth > PENETRATION_SLOP`
437
+ Baumgarte bias entirely**. Restitution stays as one-shot velocity bias
438
+ at pre-step (the iterative-clamping issue PLAN.md describes goes away
439
+ once position correction is no longer competing for the same
440
+ accumulator).
441
+
442
+ Once split-impulse is in place, TGS substepping becomes "apply forces
443
+ once at `dt`, then loop K substeps of {build pre-step at `sub_dt`,
444
+ solve velocity once at `sub_dt`, integrate position with `sub_dt`,
445
+ solve position once}". The three PLAN.md-documented issues disappear:
446
+
447
+ - Restitution × warm-start: restitution is one-shot at sub-step 0
448
+ only, applied as direct velocity delta to `lv` not as a bias.
449
+ - Baumgarte K× stronger: position pass uses `sub_dt`-scaled Baumgarte
450
+ inside its own loop, but it operates on `pos_lv` only, so it doesn't
451
+ multiply across substeps.
452
+ - Force accumulator timing: forces applied once at `dt` before substeps
453
+ begin, then accumulators zeroed.
454
+
455
+ This is a major piece of work but PLAN.md correctly classifies it as
456
+ deferred. Flagging it as the architectural inflection point.
457
+
458
+ ### 3.3 Triangle-vs-primitive — Jolt's closest-point-on-triangle
459
+
460
+ **Files:** Jolt's `CollideSphereVsTriangles.cpp` (closed-form),
461
+ `CollideConvexVsTriangles.cpp` (GJK+EPA+active-edge), our
462
+ `narrowphase/narrowphase_step.js:443-585`,
463
+ `narrowphase/compute_penetration.js`.
464
+
465
+ Jolt's sphere-vs-triangle (paraphrased):
466
+
467
+ ```cpp
468
+ uint closest_feature;
469
+ Vec3 point2 = ClosestPoint::GetClosestPointOnTriangle(v0, v1, v2, closest_feature);
470
+ float distance = (sphere_center - point2).Length();
471
+ if (distance > sphere_radius + max_separation_distance) return;
472
+ float penetration_depth = sphere_radius - distance;
473
+ // Voronoi region encoded in closest_feature (0..7 bitmask)
474
+ // 0b111 = interior (face) → normal is triangle face normal
475
+ // 0b001, 0b010, 0b100 = edge → normal is along (centre − closest_edge_point)
476
+ // Active-edge handling adjusts normal if it points along an inactive edge.
477
+ ```
478
+
479
+ This bypasses GJK entirely. Tooling for the same is already in our
480
+ codebase — `core/geom/3d/triangle/` has triangle-normal computation;
481
+ the missing piece is the Voronoi closest-point routine. Even a
482
+ straightforward Ericson Real-Time Collision Detection §5.1.5
483
+ implementation removes the entire `Triangle3D` degenerate-support
484
+ problem.
485
+
486
+ Direct sphere-vs-triangle is the highest-leverage missing primitive:
487
+ - Unblocks `narrowphase_concave.spec.js` skipped tests immediately.
488
+ - Fixes the "ball drops on heightmap, decelerates 70% then sinks over
489
+ 50 steps" failure documented in PLAN.md (PLAN.md:228-241).
490
+ - Is needed regardless of whether we ever do box-vs-triangle or
491
+ capsule-vs-triangle, because spheres-on-mesh is the single most
492
+ common gameplay primitive (character feet, projectiles, pickup
493
+ hitboxes).
494
+
495
+ Box-vs-triangle and capsule-vs-triangle are non-trivial: box-vs-triangle
496
+ is SAT with the triangle treated as having three edges plus a face
497
+ normal (13 candidate axes — 3 face normals of box, 1 triangle face,
498
+ 9 edge-edge crosses). Capsule-vs-triangle reduces to segment-vs-
499
+ triangle closest pair, then sphere-vs-triangle at that closest
500
+ segment-point.
501
+
502
+ Active-edge handling is also missing — `mesh_enumerate_triangles` emits
503
+ all triangles regardless of which edges are shared with neighbours.
504
+ Adding a precomputed active-edge bitmask to `MeshShape3D.indices`
505
+ (3 bits per triangle for the three edges, packed) and a fix-normal
506
+ pass like Jolt's would close out the ghost-contact failure mode on
507
+ adjacent-triangle seams. The heightmap doesn't need this — the
508
+ grid topology means every interior edge is shared with exactly one
509
+ neighbour, so the active-edge classification is purely a function of
510
+ the height difference (which we can compute on the fly).
511
+
512
+ ### 3.4 Manifold caching — world-space index match vs. local-space point match
513
+
514
+ **Files:** `contact/ManifoldStore.js:258-291`, `narrowphase_step.js`
515
+ candidate reduction (`reduce_candidates`), Jolt's
516
+ `ContactConstraintManager.cpp`.
517
+
518
+ Jolt's match (paraphrased):
519
+ ```cpp
520
+ if (Vec3::sLoadFloat3Unsafe(ccp->mPosition1).IsClose(p1_ls,
521
+ mPhysicsSettings.mContactPointPreserveLambdaMaxDistSq))
522
+ {
523
+ wcp.mNonPenetrationConstraint.SetTotalLambda(ccp.mNonPenetrationLambda);
524
+ ...
525
+ }
526
+ ```
527
+
528
+ For each new contact, Jolt scans the cached `CachedContactPoint`s and
529
+ transfers the impulse from any cached point within
530
+ `mContactPointPreserveLambdaMaxDistSq` (default ~1mm). Points are
531
+ stored in **body-local** space, so a body that rotated 90° between
532
+ frames still matches correctly.
533
+
534
+ Ours at `narrowphase_step.js` writes the candidate buffer into
535
+ `manifolds.set_contact(slot, k, ...)` where `k` is the slice index
536
+ inside `reduce_candidates`'s output. The warm-start impulses at slot
537
+ offset `+10..+12` (`contact/ManifoldStore.js:30`) **are preserved across
538
+ calls to `set_contact`** because `set_contact` only writes 10 floats
539
+ (positions, normal, depth) and leaves `j_n / j_t1 / j_t2` untouched.
540
+ This is good when the reduction picks the same contacts in the same
541
+ slice order — and bad when it doesn't.
542
+
543
+ `reduce_candidates` is deterministic (deepest into slot 0, then
544
+ greedy max-min-distance for the next three) — but it's deterministic
545
+ *per call*, given a candidate list. If the candidate list mutates in
546
+ order across frames (which it does: contacts come from per-collider
547
+ cross-product loops whose ordering can shift if compound colliders move
548
+ relatively, and the underlying triangle decomposition emits triangles
549
+ in spatial order that depends on the query AABB), the warm-start
550
+ impulse at slot k might be inherited from a geometrically *different*
551
+ contact in last frame's manifold.
552
+
553
+ **Concrete failure mode:** sliding contact. A box sliding on the
554
+ floor has 4 contact points that translate every frame. Frame N's
555
+ slot-0 contact (deepest) is at corner X; frame N+1's slot-0 (also
556
+ deepest) is at corner Y because the box rotated slightly and a
557
+ different corner is now deepest. Slot-0 inherits last frame's `j_n`
558
+ from corner X — which was holding up a different part of the box.
559
+ The error converges out in 10 velocity iterations, but warm-start
560
+ is supposed to **save** iterations, and it stops doing so when the
561
+ match is wrong.
562
+
563
+ **Improvement:** match by world (or better, local) position with a
564
+ small tolerance. The 13-float stride could grow to 16 by adding a
565
+ `(local_x, local_y, local_z)` triple per contact on body A (and skip
566
+ B because we can recompute via the normal). The reduction step
567
+ becomes "for each new candidate, find the nearest old contact within
568
+ tolerance, transfer its impulse to the new contact". This is
569
+ ~`O(new × old) ≤ 16` per pair — cheap. Storage cost: 3 doubles ×
570
+ 4 contacts × #slots = 96 bytes/slot. On 1k active pairs that's 96kB,
571
+ trivial.
572
+
573
+ This is materially more impactful for tall stacks than for static
574
+ piles, but it improves rolling/sliding scenarios across the board.
575
+
576
+ ### 3.5 EPA priority queue and termination
577
+
578
+ **Files:** `gjk/expanding_polytope_algorithm.js:118-127`, Jolt's
579
+ `Geometry/EPAPenetrationDepth.h`.
580
+
581
+ Our closest-face search:
582
+ ```js
583
+ let min_dist = v3_dot_array_array(faces, 0, faces, 3 * 3);
584
+ closest_face = 0;
585
+ for (let i = 1; i < num_faces; i++) {
586
+ const dist = v3_dot_array_array(faces, i * FACE_ELEMENT_COUNT, faces, i * FACE_ELEMENT_COUNT + 3 * 3);
587
+ if (dist < min_dist) { min_dist = dist; closest_face = i; }
588
+ }
589
+ ```
590
+
591
+ Linear scan over up to `EPA_MAX_NUM_FACES = 64` faces. Cost per
592
+ iteration: 64 dot products. We do up to 64 iterations. Worst case
593
+ 4096 dot products per EPA invocation.
594
+
595
+ Jolt maintains a heap-based priority queue keyed by `mClosestLenSq`
596
+ of each face. Insert on face creation, pop on access. Each
597
+ add/remove is `O(log N)`. With typical EPA convergence ~10
598
+ iterations, the savings are ~10 ×64 = 640 dot products → ~10 × log₂(40)
599
+ ≈ 50 heap ops, an order of magnitude.
600
+
601
+ This is a JS-friendly improvement (heap on a typed array is
602
+ straightforward; we already have a min-heap pattern in `BodyStorage`
603
+ and `ManifoldStore` for free lists). The catch: face removal mid-iteration
604
+ (the loose-edge step) needs to mark heap entries as deleted lazily.
605
+ Jolt's comment notes "removed triangles skipped during iteration"
606
+ — same lazy-delete pattern.
607
+
608
+ **Termination tolerance also wants attention.** Ours is absolute
609
+ (`EPA_TOLERANCE = 0.0001`); Jolt's is relative
610
+ (`dist_sq - mClosestLenSq < mClosestLenSq * inTolerance`). For a
611
+ sphere on a flat surface where the true penetration depth is 0.0001,
612
+ our absolute test passes on iteration 1 with garbage normal direction
613
+ (`closest_face` is whichever of the 4 initial-tetrahedron faces
614
+ happened to land near origin). Jolt's relative test would keep
615
+ iterating to refine the direction. This is the geometric source of
616
+ PLAN.md's "EPA degenerates on smooth shapes" — not the lack of MPR
617
+ fallback, but the **stopping criterion** failing to gate on actual
618
+ convergence of the search direction.
619
+
620
+ ### 3.6 Box-box manifold construction (Sutherland-Hodgman)
621
+
622
+ **Files:** `narrowphase/box_box_manifold.js`, Jolt's
623
+ `Geometry/ClipPoly.h`.
624
+
625
+ Both engines use SAT to find the smallest-overlap axis and
626
+ Sutherland-Hodgman to clip the incident face against the reference
627
+ face for face-vs-face contact. The implementations are
628
+ structurally identical.
629
+
630
+ Where we diverge: edge-edge contact. PLAN.md flags this — when the
631
+ SAT axis is an edge-pair cross, we emit a **single** midpoint contact
632
+ (`narrowphase/box_box_manifold.js:15-16`: "v1 fallback;
633
+ closest-edge-points refinement is a follow-up"). Jolt does the
634
+ edge-edge closest-pair computation and emits up to 2 contacts
635
+ (roughly: an edge-edge skew gives one contact per pair of
636
+ ribbons of overlap).
637
+
638
+ Practical impact: a tilted cube falling corner-onto-flat-cube relies
639
+ on the edge-edge path for the first ~5 frames before face-face takes
640
+ over. With a single midpoint contact, the impulse can be slightly off
641
+ the geometric centre of contact, producing a small tumble. The
642
+ existing 16-cube test PLAN.md mentions is run in a short window, so
643
+ this likely isn't visible in normal play but is the right next step
644
+ for box-stacking polish.
645
+
646
+ ### 3.7 Manifold reduction heuristic
647
+
648
+ **Files:** `narrowphase/narrowphase_step.js:149-178`, Jolt's
649
+ `Collision/ManifoldBetweenTwoFaces.cpp` `PruneContactPoints`.
650
+
651
+ Ours:
652
+ ```js
653
+ function reduce_candidates(n) {
654
+ // 1. deepest to slot 0
655
+ // 2. for each subsequent slot, pick the remaining candidate whose
656
+ // minimum distance to the already-kept set is largest
657
+ }
658
+ ```
659
+
660
+ Jolt's strategy:
661
+ 1. **First point:** maximises `(distance to CoM)² × (penetration depth)²`
662
+ 2. **Second point:** furthest from the first
663
+ 3. **Third and fourth:** opposite sides of the line from points 1–2,
664
+ maximising perpendicular distance.
665
+
666
+ The CoM-distance weighting in point 1 is the real divergence. It
667
+ biases toward contacts with maximum torque-arm leverage — the right
668
+ choice for stable rotational support. Our "deepest" choice
669
+ sometimes picks an under-the-CoM contact that produces no torque and
670
+ leaves the box unable to resist tipping. Practically, you'd see this
671
+ in a tall stack where outer contacts that prevent toppling get
672
+ displaced from the manifold by deeper contacts near the centre.
673
+
674
+ The CoM is available (`Transform.position` for the body — adjusted by
675
+ `com_offset` if we add one). The cost of folding `dist_to_com` into
676
+ the depth weight is trivial.
677
+
678
+ ### 3.8 Active body iteration / SoA layout
679
+
680
+ **Files:** `body/BodyStorage.js`, `broadphase/generate_pairs.js:50-51`,
681
+ Jolt's `BodyManager.cpp`.
682
+
683
+ Jolt:
684
+ ```cpp
685
+ BodyID *active_bodies = mActiveBodies[EBodyType::RigidBody];
686
+ uint32 num_active_bodies = mNumActiveBodies[EBodyType::RigidBody].load(memory_order_relaxed);
687
+ for (uint32 i = 0; i < num_active_bodies; ++i) {
688
+ BodyID id = active_bodies[i];
689
+ Body *body = mBodies[id.GetIndex()];
690
+ ...
691
+ }
692
+ ```
693
+
694
+ The body object lives behind a pointer. Each iteration is:
695
+ *read active_bodies[i]* → *index into mBodies* → *pointer chase to Body*.
696
+
697
+ Ours:
698
+ ```js
699
+ for (let i = 0; i < awake_count; i++) {
700
+ const body_idx = storage.awake_at(i);
701
+ const list = body_collider_lists[body_idx]; // sparse array
702
+ ...
703
+ }
704
+ ```
705
+
706
+ We do the same pointer chase to `RigidBody` and `Transform`. The
707
+ SoA wins of `BodyStorage` apply to **flags**, **kinds**, and
708
+ **generations** — but the simulation hot path reads `linearVelocity`,
709
+ `angularVelocity`, `inverseInertiaLocal` (on `RigidBody`), and
710
+ `position`, `rotation` (on `Transform`). Those are not in
711
+ `BodyStorage`.
712
+
713
+ This is a deliberate architectural choice (ECS shape with components
714
+ owning their own state) and the right one given meep's ECS model — but
715
+ worth being honest that it doesn't realise the SoA-locality benefit
716
+ that Jolt's pure pool design might (if Jolt were SoA, which it
717
+ isn't). Both engines have similar effective cache behaviour at the
718
+ solver's inner loop.
719
+
720
+ **No actionable change here** — flagging only because PLAN.md cites
721
+ "active list iteration" as a Jolt-derived design, and the win is
722
+ more conceptual (dense active list, no scanning all bodies) than
723
+ locality (the per-body data is still pointer-chased on both sides).
724
+
725
+ ---
726
+
727
+ ## 4. Simplicity & uniformity
728
+
729
+ ### Where the code is uniform
730
+
731
+ Both engines have clean per-stage decomposition. Our pipeline
732
+ (`PhysicsSystem.fixedUpdate`) is a single linear function with
733
+ 11 stages clearly commented. Jolt's `Update` is more elaborate
734
+ because of the job system — at least 12 distinct job types — but
735
+ the conceptual stages map 1:1.
736
+
737
+ Internal primitives (vec3 ops, AABB transforms, quaternion math)
738
+ are factored into `core/geom/` for us, equivalently into `Jolt/Math/`
739
+ and `Jolt/Geometry/`. No glaring over- or under-abstraction.
740
+
741
+ ### Where the code is specialised
742
+
743
+ **Narrowphase dispatch.** Our `dispatch_pair` (`narrowphase/narrowphase_step.js:192-353`)
744
+ is a cascade of `if (isSphereA && isBoxB)...`. Jolt has a 2-D table
745
+ `sCollideShape[NumSubShapeTypes][NumSubShapeTypes]`. The cascade is
746
+ fine while we have ~5 shape types × symmetric handling but it grows
747
+ quadratically in lines of code per new pair (sphere/box/capsule today
748
+ → +cylinder +cone +convex-hull on the PLAN backlog → roughly doubles
749
+ the cascade body).
750
+
751
+ **A table-based dispatch is worth considering** once we add the next
752
+ 2 shapes. The table is `Function[][]` keyed by shape type marker
753
+ (we already use `isUnitSphereShape3D`, `isBoxShape3D`, etc. — those
754
+ could become small integer codes). Symmetry handled by a single
755
+ "reversed" wrapper, as Jolt does. The cost is a level of indirection
756
+ per pair; the gain is "to add a new shape pair, you write one new
757
+ handler module + register it in two table slots", instead of editing
758
+ a 200-line cascade.
759
+
760
+ **Concave dispatch** is correctly factored out (`narrowphase_step.js:355-585`)
761
+ behind `is_convex === false`. The decomposition machinery
762
+ (`narrowphase/decomposition/`) is genuinely clean — `Triangle3D` as a
763
+ buffer-flyweight is a nice piece of work. The per-triangle GJK+EPA
764
+ should be replaced with closed-form primitives (see §3.3), but the
765
+ **enumeration / dispatch** structure is the right shape.
766
+
767
+ ### Code reuse
768
+
769
+ - `world_inverse_inertia_apply` is the central angular-Jacobian routine
770
+ used by the solver, the velocity integrator, and `applyImpulseAt`.
771
+ Good.
772
+ - `gjk` is shared between narrowphase, raycast (via shape_cast), and
773
+ the standalone `compute_penetration`. Good.
774
+ - `compute_penetration` is callable independently of the system,
775
+ enabling kinematic resolution. Nice abstraction.
776
+ - The min-heap pattern appears in `BodyStorage` (free body indices)
777
+ and `ManifoldStore` (free manifold slots) as **duplicated code**. A
778
+ shared `int_min_heap.js` primitive in `core/collection/` could host
779
+ both — same arguments, same algorithm. Low priority but a real
780
+ duplication.
781
+
782
+ ### Ease of extension
783
+
784
+ | Task | meep | Jolt |
785
+ |---|---|---|
786
+ | Add a new shape pair | Edit `dispatch_pair` cascade + write a new `xxx_yyy_contact.js` module | Write a handler function + register it in `CollisionDispatch::sInit` |
787
+ | Add a new constraint (joint) | Solver loop is set up to iterate `contacts ∪ joints` per PLAN.md, but the constraint pre-step + warm-start interface is not formalised — `solve_contacts.js` is hard-coded to contact-shape constraints | `Constraint` base class with `BuildIslands`, `SetupVelocityConstraint`, `WarmStartVelocityConstraint`, `SolveVelocityConstraint`, `SolvePositionConstraint` — clearly structured for arbitrary constraint types |
788
+ | Add a new query collector | Each query is its own function with its own filter pattern | One method per query type × user provides any `CollisionCollector` subclass |
789
+ | Add a new motion quality | N/A (only Discrete) | Add to `EMotionQuality`, route through `JobBuildIslandsFromConstraints` switch |
790
+
791
+ The constraint extension point is genuinely weaker on our side and
792
+ matters for the planned joints (distance, hinge, ball-socket,
793
+ prismatic per PLAN.md). The work to abstract is roughly:
794
+
795
+ 1. Define a `Constraint` interface with `setup(dt) → pre_step_data`,
796
+ `warm_start(state)`, `solve_velocity(state)`, optionally
797
+ `solve_position(state)` (in the future split-impulse world).
798
+ 2. Refactor `solve_contacts.js` so the contact path is *one
799
+ implementation* of the interface, sitting next to (eventually)
800
+ `solve_distance.js`, `solve_hinge.js`.
801
+ 3. The island builder gains a `__constraints` array that gets ordered
802
+ alongside the manifold-slot list.
803
+
804
+ This isn't strictly urgent (joints are backlog), but the time to do
805
+ it cleanly is **before** joints arrive, not after they each have
806
+ custom code paths.
807
+
808
+ ### What Jolt has that's worth adopting
809
+
810
+ 1. **`CollisionCollector` pattern for queries.** A `Collector` is a
811
+ tiny interface with `AddHit(result) → bool` (return false to early-out).
812
+ One implementation each for "all hits", "nearest", "any-hit", "first-in-layer".
813
+ Cleaner than our query-per-use-case approach, future-proofs against
814
+ batch queries.
815
+
816
+ 2. **`mContactPointPreserveLambdaMaxDistSq` (local-position-based warm-start).**
817
+ See §3.4.
818
+
819
+ 3. **Active-edge classification on triangle meshes.** See §3.3.
820
+
821
+ 4. **Closed-form sphere-vs-triangle.** See §3.3. (PLAN.md flags this.)
822
+
823
+ 5. **Last-separating-axis cache for GJK on persistent pairs.** See §3.1.
824
+
825
+ 6. **Position iteration as a separate pass (split-impulse architecture).**
826
+ See §3.2 and §3 of PLAN.md TGS backlog.
827
+
828
+ 7. **Manifold reduction weighted by torque-arm from CoM.** See §3.7.
829
+
830
+ ### What Jolt has that would be over-engineering for us
831
+
832
+ 1. **`LargeIslandSplitter`** — exists purely to enable parallel solving
833
+ within an island. PLAN.md's "out of scope without universal SAB" is
834
+ correct.
835
+ 2. **Multiple `BroadPhaseLayer`s.** Our two-tree split is enough; adding
836
+ a tree per object layer increases broadphase memory linearly in
837
+ layer count and only helps when there's a strict hierarchy of
838
+ "static vs. dynamic vs. kinematic vs. debris". Game content for our
839
+ target doesn't have that diversity.
840
+ 3. **`MutexArray` for body locking.** Single-threaded.
841
+ 4. **`ShapeRefC` reference counting.** Our shapes are managed by the
842
+ ECS lifetime; refcounts would be ceremony.
843
+ 5. **`EMotionQuality::LinearCast` with timestealing.** The simpler
844
+ "swept shape cast → clip movement → integrate at TOI" works in
845
+ single-threaded code without needing the elaborate substep state
846
+ machine Jolt builds for thread coordination.
847
+
848
+ ### Where we could simplify further
849
+
850
+ - **Heap implementation duplication** (`BodyStorage` / `ManifoldStore`)
851
+ — one shared `core/collection/IntMinHeap.js` (small).
852
+ - **PosedShape** (`narrowphase/PosedShape.js`) is fine but only used by
853
+ GJK/EPA paths; the comment-noted concave dispatch builds two of them
854
+ for the inverted convention. A single canonical "shape under pose"
855
+ abstraction used everywhere (broadphase, narrowphase, queries) would
856
+ reduce per-pair branching.
857
+ - **`generate_pairs.js`** runs `pair_filter` after the touched-flag
858
+ check. If `pair_filter` is set and the pair is currently rejected,
859
+ the manifold slot is still acquired-then-orphaned. Reordering so
860
+ the filter runs **before** `acquire` would avoid temporarily
861
+ allocating slots for rejected pairs.
862
+
863
+ ---
864
+
865
+ ## Headline takeaways
866
+
867
+ 1. **The single biggest correctness gap is closed-form sphere-vs-triangle**
868
+ (per PLAN.md). Jolt provides the blueprint in
869
+ `CollideSphereVsTriangles.cpp` — `ClosestPoint::GetClosestPointOnTriangle`
870
+ with a 3-bit Voronoi feature id. This unblocks the skipped
871
+ `narrowphase_concave.spec.js` tests and improves the heightmap drop
872
+ case fundamentally.
873
+
874
+ 2. **The biggest stability gap is split-impulse architecture.** Without
875
+ it, TGS is blocked (PLAN.md documents this). Jolt's pattern:
876
+ separate `SolvePositionConstraints` pass operating on a
877
+ pseudo-velocity, integrated for one substep and discarded. This is
878
+ a several-commit solver rewrite but the right one. Restitution
879
+ should be one-shot, not iterative.
880
+
881
+ 3. **GJK separating-axis cache** is a free win — 3 doubles per
882
+ manifold slot, ~5–10× iteration-count reduction on established
883
+ contacts, no architectural change needed.
884
+
885
+ 4. **Manifold warm-start by position match** (Jolt's
886
+ `mContactPointPreserveLambdaMaxDistSq`) closes a quiet correctness
887
+ bug in sliding/rolling contacts where warm-start currently inherits
888
+ impulses by slice index rather than by geometric identity.
889
+
890
+ 5. **EPA priority queue + relative tolerance** addresses the
891
+ "EPA-on-smooth-shapes" failure mode at its source. Current absolute
892
+ tolerance terminates with garbage normal direction on small-depth
893
+ contacts.
894
+
895
+ 6. **Active-edge classification on triangle meshes** suppresses ghost
896
+ contacts at internal seams without needing the full closed-form
897
+ triangle-vs-X family.
898
+
899
+ 7. **Manifold reduction should weight by CoM distance** for torque-arm
900
+ stability. Single-line change once `Transform.position` is
901
+ threaded through.
902
+
903
+ 8. **Constraint interface abstraction** before joints land — solver
904
+ loop is conceptually right, but the data-flow protocol for non-contact
905
+ constraints is informal.
906
+
907
+ 9. **Per-body LinearCast CCD** (Jolt's `EMotionQuality::LinearCast`)
908
+ maps directly to PLAN.md's documented falling-tower reproducer fix.
909
+
910
+ 10. **Threading, multi-tree broadphase layers, refcounted shapes,
911
+ cross-platform determinism** are all Jolt features that are correctly
912
+ out of scope for meep given the design bets in PLAN.md. Do not
913
+ pursue.