nphysics3d/algebra/
inertia2.rs1use std::mem;
2use std::ops::{Add, AddAssign, Mul, Neg};
3
4use crate::algebra::{Force2, Velocity2};
5use na::{self, Isometry2, Matrix1, Matrix3, RealField, Vector3};
6
7#[derive(Clone, Copy, Debug)]
9pub struct Inertia2<N: RealField + Copy> {
10 pub linear: N,
12 pub angular: N,
14}
15
16impl<N: RealField + Copy> Inertia2<N> {
17 pub fn new(linear: N, angular: N) -> Self {
19 Inertia2 { linear, angular }
20 }
21
22 pub fn new_with_angular_matrix(linear: N, angular: Matrix1<N>) -> Self {
24 Self::new(linear, angular.x)
25 }
26
27 pub fn mass(&self) -> N {
29 self.linear
30 }
31
32 pub fn inv_mass(&self) -> N {
36 if self.linear.is_zero() {
37 N::zero()
38 } else {
39 self.linear
40 }
41 }
42
43 pub fn zero() -> Self {
45 Inertia2::new(na::zero(), na::zero())
46 }
47
48 #[inline]
50 pub fn angular_matrix(&self) -> &Matrix1<N> {
51 unsafe { mem::transmute(&self.angular) }
52 }
53
54 pub fn to_matrix(&self) -> Matrix3<N> {
58 let diag = Vector3::new(self.linear, self.linear, self.angular);
59 Matrix3::from_diagonal(&diag)
60 }
61
62 pub fn transformed(&self, _: &Isometry2<N>) -> Self {
64 *self
65 }
66
67 pub fn inverse(&self) -> Self {
71 let inv_mass = if self.linear.is_zero() {
72 N::zero()
73 } else {
74 N::one() / self.linear
75 };
76 let inv_angular = if self.angular.is_zero() {
77 N::zero()
78 } else {
79 N::one() / self.angular
80 };
81 Inertia2::new(inv_mass, inv_angular)
82 }
83}
84
85impl<N: RealField + Copy> Neg for Inertia2<N> {
86 type Output = Self;
87
88 #[inline]
89 fn neg(self) -> Self {
90 Self::new(-self.linear, -self.angular)
91 }
92}
93
94impl<N: RealField + Copy> Add<Inertia2<N>> for Inertia2<N> {
95 type Output = Inertia2<N>;
96
97 #[inline]
98 fn add(self, rhs: Inertia2<N>) -> Inertia2<N> {
99 Inertia2::new(self.linear + rhs.linear, self.angular + rhs.angular)
100 }
101}
102
103impl<N: RealField + Copy> AddAssign<Inertia2<N>> for Inertia2<N> {
104 #[inline]
105 fn add_assign(&mut self, rhs: Inertia2<N>) {
106 self.linear += rhs.linear;
107 self.angular += rhs.angular;
108 }
109}
110
111impl<N: RealField + Copy> Mul<Velocity2<N>> for Inertia2<N> {
112 type Output = Force2<N>;
113
114 #[inline]
115 fn mul(self, rhs: Velocity2<N>) -> Force2<N> {
116 Force2::new(rhs.linear * self.linear, self.angular * rhs.angular)
117 }
118}
119
120impl<N: RealField + Copy> Mul<Force2<N>> for Inertia2<N> {
122 type Output = Velocity2<N>;
123
124 #[inline]
125 fn mul(self, rhs: Force2<N>) -> Velocity2<N> {
126 Velocity2::new(rhs.linear * self.linear, self.angular * rhs.angular)
127 }
128}