Skip to main content

linear_sim/
constraint.rs

1//! Simulation constraint entities
2
3#[cfg(feature = "derive_serdes")]
4use serde::{Deserialize, Serialize};
5
6use crate::{collision, component, geometry, math, object};
7
8/// Constraint interface trait
9pub trait Constraint <STATE> {
10  fn evaluate_position (&self, state : &STATE) -> f64;
11  fn evaluate_velocity (&self, state : &STATE) -> f64;
12}
13
14/// A planar constraint
15#[cfg_attr(feature = "derive_serdes", derive(Deserialize, Serialize))]
16#[derive(Clone, Debug, PartialEq)]
17pub struct Planar {
18  plane : geometry::primitive::Plane3 <f64>
19}
20
21/// An inter-penetration constraint
22#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct Penetration <'a, A, B> where
24  A : object::Bounded + 'static,
25  B : object::Bounded + 'static
26{
27  pub object_a : &'a A,
28  pub object_b : &'a B
29}
30
31impl Planar {
32  #[inline]
33  pub const fn new (position : math::Point3 <f64>, normal : math::Unit3 <f64>) -> Self {
34    let base  = position;
35    let plane = geometry::primitive::Plane3 { base, normal };
36    Planar { plane }
37  }
38
39  pub const fn position (&self) -> math::Point3 <f64> {
40    self.plane.base
41  }
42  pub const fn normal (&self) -> math::Unit3 <f64> {
43    self.plane.normal
44  }
45}
46
47impl From <collision::Proximity> for Planar {
48  fn from (proximity : collision::Proximity) -> Self {
49    proximity.constraint_planar()
50  }
51}
52
53#[expect(clippy::renamed_function_params)]
54impl <O : object::Temporal> Constraint <O> for Planar {
55  fn evaluate_position (&self, object : &O) -> f64 {
56    let component::Position (position) = &object.position();
57    (position.0 - self.position().0).dot (*self.normal())
58  }
59  fn evaluate_velocity (&self, object : &O) -> f64 {
60    (object.derivatives().velocity - self.position().0).dot (*self.normal())
61  }
62}
63
64impl <A, B> Penetration <'_, A, B> where A : object::Bounded, B : object::Bounded {
65  /// Apply a linear translation based on the result of a distance query on the objects.
66  ///
67  /// If distance query indicates the objects are not intersecting, 'None' is returned.
68  pub fn resolve_position (&self) -> Option <(A, B)> {
69    let distance = collision::Proximity::query (self.object_a, self.object_b);
70    match distance.relation() {
71      collision::proximity::Relation::Intersect => {
72        let mut out_a = self.object_a.clone();
73        let mut out_b = self.object_b.clone();
74        let component::Position (position_a) = self.object_a.position();
75        let component::Position (position_b) = self.object_b.position();
76        *out_a.position_mut() = component::Position (position_a + distance.half_axis);
77        *out_b.position_mut() = component::Position (position_b - distance.half_axis);
78        Some ((out_a, out_b))
79      }
80      _ => None
81    }
82  }
83}
84
85// TODO: penetration constraint impl