mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-17 00:13:17 +00:00
Allow specified tolerance for NearlyEq checks
This commit is contained in:
@@ -161,7 +161,7 @@ impl From<Vec3> for SphericalCoords {
|
||||
}
|
||||
|
||||
let pitch = f32::asin(-y / radius);
|
||||
let heading = if (pitch - 90_f32.to_radians()).abs() < 1e-3_f32 {
|
||||
let heading = if pitch.nearly_eq_within(90_f32.to_radians(), 1e-3) {
|
||||
0.
|
||||
} else {
|
||||
f32::atan2(x, z)
|
||||
|
||||
+23
-7
@@ -1,17 +1,33 @@
|
||||
use bevy::prelude::Vec3;
|
||||
|
||||
pub trait NearlyEq<Rhs> {
|
||||
fn nearly_eq(self, rhs: Rhs) -> bool;
|
||||
pub trait NearlyEq<Rhs, Tolerance = Rhs> {
|
||||
const DEFAULT_TOLERANCE: Tolerance;
|
||||
|
||||
#[inline]
|
||||
fn nearly_eq(self, rhs: Rhs) -> bool
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.nearly_eq_within(rhs, Self::DEFAULT_TOLERANCE)
|
||||
}
|
||||
|
||||
fn nearly_eq_within(self, rhs: Rhs, tolerance: Tolerance) -> bool;
|
||||
}
|
||||
|
||||
impl NearlyEq<f32> for f32 {
|
||||
fn nearly_eq(self, rhs: f32) -> bool {
|
||||
(self - rhs).abs() < Self::EPSILON
|
||||
const DEFAULT_TOLERANCE: f32 = f32::EPSILON;
|
||||
|
||||
fn nearly_eq_within(self, rhs: f32, tolerance: f32) -> bool {
|
||||
(self - rhs).abs() < tolerance
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
impl NearlyEq<Vec3, f32> for Vec3 {
|
||||
const DEFAULT_TOLERANCE: f32 = <f32 as NearlyEq<f32>>::DEFAULT_TOLERANCE;
|
||||
|
||||
fn nearly_eq_within(self, rhs: Vec3, tolerance: f32) -> bool {
|
||||
self.x.nearly_eq_within(rhs.x, tolerance)
|
||||
&& self.y.nearly_eq_within(rhs.y, tolerance)
|
||||
&& self.z.nearly_eq_within(rhs.z, tolerance)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user