Add math module

This commit is contained in:
Danny McGee
2023-10-11 12:58:57 -04:00
parent cd0ccc94ad
commit ac6773b117
4 changed files with 25 additions and 23 deletions
+1
View File
@@ -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;
+3
View File
@@ -0,0 +1,3 @@
mod util;
pub use util::*;
+17
View File
@@ -0,0 +1,17 @@
use bevy::prelude::Vec3;
pub trait NearlyEq<Rhs> {
fn nearly_eq(self, rhs: Rhs) -> bool;
}
impl NearlyEq<f32> for f32 {
fn nearly_eq(self, rhs: f32) -> bool {
(self - rhs).abs() < Self::EPSILON
}
}
impl NearlyEq<Vec3> 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)
}
}
+4 -23
View File
@@ -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)
}
}