Skip to main content

dualsense_tools/
tilt.rs

1use glam::{Quat, Vec3};
2
3/// Describe the tilt of a controller, i.e. its orientation in terms
4/// of roll (rotation around the Z axis) and pitch (rotation around the X axis).
5#[derive(Clone, Copy, Default, Debug, PartialEq)]
6pub struct Tilt {
7    pub quat: Quat,
8}
9
10impl Tilt {
11    /// Creates a Tilt instance from a raw quaternion
12    pub const fn new(quat: Quat) -> Tilt {
13        Tilt { quat }
14    }
15
16    /// Gets the roll component of the tilt quaternion
17    pub fn get_roll_radians(&self) -> Radians {
18        Radians(-self.quat.to_scaled_axis().dot(Vec3::Z))
19    }
20
21    /// Gets the pitch component of the tilt quaternion
22    pub fn get_pitch_radians(&self) -> Radians {
23        Radians(self.quat.to_scaled_axis().dot(Vec3::X))
24    }
25}
26
27/// Represents a value in radians
28#[derive(Clone, Copy, Default, Debug, PartialEq)]
29pub struct Radians(f32);
30
31impl Radians {
32    /// Gets the value as a floating point number
33    pub fn get_angle(&self) -> f32 {
34        self.0
35    }
36}
37
38impl From<f32> for Radians {
39    fn from(val: f32) -> Self {
40        Radians(val)
41    }
42}