@woosh/meep-engine 2.139.0 → 2.141.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/package.json +1 -1
  2. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.d.ts +3 -3
  3. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.d.ts.map +1 -1
  4. package/src/core/bvh2/bvh3/query/bvh_query_user_data_overlaps_aabb.js +4 -4
  5. package/src/{engine/physics/broadphase/aabb_transform_oriented.d.ts → core/geom/3d/aabb/aabb3_transform_oriented.d.ts} +2 -2
  6. package/src/core/geom/3d/aabb/aabb3_transform_oriented.d.ts.map +1 -0
  7. package/src/{engine/physics/broadphase/aabb_transform_oriented.js → core/geom/3d/aabb/aabb3_transform_oriented.js} +1 -1
  8. package/src/core/geom/3d/quaternion/quat3_multiply.d.ts +21 -0
  9. package/src/core/geom/3d/quaternion/quat3_multiply.d.ts.map +1 -0
  10. package/src/core/geom/3d/quaternion/quat3_multiply.js +25 -0
  11. package/src/core/geom/3d/quaternion/quat3_to_matrix3.d.ts +54 -0
  12. package/src/core/geom/3d/quaternion/quat3_to_matrix3.d.ts.map +1 -0
  13. package/src/core/geom/3d/quaternion/quat3_to_matrix3.js +69 -0
  14. package/src/core/geom/3d/shape/AbstractShape3D.d.ts +24 -2
  15. package/src/core/geom/3d/shape/AbstractShape3D.d.ts.map +1 -1
  16. package/src/core/geom/3d/shape/AbstractShape3D.js +24 -1
  17. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts +148 -0
  18. package/src/core/geom/3d/shape/HeightMapShape3D.d.ts.map +1 -0
  19. package/src/core/geom/3d/shape/HeightMapShape3D.js +451 -0
  20. package/src/core/geom/3d/shape/MeshShape3D.d.ts +210 -0
  21. package/src/core/geom/3d/shape/MeshShape3D.d.ts.map +1 -0
  22. package/src/core/geom/3d/shape/MeshShape3D.js +593 -0
  23. package/src/core/geom/3d/shape/TransformedShape3D.d.ts.map +1 -1
  24. package/src/core/geom/3d/shape/TransformedShape3D.js +46 -2
  25. package/src/core/geom/3d/shape/Triangle3D.d.ts +95 -0
  26. package/src/core/geom/3d/shape/Triangle3D.d.ts.map +1 -0
  27. package/src/core/geom/3d/shape/Triangle3D.js +318 -0
  28. package/src/core/geom/3d/shape/UnionShape3D.js +13 -0
  29. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts +30 -0
  30. package/src/core/geom/3d/shape/shape_mesh_from_geometry.d.ts.map +1 -0
  31. package/src/core/geom/3d/shape/shape_mesh_from_geometry.js +64 -0
  32. package/src/core/geom/3d/tetrahedra/prototype_tetrahedrize_mesh.js +9 -11
  33. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts +28 -0
  34. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.d.ts.map +1 -0
  35. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_build_vertex_to_tets_map.js +48 -0
  36. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.d.ts.map +1 -1
  37. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_improve_quality.js +40 -18
  38. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts +9 -5
  39. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.d.ts.map +1 -1
  40. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_smooth_vertex.js +38 -10
  41. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts +14 -5
  42. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.d.ts.map +1 -1
  43. package/src/core/geom/3d/tetrahedra/tetrahedral_mesh_vertex_is_boundary.js +47 -5
  44. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts +19 -0
  45. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.d.ts.map +1 -1
  46. package/src/core/geom/3d/topology/struct/binary/BinaryElementPool.js +75 -13
  47. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts +2 -2
  48. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.d.ts.map +1 -1
  49. package/src/core/geom/3d/triangle/v3_compute_triangle_normal.js +1 -1
  50. package/src/core/geom/vec3/v3_dot_array_array.d.ts +3 -3
  51. package/src/core/geom/vec3/v3_dot_array_array.d.ts.map +1 -1
  52. package/src/core/geom/vec3/v3_dot_array_array.js +2 -2
  53. package/src/core/geom/vec3/v3_negate_array.d.ts +3 -3
  54. package/src/core/geom/vec3/v3_negate_array.d.ts.map +1 -1
  55. package/src/core/geom/vec3/v3_negate_array.js +2 -2
  56. package/src/core/geom/vec3/v3_quat3_apply.d.ts +29 -0
  57. package/src/core/geom/vec3/v3_quat3_apply.d.ts.map +1 -0
  58. package/src/core/geom/vec3/v3_quat3_apply.js +39 -0
  59. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts +30 -0
  60. package/src/core/geom/vec3/v3_quat3_apply_inverse.d.ts.map +1 -0
  61. package/src/core/geom/vec3/v3_quat3_apply_inverse.js +41 -0
  62. package/src/core/geom/vec3/v3_triple_cross_product.d.ts +32 -0
  63. package/src/core/geom/vec3/v3_triple_cross_product.d.ts.map +1 -0
  64. package/src/core/geom/vec3/v3_triple_cross_product.js +45 -0
  65. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts +16 -3
  66. package/src/engine/control/first-person/FirstPersonPlayerController.d.ts.map +1 -1
  67. package/src/engine/control/first-person/FirstPersonPlayerController.js +211 -211
  68. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts +72 -8
  69. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.d.ts.map +1 -1
  70. package/src/engine/control/first-person/FirstPersonPlayerControllerConfig.js +37 -5
  71. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts +101 -3
  72. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.d.ts.map +1 -1
  73. package/src/engine/control/first-person/FirstPersonPlayerControllerSystem.js +1789 -1416
  74. package/src/engine/control/first-person/TODO.md +173 -127
  75. package/src/engine/control/first-person/abilities/Slide.d.ts.map +1 -1
  76. package/src/engine/control/first-person/abilities/Slide.js +9 -1
  77. package/src/engine/control/first-person/prototype_first_person_controller.js +88 -2
  78. package/src/engine/control/first-person/test/buildTestPlayer.d.ts.map +1 -1
  79. package/src/engine/control/first-person/test/buildTestPlayer.js +9 -1
  80. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts +42 -0
  81. package/src/engine/graphics/geometry/CapsuleGeometry.d.ts.map +1 -0
  82. package/src/engine/graphics/geometry/CapsuleGeometry.js +171 -0
  83. package/src/engine/physics/BULLET_REVIEW.md +945 -0
  84. package/src/engine/physics/CANNON_REVIEW.md +1300 -0
  85. package/src/engine/physics/JOLT_REVIEW.md +913 -0
  86. package/src/engine/physics/PLAN.md +578 -236
  87. package/src/engine/physics/RAPIER_REVIEW.md +934 -0
  88. package/src/engine/physics/REVIEW_001_ACTION_PLAN.md +642 -0
  89. package/src/engine/physics/REVIEW_002.md +151 -0
  90. package/src/engine/physics/broadphase/compute_fat_world_aabb.js +2 -2
  91. package/src/engine/physics/constraint/DofMode.d.ts +28 -0
  92. package/src/engine/physics/constraint/DofMode.d.ts.map +1 -0
  93. package/src/engine/physics/constraint/DofMode.js +35 -0
  94. package/src/engine/physics/constraint/solve_constraints.d.ts +16 -0
  95. package/src/engine/physics/constraint/solve_constraints.d.ts.map +1 -0
  96. package/src/engine/physics/constraint/solve_constraints.js +436 -0
  97. package/src/engine/physics/contact/ManifoldStore.d.ts +83 -10
  98. package/src/engine/physics/contact/ManifoldStore.d.ts.map +1 -1
  99. package/src/engine/physics/contact/ManifoldStore.js +608 -499
  100. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts +2 -2
  101. package/src/engine/physics/ecs/ColliderObserverSystem.d.ts.map +1 -1
  102. package/src/engine/physics/ecs/Joint.d.ts +179 -0
  103. package/src/engine/physics/ecs/Joint.d.ts.map +1 -0
  104. package/src/engine/physics/ecs/Joint.js +234 -0
  105. package/src/engine/physics/ecs/PhysicsSystem.d.ts +180 -20
  106. package/src/engine/physics/ecs/PhysicsSystem.d.ts.map +1 -1
  107. package/src/engine/physics/ecs/PhysicsSystem.js +1423 -1159
  108. package/src/engine/physics/fluid/FluidField.d.ts +14 -10
  109. package/src/engine/physics/fluid/FluidField.d.ts.map +1 -1
  110. package/src/engine/physics/fluid/FluidField.js +14 -10
  111. package/src/engine/physics/fluid/FluidSimulator.js +1 -1
  112. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.d.ts +17 -10
  113. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.d.ts.map +1 -1
  114. package/src/engine/physics/fluid/solver/v3_grid_compute_solid_neighbour_mask.js +18 -11
  115. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.d.ts +13 -10
  116. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.d.ts.map +1 -1
  117. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure.js +18 -13
  118. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.d.ts +4 -3
  119. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.d.ts.map +1 -1
  120. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_pcg.js +15 -11
  121. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts +30 -6
  122. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.d.ts.map +1 -1
  123. package/src/engine/physics/fluid/solver/v3_grid_subtract_pressure_gradient.js +44 -18
  124. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts +6 -6
  125. package/src/engine/physics/gjk/expanding_polytope_algorithm.d.ts.map +1 -1
  126. package/src/engine/physics/gjk/expanding_polytope_algorithm.js +68 -22
  127. package/src/engine/physics/gjk/gjk.d.ts +28 -2
  128. package/src/engine/physics/gjk/gjk.d.ts.map +1 -1
  129. package/src/engine/physics/gjk/gjk.js +421 -378
  130. package/src/engine/physics/gjk/minkowski_support.d.ts +37 -0
  131. package/src/engine/physics/gjk/minkowski_support.d.ts.map +1 -0
  132. package/src/engine/physics/gjk/minkowski_support.js +75 -0
  133. package/src/engine/physics/gjk/mpr.d.ts +56 -0
  134. package/src/engine/physics/gjk/mpr.d.ts.map +1 -0
  135. package/src/engine/physics/gjk/mpr.js +344 -0
  136. package/src/engine/physics/inertia/world_inverse_inertia.d.ts +20 -5
  137. package/src/engine/physics/inertia/world_inverse_inertia.d.ts.map +1 -1
  138. package/src/engine/physics/inertia/world_inverse_inertia.js +36 -38
  139. package/src/engine/physics/integration/integrate_position.d.ts +25 -7
  140. package/src/engine/physics/integration/integrate_position.d.ts.map +1 -1
  141. package/src/engine/physics/integration/integrate_position.js +43 -12
  142. package/src/engine/physics/integration/integrate_velocity.d.ts +30 -0
  143. package/src/engine/physics/integration/integrate_velocity.d.ts.map +1 -1
  144. package/src/engine/physics/integration/integrate_velocity.js +82 -1
  145. package/src/engine/physics/island/IslandBuilder.d.ts +4 -1
  146. package/src/engine/physics/island/IslandBuilder.d.ts.map +1 -1
  147. package/src/engine/physics/island/IslandBuilder.js +33 -16
  148. package/src/engine/physics/narrowphase/PosedShape.d.ts +0 -8
  149. package/src/engine/physics/narrowphase/PosedShape.d.ts.map +1 -1
  150. package/src/engine/physics/narrowphase/PosedShape.js +28 -30
  151. package/src/engine/physics/narrowphase/box_box_manifold.d.ts.map +1 -1
  152. package/src/engine/physics/narrowphase/box_box_manifold.js +140 -18
  153. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts +30 -0
  154. package/src/engine/physics/narrowphase/box_triangle_contact.d.ts.map +1 -0
  155. package/src/engine/physics/narrowphase/box_triangle_contact.js +811 -0
  156. package/src/engine/physics/narrowphase/capsule_contacts.d.ts.map +1 -1
  157. package/src/engine/physics/narrowphase/capsule_contacts.js +10 -56
  158. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts +71 -0
  159. package/src/engine/physics/narrowphase/capsule_triangle_contact.d.ts.map +1 -0
  160. package/src/engine/physics/narrowphase/capsule_triangle_contact.js +375 -0
  161. package/src/engine/physics/narrowphase/compute_penetration.d.ts +91 -0
  162. package/src/engine/physics/narrowphase/compute_penetration.d.ts.map +1 -0
  163. package/src/engine/physics/narrowphase/compute_penetration.js +396 -0
  164. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts +35 -0
  165. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.d.ts.map +1 -0
  166. package/src/engine/physics/narrowphase/decomposition/aabb_world_to_local.js +80 -0
  167. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts +31 -0
  168. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.d.ts.map +1 -0
  169. package/src/engine/physics/narrowphase/decomposition/decompose_to_triangles.js +55 -0
  170. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts +42 -0
  171. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.d.ts.map +1 -0
  172. package/src/engine/physics/narrowphase/decomposition/heightmap_enumerate_triangles.js +204 -0
  173. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts +42 -0
  174. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.d.ts.map +1 -0
  175. package/src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.js +94 -0
  176. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts +37 -0
  177. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.d.ts.map +1 -0
  178. package/src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.js +37 -0
  179. package/src/engine/physics/narrowphase/narrowphase_step.d.ts +41 -2
  180. package/src/engine/physics/narrowphase/narrowphase_step.d.ts.map +1 -1
  181. package/src/engine/physics/narrowphase/narrowphase_step.js +1497 -382
  182. package/src/engine/physics/narrowphase/sphere_box_contact.d.ts.map +1 -1
  183. package/src/engine/physics/narrowphase/sphere_box_contact.js +16 -23
  184. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts +48 -0
  185. package/src/engine/physics/narrowphase/sphere_triangle_contact.d.ts.map +1 -0
  186. package/src/engine/physics/narrowphase/sphere_triangle_contact.js +143 -0
  187. package/src/engine/physics/queries/overlap_shape.d.ts +51 -0
  188. package/src/engine/physics/queries/overlap_shape.d.ts.map +1 -0
  189. package/src/engine/physics/queries/overlap_shape.js +183 -0
  190. package/src/engine/physics/queries/shape_cast.d.ts +56 -0
  191. package/src/engine/physics/queries/shape_cast.d.ts.map +1 -0
  192. package/src/engine/physics/queries/shape_cast.js +387 -0
  193. package/src/engine/physics/solver/solve_contacts.d.ts +146 -32
  194. package/src/engine/physics/solver/solve_contacts.d.ts.map +1 -1
  195. package/src/engine/physics/solver/solve_contacts.js +809 -223
  196. package/src/engine/physics/broadphase/aabb_transform_oriented.d.ts.map +0 -1
  197. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts +0 -20
  198. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.d.ts.map +0 -1
  199. package/src/engine/physics/fluid/solver/v3_grid_solve_pressure_unmasked_legacy.js +0 -83
@@ -0,0 +1,204 @@
1
+ import { TRIANGLE_FEATURE_ID_OFFSET, TRIANGLE_FLOAT_STRIDE } from "./triangle_buffer_layout.js";
2
+
3
+ /**
4
+ * Write the heightmap's triangles that may overlap the given body-local
5
+ * query AABB into `output`, starting at float-index `offset`.
6
+ *
7
+ * Each triangle occupies {@link TRIANGLE_FLOAT_STRIDE} consecutive floats:
8
+ * [+0..+2] vA.xyz (body-local frame)
9
+ * [+3..+5] vB.xyz
10
+ * [+6..+8] vC.xyz
11
+ * [+9] feature_id (stable across frames; warm-start key)
12
+ *
13
+ * The feature_id encodes `(cell_y * (W - 1) + cell_x) * 2 + tri_idx`
14
+ * where (cell_x, cell_y) is the grid cell and tri_idx ∈ {0, 1}. Two
15
+ * triangles per cell, lower-left-diagonal split (matches the existing
16
+ * {@link build_height_field_geometry} winding).
17
+ *
18
+ * The query AABB is filtered against the footprint *and* against the
19
+ * cell grid; triangles outside the query AABB along the height axis are
20
+ * NOT filtered out — that test is left to the per-triangle narrowphase
21
+ * (where the precise GJK distance is computed anyway). This keeps the
22
+ * enumerator branch-free over the height field and aligned with what
23
+ * Bullet's btHeightfieldTerrainShape does.
24
+ *
25
+ * The caller is responsible for ensuring
26
+ * `output.length - offset >= cells_in_query * 2 * TRIANGLE_FLOAT_STRIDE`
27
+ * — sizing the scratch buffer for the *worst-case* query AABB is the
28
+ * right pattern (the broadphase already bounded the AABB to one body's
29
+ * world envelope, so the cell count is bounded by the footprint
30
+ * resolution).
31
+ *
32
+ * @param {Float64Array} output
33
+ * @param {number} offset float-index into output
34
+ * @param {HeightMapShape3D} shape
35
+ * @param {number} aabb_min_x query AABB in shape's body-local frame
36
+ * @param {number} aabb_min_y
37
+ * @param {number} aabb_min_z
38
+ * @param {number} aabb_max_x
39
+ * @param {number} aabb_max_y
40
+ * @param {number} aabb_max_z
41
+ * @returns {number} number of triangles written
42
+ */
43
+ export function heightmap_enumerate_triangles(
44
+ output, offset, shape,
45
+ aabb_min_x, aabb_min_y, aabb_min_z,
46
+ aabb_max_x, aabb_max_y, aabb_max_z
47
+ ) {
48
+ const sampler = shape.sampler;
49
+
50
+ if (sampler === null) {
51
+ return 0;
52
+ }
53
+
54
+ const W = sampler.width;
55
+ const H = sampler.height;
56
+
57
+ if (W < 2 || H < 2) {
58
+ // need at least one cell (one cell = 2 vertices per axis)
59
+ return 0;
60
+ }
61
+
62
+ const size_x = shape.size[0];
63
+ const size_z = shape.size[2];
64
+
65
+ if (size_x <= 0 || size_z <= 0) {
66
+ return 0;
67
+ }
68
+
69
+ // ── Project body-local AABB into heightmap-local (u, v, h) frame ─────
70
+ // Basis rows = (u_axis, v_axis, n_axis) in body-local frame. The
71
+ // body→heightmap rotation is therefore B (rows of basis), and the
72
+ // Arvo trick gives us the projected AABB extents.
73
+
74
+ shape._ensure_basis();
75
+ const b = shape._basis;
76
+
77
+ const c_x = 0.5 * (aabb_min_x + aabb_max_x);
78
+ const c_y = 0.5 * (aabb_min_y + aabb_max_y);
79
+ const c_z = 0.5 * (aabb_min_z + aabb_max_z);
80
+
81
+ const h_x = 0.5 * (aabb_max_x - aabb_min_x);
82
+ const h_y = 0.5 * (aabb_max_y - aabb_min_y);
83
+ const h_z = 0.5 * (aabb_max_z - aabb_min_z);
84
+
85
+ const u_c = b[0] * c_x + b[1] * c_y + b[2] * c_z;
86
+ const v_c = b[3] * c_x + b[4] * c_y + b[5] * c_z;
87
+ // height axis (b[6..8]) is intentionally not projected — see fn docstring
88
+
89
+ const u_e = Math.abs(b[0]) * h_x + Math.abs(b[1]) * h_y + Math.abs(b[2]) * h_z;
90
+ const v_e = Math.abs(b[3]) * h_x + Math.abs(b[4]) * h_y + Math.abs(b[5]) * h_z;
91
+
92
+ const half_u = size_x * 0.5;
93
+ const half_v = size_z * 0.5;
94
+
95
+ // ── Intersect with footprint, derive cell range ──────────────────────
96
+
97
+ const u_lo_q = u_c - u_e;
98
+ const u_hi_q = u_c + u_e;
99
+ const v_lo_q = v_c - v_e;
100
+ const v_hi_q = v_c + v_e;
101
+
102
+ if (u_lo_q > half_u || u_hi_q < -half_u) return 0;
103
+ if (v_lo_q > half_v || v_hi_q < -half_v) return 0;
104
+
105
+ const u_lo = u_lo_q > -half_u ? u_lo_q : -half_u;
106
+ const u_hi = u_hi_q < +half_u ? u_hi_q : +half_u;
107
+ const v_lo = v_lo_q > -half_v ? v_lo_q : -half_v;
108
+ const v_hi = v_hi_q < +half_v ? v_hi_q : +half_v;
109
+
110
+ const inv_cell_u = (W - 1) / size_x;
111
+ const inv_cell_v = (H - 1) / size_z;
112
+
113
+ let i_first = Math.floor((u_lo + half_u) * inv_cell_u);
114
+ let i_last = Math.ceil ((u_hi + half_u) * inv_cell_u) - 1;
115
+ let j_first = Math.floor((v_lo + half_v) * inv_cell_v);
116
+ let j_last = Math.ceil ((v_hi + half_v) * inv_cell_v) - 1;
117
+
118
+ if (i_first < 0) i_first = 0;
119
+ if (j_first < 0) j_first = 0;
120
+ if (i_last > W - 2) i_last = W - 2;
121
+ if (j_last > H - 2) j_last = H - 2;
122
+
123
+ if (i_first > i_last || j_first > j_last) return 0;
124
+
125
+ // ── Emit triangles ───────────────────────────────────────────────────
126
+ //
127
+ // For each cell (i, j) we sample 4 corner heights and emit 2 triangles
128
+ // matching the build_height_field_geometry winding:
129
+ // A = (i, j ), B = (i+1, j )
130
+ // D = (i, j+1), C = (i+1, j+1)
131
+ // tri 0 = (A, D, B)
132
+ // tri 1 = (D, C, B)
133
+ //
134
+ // body_point = u_axis * u_coord + v_axis * v_coord + n_axis * h_coord
135
+ // where columns of the body-frame projection matrix are the basis rows
136
+ // of B (since B is orthonormal, body→heightmap and heightmap→body
137
+ // matrices are mutual transposes).
138
+
139
+ const ux = b[0]; const uy = b[1]; const uz = b[2];
140
+ const vx = b[3]; const vy = b[4]; const vz = b[5];
141
+ const nx = b[6]; const ny = b[7]; const nz = b[8];
142
+
143
+ const inv_W1 = 1 / (W - 1);
144
+ const inv_H1 = 1 / (H - 1);
145
+
146
+ let count = 0;
147
+ let cursor = offset;
148
+
149
+ for (let j = j_first; j <= j_last; j++) {
150
+ const v01_lo = j * inv_H1;
151
+ const v01_hi = (j + 1) * inv_H1;
152
+ const v_lo_coord = -half_v + size_z * v01_lo;
153
+ const v_hi_coord = -half_v + size_z * v01_hi;
154
+
155
+ for (let i = i_first; i <= i_last; i++) {
156
+ const u01_lo = i * inv_W1;
157
+ const u01_hi = (i + 1) * inv_W1;
158
+ const u_lo_coord = -half_u + size_x * u01_lo;
159
+ const u_hi_coord = -half_u + size_x * u01_hi;
160
+
161
+ const hA = sampler.sampleChannelCatmullRomUV(u01_lo, v01_lo, 0);
162
+ const hB = sampler.sampleChannelCatmullRomUV(u01_hi, v01_lo, 0);
163
+ const hC = sampler.sampleChannelCatmullRomUV(u01_hi, v01_hi, 0);
164
+ const hD = sampler.sampleChannelCatmullRomUV(u01_lo, v01_hi, 0);
165
+
166
+ const Ax = ux * u_lo_coord + vx * v_lo_coord + nx * hA;
167
+ const Ay = uy * u_lo_coord + vy * v_lo_coord + ny * hA;
168
+ const Az = uz * u_lo_coord + vz * v_lo_coord + nz * hA;
169
+
170
+ const Bx = ux * u_hi_coord + vx * v_lo_coord + nx * hB;
171
+ const By = uy * u_hi_coord + vy * v_lo_coord + ny * hB;
172
+ const Bz = uz * u_hi_coord + vz * v_lo_coord + nz * hB;
173
+
174
+ const Cx = ux * u_hi_coord + vx * v_hi_coord + nx * hC;
175
+ const Cy = uy * u_hi_coord + vy * v_hi_coord + ny * hC;
176
+ const Cz = uz * u_hi_coord + vz * v_hi_coord + nz * hC;
177
+
178
+ const Dx = ux * u_lo_coord + vx * v_hi_coord + nx * hD;
179
+ const Dy = uy * u_lo_coord + vy * v_hi_coord + ny * hD;
180
+ const Dz = uz * u_lo_coord + vz * v_hi_coord + nz * hD;
181
+
182
+ const cell_idx = j * (W - 1) + i;
183
+ const fid_base = cell_idx * 2;
184
+
185
+ // triangle 0: (A, D, B)
186
+ output[cursor ] = Ax; output[cursor + 1] = Ay; output[cursor + 2] = Az;
187
+ output[cursor + 3] = Dx; output[cursor + 4] = Dy; output[cursor + 5] = Dz;
188
+ output[cursor + 6] = Bx; output[cursor + 7] = By; output[cursor + 8] = Bz;
189
+ output[cursor + TRIANGLE_FEATURE_ID_OFFSET] = fid_base;
190
+ cursor += TRIANGLE_FLOAT_STRIDE;
191
+ count++;
192
+
193
+ // triangle 1: (D, C, B)
194
+ output[cursor ] = Dx; output[cursor + 1] = Dy; output[cursor + 2] = Dz;
195
+ output[cursor + 3] = Cx; output[cursor + 4] = Cy; output[cursor + 5] = Cz;
196
+ output[cursor + 6] = Bx; output[cursor + 7] = By; output[cursor + 8] = Bz;
197
+ output[cursor + TRIANGLE_FEATURE_ID_OFFSET] = fid_base + 1;
198
+ cursor += TRIANGLE_FLOAT_STRIDE;
199
+ count++;
200
+ }
201
+ }
202
+
203
+ return count;
204
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Write the mesh's surface triangles that may overlap the given query
3
+ * AABB into `output`, starting at float-index `offset`.
4
+ *
5
+ * Each triangle occupies {@link TRIANGLE_FLOAT_STRIDE} consecutive floats:
6
+ * [+0..+2] vA.xyz (shape's body-local frame, i.e. read directly from
7
+ * {@link MeshShape3D#positions})
8
+ * [+3..+5] vB.xyz
9
+ * [+6..+8] vC.xyz
10
+ * [+9] feature_id (the surface triangle's index)
11
+ *
12
+ * Triangles are AABB-filtered: a triangle is emitted iff its tight AABB
13
+ * (min/max across its 3 vertices) overlaps the query AABB. This is a
14
+ * linear O(N) scan over the mesh's surface triangles — no surface-BVH
15
+ * yet (the existing BVH on MeshShape3D is over the *tet* decomposition
16
+ * used by `contains_point`, with different leaf semantics). For meshes
17
+ * up to a few thousand triangles the linear scan is comfortably faster
18
+ * than a BVH would be after broadphase has already given us a tight
19
+ * query AABB; we can graduate to a per-mesh surface BVH later.
20
+ *
21
+ * Determinism: triangles are emitted in the natural index order, so
22
+ * feature_id values inside `output` rise monotonically over a single
23
+ * call. The narrowphase relies on this for stable contact-manifold
24
+ * pairing across frames.
25
+ *
26
+ * Caller is responsible for ensuring
27
+ * `output.length - offset >= shape.indices.length / 3 * TRIANGLE_FLOAT_STRIDE`
28
+ * in the worst case (every triangle overlaps).
29
+ *
30
+ * @param {Float64Array} output
31
+ * @param {number} offset float-index into output
32
+ * @param {MeshShape3D} shape
33
+ * @param {number} aabb_min_x query AABB in shape's body-local frame
34
+ * @param {number} aabb_min_y
35
+ * @param {number} aabb_min_z
36
+ * @param {number} aabb_max_x
37
+ * @param {number} aabb_max_y
38
+ * @param {number} aabb_max_z
39
+ * @returns {number} number of triangles written
40
+ */
41
+ export function mesh_enumerate_triangles(output: Float64Array, offset: number, shape: MeshShape3D, aabb_min_x: number, aabb_min_y: number, aabb_min_z: number, aabb_max_x: number, aabb_max_y: number, aabb_max_z: number): number;
42
+ //# sourceMappingURL=mesh_enumerate_triangles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mesh_enumerate_triangles.d.ts","sourceRoot":"","sources":["../../../../../../src/engine/physics/narrowphase/decomposition/mesh_enumerate_triangles.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,iDAXW,YAAY,UACZ,MAAM,kCAEN,MAAM,cACN,MAAM,cACN,MAAM,cACN,MAAM,cACN,MAAM,cACN,MAAM,GACJ,MAAM,CAqDlB"}
@@ -0,0 +1,94 @@
1
+ import { TRIANGLE_FEATURE_ID_OFFSET, TRIANGLE_FLOAT_STRIDE } from "./triangle_buffer_layout.js";
2
+
3
+ /**
4
+ * Write the mesh's surface triangles that may overlap the given query
5
+ * AABB into `output`, starting at float-index `offset`.
6
+ *
7
+ * Each triangle occupies {@link TRIANGLE_FLOAT_STRIDE} consecutive floats:
8
+ * [+0..+2] vA.xyz (shape's body-local frame, i.e. read directly from
9
+ * {@link MeshShape3D#positions})
10
+ * [+3..+5] vB.xyz
11
+ * [+6..+8] vC.xyz
12
+ * [+9] feature_id (the surface triangle's index)
13
+ *
14
+ * Triangles are AABB-filtered: a triangle is emitted iff its tight AABB
15
+ * (min/max across its 3 vertices) overlaps the query AABB. This is a
16
+ * linear O(N) scan over the mesh's surface triangles — no surface-BVH
17
+ * yet (the existing BVH on MeshShape3D is over the *tet* decomposition
18
+ * used by `contains_point`, with different leaf semantics). For meshes
19
+ * up to a few thousand triangles the linear scan is comfortably faster
20
+ * than a BVH would be after broadphase has already given us a tight
21
+ * query AABB; we can graduate to a per-mesh surface BVH later.
22
+ *
23
+ * Determinism: triangles are emitted in the natural index order, so
24
+ * feature_id values inside `output` rise monotonically over a single
25
+ * call. The narrowphase relies on this for stable contact-manifold
26
+ * pairing across frames.
27
+ *
28
+ * Caller is responsible for ensuring
29
+ * `output.length - offset >= shape.indices.length / 3 * TRIANGLE_FLOAT_STRIDE`
30
+ * in the worst case (every triangle overlaps).
31
+ *
32
+ * @param {Float64Array} output
33
+ * @param {number} offset float-index into output
34
+ * @param {MeshShape3D} shape
35
+ * @param {number} aabb_min_x query AABB in shape's body-local frame
36
+ * @param {number} aabb_min_y
37
+ * @param {number} aabb_min_z
38
+ * @param {number} aabb_max_x
39
+ * @param {number} aabb_max_y
40
+ * @param {number} aabb_max_z
41
+ * @returns {number} number of triangles written
42
+ */
43
+ export function mesh_enumerate_triangles(
44
+ output, offset, shape,
45
+ aabb_min_x, aabb_min_y, aabb_min_z,
46
+ aabb_max_x, aabb_max_y, aabb_max_z
47
+ ) {
48
+ const positions = shape.positions;
49
+ const indices = shape.indices;
50
+
51
+ const tri_count = (indices.length / 3) | 0;
52
+ if (tri_count === 0) {
53
+ return 0;
54
+ }
55
+
56
+ let count = 0;
57
+ let cursor = offset;
58
+
59
+ for (let t = 0; t < tri_count; t++) {
60
+ const i_a = indices[t * 3 ] * 3;
61
+ const i_b = indices[t * 3 + 1] * 3;
62
+ const i_c = indices[t * 3 + 2] * 3;
63
+
64
+ const ax = positions[i_a ]; const ay = positions[i_a + 1]; const az = positions[i_a + 2];
65
+ const bx = positions[i_b ]; const by = positions[i_b + 1]; const bz = positions[i_b + 2];
66
+ const cx = positions[i_c ]; const cy = positions[i_c + 1]; const cz = positions[i_c + 2];
67
+
68
+ // tight per-triangle AABB
69
+ const tri_min_x = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx);
70
+ if (tri_min_x > aabb_max_x) continue;
71
+ const tri_max_x = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx);
72
+ if (tri_max_x < aabb_min_x) continue;
73
+
74
+ const tri_min_y = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy);
75
+ if (tri_min_y > aabb_max_y) continue;
76
+ const tri_max_y = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
77
+ if (tri_max_y < aabb_min_y) continue;
78
+
79
+ const tri_min_z = az < bz ? (az < cz ? az : cz) : (bz < cz ? bz : cz);
80
+ if (tri_min_z > aabb_max_z) continue;
81
+ const tri_max_z = az > bz ? (az > cz ? az : cz) : (bz > cz ? bz : cz);
82
+ if (tri_max_z < aabb_min_z) continue;
83
+
84
+ output[cursor ] = ax; output[cursor + 1] = ay; output[cursor + 2] = az;
85
+ output[cursor + 3] = bx; output[cursor + 4] = by; output[cursor + 5] = bz;
86
+ output[cursor + 6] = cx; output[cursor + 7] = cy; output[cursor + 8] = cz;
87
+ output[cursor + TRIANGLE_FEATURE_ID_OFFSET] = t;
88
+
89
+ cursor += TRIANGLE_FLOAT_STRIDE;
90
+ count++;
91
+ }
92
+
93
+ return count;
94
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Per-triangle stride in the Float64 decomposition buffer used by the
3
+ * narrowphase to consume non-convex shapes (heightmaps, meshes).
4
+ *
5
+ * Layout per triangle, starting at buffer[base]:
6
+ *
7
+ * [base + 0] vA.x
8
+ * [base + 1] vA.y
9
+ * [base + 2] vA.z
10
+ * [base + 3] vB.x
11
+ * [base + 4] vB.y
12
+ * [base + 5] vB.z
13
+ * [base + 6] vC.x
14
+ * [base + 7] vC.y
15
+ * [base + 8] vC.z
16
+ * [base + 9] feature_id (uint32 packed as f64 — safe to 2^53)
17
+ *
18
+ * Coordinates are in the producer shape's local frame. The feature_id is
19
+ * stable across frames for the same triangle of the same shape — the
20
+ * manifold cache uses it as the warm-start match key.
21
+ *
22
+ * Stride 10 (rather than 9) is the small cost we pay for clean warm-start
23
+ * matching; see the discussion in PLAN.md's "Pair-key → slot map" section
24
+ * for why ID-based matching beats position-distance matching when shapes
25
+ * slide along curved surfaces.
26
+ *
27
+ * @readonly
28
+ * @type {number}
29
+ */
30
+ export const TRIANGLE_FLOAT_STRIDE: number;
31
+ /**
32
+ * Float offset of feature_id within a triangle entry.
33
+ * @readonly
34
+ * @type {number}
35
+ */
36
+ export const TRIANGLE_FEATURE_ID_OFFSET: number;
37
+ //# sourceMappingURL=triangle_buffer_layout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"triangle_buffer_layout.d.ts","sourceRoot":"","sources":["../../../../../../src/engine/physics/narrowphase/decomposition/triangle_buffer_layout.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,oCAFU,MAAM,CAEwB;AAExC;;;;GAIG;AACH,yCAFU,MAAM,CAE4B"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Per-triangle stride in the Float64 decomposition buffer used by the
3
+ * narrowphase to consume non-convex shapes (heightmaps, meshes).
4
+ *
5
+ * Layout per triangle, starting at buffer[base]:
6
+ *
7
+ * [base + 0] vA.x
8
+ * [base + 1] vA.y
9
+ * [base + 2] vA.z
10
+ * [base + 3] vB.x
11
+ * [base + 4] vB.y
12
+ * [base + 5] vB.z
13
+ * [base + 6] vC.x
14
+ * [base + 7] vC.y
15
+ * [base + 8] vC.z
16
+ * [base + 9] feature_id (uint32 packed as f64 — safe to 2^53)
17
+ *
18
+ * Coordinates are in the producer shape's local frame. The feature_id is
19
+ * stable across frames for the same triangle of the same shape — the
20
+ * manifold cache uses it as the warm-start match key.
21
+ *
22
+ * Stride 10 (rather than 9) is the small cost we pay for clean warm-start
23
+ * matching; see the discussion in PLAN.md's "Pair-key → slot map" section
24
+ * for why ID-based matching beats position-distance matching when shapes
25
+ * slide along curved surfaces.
26
+ *
27
+ * @readonly
28
+ * @type {number}
29
+ */
30
+ export const TRIANGLE_FLOAT_STRIDE = 10;
31
+
32
+ /**
33
+ * Float offset of feature_id within a triangle entry.
34
+ * @readonly
35
+ * @type {number}
36
+ */
37
+ export const TRIANGLE_FEATURE_ID_OFFSET = 9;
@@ -5,7 +5,46 @@
5
5
  *
6
6
  * @param {PairList} pair_list
7
7
  * @param {ManifoldStore} manifolds
8
- * @param {PhysicsSystem} system supplies per-body collider lists + transforms
8
+ * @param {Array<Array<{collider: Collider, transform: Transform}>>} lists
9
+ * per-body collider lists, indexed by body-storage slot index. Typically
10
+ * `system.__body_collider_lists` — passed in directly so this helper
11
+ * has no dependency on `PhysicsSystem`.
9
12
  */
10
- export function narrowphase_step(pair_list: PairList, manifolds: ManifoldStore, system: PhysicsSystem): void;
13
+ export function narrowphase_step(pair_list: PairList, manifolds: ManifoldStore, lists: Array<Array<{
14
+ collider: Collider;
15
+ transform: Transform;
16
+ }>>): void;
17
+ /**
18
+ * Re-detect contact GEOMETRY for one existing manifold slot at the bodies'
19
+ * current poses, updating the witness points / normal / depth of the slot's
20
+ * existing contacts in place. Does NOT change the contact count, the
21
+ * feature ids, or the accumulated impulses — it only refreshes geometry.
22
+ *
23
+ * This is the per-substep concave path (TGS): for a contact pair involving a
24
+ * concave body, the contact *feature* (which triangle is deepest, and its
25
+ * normal) genuinely changes as the body rocks, so the solver's cheap analytic
26
+ * refresh — which freezes the feature for the whole outer step — pumps energy
27
+ * in. Re-running the narrowphase geometry each substep gives a fresh,
28
+ * correct normal so the body settles instead of rocking. Convex pairs keep
29
+ * the analytic refresh and never call this (their feature is stable).
30
+ *
31
+ * Matching is by feature id (stable per-triangle for the decomposition path),
32
+ * so a contact's geometry tracks the same triangle across substeps. A contact
33
+ * whose triangle isn't found this substep keeps its previous geometry (a rare
34
+ * transient; the once-per-frame {@link narrowphase_step} re-establishes the
35
+ * contact set next outer step). Count never changes here, so the solver's
36
+ * per-contact scratch (sized once at prepare) stays aligned.
37
+ *
38
+ * @param {ManifoldStore} manifolds
39
+ * @param {number} slot
40
+ * @param {Array<{collider: Collider, transform: Transform}>} list_a
41
+ * @param {Array<{collider: Collider, transform: Transform}>} list_b
42
+ */
43
+ export function redetect_pair_geometry(manifolds: ManifoldStore, slot: number, list_a: Array<{
44
+ collider: Collider;
45
+ transform: Transform;
46
+ }>, list_b: Array<{
47
+ collider: Collider;
48
+ transform: Transform;
49
+ }>): void;
11
50
  //# sourceMappingURL=narrowphase_step.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"narrowphase_step.d.ts","sourceRoot":"","sources":["../../../../../src/engine/physics/narrowphase/narrowphase_step.js"],"names":[],"mappings":"AAsUA;;;;;;;;GAQG;AACH,6GA8CC"}
1
+ {"version":3,"file":"narrowphase_step.d.ts","sourceRoot":"","sources":["../../../../../src/engine/physics/narrowphase/narrowphase_step.js"],"names":[],"mappings":"AA6uCA;;;;;;;;;;;GAWG;AACH,uFALW,MAAM,MAAM;IAAC,QAAQ,WAAW;IAAC,SAAS,YAAW;CAAC,CAAC,CAAC,QAyJlE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,uEAJW,MAAM,UACN,MAAM;IAAC,QAAQ,WAAW;IAAC,SAAS,YAAW;CAAC,CAAC,UACjD,MAAM;IAAC,QAAQ,WAAW;IAAC,SAAS,YAAW;CAAC,CAAC,QAiD3D"}