Galactica/crates/systemsim/src/util.rs

33 lines
820 B
Rust
Raw Normal View History

2024-01-01 12:01:29 -08:00
//! Conversion utilities
2023-12-30 16:57:03 -08:00
use cgmath::{Point2, Vector2};
2023-12-28 20:19:33 -08:00
use nalgebra;
use rapier2d::dynamics::RigidBody;
2024-01-01 12:01:29 -08:00
// TODO: Migrate to nalgebra fully, remove these converters
/// Convert a rigidbody position to a position in game coordinates
2023-12-28 20:19:33 -08:00
pub fn rigidbody_position(r: &RigidBody) -> cgmath::Point2<f32> {
Point2 {
x: r.translation()[0],
y: r.translation()[1],
}
}
2024-01-01 12:01:29 -08:00
/// Convert a rigidbody rotation to a rotation in game coordinates
2023-12-29 18:34:30 -08:00
pub fn rigidbody_rotation(r: &RigidBody) -> Vector2<f32> {
Vector2 {
2023-12-30 16:57:03 -08:00
x: r.rotation().re,
y: r.rotation().im,
2023-12-29 18:34:30 -08:00
}
}
2024-01-01 12:01:29 -08:00
/// Convert a rigidbody velocity to a velocity in game coordinates
2023-12-28 20:19:33 -08:00
pub fn rigidbody_velocity(r: &RigidBody) -> cgmath::Vector2<f32> {
let v = r.velocity_at_point(&nalgebra::Point2::new(
r.translation()[0],
r.translation()[1],
));
Vector2 { x: v.x, y: v.y }
}