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