@woosh/meep-engine 2.153.0 → 2.155.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 (107) hide show
  1. package/package.json +1 -1
  2. package/src/core/geom/3d/shape/ConvexHullShape3D.d.ts +112 -0
  3. package/src/core/geom/3d/shape/ConvexHullShape3D.d.ts.map +1 -0
  4. package/src/core/geom/3d/shape/ConvexHullShape3D.js +325 -0
  5. package/src/core/geom/vec3/v3_array_copy.d.ts +3 -3
  6. package/src/core/geom/vec3/v3_array_copy.d.ts.map +1 -1
  7. package/src/core/geom/vec3/v3_array_copy.js +2 -2
  8. package/src/core/geom/vec3/v3_cross.d.ts +17 -0
  9. package/src/core/geom/vec3/v3_cross.d.ts.map +1 -0
  10. package/src/core/geom/vec3/v3_cross.js +20 -0
  11. package/src/core/geom/vec3/v3_subtract.d.ts +16 -0
  12. package/src/core/geom/vec3/v3_subtract.d.ts.map +1 -0
  13. package/src/core/geom/vec3/v3_subtract.js +19 -0
  14. package/src/engine/graphics/ecs/decal/v2/FPDecalSystem.d.ts.map +1 -1
  15. package/src/engine/graphics/ecs/decal/v2/FPDecalSystem.js +8 -0
  16. package/src/engine/graphics/ecs/trail2d/Trail2D.d.ts +4 -0
  17. package/src/engine/graphics/ecs/trail2d/Trail2D.d.ts.map +1 -1
  18. package/src/engine/graphics/ecs/trail2d/Trail2D.js +21 -0
  19. package/src/engine/physics/PLAN.md +4 -4
  20. package/src/engine/physics/body/BodyStorage.d.ts +3 -1
  21. package/src/engine/physics/body/BodyStorage.d.ts.map +1 -1
  22. package/src/engine/physics/body/BodyStorage.js +452 -450
  23. package/src/engine/physics/body/SolverBodyState.d.ts.map +1 -1
  24. package/src/engine/physics/body/SolverBodyState.js +6 -5
  25. package/src/engine/physics/broadphase/generate_pairs.d.ts.map +1 -1
  26. package/src/engine/physics/broadphase/generate_pairs.js +9 -1
  27. package/src/engine/physics/ccd/linear_sweep.d.ts.map +1 -1
  28. package/src/engine/physics/ccd/linear_sweep.js +237 -238
  29. package/src/engine/physics/computeInterceptPoint.d.ts.map +1 -1
  30. package/src/engine/physics/computeInterceptPoint.js +8 -3
  31. package/src/engine/physics/contact/ManifoldStore.d.ts +0 -16
  32. package/src/engine/physics/contact/ManifoldStore.d.ts.map +1 -1
  33. package/src/engine/physics/contact/ManifoldStore.js +1 -38
  34. package/src/engine/physics/ecs/BodyKind.d.ts +3 -2
  35. package/src/engine/physics/ecs/BodyKind.d.ts.map +1 -1
  36. package/src/engine/physics/ecs/BodyKind.js +25 -24
  37. package/src/engine/physics/ecs/PhysicsEvents.d.ts +4 -5
  38. package/src/engine/physics/ecs/PhysicsEvents.d.ts.map +1 -1
  39. package/src/engine/physics/ecs/PhysicsEvents.js +15 -16
  40. package/src/engine/physics/ecs/PhysicsSystem.d.ts +5 -30
  41. package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
  42. package/src/engine/physics/ecs/PhysicsSystem.js +13 -45
  43. package/src/engine/physics/ecs/RigidBodySerializationAdapter.d.ts.map +1 -1
  44. package/src/engine/physics/ecs/RigidBodySerializationAdapter.js +85 -81
  45. package/src/engine/physics/ecs/is_sensor.d.ts +18 -0
  46. package/src/engine/physics/ecs/is_sensor.d.ts.map +1 -0
  47. package/src/engine/physics/ecs/is_sensor.js +27 -0
  48. package/src/engine/physics/events/ContactEventBuffer.d.ts +2 -1
  49. package/src/engine/physics/events/ContactEventBuffer.d.ts.map +1 -1
  50. package/src/engine/physics/events/ContactEventBuffer.js +84 -83
  51. package/src/engine/physics/gjk/gjk.d.ts +0 -26
  52. package/src/engine/physics/gjk/gjk.d.ts.map +1 -1
  53. package/src/engine/physics/gjk/gjk.js +3 -52
  54. package/src/engine/physics/gjk/gjk_epa_penetration.d.ts +20 -0
  55. package/src/engine/physics/gjk/gjk_epa_penetration.d.ts.map +1 -0
  56. package/src/engine/physics/gjk/gjk_epa_penetration.js +548 -0
  57. package/src/engine/physics/gjk/minkowski_support.d.ts +4 -9
  58. package/src/engine/physics/gjk/minkowski_support.d.ts.map +1 -1
  59. package/src/engine/physics/gjk/minkowski_support.js +70 -75
  60. package/src/engine/physics/gjk/mpr.d.ts +1 -1
  61. package/src/engine/physics/gjk/mpr.d.ts.map +1 -1
  62. package/src/engine/physics/gjk/mpr.js +362 -344
  63. package/src/engine/physics/island/IslandBuilder.d.ts.map +1 -1
  64. package/src/engine/physics/island/IslandBuilder.js +431 -428
  65. package/src/engine/physics/narrowphase/box_box_manifold.d.ts.map +1 -1
  66. package/src/engine/physics/narrowphase/box_box_manifold.js +4 -81
  67. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts.map +1 -1
  68. package/src/engine/physics/narrowphase/box_triangle_contact.js +4 -39
  69. package/src/engine/physics/narrowphase/capsule_contacts.d.ts.map +1 -1
  70. package/src/engine/physics/narrowphase/capsule_contacts.js +459 -462
  71. package/src/engine/physics/narrowphase/clip_against_axis_uv.d.ts.map +1 -1
  72. package/src/engine/physics/narrowphase/clip_against_axis_uv.js +4 -1
  73. package/src/engine/physics/narrowphase/convex_convex_manifold.d.ts +83 -0
  74. package/src/engine/physics/narrowphase/convex_convex_manifold.d.ts.map +1 -0
  75. package/src/engine/physics/narrowphase/convex_convex_manifold.js +425 -0
  76. package/src/engine/physics/narrowphase/convex_decomposition.d.ts +32 -0
  77. package/src/engine/physics/narrowphase/convex_decomposition.d.ts.map +1 -0
  78. package/src/engine/physics/narrowphase/convex_decomposition.js +293 -0
  79. package/src/engine/physics/narrowphase/mesh_convex_hull.d.ts +41 -0
  80. package/src/engine/physics/narrowphase/mesh_convex_hull.d.ts.map +1 -0
  81. package/src/engine/physics/narrowphase/mesh_convex_hull.js +106 -0
  82. package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.d.ts +8 -0
  83. package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.d.ts.map +1 -0
  84. package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.js +117 -0
  85. package/src/engine/physics/narrowphase/narrowphase_step.d.ts.map +1 -1
  86. package/src/engine/physics/narrowphase/narrowphase_step.js +105 -102
  87. package/src/engine/physics/narrowphase/reduce_manifold_contacts.d.ts +29 -0
  88. package/src/engine/physics/narrowphase/reduce_manifold_contacts.d.ts.map +1 -0
  89. package/src/engine/physics/narrowphase/reduce_manifold_contacts.js +69 -0
  90. package/src/engine/physics/narrowphase/refine_ray_concave.d.ts.map +1 -1
  91. package/src/engine/physics/narrowphase/refine_ray_concave.js +152 -145
  92. package/src/engine/physics/narrowphase/sphere_box_contact.d.ts.map +1 -1
  93. package/src/engine/physics/narrowphase/sphere_box_contact.js +132 -123
  94. package/src/engine/physics/queries/overlap_shape.d.ts.map +1 -1
  95. package/src/engine/physics/queries/overlap_shape.js +16 -17
  96. package/src/engine/physics/queries/raycast.d.ts +5 -0
  97. package/src/engine/physics/queries/raycast.d.ts.map +1 -1
  98. package/src/engine/physics/queries/raycast.js +16 -8
  99. package/src/engine/physics/queries/shape_cast.d.ts.map +1 -1
  100. package/src/engine/physics/queries/shape_cast.js +13 -7
  101. package/src/engine/physics/solver/solve_contacts.d.ts.map +1 -1
  102. package/src/engine/physics/solver/solve_contacts.js +8 -11
  103. package/src/engine/physics/vehicle/RaycastVehicle.d.ts.map +1 -1
  104. package/src/engine/physics/vehicle/RaycastVehicle.js +339 -333
  105. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts +0 -13
  106. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts.map +0 -1
  107. package/src/engine/physics/gjk/expanding_polytope_algorithm.js +0 -399
@@ -1,462 +1,459 @@
1
- import { line3_closest_points_segment_segment } from "../../../core/geom/3d/line/line3_closest_points_segment_segment.js";
2
- import { line3_compute_segment_nearest_point_to_point_t } from "../../../core/geom/3d/line/line3_compute_segment_nearest_point_to_point_t.js";
3
- import { v3_quat3_apply } from "../../../core/geom/vec3/v3_quat3_apply.js";
4
- import { v3_quat3_apply_inverse } from "../../../core/geom/vec3/v3_quat3_apply_inverse.js";
5
- import { sphere_box_contact } from "./sphere_box_contact.js";
6
-
7
- /**
8
- * Closed-form narrowphase helpers for the capsule family. A capsule is the
9
- * Minkowski sum of a line segment and a sphere of radius `r`, so contact
10
- * resolution reduces to:
11
- * - capsule-sphere → point-on-segment distance, then sphere-style normal
12
- * - capsule-capsule → segment-segment closest pair
13
- * - capsule-box → segment-vs-OBB closest point (iterative)
14
- *
15
- * Output convention (matches the rest of the narrowphase family):
16
- * out[0..2] : normal pointing from B toward A
17
- * out[3] : penetration depth (positive)
18
- * out[4..6] : world contact on A's surface
19
- * out[7..9] : world contact on B's surface
20
- *
21
- * @author Alex Goldring
22
- * @copyright Company Named Limited (c) 2026
23
- */
24
-
25
- const scratch_st = new Float64Array(2);
26
-
27
- const scratch_seg = new Float64Array(6);
28
-
29
- /**
30
- * Compute the two world endpoints of a capsule's central segment given its
31
- * pose. The capsule is Y-aligned in body frame; the segment runs from
32
- * `(0, -h/2, 0)` to `(0, +h/2, 0)`. Writes endpoints into `out` as
33
- * `[ax, ay, az, bx, by, bz]`.
34
- *
35
- * @param {number[]|Float64Array} out length >= 6
36
- * @param {number} cx capsule centre x
37
- * @param {number} cy
38
- * @param {number} cz
39
- * @param {number} qx quaternion x
40
- * @param {number} qy
41
- * @param {number} qz
42
- * @param {number} qw
43
- * @param {number} half_height
44
- */
45
- export function capsule_world_segment(out, cx, cy, cz, qx, qy, qz, qw, half_height) {
46
- v3_quat3_apply(out, 0, 0, -half_height, 0, qx, qy, qz, qw);
47
- v3_quat3_apply(out, 3, 0, half_height, 0, qx, qy, qz, qw);
48
- out[0] += cx; out[1] += cy; out[2] += cz;
49
- out[3] += cx; out[4] += cy; out[5] += cz;
50
- }
51
-
52
- // --- capsule vs sphere ------------------------------------------------------
53
-
54
- /**
55
- * Capsule (A) vs sphere (B) contact, closed-form via point-segment distance.
56
- *
57
- * @param {number[]|Float64Array} out length >= 10
58
- * @param {number} a_cx capsule centre x
59
- * @param {number} a_cy
60
- * @param {number} a_cz
61
- * @param {number} a_qx capsule rotation
62
- * @param {number} a_qy
63
- * @param {number} a_qz
64
- * @param {number} a_qw
65
- * @param {number} a_radius
66
- * @param {number} a_half_h capsule half-height (not including caps)
67
- * @param {number} b_cx sphere centre x
68
- * @param {number} b_cy
69
- * @param {number} b_cz
70
- * @param {number} b_radius
71
- * @returns {boolean} true on overlap
72
- */
73
- export function capsule_sphere_contact(
74
- out,
75
- a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
76
- b_cx, b_cy, b_cz, b_radius
77
- ) {
78
- capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
79
- const sax = scratch_seg[0], say = scratch_seg[1], saz = scratch_seg[2];
80
- const sbx = scratch_seg[3], sby = scratch_seg[4], sbz = scratch_seg[5];
81
-
82
- const t = line3_compute_segment_nearest_point_to_point_t(sax, say, saz, sbx, sby, sbz, b_cx, b_cy, b_cz);
83
-
84
- const px = sax + (sbx - sax) * t;
85
- const py = say + (sby - say) * t;
86
- const pz = saz + (sbz - saz) * t;
87
-
88
- const dx = px - b_cx;
89
- const dy = py - b_cy;
90
- const dz = pz - b_cz;
91
- const dist_sqr = dx * dx + dy * dy + dz * dz;
92
- const sum = a_radius + b_radius;
93
- if (dist_sqr >= sum * sum) return false;
94
-
95
- const dist = Math.sqrt(dist_sqr);
96
- let nx, ny, nz;
97
- if (dist > 0) {
98
- const inv = 1 / dist;
99
- nx = dx * inv; ny = dy * inv; nz = dz * inv;
100
- } else {
101
- // Sphere centre exactly on the capsule segment — pick a deterministic tie-break.
102
- nx = 1; ny = 0; nz = 0;
103
- }
104
-
105
- out[0] = nx; out[1] = ny; out[2] = nz; // normal from B to A
106
- out[3] = sum - dist;
107
- // World contact on A (capsule): segment point pushed back by capsule radius along -normal
108
- out[4] = px - nx * a_radius;
109
- out[5] = py - ny * a_radius;
110
- out[6] = pz - nz * a_radius;
111
- // World contact on B (sphere): sphere centre pushed along +normal by sphere radius
112
- out[7] = b_cx + nx * b_radius;
113
- out[8] = b_cy + ny * b_radius;
114
- out[9] = b_cz + nz * b_radius;
115
- return true;
116
- }
117
-
118
- // --- capsule vs capsule -----------------------------------------------------
119
-
120
- const scratch_seg_b = new Float64Array(6);
121
-
122
- /**
123
- * Two capsules (A and B). Reduces to segment-segment closest pair + radius.
124
- *
125
- * @param {number[]|Float64Array} out length >= 10
126
- * @returns {boolean} true on overlap
127
- */
128
- export function capsule_capsule_contact(
129
- out,
130
- a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
131
- b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_radius, b_half_h
132
- ) {
133
- capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
134
- capsule_world_segment(scratch_seg_b, b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_half_h);
135
-
136
- const a0x = scratch_seg[0], a0y = scratch_seg[1], a0z = scratch_seg[2];
137
- const a1x = scratch_seg[3], a1y = scratch_seg[4], a1z = scratch_seg[5];
138
- const b0x = scratch_seg_b[0], b0y = scratch_seg_b[1], b0z = scratch_seg_b[2];
139
- const b1x = scratch_seg_b[3], b1y = scratch_seg_b[4], b1z = scratch_seg_b[5];
140
-
141
- line3_closest_points_segment_segment(
142
- scratch_st,
143
- a0x, a0y, a0z, a1x, a1y, a1z,
144
- b0x, b0y, b0z, b1x, b1y, b1z
145
- );
146
-
147
- const s = scratch_st[0];
148
- const t = scratch_st[1];
149
-
150
- const pax = a0x + (a1x - a0x) * s;
151
- const pay = a0y + (a1y - a0y) * s;
152
- const paz = a0z + (a1z - a0z) * s;
153
- const pbx = b0x + (b1x - b0x) * t;
154
- const pby = b0y + (b1y - b0y) * t;
155
- const pbz = b0z + (b1z - b0z) * t;
156
-
157
- const dx = pax - pbx;
158
- const dy = pay - pby;
159
- const dz = paz - pbz;
160
- const dist_sqr = dx * dx + dy * dy + dz * dz;
161
- const sum = a_radius + b_radius;
162
- if (dist_sqr >= sum * sum) return false;
163
-
164
- const dist = Math.sqrt(dist_sqr);
165
- let nx, ny, nz;
166
- if (dist > 0) {
167
- const inv = 1 / dist;
168
- nx = dx * inv; ny = dy * inv; nz = dz * inv;
169
- } else {
170
- // Segments touch — fall back to the world-frame direction from B's
171
- // segment centre toward A's segment centre as a tie-break.
172
- const acx = (a0x + a1x) * 0.5;
173
- const acy = (a0y + a1y) * 0.5;
174
- const acz = (a0z + a1z) * 0.5;
175
- const bcx = (b0x + b1x) * 0.5;
176
- const bcy = (b0y + b1y) * 0.5;
177
- const bcz = (b0z + b1z) * 0.5;
178
- const ddx = acx - bcx, ddy = acy - bcy, ddz = acz - bcz;
179
- const dd_sqr = ddx * ddx + ddy * ddy + ddz * ddz;
180
- if (dd_sqr > 0) {
181
- const inv2 = 1 / Math.sqrt(dd_sqr);
182
- nx = ddx * inv2; ny = ddy * inv2; nz = ddz * inv2;
183
- } else {
184
- nx = 1; ny = 0; nz = 0;
185
- }
186
- }
187
-
188
- out[0] = nx; out[1] = ny; out[2] = nz;
189
- out[3] = sum - dist;
190
- out[4] = pax - nx * a_radius;
191
- out[5] = pay - ny * a_radius;
192
- out[6] = paz - nz * a_radius;
193
- out[7] = pbx + nx * b_radius;
194
- out[8] = pby + ny * b_radius;
195
- out[9] = pbz + nz * b_radius;
196
- return true;
197
- }
198
-
199
- // --- capsule vs oriented box ------------------------------------------------
200
-
201
- const scratch_a0_local = new Float64Array(3);
202
- const scratch_a1_local = new Float64Array(3);
203
-
204
- /**
205
- * Capsule (A) vs oriented box (B). Brings the capsule segment into box-local
206
- * space, then refines via 2 fixed-point iterations between:
207
- * - closest point on the segment to the current box-side candidate
208
- * - clamped point on the box closest to the current segment-side candidate
209
- *
210
- * Two iterations suffice for the cases this engine targets (sphere-like cap
211
- * geometry against axis-aligned-in-local box). When the segment passes
212
- * through the box, the "inside" branch picks the smallest-overlap face
213
- * deterministically, mirroring {@link sphere_box_contact}.
214
- *
215
- * @param {number[]|Float64Array} out length >= 10
216
- * @returns {boolean}
217
- */
218
- export function capsule_box_contact(
219
- out,
220
- a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
221
- b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
222
- ) {
223
- // Bring the capsule's segment endpoints into box-local space.
224
- v3_quat3_apply_inverse(scratch_a0_local, 0, -0, -a_half_h, 0, a_qx, a_qy, a_qz, a_qw);
225
- // Note: we constructed the segment ends in body frame as (0, ±h, 0). We
226
- // need them in world first, then box-local. Build world here directly
227
- // for clarity.
228
- capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
229
- // World box-local: subtract box centre, then rotate by conjugate of box quat.
230
- const w_a0x = scratch_seg[0] - b_cx, w_a0y = scratch_seg[1] - b_cy, w_a0z = scratch_seg[2] - b_cz;
231
- const w_a1x = scratch_seg[3] - b_cx, w_a1y = scratch_seg[4] - b_cy, w_a1z = scratch_seg[5] - b_cz;
232
- v3_quat3_apply_inverse(scratch_a0_local, 0, w_a0x, w_a0y, w_a0z, b_qx, b_qy, b_qz, b_qw);
233
- v3_quat3_apply_inverse(scratch_a1_local, 0, w_a1x, w_a1y, w_a1z, b_qx, b_qy, b_qz, b_qw);
234
-
235
- const a0lx = scratch_a0_local[0], a0ly = scratch_a0_local[1], a0lz = scratch_a0_local[2];
236
- const a1lx = scratch_a1_local[0], a1ly = scratch_a1_local[1], a1lz = scratch_a1_local[2];
237
-
238
- // Fixed-point iteration: start with t at the closest point to the box centre.
239
- let t = line3_compute_segment_nearest_point_to_point_t(a0lx, a0ly, a0lz, a1lx, a1ly, a1lz, 0, 0, 0);
240
- let plx = a0lx + (a1lx - a0lx) * t;
241
- let ply = a0ly + (a1ly - a0ly) * t;
242
- let plz = a0lz + (a1lz - a0lz) * t;
243
-
244
- for (let iter = 0; iter < 2; iter++) {
245
- // Clamp current segment point to the box closest box-surface point.
246
- const qlx = plx < -b_hx ? -b_hx : (plx > b_hx ? b_hx : plx);
247
- const qly = ply < -b_hy ? -b_hy : (ply > b_hy ? b_hy : ply);
248
- const qlz = plz < -b_hz ? -b_hz : (plz > b_hz ? b_hz : plz);
249
-
250
- // Re-find closest segment point to that box point.
251
- t = line3_compute_segment_nearest_point_to_point_t(
252
- a0lx, a0ly, a0lz, a1lx, a1ly, a1lz,
253
- qlx, qly, qlz
254
- );
255
- plx = a0lx + (a1lx - a0lx) * t;
256
- ply = a0ly + (a1ly - a0ly) * t;
257
- plz = a0lz + (a1lz - a0lz) * t;
258
- }
259
-
260
- // Final box-side point.
261
- const qlx = plx < -b_hx ? -b_hx : (plx > b_hx ? b_hx : plx);
262
- const qly = ply < -b_hy ? -b_hy : (ply > b_hy ? b_hy : ply);
263
- const qlz = plz < -b_hz ? -b_hz : (plz > b_hz ? b_hz : plz);
264
- const inside = qlx === plx && qly === ply && qlz === plz;
265
-
266
- let nlx, nly, nlz, dist;
267
- if (!inside) {
268
- const ex = plx - qlx, ey = ply - qly, ez = plz - qlz;
269
- const d_sqr = ex * ex + ey * ey + ez * ez;
270
- if (d_sqr >= a_radius * a_radius) return false;
271
- dist = Math.sqrt(d_sqr);
272
- if (dist > 0) {
273
- const inv = 1 / dist;
274
- nlx = ex * inv; nly = ey * inv; nlz = ez * inv;
275
- } else {
276
- nlx = 1; nly = 0; nlz = 0;
277
- }
278
- } else {
279
- // Segment point is inside the box. Pick smallest-overlap face.
280
- const dxf = b_hx - Math.abs(plx);
281
- const dyf = b_hy - Math.abs(ply);
282
- const dzf = b_hz - Math.abs(plz);
283
- if (dxf <= dyf && dxf <= dzf) {
284
- nlx = plx >= 0 ? 1 : -1; nly = 0; nlz = 0; dist = -dxf;
285
- } else if (dyf <= dzf) {
286
- nlx = 0; nly = ply >= 0 ? 1 : -1; nlz = 0; dist = -dyf;
287
- } else {
288
- nlx = 0; nly = 0; nlz = plz >= 0 ? 1 : -1; dist = -dzf;
289
- }
290
- }
291
-
292
- // Normal (box capsule axis point) into world.
293
- v3_quat3_apply(out, 0, nlx, nly, nlz, b_qx, b_qy, b_qz, b_qw);
294
- const nx = out[0], ny = out[1], nz = out[2];
295
- out[3] = a_radius - dist;
296
-
297
- // Capsule-side contact: world segment point pushed back by radius.
298
- v3_quat3_apply(out, 4, plx, ply, plz, b_qx, b_qy, b_qz, b_qw);
299
- out[4] += b_cx - nx * a_radius;
300
- out[5] += b_cy - ny * a_radius;
301
- out[6] += b_cz - nz * a_radius;
302
-
303
- // Box-side contact: world projection of the box-surface point.
304
- v3_quat3_apply(out, 7, qlx, qly, qlz, b_qx, b_qy, b_qz, b_qw);
305
- out[7] += b_cx;
306
- out[8] += b_cy;
307
- out[9] += b_cz;
308
-
309
- return true;
310
- }
311
-
312
- // --- capsule vs oriented box, multi-point manifold --------------------------
313
-
314
- /**
315
- * Output stride per contact in {@link capsule_box_multi_contacts}.
316
- * Matches the candidate-buffer layout used by `narrowphase_step`.
317
- *
318
- * 0..2 : world contact on capsule side
319
- * 3..5 : world contact on box side
320
- * 6..8 : normal from box (B) toward capsule (A)
321
- * 9 : depth (positive = penetration)
322
- *
323
- * @type {number}
324
- */
325
- export const CAPSULE_BOX_CONTACT_STRIDE = 10;
326
-
327
- /**
328
- * Maximum contact points {@link capsule_box_multi_contacts} can emit.
329
- * - 1 from the closest-point-on-segment vs OBB query (primary)
330
- * - up to 2 from the cap-centre sphere-vs-OBB queries (one per endpoint)
331
- * @type {number}
332
- */
333
- export const CAPSULE_BOX_MAX_CONTACTS = 3;
334
-
335
- const scratch_primary = new Float64Array(10);
336
- const scratch_endpoint = new Float64Array(10);
337
-
338
- /**
339
- * Two world positions are treated as the same contact when their squared
340
- * distance is below this threshold. Picked so that a capsule resting flat
341
- * doesn't double up the primary closest-segment-point contact with whichever
342
- * endpoint it happens to coincide with.
343
- *
344
- * Looser than the manifold's warm-start match tolerance (≈0.02) because we
345
- * specifically want to dedupe contacts at the *same* point; small lateral
346
- * shifts of the capsule produce distinct contacts that should both be kept.
347
- * @type {number}
348
- */
349
- const DEDUPE_DIST_SQR = 1e-6;
350
-
351
- /**
352
- * Multi-point capsule-vs-OBB contact generation. Emits up to
353
- * {@link CAPSULE_BOX_MAX_CONTACTS} contacts into `out` in the
354
- * `(wax, way, waz, wbx, wby, wbz, nx, ny, nz, depth)` stride layout.
355
- *
356
- * The motivation: a capsule lying flat on a face needs more than one
357
- * contact to rest stably a single contact at the segment's closest point
358
- * is positionally ambiguous (every interior segment point is equidistant
359
- * from the face), and the body wobbles around whichever point the
360
- * iterative closest-point solver happened to converge on. Two contacts at
361
- * the cap centres pin the capsule's rotation about its long axis.
362
- *
363
- * Algorithm:
364
- * 1. Closest-point-on-segment vs OBB primary contact (same as
365
- * {@link capsule_box_contact}).
366
- * 2. Treat each cap centre as a sphere of cap-radius and run
367
- * {@link sphere_box_contact} up to two additional contacts.
368
- * 3. Dedupe spatially against the primary so a capsule touching with one
369
- * cap doesn't emit two contacts at the same point.
370
- *
371
- * @param {Float64Array} out length >= CAPSULE_BOX_MAX_CONTACTS * CAPSULE_BOX_CONTACT_STRIDE
372
- * @param {number} a_cx capsule centre x
373
- * @param {number} a_cy
374
- * @param {number} a_cz
375
- * @param {number} a_qx capsule rotation
376
- * @param {number} a_qy
377
- * @param {number} a_qz
378
- * @param {number} a_qw
379
- * @param {number} a_radius
380
- * @param {number} a_half_h
381
- * @param {number} b_cx box centre x
382
- * @param {number} b_cy
383
- * @param {number} b_cz
384
- * @param {number} b_qx
385
- * @param {number} b_qy
386
- * @param {number} b_qz
387
- * @param {number} b_qw
388
- * @param {number} b_hx
389
- * @param {number} b_hy
390
- * @param {number} b_hz
391
- * @returns {number} number of contacts emitted, in `[0, CAPSULE_BOX_MAX_CONTACTS]`
392
- */
393
- export function capsule_box_multi_contacts(
394
- out,
395
- a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
396
- b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
397
- ) {
398
- // 1. Primary contact: closest segment point vs OBB.
399
- const primary_ok = capsule_box_contact(
400
- scratch_primary,
401
- a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
402
- b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
403
- );
404
- if (!primary_ok) return 0;
405
-
406
- // Repack scratch_primary (normal, depth, A, B) into the candidate stride
407
- // (A, B, normal, depth) at out[0..10).
408
- const p_nx = scratch_primary[0], p_ny = scratch_primary[1], p_nz = scratch_primary[2];
409
- const p_depth = scratch_primary[3];
410
- const p_ax = scratch_primary[4], p_ay = scratch_primary[5], p_az = scratch_primary[6];
411
- const p_bx = scratch_primary[7], p_by = scratch_primary[8], p_bz = scratch_primary[9];
412
- out[0] = p_ax; out[1] = p_ay; out[2] = p_az;
413
- out[3] = p_bx; out[4] = p_by; out[5] = p_bz;
414
- out[6] = p_nx; out[7] = p_ny; out[8] = p_nz;
415
- out[9] = p_depth;
416
- let count = 1;
417
-
418
- // 2. Endpoint contacts: treat each cap centre as a sphere of cap-radius.
419
- capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
420
- const ep_x = [scratch_seg[0], scratch_seg[3]];
421
- const ep_y = [scratch_seg[1], scratch_seg[4]];
422
- const ep_z = [scratch_seg[2], scratch_seg[5]];
423
-
424
- for (let i = 0; i < 2; i++) {
425
- const ok = sphere_box_contact(
426
- scratch_endpoint,
427
- ep_x[i], ep_y[i], ep_z[i], a_radius,
428
- b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw,
429
- b_hx, b_hy, b_hz
430
- );
431
- if (!ok) continue;
432
-
433
- const e_nx = scratch_endpoint[0], e_ny = scratch_endpoint[1], e_nz = scratch_endpoint[2];
434
- const e_depth = scratch_endpoint[3];
435
- const e_ax = scratch_endpoint[4], e_ay = scratch_endpoint[5], e_az = scratch_endpoint[6];
436
- const e_bx = scratch_endpoint[7], e_by = scratch_endpoint[8], e_bz = scratch_endpoint[9];
437
-
438
- // Dedupe against every already-emitted contact (primary, then any
439
- // prior endpoint we've already accepted). The capsule-side world
440
- // point uniquely identifies a contact for this purpose — two distinct
441
- // box-surface points always have distinct capsule-side projections
442
- // along the local-A axis.
443
- let duplicate = false;
444
- for (let k = 0; k < count; k++) {
445
- const ko = k * CAPSULE_BOX_CONTACT_STRIDE;
446
- const dx = e_ax - out[ko];
447
- const dy = e_ay - out[ko + 1];
448
- const dz = e_az - out[ko + 2];
449
- if (dx * dx + dy * dy + dz * dz < DEDUPE_DIST_SQR) { duplicate = true; break; }
450
- }
451
- if (duplicate) continue;
452
-
453
- const o = count * CAPSULE_BOX_CONTACT_STRIDE;
454
- out[o] = e_ax; out[o + 1] = e_ay; out[o + 2] = e_az;
455
- out[o + 3] = e_bx; out[o + 4] = e_by; out[o + 5] = e_bz;
456
- out[o + 6] = e_nx; out[o + 7] = e_ny; out[o + 8] = e_nz;
457
- out[o + 9] = e_depth;
458
- count++;
459
- }
460
-
461
- return count;
462
- }
1
+ import { line3_closest_points_segment_segment } from "../../../core/geom/3d/line/line3_closest_points_segment_segment.js";
2
+ import { line3_compute_segment_nearest_point_to_point_t } from "../../../core/geom/3d/line/line3_compute_segment_nearest_point_to_point_t.js";
3
+ import { v3_quat3_apply } from "../../../core/geom/vec3/v3_quat3_apply.js";
4
+ import { v3_quat3_apply_inverse } from "../../../core/geom/vec3/v3_quat3_apply_inverse.js";
5
+ import { sphere_box_contact } from "./sphere_box_contact.js";
6
+
7
+ /**
8
+ * Closed-form narrowphase helpers for the capsule family. A capsule is the
9
+ * Minkowski sum of a line segment and a sphere of radius `r`, so contact
10
+ * resolution reduces to:
11
+ * - capsule-sphere → point-on-segment distance, then sphere-style normal
12
+ * - capsule-capsule → segment-segment closest pair
13
+ * - capsule-box → segment-vs-OBB closest point (iterative)
14
+ *
15
+ * Output convention (matches the rest of the narrowphase family):
16
+ * out[0..2] : normal pointing from B toward A
17
+ * out[3] : penetration depth (positive)
18
+ * out[4..6] : world contact on A's surface
19
+ * out[7..9] : world contact on B's surface
20
+ *
21
+ * @author Alex Goldring
22
+ * @copyright Company Named Limited (c) 2026
23
+ */
24
+
25
+ const scratch_st = new Float64Array(2);
26
+
27
+ const scratch_seg = new Float64Array(6);
28
+
29
+ /**
30
+ * Compute the two world endpoints of a capsule's central segment given its
31
+ * pose. The capsule is Y-aligned in body frame; the segment runs from
32
+ * `(0, -h/2, 0)` to `(0, +h/2, 0)`. Writes endpoints into `out` as
33
+ * `[ax, ay, az, bx, by, bz]`.
34
+ *
35
+ * @param {number[]|Float64Array} out length >= 6
36
+ * @param {number} cx capsule centre x
37
+ * @param {number} cy
38
+ * @param {number} cz
39
+ * @param {number} qx quaternion x
40
+ * @param {number} qy
41
+ * @param {number} qz
42
+ * @param {number} qw
43
+ * @param {number} half_height
44
+ */
45
+ export function capsule_world_segment(out, cx, cy, cz, qx, qy, qz, qw, half_height) {
46
+ v3_quat3_apply(out, 0, 0, -half_height, 0, qx, qy, qz, qw);
47
+ v3_quat3_apply(out, 3, 0, half_height, 0, qx, qy, qz, qw);
48
+ out[0] += cx; out[1] += cy; out[2] += cz;
49
+ out[3] += cx; out[4] += cy; out[5] += cz;
50
+ }
51
+
52
+ // --- capsule vs sphere ------------------------------------------------------
53
+
54
+ /**
55
+ * Capsule (A) vs sphere (B) contact, closed-form via point-segment distance.
56
+ *
57
+ * @param {number[]|Float64Array} out length >= 10
58
+ * @param {number} a_cx capsule centre x
59
+ * @param {number} a_cy
60
+ * @param {number} a_cz
61
+ * @param {number} a_qx capsule rotation
62
+ * @param {number} a_qy
63
+ * @param {number} a_qz
64
+ * @param {number} a_qw
65
+ * @param {number} a_radius
66
+ * @param {number} a_half_h capsule half-height (not including caps)
67
+ * @param {number} b_cx sphere centre x
68
+ * @param {number} b_cy
69
+ * @param {number} b_cz
70
+ * @param {number} b_radius
71
+ * @returns {boolean} true on overlap
72
+ */
73
+ export function capsule_sphere_contact(
74
+ out,
75
+ a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
76
+ b_cx, b_cy, b_cz, b_radius
77
+ ) {
78
+ capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
79
+ const sax = scratch_seg[0], say = scratch_seg[1], saz = scratch_seg[2];
80
+ const sbx = scratch_seg[3], sby = scratch_seg[4], sbz = scratch_seg[5];
81
+
82
+ const t = line3_compute_segment_nearest_point_to_point_t(sax, say, saz, sbx, sby, sbz, b_cx, b_cy, b_cz);
83
+
84
+ const px = sax + (sbx - sax) * t;
85
+ const py = say + (sby - say) * t;
86
+ const pz = saz + (sbz - saz) * t;
87
+
88
+ const dx = px - b_cx;
89
+ const dy = py - b_cy;
90
+ const dz = pz - b_cz;
91
+ const dist_sqr = dx * dx + dy * dy + dz * dz;
92
+ const sum = a_radius + b_radius;
93
+ if (dist_sqr >= sum * sum) return false;
94
+
95
+ const dist = Math.sqrt(dist_sqr);
96
+ let nx, ny, nz;
97
+ if (dist > 0) {
98
+ const inv = 1 / dist;
99
+ nx = dx * inv; ny = dy * inv; nz = dz * inv;
100
+ } else {
101
+ // Sphere centre exactly on the capsule segment — pick a deterministic tie-break.
102
+ nx = 1; ny = 0; nz = 0;
103
+ }
104
+
105
+ out[0] = nx; out[1] = ny; out[2] = nz; // normal from B to A
106
+ out[3] = sum - dist;
107
+ // World contact on A (capsule): segment point pushed back by capsule radius along -normal
108
+ out[4] = px - nx * a_radius;
109
+ out[5] = py - ny * a_radius;
110
+ out[6] = pz - nz * a_radius;
111
+ // World contact on B (sphere): sphere centre pushed along +normal by sphere radius
112
+ out[7] = b_cx + nx * b_radius;
113
+ out[8] = b_cy + ny * b_radius;
114
+ out[9] = b_cz + nz * b_radius;
115
+ return true;
116
+ }
117
+
118
+ // --- capsule vs capsule -----------------------------------------------------
119
+
120
+ const scratch_seg_b = new Float64Array(6);
121
+
122
+ /**
123
+ * Two capsules (A and B). Reduces to segment-segment closest pair + radius.
124
+ *
125
+ * @param {number[]|Float64Array} out length >= 10
126
+ * @returns {boolean} true on overlap
127
+ */
128
+ export function capsule_capsule_contact(
129
+ out,
130
+ a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
131
+ b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_radius, b_half_h
132
+ ) {
133
+ capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
134
+ capsule_world_segment(scratch_seg_b, b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_half_h);
135
+
136
+ const a0x = scratch_seg[0], a0y = scratch_seg[1], a0z = scratch_seg[2];
137
+ const a1x = scratch_seg[3], a1y = scratch_seg[4], a1z = scratch_seg[5];
138
+ const b0x = scratch_seg_b[0], b0y = scratch_seg_b[1], b0z = scratch_seg_b[2];
139
+ const b1x = scratch_seg_b[3], b1y = scratch_seg_b[4], b1z = scratch_seg_b[5];
140
+
141
+ line3_closest_points_segment_segment(
142
+ scratch_st,
143
+ a0x, a0y, a0z, a1x, a1y, a1z,
144
+ b0x, b0y, b0z, b1x, b1y, b1z
145
+ );
146
+
147
+ const s = scratch_st[0];
148
+ const t = scratch_st[1];
149
+
150
+ const pax = a0x + (a1x - a0x) * s;
151
+ const pay = a0y + (a1y - a0y) * s;
152
+ const paz = a0z + (a1z - a0z) * s;
153
+ const pbx = b0x + (b1x - b0x) * t;
154
+ const pby = b0y + (b1y - b0y) * t;
155
+ const pbz = b0z + (b1z - b0z) * t;
156
+
157
+ const dx = pax - pbx;
158
+ const dy = pay - pby;
159
+ const dz = paz - pbz;
160
+ const dist_sqr = dx * dx + dy * dy + dz * dz;
161
+ const sum = a_radius + b_radius;
162
+ if (dist_sqr >= sum * sum) return false;
163
+
164
+ const dist = Math.sqrt(dist_sqr);
165
+ let nx, ny, nz;
166
+ if (dist > 0) {
167
+ const inv = 1 / dist;
168
+ nx = dx * inv; ny = dy * inv; nz = dz * inv;
169
+ } else {
170
+ // Segments touch — fall back to the world-frame direction from B's
171
+ // segment centre toward A's segment centre as a tie-break.
172
+ const acx = (a0x + a1x) * 0.5;
173
+ const acy = (a0y + a1y) * 0.5;
174
+ const acz = (a0z + a1z) * 0.5;
175
+ const bcx = (b0x + b1x) * 0.5;
176
+ const bcy = (b0y + b1y) * 0.5;
177
+ const bcz = (b0z + b1z) * 0.5;
178
+ const ddx = acx - bcx, ddy = acy - bcy, ddz = acz - bcz;
179
+ const dd_sqr = ddx * ddx + ddy * ddy + ddz * ddz;
180
+ if (dd_sqr > 0) {
181
+ const inv2 = 1 / Math.sqrt(dd_sqr);
182
+ nx = ddx * inv2; ny = ddy * inv2; nz = ddz * inv2;
183
+ } else {
184
+ nx = 1; ny = 0; nz = 0;
185
+ }
186
+ }
187
+
188
+ out[0] = nx; out[1] = ny; out[2] = nz;
189
+ out[3] = sum - dist;
190
+ out[4] = pax - nx * a_radius;
191
+ out[5] = pay - ny * a_radius;
192
+ out[6] = paz - nz * a_radius;
193
+ out[7] = pbx + nx * b_radius;
194
+ out[8] = pby + ny * b_radius;
195
+ out[9] = pbz + nz * b_radius;
196
+ return true;
197
+ }
198
+
199
+ // --- capsule vs oriented box ------------------------------------------------
200
+
201
+ const scratch_a0_local = new Float64Array(3);
202
+ const scratch_a1_local = new Float64Array(3);
203
+
204
+ /**
205
+ * Capsule (A) vs oriented box (B). Brings the capsule segment into box-local
206
+ * space, then refines via 2 fixed-point iterations between:
207
+ * - closest point on the segment to the current box-side candidate
208
+ * - clamped point on the box closest to the current segment-side candidate
209
+ *
210
+ * Two iterations suffice for the cases this engine targets (sphere-like cap
211
+ * geometry against axis-aligned-in-local box). When the segment passes
212
+ * through the box, the "inside" branch picks the smallest-overlap face
213
+ * deterministically, mirroring {@link sphere_box_contact}.
214
+ *
215
+ * @param {number[]|Float64Array} out length >= 10
216
+ * @returns {boolean}
217
+ */
218
+ export function capsule_box_contact(
219
+ out,
220
+ a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
221
+ b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
222
+ ) {
223
+ // Bring the capsule's segment endpoints into box-local space: build them in
224
+ // world first (from the capsule centre + rotated half-height axis), then map
225
+ // world box-local below.
226
+ capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
227
+ // World → box-local: subtract box centre, then rotate by conjugate of box quat.
228
+ const w_a0x = scratch_seg[0] - b_cx, w_a0y = scratch_seg[1] - b_cy, w_a0z = scratch_seg[2] - b_cz;
229
+ const w_a1x = scratch_seg[3] - b_cx, w_a1y = scratch_seg[4] - b_cy, w_a1z = scratch_seg[5] - b_cz;
230
+ v3_quat3_apply_inverse(scratch_a0_local, 0, w_a0x, w_a0y, w_a0z, b_qx, b_qy, b_qz, b_qw);
231
+ v3_quat3_apply_inverse(scratch_a1_local, 0, w_a1x, w_a1y, w_a1z, b_qx, b_qy, b_qz, b_qw);
232
+
233
+ const a0lx = scratch_a0_local[0], a0ly = scratch_a0_local[1], a0lz = scratch_a0_local[2];
234
+ const a1lx = scratch_a1_local[0], a1ly = scratch_a1_local[1], a1lz = scratch_a1_local[2];
235
+
236
+ // Fixed-point iteration: start with t at the closest point to the box centre.
237
+ let t = line3_compute_segment_nearest_point_to_point_t(a0lx, a0ly, a0lz, a1lx, a1ly, a1lz, 0, 0, 0);
238
+ let plx = a0lx + (a1lx - a0lx) * t;
239
+ let ply = a0ly + (a1ly - a0ly) * t;
240
+ let plz = a0lz + (a1lz - a0lz) * t;
241
+
242
+ for (let iter = 0; iter < 2; iter++) {
243
+ // Clamp current segment point to the box → closest box-surface point.
244
+ const qlx = plx < -b_hx ? -b_hx : (plx > b_hx ? b_hx : plx);
245
+ const qly = ply < -b_hy ? -b_hy : (ply > b_hy ? b_hy : ply);
246
+ const qlz = plz < -b_hz ? -b_hz : (plz > b_hz ? b_hz : plz);
247
+
248
+ // Re-find closest segment point to that box point.
249
+ t = line3_compute_segment_nearest_point_to_point_t(
250
+ a0lx, a0ly, a0lz, a1lx, a1ly, a1lz,
251
+ qlx, qly, qlz
252
+ );
253
+ plx = a0lx + (a1lx - a0lx) * t;
254
+ ply = a0ly + (a1ly - a0ly) * t;
255
+ plz = a0lz + (a1lz - a0lz) * t;
256
+ }
257
+
258
+ // Final box-side point.
259
+ const qlx = plx < -b_hx ? -b_hx : (plx > b_hx ? b_hx : plx);
260
+ const qly = ply < -b_hy ? -b_hy : (ply > b_hy ? b_hy : ply);
261
+ const qlz = plz < -b_hz ? -b_hz : (plz > b_hz ? b_hz : plz);
262
+ const inside = qlx === plx && qly === ply && qlz === plz;
263
+
264
+ let nlx, nly, nlz, dist;
265
+ if (!inside) {
266
+ const ex = plx - qlx, ey = ply - qly, ez = plz - qlz;
267
+ const d_sqr = ex * ex + ey * ey + ez * ez;
268
+ if (d_sqr >= a_radius * a_radius) return false;
269
+ dist = Math.sqrt(d_sqr);
270
+ if (dist > 0) {
271
+ const inv = 1 / dist;
272
+ nlx = ex * inv; nly = ey * inv; nlz = ez * inv;
273
+ } else {
274
+ nlx = 1; nly = 0; nlz = 0;
275
+ }
276
+ } else {
277
+ // Segment point is inside the box. Pick smallest-overlap face.
278
+ const dxf = b_hx - Math.abs(plx);
279
+ const dyf = b_hy - Math.abs(ply);
280
+ const dzf = b_hz - Math.abs(plz);
281
+ if (dxf <= dyf && dxf <= dzf) {
282
+ nlx = plx >= 0 ? 1 : -1; nly = 0; nlz = 0; dist = -dxf;
283
+ } else if (dyf <= dzf) {
284
+ nlx = 0; nly = ply >= 0 ? 1 : -1; nlz = 0; dist = -dyf;
285
+ } else {
286
+ nlx = 0; nly = 0; nlz = plz >= 0 ? 1 : -1; dist = -dzf;
287
+ }
288
+ }
289
+
290
+ // Normal (box → capsule axis point) into world.
291
+ v3_quat3_apply(out, 0, nlx, nly, nlz, b_qx, b_qy, b_qz, b_qw);
292
+ const nx = out[0], ny = out[1], nz = out[2];
293
+ out[3] = a_radius - dist;
294
+
295
+ // Capsule-side contact: world segment point pushed back by radius.
296
+ v3_quat3_apply(out, 4, plx, ply, plz, b_qx, b_qy, b_qz, b_qw);
297
+ out[4] += b_cx - nx * a_radius;
298
+ out[5] += b_cy - ny * a_radius;
299
+ out[6] += b_cz - nz * a_radius;
300
+
301
+ // Box-side contact: world projection of the box-surface point.
302
+ v3_quat3_apply(out, 7, qlx, qly, qlz, b_qx, b_qy, b_qz, b_qw);
303
+ out[7] += b_cx;
304
+ out[8] += b_cy;
305
+ out[9] += b_cz;
306
+
307
+ return true;
308
+ }
309
+
310
+ // --- capsule vs oriented box, multi-point manifold --------------------------
311
+
312
+ /**
313
+ * Output stride per contact in {@link capsule_box_multi_contacts}.
314
+ * Matches the candidate-buffer layout used by `narrowphase_step`.
315
+ *
316
+ * 0..2 : world contact on capsule side
317
+ * 3..5 : world contact on box side
318
+ * 6..8 : normal from box (B) toward capsule (A)
319
+ * 9 : depth (positive = penetration)
320
+ *
321
+ * @type {number}
322
+ */
323
+ export const CAPSULE_BOX_CONTACT_STRIDE = 10;
324
+
325
+ /**
326
+ * Maximum contact points {@link capsule_box_multi_contacts} can emit.
327
+ * - 1 from the closest-point-on-segment vs OBB query (primary)
328
+ * - up to 2 from the cap-centre sphere-vs-OBB queries (one per endpoint)
329
+ * @type {number}
330
+ */
331
+ export const CAPSULE_BOX_MAX_CONTACTS = 3;
332
+
333
+ const scratch_primary = new Float64Array(10);
334
+ const scratch_endpoint = new Float64Array(10);
335
+
336
+ /**
337
+ * Two world positions are treated as the same contact when their squared
338
+ * distance is below this threshold. Picked so that a capsule resting flat
339
+ * doesn't double up the primary closest-segment-point contact with whichever
340
+ * endpoint it happens to coincide with.
341
+ *
342
+ * Looser than the manifold's warm-start match tolerance (≈0.02) because we
343
+ * specifically want to dedupe contacts at the *same* point; small lateral
344
+ * shifts of the capsule produce distinct contacts that should both be kept.
345
+ * @type {number}
346
+ */
347
+ const DEDUPE_DIST_SQR = 1e-6;
348
+
349
+ /**
350
+ * Multi-point capsule-vs-OBB contact generation. Emits up to
351
+ * {@link CAPSULE_BOX_MAX_CONTACTS} contacts into `out` in the
352
+ * `(wax, way, waz, wbx, wby, wbz, nx, ny, nz, depth)` stride layout.
353
+ *
354
+ * The motivation: a capsule lying flat on a face needs more than one
355
+ * contact to rest stably — a single contact at the segment's closest point
356
+ * is positionally ambiguous (every interior segment point is equidistant
357
+ * from the face), and the body wobbles around whichever point the
358
+ * iterative closest-point solver happened to converge on. Two contacts at
359
+ * the cap centres pin the capsule's rotation about its long axis.
360
+ *
361
+ * Algorithm:
362
+ * 1. Closest-point-on-segment vs OBB → primary contact (same as
363
+ * {@link capsule_box_contact}).
364
+ * 2. Treat each cap centre as a sphere of cap-radius and run
365
+ * {@link sphere_box_contact} → up to two additional contacts.
366
+ * 3. Dedupe spatially against the primary so a capsule touching with one
367
+ * cap doesn't emit two contacts at the same point.
368
+ *
369
+ * @param {Float64Array} out length >= CAPSULE_BOX_MAX_CONTACTS * CAPSULE_BOX_CONTACT_STRIDE
370
+ * @param {number} a_cx capsule centre x
371
+ * @param {number} a_cy
372
+ * @param {number} a_cz
373
+ * @param {number} a_qx capsule rotation
374
+ * @param {number} a_qy
375
+ * @param {number} a_qz
376
+ * @param {number} a_qw
377
+ * @param {number} a_radius
378
+ * @param {number} a_half_h
379
+ * @param {number} b_cx box centre x
380
+ * @param {number} b_cy
381
+ * @param {number} b_cz
382
+ * @param {number} b_qx
383
+ * @param {number} b_qy
384
+ * @param {number} b_qz
385
+ * @param {number} b_qw
386
+ * @param {number} b_hx
387
+ * @param {number} b_hy
388
+ * @param {number} b_hz
389
+ * @returns {number} number of contacts emitted, in `[0, CAPSULE_BOX_MAX_CONTACTS]`
390
+ */
391
+ export function capsule_box_multi_contacts(
392
+ out,
393
+ a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
394
+ b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
395
+ ) {
396
+ // 1. Primary contact: closest segment point vs OBB.
397
+ const primary_ok = capsule_box_contact(
398
+ scratch_primary,
399
+ a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_radius, a_half_h,
400
+ b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw, b_hx, b_hy, b_hz
401
+ );
402
+ if (!primary_ok) return 0;
403
+
404
+ // Repack scratch_primary (normal, depth, A, B) into the candidate stride
405
+ // (A, B, normal, depth) at out[0..10).
406
+ const p_nx = scratch_primary[0], p_ny = scratch_primary[1], p_nz = scratch_primary[2];
407
+ const p_depth = scratch_primary[3];
408
+ const p_ax = scratch_primary[4], p_ay = scratch_primary[5], p_az = scratch_primary[6];
409
+ const p_bx = scratch_primary[7], p_by = scratch_primary[8], p_bz = scratch_primary[9];
410
+ out[0] = p_ax; out[1] = p_ay; out[2] = p_az;
411
+ out[3] = p_bx; out[4] = p_by; out[5] = p_bz;
412
+ out[6] = p_nx; out[7] = p_ny; out[8] = p_nz;
413
+ out[9] = p_depth;
414
+ let count = 1;
415
+
416
+ // 2. Endpoint contacts: treat each cap centre as a sphere of cap-radius.
417
+ capsule_world_segment(scratch_seg, a_cx, a_cy, a_cz, a_qx, a_qy, a_qz, a_qw, a_half_h);
418
+
419
+ for (let i = 0; i < 2; i++) {
420
+ // Endpoint i lives at scratch_seg[i*3 .. i*3+2] — index directly rather
421
+ // than allocating three throwaway arrays per call (hot narrowphase path).
422
+ const ok = sphere_box_contact(
423
+ scratch_endpoint,
424
+ scratch_seg[i * 3], scratch_seg[i * 3 + 1], scratch_seg[i * 3 + 2], a_radius,
425
+ b_cx, b_cy, b_cz, b_qx, b_qy, b_qz, b_qw,
426
+ b_hx, b_hy, b_hz
427
+ );
428
+ if (!ok) continue;
429
+
430
+ const e_nx = scratch_endpoint[0], e_ny = scratch_endpoint[1], e_nz = scratch_endpoint[2];
431
+ const e_depth = scratch_endpoint[3];
432
+ const e_ax = scratch_endpoint[4], e_ay = scratch_endpoint[5], e_az = scratch_endpoint[6];
433
+ const e_bx = scratch_endpoint[7], e_by = scratch_endpoint[8], e_bz = scratch_endpoint[9];
434
+
435
+ // Dedupe against every already-emitted contact (primary, then any
436
+ // prior endpoint we've already accepted). The capsule-side world
437
+ // point uniquely identifies a contact for this purpose — two distinct
438
+ // box-surface points always have distinct capsule-side projections
439
+ // along the local-A axis.
440
+ let duplicate = false;
441
+ for (let k = 0; k < count; k++) {
442
+ const ko = k * CAPSULE_BOX_CONTACT_STRIDE;
443
+ const dx = e_ax - out[ko];
444
+ const dy = e_ay - out[ko + 1];
445
+ const dz = e_az - out[ko + 2];
446
+ if (dx * dx + dy * dy + dz * dz < DEDUPE_DIST_SQR) { duplicate = true; break; }
447
+ }
448
+ if (duplicate) continue;
449
+
450
+ const o = count * CAPSULE_BOX_CONTACT_STRIDE;
451
+ out[o] = e_ax; out[o + 1] = e_ay; out[o + 2] = e_az;
452
+ out[o + 3] = e_bx; out[o + 4] = e_by; out[o + 5] = e_bz;
453
+ out[o + 6] = e_nx; out[o + 7] = e_ny; out[o + 8] = e_nz;
454
+ out[o + 9] = e_depth;
455
+ count++;
456
+ }
457
+
458
+ return count;
459
+ }