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, 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 fn new (
34 position : math::Point3 <f64>, normal : math::Unit3 <f64>
35 ) -> Self {
36 let base = position;
37 let plane = geometry::primitive::Plane3 { base, normal };
38 Planar { plane }
39 }
40
41 pub fn position (&self) -> math::Point3 <f64> {
42 self.plane.base
43 }
44 pub fn normal (&self) -> math::Unit3 <f64> {
45 self.plane.normal
46 }
47}
48
49impl From <collision::Proximity> for Planar {
50 fn from (proximity : collision::Proximity) -> Self {
51 Planar::new (proximity.midpoint, proximity.normal)
52 }
53}
54
55impl <O : object::Temporal> Constraint <O> for Planar {
56 fn evaluate_position (&self, object : &O) -> f64 {
57 let component::Position (position) = &object.position();
58 (position.0 - self.position().0).dot (*self.normal())
59 }
60 fn evaluate_velocity (&self, object : &O) -> f64 {
61 (object.derivatives().velocity - self.position().0)
62 .dot (*self.normal())
63 }
64}
65
66impl <'a, A, B> Penetration <'a, A, B> where
67 A : object::Bounded, B : object::Bounded
68{
69
70 pub fn resolve_position (&self) -> Option <(A, B)> {
76 let distance = collision::Proximity::query (self.object_a, self.object_b);
77 match distance.relation() {
78 collision::proximity::Relation::Intersect => {
79 let mut out_a = self.object_a.clone();
80 let mut out_b = self.object_b.clone();
81 let component::Position (position_a) = self.object_a.position();
82 let component::Position (position_b) = self.object_b.position();
83 *out_a.position_mut() =
84 component::Position (position_a + distance.half_axis);
85 *out_b.position_mut() =
86 component::Position (position_b - distance.half_axis);
87 Some ((out_a, out_b))
88 }
89 _ => None
90 }
91 }
92
93}
94
95