From ac6773b11704f7e47bd077e24bb9691cec9e88ac Mon Sep 17 00:00:00 2001 From: Danny McGee Date: Wed, 11 Oct 2023 12:58:57 -0400 Subject: [PATCH] Add math module --- studio/src/lib.rs | 1 + studio/src/math/mod.rs | 3 +++ studio/src/math/util.rs | 17 +++++++++++++++++ studio/src/orbit_cam.rs | 27 ++++----------------------- 4 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 studio/src/math/mod.rs create mode 100644 studio/src/math/util.rs diff --git a/studio/src/lib.rs b/studio/src/lib.rs index 46541f2..2de5bbd 100644 --- a/studio/src/lib.rs +++ b/studio/src/lib.rs @@ -18,6 +18,7 @@ use wasm_bindgen::prelude::*; mod design_system; mod input; mod interaction; +mod math; mod orbit_cam; mod skybox; mod test_scene; diff --git a/studio/src/math/mod.rs b/studio/src/math/mod.rs new file mode 100644 index 0000000..c8183a1 --- /dev/null +++ b/studio/src/math/mod.rs @@ -0,0 +1,3 @@ +mod util; + +pub use util::*; diff --git a/studio/src/math/util.rs b/studio/src/math/util.rs new file mode 100644 index 0000000..c7dafe2 --- /dev/null +++ b/studio/src/math/util.rs @@ -0,0 +1,17 @@ +use bevy::prelude::Vec3; + +pub trait NearlyEq { + fn nearly_eq(self, rhs: Rhs) -> bool; +} + +impl NearlyEq for f32 { + fn nearly_eq(self, rhs: f32) -> bool { + (self - rhs).abs() < Self::EPSILON + } +} + +impl NearlyEq for Vec3 { + fn nearly_eq(self, rhs: Vec3) -> bool { + self.x.nearly_eq(rhs.x) && self.y.nearly_eq(rhs.y) && self.z.nearly_eq(rhs.z) + } +} diff --git a/studio/src/orbit_cam.rs b/studio/src/orbit_cam.rs index 530848b..57c04d1 100644 --- a/studio/src/orbit_cam.rs +++ b/studio/src/orbit_cam.rs @@ -6,7 +6,10 @@ use bevy::{ window::{CursorGrabMode, PrimaryWindow}, }; -use crate::input::{Action, CameraMotion, InputAction, Lifecycle}; +use crate::{ + input::{Action, CameraMotion, InputAction, Lifecycle}, + math::NearlyEq, +}; pub struct OrbitCamPlugin; @@ -286,25 +289,3 @@ fn do_rotate(win: &Window, delta: Vec2, xform: &mut Transform, orbit_cam: &mut O fn curve_sin(t: f32) -> f32 { -1. * (f32::cos(t * consts::PI) / 2.) + 0.5 } - -trait NearlyEq { - type Rhs; - - fn nearly_eq(self, rhs: Self::Rhs) -> bool; -} - -impl NearlyEq for f32 { - type Rhs = Self; - - fn nearly_eq(self, rhs: Self::Rhs) -> bool { - (self - rhs).abs() < Self::EPSILON - } -} - -impl NearlyEq for Vec3 { - type Rhs = Self; - - fn nearly_eq(self, rhs: Self::Rhs) -> bool { - self.x.nearly_eq(rhs.x) && self.y.nearly_eq(rhs.y) && self.z.nearly_eq(rhs.z) - } -}