@woosh/meep-engine 2.139.0 → 2.140.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 (172) 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_to_matrix3.d.ts +54 -0
  9. package/src/core/geom/3d/quaternion/quat3_to_matrix3.d.ts.map +1 -0
  10. package/src/core/geom/3d/quaternion/quat3_to_matrix3.js +69 -0
  11. package/src/core/geom/3d/shape/AbstractShape3D.d.ts +24 -2
  12. package/src/core/geom/3d/shape/AbstractShape3D.d.ts.map +1 -1
  13. package/src/core/geom/3d/shape/AbstractShape3D.js +24 -1
  14. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts +148 -0
  15. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts.map +1 -0
  16. package/src/core/geom/3d/shape/HeightMapShape3D.js +451 -0
  17. package/src/core/geom/3d/shape/MeshShape3D.d.ts +210 -0
  18. package/src/core/geom/3d/shape/MeshShape3D.d.ts.map +1 -0
  19. package/src/core/geom/3d/shape/MeshShape3D.js +593 -0
  20. package/src/core/geom/3d/shape/TransformedShape3D.d.ts.map +1 -1
  21. package/src/core/geom/3d/shape/TransformedShape3D.js +46 -2
  22. package/src/core/geom/3d/shape/Triangle3D.d.ts +95 -0
  23. package/src/core/geom/3d/shape/Triangle3D.d.ts.map +1 -0
  24. package/src/core/geom/3d/shape/Triangle3D.js +318 -0
  25. package/src/core/geom/3d/shape/UnionShape3D.js +13 -0
  26. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts +30 -0
  27. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts.map +1 -0
  28. package/src/core/geom/3d/shape/shape_mesh_from_geometry.js +64 -0
  29. package/src/core/geom/3d/tetrahedra/prototype_tetrahedrize_mesh.js +9 -11
  30. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts +28 -0
  31. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts.map +1 -0
  32. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.js +48 -0
  33. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.d.ts.map +1 -1
  34. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.js +40 -18
  35. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts +9 -5
  36. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts.map +1 -1
  37. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.js +38 -10
  38. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts +14 -5
  39. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts.map +1 -1
  40. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.js +47 -5
  41. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts +19 -0
  42. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts.map +1 -1
  43. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.js +75 -13
  44. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts +2 -2
  45. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts.map +1 -1
  46. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.js +1 -1
  47. package/src/core/geom/vec3/v3_dot_array_array.d.ts +3 -3
  48. package/src/core/geom/vec3/v3_dot_array_array.d.ts.map +1 -1
  49. package/src/core/geom/vec3/v3_dot_array_array.js +2 -2
  50. package/src/core/geom/vec3/v3_negate_array.d.ts +3 -3
  51. package/src/core/geom/vec3/v3_negate_array.d.ts.map +1 -1
  52. package/src/core/geom/vec3/v3_negate_array.js +2 -2
  53. package/src/core/geom/vec3/v3_quat3_apply.d.ts +29 -0
  54. package/src/core/geom/vec3/v3_quat3_apply.d.ts.map +1 -0
  55. package/src/core/geom/vec3/v3_quat3_apply.js +39 -0
  56. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts +30 -0
  57. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts.map +1 -0
  58. package/src/core/geom/vec3/v3_quat3_apply_inverse.js +41 -0
  59. package/src/core/geom/vec3/v3_triple_cross_product.d.ts +32 -0
  60. package/src/core/geom/vec3/v3_triple_cross_product.d.ts.map +1 -0
  61. package/src/core/geom/vec3/v3_triple_cross_product.js +45 -0
  62. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts +16 -3
  63. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts.map +1 -1
  64. package/src/engine/control/first-person/FirstPersonPlayerController.js +211 -211
  65. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts +72 -8
  66. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts.map +1 -1
  67. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.js +37 -5
  68. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts +101 -3
  69. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts.map +1 -1
  70. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.js +1789 -1416
  71. package/src/engine/control/first-person/TODO.md +173 -127
  72. package/src/engine/control/first-person/abilities/Slide.d.ts.map +1 -1
  73. package/src/engine/control/first-person/abilities/Slide.js +9 -1
  74. package/src/engine/control/first-person/prototype_first_person_controller.js +88 -2
  75. package/src/engine/control/first-person/test/buildTestPlayer.d.ts.map +1 -1
  76. package/src/engine/control/first-person/test/buildTestPlayer.js +9 -1
  77. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts +42 -0
  78. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts.map +1 -0
  79. package/src/engine/graphics/geometry/CapsuleGeometry.js +171 -0
  80. package/src/engine/physics/BULLET_REVIEW.md +945 -0
  81. package/src/engine/physics/CANNON_REVIEW.md +1300 -0
  82. package/src/engine/physics/JOLT_REVIEW.md +913 -0
  83. package/src/engine/physics/PLAN.md +461 -236
  84. package/src/engine/physics/RAPIER_REVIEW.md +934 -0
  85. package/src/engine/physics/REVIEW_001_ACTION_PLAN.md +642 -0
  86. package/src/engine/physics/broadphase/compute_fat_world_aabb.js +2 -2
  87. package/src/engine/physics/contact/ManifoldStore.d.ts +83 -10
  88. package/src/engine/physics/contact/ManifoldStore.d.ts.map +1 -1
  89. package/src/engine/physics/contact/ManifoldStore.js +608 -499
  90. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts +2 -2
  91. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts.map +1 -1
  92. package/src/engine/physics/ecs/PhysicsSystem.d.ts +128 -20
  93. package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
  94. package/src/engine/physics/ecs/PhysicsSystem.js +1301 -1159
  95. package/src/engine/physics/fluid/FluidSimulator.d.ts.map +1 -1
  96. package/src/engine/physics/fluid/FluidSimulator.js +2 -1
  97. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts +28 -6
  98. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts.map +1 -1
  99. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.js +39 -17
  100. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts +6 -6
  101. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts.map +1 -1
  102. package/src/engine/physics/gjk/expanding_polytope_algorithm.js +68 -22
  103. package/src/engine/physics/gjk/gjk.d.ts +28 -2
  104. package/src/engine/physics/gjk/gjk.d.ts.map +1 -1
  105. package/src/engine/physics/gjk/gjk.js +421 -378
  106. package/src/engine/physics/gjk/minkowski_support.d.ts +37 -0
  107. package/src/engine/physics/gjk/minkowski_support.d.ts.map +1 -0
  108. package/src/engine/physics/gjk/minkowski_support.js +75 -0
  109. package/src/engine/physics/gjk/mpr.d.ts +56 -0
  110. package/src/engine/physics/gjk/mpr.d.ts.map +1 -0
  111. package/src/engine/physics/gjk/mpr.js +344 -0
  112. package/src/engine/physics/inertia/world_inverse_inertia.d.ts +20 -5
  113. package/src/engine/physics/inertia/world_inverse_inertia.d.ts.map +1 -1
  114. package/src/engine/physics/inertia/world_inverse_inertia.js +36 -38
  115. package/src/engine/physics/integration/integrate_position.d.ts +25 -7
  116. package/src/engine/physics/integration/integrate_position.d.ts.map +1 -1
  117. package/src/engine/physics/integration/integrate_position.js +43 -12
  118. package/src/engine/physics/integration/integrate_velocity.d.ts +30 -0
  119. package/src/engine/physics/integration/integrate_velocity.d.ts.map +1 -1
  120. package/src/engine/physics/integration/integrate_velocity.js +82 -1
  121. package/src/engine/physics/narrowphase/PosedShape.d.ts +0 -8
  122. package/src/engine/physics/narrowphase/PosedShape.d.ts.map +1 -1
  123. package/src/engine/physics/narrowphase/PosedShape.js +28 -30
  124. package/src/engine/physics/narrowphase/box_box_manifold.d.ts.map +1 -1
  125. package/src/engine/physics/narrowphase/box_box_manifold.js +113 -17
  126. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts +30 -0
  127. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts.map +1 -0
  128. package/src/engine/physics/narrowphase/box_triangle_contact.js +811 -0
  129. package/src/engine/physics/narrowphase/capsule_contacts.d.ts.map +1 -1
  130. package/src/engine/physics/narrowphase/capsule_contacts.js +10 -56
  131. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts +71 -0
  132. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts.map +1 -0
  133. package/src/engine/physics/narrowphase/capsule_triangle_contact.js +375 -0
  134. package/src/engine/physics/narrowphase/compute_penetration.d.ts +91 -0
  135. package/src/engine/physics/narrowphase/compute_penetration.d.ts.map +1 -0
  136. package/src/engine/physics/narrowphase/compute_penetration.js +396 -0
  137. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts +35 -0
  138. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts.map +1 -0
  139. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.js +80 -0
  140. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts +31 -0
  141. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts.map +1 -0
  142. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.js +55 -0
  143. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts +42 -0
  144. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts.map +1 -0
  145. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.js +204 -0
  146. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts +42 -0
  147. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts.map +1 -0
  148. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.js +94 -0
  149. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts +37 -0
  150. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts.map +1 -0
  151. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.js +37 -0
  152. package/src/engine/physics/narrowphase/narrowphase_step.d.ts +8 -2
  153. package/src/engine/physics/narrowphase/narrowphase_step.d.ts.map +1 -1
  154. package/src/engine/physics/narrowphase/narrowphase_step.js +1422 -382
  155. package/src/engine/physics/narrowphase/sphere_box_contact.d.ts.map +1 -1
  156. package/src/engine/physics/narrowphase/sphere_box_contact.js +16 -23
  157. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts +48 -0
  158. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts.map +1 -0
  159. package/src/engine/physics/narrowphase/sphere_triangle_contact.js +143 -0
  160. package/src/engine/physics/queries/overlap_shape.d.ts +51 -0
  161. package/src/engine/physics/queries/overlap_shape.d.ts.map +1 -0
  162. package/src/engine/physics/queries/overlap_shape.js +183 -0
  163. package/src/engine/physics/queries/shape_cast.d.ts +56 -0
  164. package/src/engine/physics/queries/shape_cast.d.ts.map +1 -0
  165. package/src/engine/physics/queries/shape_cast.js +387 -0
  166. package/src/engine/physics/solver/solve_contacts.d.ts +116 -30
  167. package/src/engine/physics/solver/solve_contacts.d.ts.map +1 -1
  168. package/src/engine/physics/solver/solve_contacts.js +641 -223
  169. package/src/engine/physics/broadphase/aabb_transform_oriented.d.ts.map +0 -1
  170. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts +0 -20
  171. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts.map +0 -1
  172. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.js +0 -83
@@ -1,499 +1,608 @@
1
- import { assert } from "../../../core/assert.js";
2
- import { PAIR_UINT32_MAP_ABSENT, PairUint32Map } from "../../../core/collection/PairUint32Map.js";
3
-
4
- /**
5
- * Maximum contact points per manifold. Four is the universal industry choice
6
- * (Bullet's btPersistentManifold, Box2D, Jolt) — enough to express stable
7
- * face-on-face contact, small enough to fit inertia-driven solver passes.
8
- * @type {number}
9
- */
10
- export const MAX_CONTACTS_PER_MANIFOLD = 4;
11
-
12
- /**
13
- * Per-contact field stride in the data Float64Array.
14
- *
15
- * Layout per contact (all world-space):
16
- * 0..2 : world_a (contact point on body A's surface)
17
- * 3..5 : world_b (contact point on body B's surface)
18
- * 6..8 : normal_w (from B toward A)
19
- * 9 : depth (positive = penetration, negative = speculative gap)
20
- * 10 : j_n (accumulated normal impulse, warm-start)
21
- * 11 : j_t1 (accumulated tangent impulse, axis 1)
22
- * 12 : j_t2 (accumulated tangent impulse, axis 2)
23
- *
24
- * Solver uses `(world_a + world_b) * 0.5` as the application point; storing
25
- * both surface points keeps the door open for richer warm-start matching
26
- * across frames in a follow-up slice.
27
- *
28
- * @type {number}
29
- */
30
- export const CONTACT_STRIDE = 13;
31
-
32
- /**
33
- * Per-slot Float64 stride: room for {@link MAX_CONTACTS_PER_MANIFOLD} contacts.
34
- * @type {number}
35
- */
36
- export const SLOT_DATA_STRIDE = CONTACT_STRIDE * MAX_CONTACTS_PER_MANIFOLD;
37
-
38
- /**
39
- * Per-slot meta stride in the Uint32 meta buffer:
40
- * 0 : bodyA
41
- * 1 : bodyB
42
- * 2 : packed = count(0..3) | touched(bit 4) | prev_touched(bit 5) | grace(bits 6..13)
43
- * @type {number}
44
- */
45
- const SLOT_META_STRIDE = 3;
46
-
47
- const FLAG_TOUCHED = 1 << 4;
48
- const FLAG_PREV_TOUCHED = 1 << 5;
49
- const COUNT_MASK = 0x0F;
50
- const GRACE_SHIFT = 6;
51
- const GRACE_MASK = 0xFF; // 8 bits
52
-
53
- /**
54
- * Number of consecutive frames a manifold may stay untouched before eviction.
55
- * Two frames of grace lets a one-frame separation survive (jitter friendly)
56
- * while still keeping the cache bounded.
57
- * @type {number}
58
- */
59
- const DEFAULT_GRACE = 2;
60
-
61
- /**
62
- * Sentinel for "no slot".
63
- * @type {number}
64
- */
65
- export const MANIFOLD_SLOT_ABSENT = -1;
66
-
67
- const DEFAULT_INITIAL_CAPACITY = 64;
68
-
69
- /**
70
- * Persistent contact-manifold cache keyed by canonical body-id pairs.
71
- *
72
- * Owns:
73
- * - the SoA `Float64Array` of contact points (per-slot stride 52),
74
- * - the small meta `Uint32Array` (bodyA, bodyB, packed flags + count),
75
- * - a {@link PairUint32Map} from `(idA, idB)` to slot id,
76
- * - a dense `__live_slots` array of currently-allocated slot ids,
77
- * - a min-heap-free-stack of released slot ids so reuse is deterministic.
78
- *
79
- * Lifecycle per simulation step:
80
- * 1. Broadphase calls {@link acquire} for every encountered pair, which marks
81
- * each touched slot as "fresh this frame".
82
- * 2. Narrowphase fills in contact points via {@link set_contact} /
83
- * {@link clear_contacts}.
84
- * 3. End-of-step calls {@link advance_frame} to roll touched prev_touched
85
- * and evict slots that have been untouched for {@link DEFAULT_GRACE}
86
- * consecutive frames.
87
- *
88
- * @author Alex Goldring
89
- * @copyright Company Named Limited (c) 2026
90
- */
91
- export class ManifoldStore {
92
-
93
- /**
94
- * @param {number} [initial_capacity] slots
95
- */
96
- constructor(initial_capacity = DEFAULT_INITIAL_CAPACITY) {
97
- assert.isNonNegativeInteger(initial_capacity, 'initial_capacity');
98
- this.__capacity = Math.max(4, initial_capacity);
99
- this.__high_water = 0;
100
-
101
- this.__data = new Float64Array(this.__capacity * SLOT_DATA_STRIDE);
102
- this.__meta = new Uint32Array(this.__capacity * SLOT_META_STRIDE);
103
-
104
- this.__pair_index = new PairUint32Map(this.__capacity * 2);
105
-
106
- // Dense list of currently-allocated slot ids + reverse map.
107
- this.__live_slots = new Uint32Array(this.__capacity);
108
- this.__live_pos = new Int32Array(this.__capacity);
109
- this.__live_pos.fill(-1);
110
- this.__live_count = 0;
111
-
112
- // Min-heap of released slot ids.
113
- this.__free_heap = new Uint32Array(this.__capacity);
114
- this.__free_count = 0;
115
- }
116
-
117
- /**
118
- * @returns {number} number of currently-live slots
119
- */
120
- get count() {
121
- return this.__live_count;
122
- }
123
-
124
- /**
125
- * @returns {number} total allocated slot capacity
126
- */
127
- get capacity() {
128
- return this.__capacity;
129
- }
130
-
131
- /**
132
- * Look up a slot by canonical pair. Caller is responsible for ordering
133
- * `(idA, idB)` such that the same pair always hashes the same way.
134
- *
135
- * @param {number} idA
136
- * @param {number} idB
137
- * @returns {number} slot id or {@link MANIFOLD_SLOT_ABSENT}
138
- */
139
- find(idA, idB) {
140
- const v = this.__pair_index.get(idA, idB);
141
- return v === PAIR_UINT32_MAP_ABSENT ? MANIFOLD_SLOT_ABSENT : v;
142
- }
143
-
144
- /**
145
- * Acquire a slot for the pair, creating one if absent, and mark it
146
- * "touched this frame". Returns the slot id. Calling `acquire` twice in
147
- * the same step on the same pair is a no-op for the second call.
148
- *
149
- * @param {number} idA
150
- * @param {number} idB
151
- * @returns {number} slot id
152
- */
153
- acquire(idA, idB) {
154
- let slot = this.find(idA, idB);
155
- if (slot === MANIFOLD_SLOT_ABSENT) {
156
- slot = this.__allocate_slot();
157
- const meta_off = slot * SLOT_META_STRIDE;
158
- this.__meta[meta_off] = idA;
159
- this.__meta[meta_off + 1] = idB;
160
- this.__meta[meta_off + 2] = 0; // count=0, no flags, grace=0
161
- // Zero the contact data — important so a recycled slot's warm-start
162
- // impulses don't leak across pair identities.
163
- const data_off = slot * SLOT_DATA_STRIDE;
164
- this.__data.fill(0, data_off, data_off + SLOT_DATA_STRIDE);
165
-
166
- this.__pair_index.set(idA, idB, slot);
167
- }
168
- const meta_off = slot * SLOT_META_STRIDE;
169
- // Set touched bit, clear grace.
170
- let packed = this.__meta[meta_off + 2];
171
- packed |= FLAG_TOUCHED;
172
- packed &= ~(GRACE_MASK << GRACE_SHIFT);
173
- this.__meta[meta_off + 2] = packed;
174
- return slot;
175
- }
176
-
177
- /**
178
- * @param {number} slot
179
- * @returns {number} body A id stored at this slot
180
- */
181
- bodyA(slot) {
182
- return this.__meta[slot * SLOT_META_STRIDE];
183
- }
184
-
185
- /**
186
- * @param {number} slot
187
- * @returns {number}
188
- */
189
- bodyB(slot) {
190
- return this.__meta[slot * SLOT_META_STRIDE + 1];
191
- }
192
-
193
- /**
194
- * @param {number} slot
195
- * @returns {number} 0..MAX_CONTACTS_PER_MANIFOLD
196
- */
197
- contact_count(slot) {
198
- return this.__meta[slot * SLOT_META_STRIDE + 2] & COUNT_MASK;
199
- }
200
-
201
- /**
202
- * @param {number} slot
203
- * @returns {boolean}
204
- */
205
- is_touched(slot) {
206
- return (this.__meta[slot * SLOT_META_STRIDE + 2] & FLAG_TOUCHED) !== 0;
207
- }
208
-
209
- /**
210
- * @param {number} slot
211
- * @returns {boolean}
212
- */
213
- was_touched_prev(slot) {
214
- return (this.__meta[slot * SLOT_META_STRIDE + 2] & FLAG_PREV_TOUCHED) !== 0;
215
- }
216
-
217
- /**
218
- * Iterate the dense list of currently-live slots without going through
219
- * the pair map. `i` ranges `[0, count)`.
220
- * @param {number} i
221
- * @returns {number}
222
- */
223
- live_at(i) {
224
- return this.__live_slots[i];
225
- }
226
-
227
- /**
228
- * Clear the contact list for a slot (counts go to zero, warm-start
229
- * impulses also reset). Used when narrowphase determines the pair is
230
- * separated this frame.
231
- * @param {number} slot
232
- */
233
- clear_contacts(slot) {
234
- const meta_off = slot * SLOT_META_STRIDE;
235
- this.__meta[meta_off + 2] = this.__meta[meta_off + 2] & ~COUNT_MASK;
236
-
237
- const data_off = slot * SLOT_DATA_STRIDE;
238
- this.__data.fill(0, data_off, data_off + SLOT_DATA_STRIDE);
239
- }
240
-
241
- /**
242
- * Write a contact point into a slot. Increments contact_count if `idx`
243
- * exceeds the current count.
244
- *
245
- * @param {number} slot
246
- * @param {number} idx contact index in `[0, MAX_CONTACTS_PER_MANIFOLD)`
247
- * @param {number} lax
248
- * @param {number} lay
249
- * @param {number} laz
250
- * @param {number} lbx
251
- * @param {number} lby
252
- * @param {number} lbz
253
- * @param {number} nx normal world x (B toward A)
254
- * @param {number} ny
255
- * @param {number} nz
256
- * @param {number} depth
257
- */
258
- set_contact(slot, idx, lax, lay, laz, lbx, lby, lbz, nx, ny, nz, depth) {
259
- const off = slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE;
260
- this.__data[off] = lax;
261
- this.__data[off + 1] = lay;
262
- this.__data[off + 2] = laz;
263
- this.__data[off + 3] = lbx;
264
- this.__data[off + 4] = lby;
265
- this.__data[off + 5] = lbz;
266
- this.__data[off + 6] = nx;
267
- this.__data[off + 7] = ny;
268
- this.__data[off + 8] = nz;
269
- this.__data[off + 9] = depth;
270
- // j_n, j_t1, j_t2 are warm-start; preserved across calls.
271
-
272
- const meta_off = slot * SLOT_META_STRIDE;
273
- const count_now = this.__meta[meta_off + 2] & COUNT_MASK;
274
- if (idx + 1 > count_now) {
275
- this.__meta[meta_off + 2] = (this.__meta[meta_off + 2] & ~COUNT_MASK) | (idx + 1);
276
- }
277
- }
278
-
279
- /**
280
- * Read a contact point. Writes 10 floats: lax, lay, laz, lbx, lby, lbz, nx, ny, nz, depth.
281
- * @param {number} slot
282
- * @param {number} idx
283
- * @param {number[]|Float64Array} out
284
- * @param {number} out_offset
285
- */
286
- get_contact(slot, idx, out, out_offset) {
287
- const off = slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE;
288
- for (let i = 0; i < 10; i++) {
289
- out[out_offset + i] = this.__data[off + i];
290
- }
291
- }
292
-
293
- /**
294
- * Get the contact-normal world x component for a given contact.
295
- * Provided for testability without exposing the raw Float64Array view.
296
- * @param {number} slot
297
- * @param {number} idx
298
- * @returns {number}
299
- */
300
- contact_normal_x(slot, idx) {
301
- return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 6];
302
- }
303
-
304
- /** @param {number} slot @param {number} idx @returns {number} */
305
- contact_normal_y(slot, idx) {
306
- return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 7];
307
- }
308
-
309
- /** @param {number} slot @param {number} idx @returns {number} */
310
- contact_normal_z(slot, idx) {
311
- return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 8];
312
- }
313
-
314
- /** @param {number} slot @param {number} idx @returns {number} */
315
- contact_depth(slot, idx) {
316
- return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 9];
317
- }
318
-
319
- /**
320
- * Raw contact-point buffer. Hot-path solver code dereferences this
321
- * directly via {@link slot_data_offset} + {@link CONTACT_STRIDE} to avoid
322
- * per-field accessor overhead. Buffer identity is stable until grow.
323
- * @returns {Float64Array}
324
- */
325
- get data_buffer() {
326
- return this.__data;
327
- }
328
-
329
- /**
330
- * Word offset into {@link data_buffer} where the first contact of `slot`
331
- * begins. Subsequent contacts within the slot are at
332
- * `offset + c * CONTACT_STRIDE`.
333
- * @param {number} slot
334
- * @returns {number}
335
- */
336
- slot_data_offset(slot) {
337
- return slot * SLOT_DATA_STRIDE;
338
- }
339
-
340
- /**
341
- * Advance touched flags by one frame and evict slots that have been
342
- * untouched for {@link DEFAULT_GRACE} consecutive frames.
343
- */
344
- advance_frame() {
345
- // Iterate live slots backwards so swap-with-last during release doesn't
346
- // skip indices.
347
- for (let i = this.__live_count - 1; i >= 0; i--) {
348
- const slot = this.__live_slots[i];
349
- const meta_off = slot * SLOT_META_STRIDE;
350
- let packed = this.__meta[meta_off + 2];
351
-
352
- const was_touched = (packed & FLAG_TOUCHED) !== 0;
353
- // Roll touched prev_touched, clear touched.
354
- if (was_touched) {
355
- packed = (packed & ~FLAG_PREV_TOUCHED) | FLAG_PREV_TOUCHED;
356
- packed &= ~FLAG_TOUCHED;
357
- // Reset grace.
358
- packed &= ~(GRACE_MASK << GRACE_SHIFT);
359
- this.__meta[meta_off + 2] = packed;
360
- } else {
361
- // Bump grace.
362
- const grace = ((packed >>> GRACE_SHIFT) & GRACE_MASK) + 1;
363
- packed &= ~FLAG_PREV_TOUCHED;
364
- packed &= ~(GRACE_MASK << GRACE_SHIFT);
365
- packed |= (grace & GRACE_MASK) << GRACE_SHIFT;
366
- this.__meta[meta_off + 2] = packed;
367
-
368
- if (grace >= DEFAULT_GRACE) {
369
- this.__release_slot(slot);
370
- }
371
- }
372
- }
373
- }
374
-
375
- /**
376
- * @private
377
- * @returns {number} newly-allocated slot id
378
- */
379
- __allocate_slot() {
380
- let slot;
381
- if (this.__free_count > 0) {
382
- slot = this.__heap_pop();
383
- } else {
384
- if (this.__high_water === this.__capacity) {
385
- this.__grow();
386
- }
387
- slot = this.__high_water++;
388
- }
389
- const pos = this.__live_count++;
390
- this.__live_slots[pos] = slot;
391
- this.__live_pos[slot] = pos;
392
- return slot;
393
- }
394
-
395
- /**
396
- * @private
397
- * @param {number} slot
398
- */
399
- __release_slot(slot) {
400
- const meta_off = slot * SLOT_META_STRIDE;
401
- const idA = this.__meta[meta_off];
402
- const idB = this.__meta[meta_off + 1];
403
-
404
- this.__pair_index.delete(idA, idB);
405
-
406
- // Remove from dense live list (swap with last).
407
- const pos = this.__live_pos[slot];
408
- const last_pos = --this.__live_count;
409
- if (pos !== last_pos) {
410
- const swapped = this.__live_slots[last_pos];
411
- this.__live_slots[pos] = swapped;
412
- this.__live_pos[swapped] = pos;
413
- }
414
- this.__live_pos[slot] = -1;
415
-
416
- this.__heap_push(slot);
417
- }
418
-
419
- /**
420
- * @private
421
- */
422
- __grow() {
423
- const new_capacity = this.__capacity << 1;
424
- this.__capacity = new_capacity;
425
-
426
- const grow_f64 = (a, stride) => {
427
- const n = new Float64Array(new_capacity * stride);
428
- n.set(a);
429
- return n;
430
- };
431
- const grow_u32 = (a, stride) => {
432
- const n = new Uint32Array(new_capacity * stride);
433
- n.set(a);
434
- return n;
435
- };
436
-
437
- this.__data = grow_f64(this.__data, SLOT_DATA_STRIDE);
438
- this.__meta = grow_u32(this.__meta, SLOT_META_STRIDE);
439
-
440
- const next_live = new Uint32Array(new_capacity);
441
- next_live.set(this.__live_slots);
442
- this.__live_slots = next_live;
443
-
444
- const next_live_pos = new Int32Array(new_capacity);
445
- next_live_pos.set(this.__live_pos);
446
- next_live_pos.fill(-1, this.__live_pos.length);
447
- this.__live_pos = next_live_pos;
448
-
449
- const next_free = new Uint32Array(new_capacity);
450
- next_free.set(this.__free_heap);
451
- this.__free_heap = next_free;
452
- }
453
-
454
- /**
455
- * @private
456
- * @param {number} v
457
- */
458
- __heap_push(v) {
459
- const heap = this.__free_heap;
460
- let i = this.__free_count++;
461
- heap[i] = v;
462
- while (i > 0) {
463
- const parent = (i - 1) >>> 1;
464
- if (heap[parent] > heap[i]) {
465
- const tmp = heap[parent];
466
- heap[parent] = heap[i];
467
- heap[i] = tmp;
468
- i = parent;
469
- } else break;
470
- }
471
- }
472
-
473
- /**
474
- * @private
475
- * @returns {number}
476
- */
477
- __heap_pop() {
478
- const heap = this.__free_heap;
479
- const result = heap[0];
480
- const new_count = --this.__free_count;
481
- if (new_count > 0) {
482
- heap[0] = heap[new_count];
483
- let i = 0;
484
- while (true) {
485
- const l = (i << 1) + 1;
486
- const r = l + 1;
487
- let smallest = i;
488
- if (l < new_count && heap[l] < heap[smallest]) smallest = l;
489
- if (r < new_count && heap[r] < heap[smallest]) smallest = r;
490
- if (smallest === i) break;
491
- const tmp = heap[i];
492
- heap[i] = heap[smallest];
493
- heap[smallest] = tmp;
494
- i = smallest;
495
- }
496
- }
497
- return result;
498
- }
499
- }
1
+ import { assert } from "../../../core/assert.js";
2
+ import { PAIR_UINT32_MAP_ABSENT, PairUint32Map } from "../../../core/collection/PairUint32Map.js";
3
+
4
+ /**
5
+ * Maximum contact points per manifold. Four is the universal industry choice
6
+ * (Bullet's btPersistentManifold, Box2D, Jolt) — enough to express stable
7
+ * face-on-face contact, small enough to fit inertia-driven solver passes.
8
+ * @type {number}
9
+ */
10
+ export const MAX_CONTACTS_PER_MANIFOLD = 4;
11
+
12
+ /**
13
+ * Per-contact field stride in the data Float64Array.
14
+ *
15
+ * Layout per contact (all world-space):
16
+ * 0..2 : world_a (contact point on body A's surface)
17
+ * 3..5 : world_b (contact point on body B's surface)
18
+ * 6..8 : normal_w (from B toward A)
19
+ * 9 : depth (positive = penetration, negative = speculative gap)
20
+ * 10 : j_n (accumulated normal impulse, warm-start)
21
+ * 11 : j_t1 (accumulated tangent impulse, axis 1)
22
+ * 12 : j_t2 (accumulated tangent impulse, axis 2)
23
+ * 13 : feature_id (uint32 packed as f64 — stable cross-frame ID of the
24
+ * geometric feature pair that produced this contact;
25
+ * 0 means "no info, fall back to position matching")
26
+ *
27
+ * Solver uses `(world_a + world_b) * 0.5` as the application point; storing
28
+ * both surface points enables the per-frame warm-start matcher in the
29
+ * narrowphase to compare contact-point positions when feature_id is 0.
30
+ *
31
+ * @type {number}
32
+ */
33
+ export const CONTACT_STRIDE = 14;
34
+
35
+ /**
36
+ * Per-contact feature_id offset within {@link CONTACT_STRIDE}.
37
+ * Exposed for the narrowphase matcher; solver code does not read this.
38
+ * @type {number}
39
+ */
40
+ export const CONTACT_FEATURE_ID_OFFSET = 13;
41
+
42
+ /**
43
+ * Per-slot Float64 stride: room for {@link MAX_CONTACTS_PER_MANIFOLD} contacts.
44
+ * @type {number}
45
+ */
46
+ export const SLOT_DATA_STRIDE = CONTACT_STRIDE * MAX_CONTACTS_PER_MANIFOLD;
47
+
48
+ /**
49
+ * Per-slot meta stride in the Uint32 meta buffer:
50
+ * 0 : bodyA
51
+ * 1 : bodyB
52
+ * 2 : packed = count(0..3) | touched(bit 4) | prev_touched(bit 5) | grace(bits 6..13)
53
+ * @type {number}
54
+ */
55
+ const SLOT_META_STRIDE = 3;
56
+
57
+ const FLAG_TOUCHED = 1 << 4;
58
+ const FLAG_PREV_TOUCHED = 1 << 5;
59
+ const COUNT_MASK = 0x0F;
60
+ const GRACE_SHIFT = 6;
61
+ const GRACE_MASK = 0xFF; // 8 bits
62
+
63
+ /**
64
+ * Number of consecutive frames a manifold may stay untouched before eviction.
65
+ * Two frames of grace lets a one-frame separation survive (jitter friendly)
66
+ * while still keeping the cache bounded.
67
+ * @type {number}
68
+ */
69
+ const DEFAULT_GRACE = 2;
70
+
71
+ /**
72
+ * Sentinel for "no slot".
73
+ * @type {number}
74
+ */
75
+ export const MANIFOLD_SLOT_ABSENT = -1;
76
+
77
+ const DEFAULT_INITIAL_CAPACITY = 64;
78
+
79
+ /**
80
+ * Persistent contact-manifold cache keyed by canonical body-id pairs.
81
+ *
82
+ * Owns:
83
+ * - the SoA `Float64Array` of contact points (per-slot stride 52),
84
+ * - the small meta `Uint32Array` (bodyA, bodyB, packed flags + count),
85
+ * - a {@link PairUint32Map} from `(idA, idB)` to slot id,
86
+ * - a dense `__live_slots` array of currently-allocated slot ids,
87
+ * - a min-heap-free-stack of released slot ids so reuse is deterministic.
88
+ *
89
+ * Lifecycle per simulation step:
90
+ * 1. Broadphase calls {@link acquire} for every encountered pair, which marks
91
+ * each touched slot as "fresh this frame".
92
+ * 2. Narrowphase fills in contact points via {@link set_contact} /
93
+ * {@link clear_contacts}.
94
+ * 3. End-of-step calls {@link advance_frame} to roll touched → prev_touched
95
+ * and evict slots that have been untouched for {@link DEFAULT_GRACE}
96
+ * consecutive frames.
97
+ *
98
+ * @author Alex Goldring
99
+ * @copyright Company Named Limited (c) 2026
100
+ */
101
+ export class ManifoldStore {
102
+
103
+ /**
104
+ * @param {number} [initial_capacity] slots
105
+ */
106
+ constructor(initial_capacity = DEFAULT_INITIAL_CAPACITY) {
107
+ assert.isNonNegativeInteger(initial_capacity, 'initial_capacity');
108
+ this.__capacity = Math.max(4, initial_capacity);
109
+ this.__high_water = 0;
110
+
111
+ this.__data = new Float64Array(this.__capacity * SLOT_DATA_STRIDE);
112
+ this.__meta = new Uint32Array(this.__capacity * SLOT_META_STRIDE);
113
+
114
+ // Per-slot cached GJK separating-axis seed (3 floats / slot).
115
+ // Hot-path GJK callers thread `slot_axis_buffer` +
116
+ // `slot_axis_offset(slot)` into `gjk_with_axis(...)` so the
117
+ // cached axis seeds the next frame's iteration. Initialised to
118
+ // zeros the `gjk_with_axis` guard catches that and uses
119
+ // (1, 0, 0) as the cold-start fallback.
120
+ this.__slot_axis = new Float64Array(this.__capacity * 3);
121
+
122
+ this.__pair_index = new PairUint32Map(this.__capacity * 2);
123
+
124
+ // Dense list of currently-allocated slot ids + reverse map.
125
+ this.__live_slots = new Uint32Array(this.__capacity);
126
+ this.__live_pos = new Int32Array(this.__capacity);
127
+ this.__live_pos.fill(-1);
128
+ this.__live_count = 0;
129
+
130
+ // Min-heap of released slot ids.
131
+ this.__free_heap = new Uint32Array(this.__capacity);
132
+ this.__free_count = 0;
133
+ }
134
+
135
+ /**
136
+ * @returns {number} number of currently-live slots
137
+ */
138
+ get count() {
139
+ return this.__live_count;
140
+ }
141
+
142
+ /**
143
+ * @returns {number} total allocated slot capacity
144
+ */
145
+ get capacity() {
146
+ return this.__capacity;
147
+ }
148
+
149
+ /**
150
+ * Look up a slot by canonical pair. Caller is responsible for ordering
151
+ * `(idA, idB)` such that the same pair always hashes the same way.
152
+ *
153
+ * @param {number} idA
154
+ * @param {number} idB
155
+ * @returns {number} slot id or {@link MANIFOLD_SLOT_ABSENT}
156
+ */
157
+ find(idA, idB) {
158
+ const v = this.__pair_index.get(idA, idB);
159
+ return v === PAIR_UINT32_MAP_ABSENT ? MANIFOLD_SLOT_ABSENT : v;
160
+ }
161
+
162
+ /**
163
+ * Acquire a slot for the pair, creating one if absent, and mark it
164
+ * "touched this frame". Returns the slot id. Calling `acquire` twice in
165
+ * the same step on the same pair is a no-op for the second call.
166
+ *
167
+ * @param {number} idA
168
+ * @param {number} idB
169
+ * @returns {number} slot id
170
+ */
171
+ acquire(idA, idB) {
172
+ let slot = this.find(idA, idB);
173
+ if (slot === MANIFOLD_SLOT_ABSENT) {
174
+ slot = this.__allocate_slot();
175
+ const meta_off = slot * SLOT_META_STRIDE;
176
+ this.__meta[meta_off] = idA;
177
+ this.__meta[meta_off + 1] = idB;
178
+ this.__meta[meta_off + 2] = 0; // count=0, no flags, grace=0
179
+ // Zero the contact data important so a recycled slot's warm-start
180
+ // impulses don't leak across pair identities.
181
+ const data_off = slot * SLOT_DATA_STRIDE;
182
+ this.__data.fill(0, data_off, data_off + SLOT_DATA_STRIDE);
183
+ // Zero the cached GJK axis. A stale axis from a previously
184
+ // released pair would mislead `gjk_with_axis` on this fresh
185
+ // pair; resetting to (0, 0, 0) hits the cold-start guard.
186
+ const axis_off = slot * 3;
187
+ this.__slot_axis[axis_off] = 0;
188
+ this.__slot_axis[axis_off + 1] = 0;
189
+ this.__slot_axis[axis_off + 2] = 0;
190
+
191
+ this.__pair_index.set(idA, idB, slot);
192
+ }
193
+ const meta_off = slot * SLOT_META_STRIDE;
194
+ // Set touched bit, clear grace.
195
+ let packed = this.__meta[meta_off + 2];
196
+ packed |= FLAG_TOUCHED;
197
+ packed &= ~(GRACE_MASK << GRACE_SHIFT);
198
+ this.__meta[meta_off + 2] = packed;
199
+ return slot;
200
+ }
201
+
202
+ /**
203
+ * @param {number} slot
204
+ * @returns {number} body A id stored at this slot
205
+ */
206
+ bodyA(slot) {
207
+ return this.__meta[slot * SLOT_META_STRIDE];
208
+ }
209
+
210
+ /**
211
+ * @param {number} slot
212
+ * @returns {number}
213
+ */
214
+ bodyB(slot) {
215
+ return this.__meta[slot * SLOT_META_STRIDE + 1];
216
+ }
217
+
218
+ /**
219
+ * @param {number} slot
220
+ * @returns {number} 0..MAX_CONTACTS_PER_MANIFOLD
221
+ */
222
+ contact_count(slot) {
223
+ return this.__meta[slot * SLOT_META_STRIDE + 2] & COUNT_MASK;
224
+ }
225
+
226
+ /**
227
+ * @param {number} slot
228
+ * @returns {boolean}
229
+ */
230
+ is_touched(slot) {
231
+ return (this.__meta[slot * SLOT_META_STRIDE + 2] & FLAG_TOUCHED) !== 0;
232
+ }
233
+
234
+ /**
235
+ * @param {number} slot
236
+ * @returns {boolean}
237
+ */
238
+ was_touched_prev(slot) {
239
+ return (this.__meta[slot * SLOT_META_STRIDE + 2] & FLAG_PREV_TOUCHED) !== 0;
240
+ }
241
+
242
+ /**
243
+ * Iterate the dense list of currently-live slots without going through
244
+ * the pair map. `i` ranges `[0, count)`.
245
+ * @param {number} i
246
+ * @returns {number}
247
+ */
248
+ live_at(i) {
249
+ return this.__live_slots[i];
250
+ }
251
+
252
+ /**
253
+ * Clear the contact list for a slot (counts go to zero, warm-start
254
+ * impulses also reset). Used when narrowphase determines the pair is
255
+ * separated this frame.
256
+ * @param {number} slot
257
+ */
258
+ clear_contacts(slot) {
259
+ const meta_off = slot * SLOT_META_STRIDE;
260
+ this.__meta[meta_off + 2] = this.__meta[meta_off + 2] & ~COUNT_MASK;
261
+
262
+ const data_off = slot * SLOT_DATA_STRIDE;
263
+ this.__data.fill(0, data_off, data_off + SLOT_DATA_STRIDE);
264
+ }
265
+
266
+ /**
267
+ * Reset the contact count to zero so the next `set_contact(slot, 0, …)`
268
+ * lands at index 0, but DO NOT zero the data slab. Warm-start impulses
269
+ * (`j_n`, `j_t1`, `j_t2`) at each contact slot survive — the subsequent
270
+ * `set_contact` calls only overwrite the first 10 floats (position,
271
+ * normal, depth) per contact.
272
+ *
273
+ * Use this when narrowphase determines the pair still has overlap and
274
+ * is about to refill the slot with the new frame's contacts. Use
275
+ * {@link clear_contacts} instead when the pair has genuinely separated
276
+ * and the cached impulses should be evicted.
277
+ *
278
+ * @param {number} slot
279
+ */
280
+ begin_refill(slot) {
281
+ const meta_off = slot * SLOT_META_STRIDE;
282
+ this.__meta[meta_off + 2] = this.__meta[meta_off + 2] & ~COUNT_MASK;
283
+ }
284
+
285
+ /**
286
+ * Write a contact point into a slot. Increments contact_count if `idx`
287
+ * exceeds the current count.
288
+ *
289
+ * Writes geometry (offsets 0..9) and the feature_id at offset 13.
290
+ * Deliberately does NOT touch the warm-start impulse fields (offsets
291
+ * 10..12) — the narrowphase match-and-merge pass uses this to refill
292
+ * a slot while preserving the cached impulses that the solver's
293
+ * warm-start needs.
294
+ *
295
+ * @param {number} slot
296
+ * @param {number} idx contact index in `[0, MAX_CONTACTS_PER_MANIFOLD)`
297
+ * @param {number} lax
298
+ * @param {number} lay
299
+ * @param {number} laz
300
+ * @param {number} lbx
301
+ * @param {number} lby
302
+ * @param {number} lbz
303
+ * @param {number} nx normal world x (B toward A)
304
+ * @param {number} ny
305
+ * @param {number} nz
306
+ * @param {number} depth
307
+ * @param {number} feature_id stable cross-frame identifier of the
308
+ * geometric feature pair that produced this contact; 0 means
309
+ * "no info, fall back to position matching"
310
+ */
311
+ set_contact(slot, idx, lax, lay, laz, lbx, lby, lbz, nx, ny, nz, depth, feature_id = 0) {
312
+ const off = slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE;
313
+ this.__data[off] = lax;
314
+ this.__data[off + 1] = lay;
315
+ this.__data[off + 2] = laz;
316
+ this.__data[off + 3] = lbx;
317
+ this.__data[off + 4] = lby;
318
+ this.__data[off + 5] = lbz;
319
+ this.__data[off + 6] = nx;
320
+ this.__data[off + 7] = ny;
321
+ this.__data[off + 8] = nz;
322
+ this.__data[off + 9] = depth;
323
+ // j_n, j_t1, j_t2 (offsets 10..12) are warm-start; preserved across calls.
324
+ this.__data[off + 13] = feature_id;
325
+
326
+ const meta_off = slot * SLOT_META_STRIDE;
327
+ const count_now = this.__meta[meta_off + 2] & COUNT_MASK;
328
+ if (idx + 1 > count_now) {
329
+ this.__meta[meta_off + 2] = (this.__meta[meta_off + 2] & ~COUNT_MASK) | (idx + 1);
330
+ }
331
+ }
332
+
333
+ /**
334
+ * Zero the warm-start impulse fields (j_n, j_t1, j_t2) for one contact
335
+ * index inside a slot. Called by the narrowphase match-and-merge pass
336
+ * after writing a candidate that did NOT match any prior-frame contact:
337
+ * the geometry at this index is "new" and inheriting impulses from
338
+ * whatever happened to be cached at this index last frame would
339
+ * destabilise the solver.
340
+ *
341
+ * Distinct from {@link clear_contacts} (which wipes the entire slot's
342
+ * data slab including geometry) `clear_impulses` only touches the
343
+ * three impulse fields at one contact index, leaving the geometric
344
+ * fields and feature_id intact.
345
+ *
346
+ * @param {number} slot
347
+ * @param {number} idx
348
+ */
349
+ clear_impulses(slot, idx) {
350
+ const off = slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE;
351
+ this.__data[off + 10] = 0;
352
+ this.__data[off + 11] = 0;
353
+ this.__data[off + 12] = 0;
354
+ }
355
+
356
+ /**
357
+ * Read the feature_id stored at one contact within a slot.
358
+ * @param {number} slot
359
+ * @param {number} idx
360
+ * @returns {number}
361
+ */
362
+ feature_id_of(slot, idx) {
363
+ return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 13];
364
+ }
365
+
366
+ /**
367
+ * Read a contact point. Writes 10 floats: lax, lay, laz, lbx, lby, lbz, nx, ny, nz, depth.
368
+ * @param {number} slot
369
+ * @param {number} idx
370
+ * @param {number[]|Float64Array} out
371
+ * @param {number} out_offset
372
+ */
373
+ get_contact(slot, idx, out, out_offset) {
374
+ const off = slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE;
375
+ for (let i = 0; i < 10; i++) {
376
+ out[out_offset + i] = this.__data[off + i];
377
+ }
378
+ }
379
+
380
+ /**
381
+ * Get the contact-normal world x component for a given contact.
382
+ * Provided for testability without exposing the raw Float64Array view.
383
+ * @param {number} slot
384
+ * @param {number} idx
385
+ * @returns {number}
386
+ */
387
+ contact_normal_x(slot, idx) {
388
+ return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 6];
389
+ }
390
+
391
+ /** @param {number} slot @param {number} idx @returns {number} */
392
+ contact_normal_y(slot, idx) {
393
+ return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 7];
394
+ }
395
+
396
+ /** @param {number} slot @param {number} idx @returns {number} */
397
+ contact_normal_z(slot, idx) {
398
+ return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 8];
399
+ }
400
+
401
+ /** @param {number} slot @param {number} idx @returns {number} */
402
+ contact_depth(slot, idx) {
403
+ return this.__data[slot * SLOT_DATA_STRIDE + idx * CONTACT_STRIDE + 9];
404
+ }
405
+
406
+ /**
407
+ * Raw contact-point buffer. Hot-path solver code dereferences this
408
+ * directly via {@link slot_data_offset} + {@link CONTACT_STRIDE} to avoid
409
+ * per-field accessor overhead. Buffer identity is stable until grow.
410
+ * @returns {Float64Array}
411
+ */
412
+ get data_buffer() {
413
+ return this.__data;
414
+ }
415
+
416
+ /**
417
+ * Per-slot cached GJK separating axis buffer. Hot-path GJK callers
418
+ * pass this together with {@link slot_axis_offset} to
419
+ * `gjk_with_axis(...)` so the cached axis seeds the next iteration.
420
+ * Buffer identity is stable until grow.
421
+ * @returns {Float64Array}
422
+ */
423
+ get slot_axis_buffer() {
424
+ return this.__slot_axis;
425
+ }
426
+
427
+ /**
428
+ * Float offset into {@link slot_axis_buffer} where this slot's
429
+ * cached axis lives (3 floats: x, y, z).
430
+ * @param {number} slot
431
+ * @returns {number}
432
+ */
433
+ slot_axis_offset(slot) {
434
+ return slot * 3;
435
+ }
436
+
437
+ /**
438
+ * Word offset into {@link data_buffer} where the first contact of `slot`
439
+ * begins. Subsequent contacts within the slot are at
440
+ * `offset + c * CONTACT_STRIDE`.
441
+ * @param {number} slot
442
+ * @returns {number}
443
+ */
444
+ slot_data_offset(slot) {
445
+ return slot * SLOT_DATA_STRIDE;
446
+ }
447
+
448
+ /**
449
+ * Advance touched flags by one frame and evict slots that have been
450
+ * untouched for {@link DEFAULT_GRACE} consecutive frames.
451
+ */
452
+ advance_frame() {
453
+ // Iterate live slots backwards so swap-with-last during release doesn't
454
+ // skip indices.
455
+ for (let i = this.__live_count - 1; i >= 0; i--) {
456
+ const slot = this.__live_slots[i];
457
+ const meta_off = slot * SLOT_META_STRIDE;
458
+ let packed = this.__meta[meta_off + 2];
459
+
460
+ const was_touched = (packed & FLAG_TOUCHED) !== 0;
461
+ // Roll touched → prev_touched, clear touched.
462
+ if (was_touched) {
463
+ packed = (packed & ~FLAG_PREV_TOUCHED) | FLAG_PREV_TOUCHED;
464
+ packed &= ~FLAG_TOUCHED;
465
+ // Reset grace.
466
+ packed &= ~(GRACE_MASK << GRACE_SHIFT);
467
+ this.__meta[meta_off + 2] = packed;
468
+ } else {
469
+ // Bump grace.
470
+ const grace = ((packed >>> GRACE_SHIFT) & GRACE_MASK) + 1;
471
+ packed &= ~FLAG_PREV_TOUCHED;
472
+ packed &= ~(GRACE_MASK << GRACE_SHIFT);
473
+ packed |= (grace & GRACE_MASK) << GRACE_SHIFT;
474
+ this.__meta[meta_off + 2] = packed;
475
+
476
+ if (grace >= DEFAULT_GRACE) {
477
+ this.__release_slot(slot);
478
+ }
479
+ }
480
+ }
481
+ }
482
+
483
+ /**
484
+ * @private
485
+ * @returns {number} newly-allocated slot id
486
+ */
487
+ __allocate_slot() {
488
+ let slot;
489
+ if (this.__free_count > 0) {
490
+ slot = this.__heap_pop();
491
+ } else {
492
+ if (this.__high_water === this.__capacity) {
493
+ this.__grow();
494
+ }
495
+ slot = this.__high_water++;
496
+ }
497
+ const pos = this.__live_count++;
498
+ this.__live_slots[pos] = slot;
499
+ this.__live_pos[slot] = pos;
500
+ return slot;
501
+ }
502
+
503
+ /**
504
+ * @private
505
+ * @param {number} slot
506
+ */
507
+ __release_slot(slot) {
508
+ const meta_off = slot * SLOT_META_STRIDE;
509
+ const idA = this.__meta[meta_off];
510
+ const idB = this.__meta[meta_off + 1];
511
+
512
+ this.__pair_index.delete(idA, idB);
513
+
514
+ // Remove from dense live list (swap with last).
515
+ const pos = this.__live_pos[slot];
516
+ const last_pos = --this.__live_count;
517
+ if (pos !== last_pos) {
518
+ const swapped = this.__live_slots[last_pos];
519
+ this.__live_slots[pos] = swapped;
520
+ this.__live_pos[swapped] = pos;
521
+ }
522
+ this.__live_pos[slot] = -1;
523
+
524
+ this.__heap_push(slot);
525
+ }
526
+
527
+ /**
528
+ * @private
529
+ */
530
+ __grow() {
531
+ const new_capacity = this.__capacity << 1;
532
+ this.__capacity = new_capacity;
533
+
534
+ const grow_f64 = (a, stride) => {
535
+ const n = new Float64Array(new_capacity * stride);
536
+ n.set(a);
537
+ return n;
538
+ };
539
+ const grow_u32 = (a, stride) => {
540
+ const n = new Uint32Array(new_capacity * stride);
541
+ n.set(a);
542
+ return n;
543
+ };
544
+
545
+ this.__data = grow_f64(this.__data, SLOT_DATA_STRIDE);
546
+ this.__meta = grow_u32(this.__meta, SLOT_META_STRIDE);
547
+ this.__slot_axis = grow_f64(this.__slot_axis, 3);
548
+
549
+ const next_live = new Uint32Array(new_capacity);
550
+ next_live.set(this.__live_slots);
551
+ this.__live_slots = next_live;
552
+
553
+ const next_live_pos = new Int32Array(new_capacity);
554
+ next_live_pos.set(this.__live_pos);
555
+ next_live_pos.fill(-1, this.__live_pos.length);
556
+ this.__live_pos = next_live_pos;
557
+
558
+ const next_free = new Uint32Array(new_capacity);
559
+ next_free.set(this.__free_heap);
560
+ this.__free_heap = next_free;
561
+ }
562
+
563
+ /**
564
+ * @private
565
+ * @param {number} v
566
+ */
567
+ __heap_push(v) {
568
+ const heap = this.__free_heap;
569
+ let i = this.__free_count++;
570
+ heap[i] = v;
571
+ while (i > 0) {
572
+ const parent = (i - 1) >>> 1;
573
+ if (heap[parent] > heap[i]) {
574
+ const tmp = heap[parent];
575
+ heap[parent] = heap[i];
576
+ heap[i] = tmp;
577
+ i = parent;
578
+ } else break;
579
+ }
580
+ }
581
+
582
+ /**
583
+ * @private
584
+ * @returns {number}
585
+ */
586
+ __heap_pop() {
587
+ const heap = this.__free_heap;
588
+ const result = heap[0];
589
+ const new_count = --this.__free_count;
590
+ if (new_count > 0) {
591
+ heap[0] = heap[new_count];
592
+ let i = 0;
593
+ while (true) {
594
+ const l = (i << 1) + 1;
595
+ const r = l + 1;
596
+ let smallest = i;
597
+ if (l < new_count && heap[l] < heap[smallest]) smallest = l;
598
+ if (r < new_count && heap[r] < heap[smallest]) smallest = r;
599
+ if (smallest === i) break;
600
+ const tmp = heap[i];
601
+ heap[i] = heap[smallest];
602
+ heap[smallest] = tmp;
603
+ i = smallest;
604
+ }
605
+ }
606
+ return result;
607
+ }
608
+ }