1use core::ops::{Mul, MulAssign};
2use glam::{Mat4, Quat, Vec3};
3
4#[derive(Debug, Clone, Copy, PartialEq, Default)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9#[cfg_attr(
10 feature = "rkyv-08",
11 derive(rkyv_08::Archive, rkyv_08::Serialize, rkyv_08::Deserialize),
12 rkyv(crate = rkyv_08)
13)]
14#[repr(C)]
15pub struct Rigid {
16 pub rotation: Quat,
18 pub translation: Vec3,
20}
21
22impl Rigid {
23 pub const IDENTITY: Self = Self {
25 rotation: Quat::IDENTITY,
26 translation: Vec3::ZERO,
27 };
28
29 #[must_use]
31 pub const fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
32 Self {
33 rotation,
34 translation,
35 }
36 }
37 #[must_use]
39 pub fn inverse(&self) -> Self {
40 let rotation = self.rotation.inverse();
41 Self {
42 rotation,
43 translation: rotation * -self.translation,
44 }
45 }
46 #[must_use]
48 pub fn transform_point(&self, point: Vec3) -> Vec3 {
49 self.rotation * point + self.translation
50 }
51 #[must_use]
53 pub fn transform_vector(&self, vector: Vec3) -> Vec3 {
54 self.rotation * vector
55 }
56 #[must_use]
58 pub fn to_mat4(&self) -> Mat4 {
59 Mat4::from_rotation_translation(self.rotation, self.translation)
60 }
61 #[must_use]
63 pub fn abs_diff_eq(&self, other: Self, max_abs_diff: f32) -> bool {
64 self.rotation.abs_diff_eq(other.rotation, max_abs_diff)
65 && self
66 .translation
67 .abs_diff_eq(other.translation, max_abs_diff)
68 }
69}
70
71impl From<Rigid> for Mat4 {
72 fn from(rigid: Rigid) -> Self {
73 rigid.to_mat4()
74 }
75}
76
77impl Mul for Rigid {
78 type Output = Self;
79
80 fn mul(self, rhs: Self) -> Self {
81 Self {
82 rotation: self.rotation * rhs.rotation,
83 translation: self.transform_point(rhs.translation),
84 }
85 }
86}
87
88impl MulAssign for Rigid {
89 fn mul_assign(&mut self, rhs: Self) {
90 *self = *self * rhs;
91 }
92}
93
94impl Mul<Vec3> for Rigid {
95 type Output = Vec3;
96
97 fn mul(self, rhs: Vec3) -> Vec3 {
99 self.transform_point(rhs)
100 }
101}