Skip to main content

euv_core/game/algorithms/kinematics/
struct.rs

1use super::*;
2
3/// Angular cone joint limit applied during Cyclic Coordinate Descent (CCD) IK solving.
4#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
5pub struct IkBoneConstraint {
6    /// Maximum allowed bend half-angle in radians from the parent bone axis (or `reference_axis` at root).
7    #[get(type(copy))]
8    pub(crate) max_cone_angle_rad: f32,
9    /// Reference axis used at root joint index `0` when no parent bone exists.
10    #[get(type(copy))]
11    pub(crate) reference_axis: Vec3,
12}
13
14/// Result returned by analytic 2-bone inverse kinematics (`IkSolver3D::solve_two_bone`).
15#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
16pub struct IkTwoBoneResult {
17    /// Solved middle joint (elbow or knee) position in world space.
18    #[get(type(copy))]
19    pub(crate) joint_mid: Vec3,
20    /// Solved end-effector position in world space.
21    #[get(type(copy))]
22    pub(crate) effector: Vec3,
23    /// World orientation rotating `+Y` onto the upper bone segment.
24    #[get(type(copy))]
25    pub(crate) upper_rotation: Quat,
26    /// World orientation rotating `+Y` onto the lower bone segment.
27    #[get(type(copy))]
28    pub(crate) lower_rotation: Quat,
29    /// Residual Euclidean distance from `effector` to requested target.
30    #[get(type(copy))]
31    pub(crate) error: f32,
32    /// Whether `error` satisfied reach tolerance.
33    #[get(type(copy))]
34    pub(crate) reached: bool,
35}
36
37/// Convergence report returned by iterative multi-bone IK solvers (FABRIK and CCD).
38#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
39pub struct IkChainResult {
40    /// Number of solver iterations executed.
41    #[get(type(copy))]
42    pub(crate) iterations: usize,
43    /// Final Euclidean distance from end-effector to target.
44    #[get(type(copy))]
45    pub(crate) error: f32,
46    /// Whether the final error met the caller's tolerance.
47    #[get(type(copy))]
48    pub(crate) reached: bool,
49}
50
51/// Stateless 3D Inverse Kinematics solver suite (Analytic 2-Bone, FABRIK, and cone-limited CCD).
52#[derive(Clone, Copy, Data, Debug, Default, New, PartialEq)]
53pub struct IkSolver3D {
54    /// Default convergence tolerance in world units.
55    #[get(type(copy))]
56    pub(crate) default_tolerance: f32,
57}
58
59/// Triangle face stored inside the Expanding Polytope Algorithm (EPA) hull.
60#[derive(Clone, Copy, Debug)]
61pub(crate) struct EpaFace3D {
62    /// Vertex indices `(i0, i1, i2)` in counter-clockwise outward winding.
63    pub(crate) indices: [usize; 3],
64    /// Outward unit face normal.
65    pub(crate) normal: Vec3,
66    /// Signed distance from the origin along `normal`.
67    pub(crate) distance: f32,
68}
69
70/// Reusable scratch buffers for zero-allocation GJK + EPA collision queries.
71#[derive(Clone, Debug, Default)]
72pub(crate) struct GjkEpaScratch {
73    /// Polytope vertices expanded during EPA.
74    pub(crate) vertices: Vec<Vec3>,
75    /// Active polytope boundary faces.
76    pub(crate) faces: Vec<EpaFace3D>,
77    /// Silhouette boundary half-edges gathered during polytope horizon stitching.
78    pub(crate) loose_edges: Vec<(usize, usize)>,
79}
80
81/// Exact Minimum Translation Vector (MTV) contact produced by `GjkEpaSolver3D`.
82#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
83pub struct GjkEpaContact3D {
84    /// Unit separation normal pushing shape A out of shape B (translate A by `normal * depth`).
85    #[get(type(copy))]
86    pub(crate) normal: Vec3,
87    /// Exact non-negative penetration depth along `normal`.
88    #[get(type(copy))]
89    pub(crate) depth: f32,
90}
91
92/// Reusable 3D Gilbert-Johnson-Keerthi (GJK) and Expanding Polytope Algorithm (EPA) convex collision solver.
93#[derive(Clone, Data, Debug, Default)]
94pub struct GjkEpaSolver3D {
95    /// Pre-allocated EPA polytope scratch buffers.
96    #[get(pub(crate))]
97    #[get_mut(pub(crate))]
98    #[set(pub(crate))]
99    pub(crate) scratch: GjkEpaScratch,
100}
101
102/// Single Verlet / PBD particle with inverse mass (`0.0` denotes a permanently pinned anchor).
103#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
104pub struct VerletParticle3D {
105    /// Current world-space particle position.
106    #[get(type(copy))]
107    pub(crate) position: Vec3,
108    /// Previous-step world-space position encoding implicit velocity.
109    #[get(type(copy))]
110    pub(crate) prev_position: Vec3,
111    /// Reciprocal mass (`0.0` = immovable pinned anchor).
112    #[get(type(copy))]
113    pub(crate) inv_mass: f32,
114}
115
116/// Pairwise distance or bending constraint between two Verlet particles.
117#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
118pub struct VerletDistanceConstraint {
119    /// First particle index.
120    #[get(type(copy))]
121    pub(crate) index_a: usize,
122    /// Second particle index.
123    #[get(type(copy))]
124    pub(crate) index_b: usize,
125    /// Target rest distance between particles.
126    #[get(type(copy))]
127    pub(crate) rest_length: f32,
128    /// Constraint projection stiffness in `(0.0, 1.0]`.
129    #[get(type(copy))]
130    pub(crate) stiffness: f32,
131}
132
133/// Solid sphere collider pushing dynamic Verlet particles out to `radius`.
134#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
135pub struct VerletSphereCollider {
136    /// World-space sphere center.
137    #[get(type(copy))]
138    pub(crate) center: Vec3,
139    /// Positive sphere radius.
140    #[get(type(copy))]
141    pub(crate) radius: f32,
142}
143
144/// Solid infinite half-space plane collider enforcing `position ยท normal >= offset`.
145#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
146pub struct VerletPlaneCollider {
147    /// Unit outward plane normal.
148    #[get(type(copy))]
149    pub(crate) normal: Vec3,
150    /// Signed plane offset along `normal`.
151    #[get(type(copy))]
152    pub(crate) offset: f32,
153}
154
155/// Position-Based Dynamics / Verlet 3D cloth grid solver with structural, shear, and bend constraints.
156#[derive(Clone, Data, Debug)]
157pub struct VerletClothSolver {
158    /// Number of cloth vertex columns.
159    #[get(type(copy))]
160    pub(crate) cols: usize,
161    /// Number of cloth vertex rows.
162    #[get(type(copy))]
163    pub(crate) rows: usize,
164    /// Gravity acceleration vector (e.g. `(0, -9.81, 0)`).
165    #[get(type(copy))]
166    pub(crate) gravity: Vec3,
167    /// Wind acceleration vector acting on free particles.
168    #[get(type(copy))]
169    pub(crate) wind: Vec3,
170    /// Velocity damping factor per step in `(0.0, 1.0]`.
171    #[get(type(copy))]
172    pub(crate) damping: f32,
173    /// Gauss-Seidel constraint projection iterations per step.
174    #[get(type(copy))]
175    pub(crate) solver_iterations: usize,
176    /// Simulated cloth particle array of length `cols * rows`.
177    pub(crate) particles: Vec<VerletParticle3D>,
178    /// Structural, shear, and 2-hop bend distance constraints.
179    pub(crate) constraints: Vec<VerletDistanceConstraint>,
180    /// Registered solid sphere colliders.
181    pub(crate) sphere_colliders: Vec<VerletSphereCollider>,
182    /// Registered solid plane colliders.
183    pub(crate) plane_colliders: Vec<VerletPlaneCollider>,
184}
185
186/// Position-Based Dynamics / Verlet 3D rope solver with distance and bend constraints.
187#[derive(Clone, Data, Debug)]
188pub struct VerletRopeSolver {
189    /// Gravity acceleration vector.
190    #[get(type(copy))]
191    pub(crate) gravity: Vec3,
192    /// External wind acceleration vector.
193    #[get(type(copy))]
194    pub(crate) wind: Vec3,
195    /// Velocity damping factor per step in `(0.0, 1.0]`.
196    #[get(type(copy))]
197    pub(crate) damping: f32,
198    /// Constraint projection iterations per step.
199    #[get(type(copy))]
200    pub(crate) solver_iterations: usize,
201    /// Simulated rope particle chain.
202    pub(crate) particles: Vec<VerletParticle3D>,
203    /// Structural and 2-hop bend distance constraints.
204    pub(crate) constraints: Vec<VerletDistanceConstraint>,
205    /// Registered solid sphere colliders.
206    pub(crate) sphere_colliders: Vec<VerletSphereCollider>,
207    /// Registered solid plane colliders.
208    pub(crate) plane_colliders: Vec<VerletPlaneCollider>,
209}
210
211/// Keyframe control point along a Catmull-Rom cinematic camera spline rail.
212#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
213pub struct CameraSplinePoint {
214    /// Camera eye position at this control point.
215    #[get(type(copy))]
216    pub(crate) position: Vec3,
217    /// World look-at target position at this control point.
218    #[get(type(copy))]
219    pub(crate) look_target: Vec3,
220    /// Vertical field of view in degrees.
221    #[get(type(copy))]
222    pub(crate) fov_deg: f32,
223}
224
225/// Configuration settings for `CameraRig3D` across Spring-Arm, Orbit, First-Person, Spline, and Shake modes.
226#[derive(Clone, Copy, Data, Debug, PartialEq)]
227pub struct CameraRigConfig {
228    /// Active camera controller mode.
229    #[get(type(copy))]
230    pub(crate) mode: CameraRigMode,
231    /// Desired spring-arm or orbit distance from pivot.
232    #[get(type(copy))]
233    pub(crate) arm_length: f32,
234    /// Minimum arm length during obstacle pullback.
235    #[get(type(copy))]
236    pub(crate) arm_min_length: f32,
237    /// Local target pivot offset (e.g., over-the-shoulder offset).
238    #[get(type(copy))]
239    pub(crate) pivot_offset: Vec3,
240    /// Exponential position tracking stiffness (`1/s`).
241    #[get(type(copy))]
242    pub(crate) position_stiffness: f32,
243    /// Exponential orientation tracking stiffness (`1/s`).
244    #[get(type(copy))]
245    pub(crate) rotation_stiffness: f32,
246    /// Minimum vertical pitch angle in radians.
247    #[get(type(copy))]
248    pub(crate) min_pitch: f32,
249    /// Maximum vertical pitch angle in radians.
250    #[get(type(copy))]
251    pub(crate) max_pitch: f32,
252    /// Eye height above actor origin in `FirstPerson` mode.
253    #[get(type(copy))]
254    pub(crate) eye_height: f32,
255    /// Vertical field of view in degrees.
256    #[get(type(copy))]
257    pub(crate) fov_deg: f32,
258    /// Near clipping distance.
259    #[get(type(copy))]
260    pub(crate) near: f32,
261    /// Far clipping distance.
262    #[get(type(copy))]
263    pub(crate) far: f32,
264    /// Trauma linear decay rate per second.
265    #[get(type(copy))]
266    pub(crate) trauma_decay: f32,
267    /// Maximum translational shake displacement at trauma `1.0`.
268    #[get(type(copy))]
269    pub(crate) shake_max_offset: f32,
270    /// Maximum rotational shake angle in radians at trauma `1.0`.
271    #[get(type(copy))]
272    pub(crate) shake_max_angle_rad: f32,
273    /// Perlin noise sampling frequency in Hz for trauma shake.
274    #[get(type(copy))]
275    pub(crate) shake_frequency: f32,
276    /// Whether `SplineRail` mode wraps seamlessly from the last point back to the first.
277    #[get(type(copy))]
278    pub(crate) spline_loop: bool,
279}
280
281/// Solved world-space camera pose produced by `CameraRig3D` each frame.
282#[derive(Clone, Copy, Data, Debug, New, PartialEq)]
283pub struct CameraRigState {
284    /// Final shaken eye position in world space.
285    #[get(type(copy))]
286    pub(crate) position: Vec3,
287    /// Final look-at focus point in world space.
288    #[get(type(copy))]
289    pub(crate) target: Vec3,
290    /// Final camera orientation quaternion.
291    #[get(type(copy))]
292    pub(crate) rotation: Quat,
293    /// Final yaw angle in radians.
294    #[get(type(copy))]
295    pub(crate) yaw: f32,
296    /// Final pitch angle in radians.
297    #[get(type(copy))]
298    pub(crate) pitch: f32,
299    /// Final camera roll angle in radians (includes trauma roll shake).
300    #[get(type(copy))]
301    pub(crate) roll: f32,
302    /// Active vertical field of view in degrees.
303    #[get(type(copy))]
304    pub(crate) fov_deg: f32,
305}
306
307/// Production 3D game camera rig supporting Spring-Arm, Orbit, First-Person, Catmull-Rom Spline, and Perlin Trauma Shake.
308#[derive(Clone, Data, Debug)]
309pub struct CameraRig3D {
310    /// Camera rig configuration parameters.
311    #[get(type(copy))]
312    pub(crate) config: CameraRigConfig,
313    /// Desired horizontal yaw angle in radians.
314    #[get(type(copy))]
315    pub(crate) desired_yaw: f32,
316    /// Desired vertical pitch angle in radians.
317    #[get(type(copy))]
318    pub(crate) desired_pitch: f32,
319    /// Current smoothed base eye position (prior to trauma shake offset).
320    #[get(type(copy))]
321    pub(crate) smooth_position: Vec3,
322    /// Current smoothed look-at target (prior to trauma shake offset).
323    #[get(type(copy))]
324    pub(crate) smooth_target: Vec3,
325    /// Current smoothed yaw angle.
326    #[get(type(copy))]
327    pub(crate) smooth_yaw: f32,
328    /// Current smoothed pitch angle.
329    #[get(type(copy))]
330    pub(crate) smooth_pitch: f32,
331    /// Current smoothed field of view in degrees.
332    #[get(type(copy))]
333    pub(crate) smooth_fov_deg: f32,
334    /// Current spring-arm distance after obstacle pullback smoothing.
335    #[get(type(copy))]
336    pub(crate) current_arm_length: f32,
337    /// Current trauma intensity in `[0.0, 1.0]`.
338    #[get(type(copy))]
339    pub(crate) trauma: f32,
340    /// Elapsed continuous clock driving Perlin trauma shake.
341    #[get(type(copy))]
342    pub(crate) shake_clock: f32,
343    /// Normalized parameter `[0.0, 1.0]` along `spline_points` in `SplineRail` mode.
344    #[get(type(copy))]
345    pub(crate) spline_t: f32,
346    /// Cinematic Catmull-Rom spline control points.
347    pub(crate) spline_points: Vec<CameraSplinePoint>,
348    /// Deterministic Perlin noise field driving smooth continuous camera trauma shake.
349    #[get(pub(crate))]
350    #[get_mut(pub(crate))]
351    #[set(pub(crate))]
352    pub(crate) noise: NoiseField,
353    /// Latest evaluated camera rig output state.
354    #[get(type(copy))]
355    pub(crate) state: CameraRigState,
356}