Galactica/crates/util/src/lib.rs

36 lines
789 B
Rust
Raw Normal View History

2024-01-10 18:53:19 -08:00
#![warn(missing_docs)]
//! Various utilities
2024-02-04 11:45:49 -08:00
use anyhow::bail;
2024-01-14 11:09:51 -08:00
use nalgebra::Vector2;
2024-01-10 18:53:19 -08:00
pub mod constants;
pub mod timing;
2024-01-12 22:47:40 -08:00
/// Convert an angle in degrees to radians
pub fn to_radians(degrees: f32) -> f32 {
return (degrees / 360.0) * std::f32::consts::TAU;
}
2024-01-14 11:09:51 -08:00
/// Compute the clockwise angle between two vectors
2024-01-14 11:50:19 -08:00
/// Returns a value in [-pi, pi]
2024-01-14 11:09:51 -08:00
pub fn clockwise_angle(a: &Vector2<f32>, b: &Vector2<f32>) -> f32 {
(a.x * b.y - b.x * a.y).atan2(a.dot(&b))
}
2024-02-04 11:45:49 -08:00
/// Convert a rhai error to an anyhow error.
/// We can't do this directly, since anyhow requires send + sync,
/// and we don't need send+sync on rhai.
pub fn rhai_error_to_anyhow<T, E>(r: Result<T, E>) -> anyhow::Result<T>
where
E: ToString,
{
match r {
Ok(x) => Ok(x),
Err(e) => {
bail!("{}", e.to_string())
}
}
}