1#[cfg(feature = "derive_serdes")]
4use serde::{Deserialize, Serialize};
5
6use crate::{collision, component, geometry, math, object};
7
8pub trait Constraint <STATE> {
10 fn evaluate_position (&self, state : &STATE) -> f64;
11 fn evaluate_velocity (&self, state : &STATE) -> f64;
12}
13
14#[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#[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
65 A : object::Bounded + Clone,
66 B : object::Bounded + Clone
67{
68 pub fn resolve_position (&self) -> Option <(A, B)> {
72 let distance = collision::Proximity::query (self.object_a, self.object_b);
73 match distance.relation() {
74 collision::proximity::Relation::Intersect => {
75 let mut out_a = self.object_a.clone();
76 let mut out_b = self.object_b.clone();
77 let component::Position (position_a) = self.object_a.position();
78 let component::Position (position_b) = self.object_b.position();
79 *out_a.position_mut() = component::Position (position_a + distance.half_axis);
80 *out_b.position_mut() = component::Position (position_b - distance.half_axis);
81 Some ((out_a, out_b))
82 }
83 _ => None
84 }
85 }
86}
87
88