@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.
- package/package.json +1 -1
- package/src/core/geom/3d/shape/ConvexHullShape3D.d.ts +112 -0
- package/src/core/geom/3d/shape/ConvexHullShape3D.d.ts.map +1 -0
- package/src/core/geom/3d/shape/ConvexHullShape3D.js +325 -0
- package/src/core/geom/vec3/v3_array_copy.d.ts +3 -3
- package/src/core/geom/vec3/v3_array_copy.d.ts.map +1 -1
- package/src/core/geom/vec3/v3_array_copy.js +2 -2
- package/src/core/geom/vec3/v3_cross.d.ts +17 -0
- package/src/core/geom/vec3/v3_cross.d.ts.map +1 -0
- package/src/core/geom/vec3/v3_cross.js +20 -0
- package/src/core/geom/vec3/v3_subtract.d.ts +16 -0
- package/src/core/geom/vec3/v3_subtract.d.ts.map +1 -0
- package/src/core/geom/vec3/v3_subtract.js +19 -0
- package/src/engine/graphics/ecs/decal/v2/FPDecalSystem.d.ts.map +1 -1
- package/src/engine/graphics/ecs/decal/v2/FPDecalSystem.js +8 -0
- package/src/engine/graphics/ecs/trail2d/Trail2D.d.ts +4 -0
- package/src/engine/graphics/ecs/trail2d/Trail2D.d.ts.map +1 -1
- package/src/engine/graphics/ecs/trail2d/Trail2D.js +21 -0
- package/src/engine/physics/PLAN.md +4 -4
- package/src/engine/physics/body/BodyStorage.d.ts +3 -1
- package/src/engine/physics/body/BodyStorage.d.ts.map +1 -1
- package/src/engine/physics/body/BodyStorage.js +452 -450
- package/src/engine/physics/body/SolverBodyState.d.ts.map +1 -1
- package/src/engine/physics/body/SolverBodyState.js +6 -5
- package/src/engine/physics/broadphase/generate_pairs.d.ts.map +1 -1
- package/src/engine/physics/broadphase/generate_pairs.js +9 -1
- package/src/engine/physics/ccd/linear_sweep.d.ts.map +1 -1
- package/src/engine/physics/ccd/linear_sweep.js +237 -238
- package/src/engine/physics/computeInterceptPoint.d.ts.map +1 -1
- package/src/engine/physics/computeInterceptPoint.js +8 -3
- package/src/engine/physics/contact/ManifoldStore.d.ts +0 -16
- package/src/engine/physics/contact/ManifoldStore.d.ts.map +1 -1
- package/src/engine/physics/contact/ManifoldStore.js +1 -38
- package/src/engine/physics/ecs/BodyKind.d.ts +3 -2
- package/src/engine/physics/ecs/BodyKind.d.ts.map +1 -1
- package/src/engine/physics/ecs/BodyKind.js +25 -24
- package/src/engine/physics/ecs/PhysicsEvents.d.ts +4 -5
- package/src/engine/physics/ecs/PhysicsEvents.d.ts.map +1 -1
- package/src/engine/physics/ecs/PhysicsEvents.js +15 -16
- package/src/engine/physics/ecs/PhysicsSystem.d.ts +5 -30
- package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
- package/src/engine/physics/ecs/PhysicsSystem.js +13 -45
- package/src/engine/physics/ecs/RigidBodySerializationAdapter.d.ts.map +1 -1
- package/src/engine/physics/ecs/RigidBodySerializationAdapter.js +85 -81
- package/src/engine/physics/ecs/is_sensor.d.ts +18 -0
- package/src/engine/physics/ecs/is_sensor.d.ts.map +1 -0
- package/src/engine/physics/ecs/is_sensor.js +27 -0
- package/src/engine/physics/events/ContactEventBuffer.d.ts +2 -1
- package/src/engine/physics/events/ContactEventBuffer.d.ts.map +1 -1
- package/src/engine/physics/events/ContactEventBuffer.js +84 -83
- package/src/engine/physics/gjk/gjk.d.ts +0 -26
- package/src/engine/physics/gjk/gjk.d.ts.map +1 -1
- package/src/engine/physics/gjk/gjk.js +3 -52
- package/src/engine/physics/gjk/gjk_epa_penetration.d.ts +20 -0
- package/src/engine/physics/gjk/gjk_epa_penetration.d.ts.map +1 -0
- package/src/engine/physics/gjk/gjk_epa_penetration.js +548 -0
- package/src/engine/physics/gjk/minkowski_support.d.ts +4 -9
- package/src/engine/physics/gjk/minkowski_support.d.ts.map +1 -1
- package/src/engine/physics/gjk/minkowski_support.js +70 -75
- package/src/engine/physics/gjk/mpr.d.ts +1 -1
- package/src/engine/physics/gjk/mpr.d.ts.map +1 -1
- package/src/engine/physics/gjk/mpr.js +362 -344
- package/src/engine/physics/island/IslandBuilder.d.ts.map +1 -1
- package/src/engine/physics/island/IslandBuilder.js +431 -428
- package/src/engine/physics/narrowphase/box_box_manifold.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/box_box_manifold.js +4 -81
- package/src/engine/physics/narrowphase/box_triangle_contact.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/box_triangle_contact.js +4 -39
- package/src/engine/physics/narrowphase/capsule_contacts.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/capsule_contacts.js +459 -462
- package/src/engine/physics/narrowphase/clip_against_axis_uv.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/clip_against_axis_uv.js +4 -1
- package/src/engine/physics/narrowphase/convex_convex_manifold.d.ts +83 -0
- package/src/engine/physics/narrowphase/convex_convex_manifold.d.ts.map +1 -0
- package/src/engine/physics/narrowphase/convex_convex_manifold.js +425 -0
- package/src/engine/physics/narrowphase/convex_decomposition.d.ts +32 -0
- package/src/engine/physics/narrowphase/convex_decomposition.d.ts.map +1 -0
- package/src/engine/physics/narrowphase/convex_decomposition.js +293 -0
- package/src/engine/physics/narrowphase/mesh_convex_hull.d.ts +41 -0
- package/src/engine/physics/narrowphase/mesh_convex_hull.d.ts.map +1 -0
- package/src/engine/physics/narrowphase/mesh_convex_hull.js +106 -0
- package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.d.ts +8 -0
- package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.d.ts.map +1 -0
- package/src/engine/physics/narrowphase/mesh_mesh_tet_manifold.js +117 -0
- package/src/engine/physics/narrowphase/narrowphase_step.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/narrowphase_step.js +105 -102
- package/src/engine/physics/narrowphase/reduce_manifold_contacts.d.ts +29 -0
- package/src/engine/physics/narrowphase/reduce_manifold_contacts.d.ts.map +1 -0
- package/src/engine/physics/narrowphase/reduce_manifold_contacts.js +69 -0
- package/src/engine/physics/narrowphase/refine_ray_concave.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/refine_ray_concave.js +152 -145
- package/src/engine/physics/narrowphase/sphere_box_contact.d.ts.map +1 -1
- package/src/engine/physics/narrowphase/sphere_box_contact.js +132 -123
- package/src/engine/physics/queries/overlap_shape.d.ts.map +1 -1
- package/src/engine/physics/queries/overlap_shape.js +16 -17
- package/src/engine/physics/queries/raycast.d.ts +5 -0
- package/src/engine/physics/queries/raycast.d.ts.map +1 -1
- package/src/engine/physics/queries/raycast.js +16 -8
- package/src/engine/physics/queries/shape_cast.d.ts.map +1 -1
- package/src/engine/physics/queries/shape_cast.js +13 -7
- package/src/engine/physics/solver/solve_contacts.d.ts.map +1 -1
- package/src/engine/physics/solver/solve_contacts.js +8 -11
- package/src/engine/physics/vehicle/RaycastVehicle.d.ts.map +1 -1
- package/src/engine/physics/vehicle/RaycastVehicle.js +339 -333
- package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts +0 -13
- package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts.map +0 -1
- 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
|
-
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
//
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
let
|
|
240
|
-
let
|
|
241
|
-
|
|
242
|
-
let
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
);
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
const
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
nlx =
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (
|
|
284
|
-
nlx =
|
|
285
|
-
} else
|
|
286
|
-
nlx = 0; nly =
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
out[
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
out[
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
*
|
|
321
|
-
*
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
*
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
362
|
-
*
|
|
363
|
-
*
|
|
364
|
-
*
|
|
365
|
-
* {@link
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
369
|
-
*
|
|
370
|
-
*
|
|
371
|
-
* @param {
|
|
372
|
-
* @param {number}
|
|
373
|
-
* @param {number}
|
|
374
|
-
* @param {number}
|
|
375
|
-
* @param {number}
|
|
376
|
-
* @param {number}
|
|
377
|
-
* @param {number}
|
|
378
|
-
* @param {number}
|
|
379
|
-
* @param {number}
|
|
380
|
-
* @param {number}
|
|
381
|
-
* @param {number}
|
|
382
|
-
* @param {number}
|
|
383
|
-
* @param {number}
|
|
384
|
-
* @param {number}
|
|
385
|
-
* @param {number}
|
|
386
|
-
* @param {number}
|
|
387
|
-
* @param {number}
|
|
388
|
-
* @param {number}
|
|
389
|
-
* @
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
const
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
out[
|
|
413
|
-
out[
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
const
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
//
|
|
439
|
-
//
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
const
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
out[o
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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
|
+
}
|