bevy_tnua_physics_integration_layer/
math.rs

1#[cfg(feature = "f64")]
2pub type Float = f64;
3#[cfg(not(feature = "f64"))]
4pub type Float = f32;
5
6#[cfg(not(feature = "f64"))]
7pub use std::f32::consts as float_consts;
8#[cfg(feature = "f64")]
9pub use std::f64::consts as float_consts;
10
11use bevy::math::{DQuat, DVec2, DVec3};
12use bevy::math::{Quat, Vec2, Vec3};
13
14#[cfg(feature = "f64")]
15pub type Vector3 = DVec3;
16#[cfg(not(feature = "f64"))]
17pub type Vector3 = Vec3;
18
19#[cfg(feature = "f64")]
20pub type Vector2 = DVec2;
21#[cfg(not(feature = "f64"))]
22pub type Vector2 = Vec2;
23
24#[cfg(feature = "f64")]
25pub type Quaternion = DQuat;
26#[cfg(not(feature = "f64"))]
27pub type Quaternion = Quat;
28
29// Taken from `avian` https://github.com/Jondolf/avian/blob/main/src/math/double.rs#L39
30/// Adjust the precision of the math construct to the precision chosen for compilation.
31pub trait AdjustPrecision {
32    /// A math construct type with the desired precision.
33    type Adjusted;
34    /// Adjusts the precision of [`self`] to [`Self::Adjusted`](#associatedtype.Adjusted).
35    fn adjust_precision(&self) -> Self::Adjusted;
36}
37
38impl AdjustPrecision for f32 {
39    type Adjusted = Float;
40    fn adjust_precision(&self) -> Self::Adjusted {
41        #[cfg(feature = "f64")]
42        return (*self).into();
43        #[cfg(not(feature = "f64"))]
44        return *self;
45    }
46}
47
48#[cfg(feature = "f64")]
49impl AdjustPrecision for f64 {
50    type Adjusted = Float;
51    fn adjust_precision(&self) -> Self::Adjusted {
52        #[cfg(feature = "f64")]
53        return *self;
54        #[cfg(not(feature = "f64"))]
55        return (*self).into();
56    }
57}
58
59impl AdjustPrecision for Vec3 {
60    type Adjusted = Vector3;
61    fn adjust_precision(&self) -> Self::Adjusted {
62        #[cfg(feature = "f64")]
63        return self.as_dvec3();
64        #[cfg(not(feature = "f64"))]
65        return *self;
66    }
67}
68
69#[cfg(feature = "f64")]
70impl AdjustPrecision for DVec3 {
71    type Adjusted = Vector3;
72    fn adjust_precision(&self) -> Self::Adjusted {
73        #[cfg(feature = "f64")]
74        return *self;
75        #[cfg(not(feature = "f64"))]
76        return self.as_vec3();
77    }
78}
79
80impl AdjustPrecision for Quat {
81    type Adjusted = Quaternion;
82    fn adjust_precision(&self) -> Self::Adjusted {
83        #[cfg(feature = "f64")]
84        return self.as_dquat();
85        #[cfg(not(feature = "f64"))]
86        return *self;
87    }
88}
89
90#[cfg(feature = "f64")]
91impl AdjustPrecision for DQuat {
92    type Adjusted = Quaternion;
93    fn adjust_precision(&self) -> Self::Adjusted {
94        #[cfg(feature = "f64")]
95        return *self;
96        #[cfg(not(feature = "f64"))]
97        return self.as_quat();
98    }
99}
100
101impl AdjustPrecision for Vec2 {
102    type Adjusted = Vector2;
103    fn adjust_precision(&self) -> Self::Adjusted {
104        #[cfg(feature = "f64")]
105        return self.as_dvec2();
106        #[cfg(not(feature = "f64"))]
107        return *self;
108    }
109}
110
111#[cfg(feature = "f64")]
112impl AdjustPrecision for DVec2 {
113    type Adjusted = Vector2;
114    fn adjust_precision(&self) -> Self::Adjusted {
115        #[cfg(feature = "f64")]
116        return *self;
117        #[cfg(not(feature = "f64"))]
118        return self.as_vec2();
119    }
120}
121
122/// Adjust the precision down to `f32` regardless of compilation.
123pub trait AsF32 {
124    /// The `f32` version of a math construct.
125    type F32;
126    /// Returns the `f32` version of this type.
127    fn f32(&self) -> Self::F32;
128}
129
130impl AsF32 for f32 {
131    type F32 = f32;
132    fn f32(&self) -> Self::F32 {
133        *self
134    }
135}
136
137impl AsF32 for f64 {
138    type F32 = f32;
139    fn f32(&self) -> Self::F32 {
140        *self as f32
141    }
142}
143
144impl AsF32 for DVec3 {
145    type F32 = Vec3;
146    fn f32(&self) -> Self::F32 {
147        self.as_vec3()
148    }
149}
150
151impl AsF32 for Vec3 {
152    type F32 = Self;
153    fn f32(&self) -> Self::F32 {
154        *self
155    }
156}
157
158impl AsF32 for DVec2 {
159    type F32 = Vec2;
160    fn f32(&self) -> Self::F32 {
161        self.as_vec2()
162    }
163}
164
165impl AsF32 for Vec2 {
166    type F32 = Self;
167    fn f32(&self) -> Self::F32 {
168        *self
169    }
170}
171
172impl AsF32 for DQuat {
173    type F32 = Quat;
174    fn f32(&self) -> Self::F32 {
175        self.as_quat()
176    }
177}
178
179impl AsF32 for Quat {
180    type F32 = Quat;
181    fn f32(&self) -> Self::F32 {
182        *self
183    }
184}