use std::collections::HashMap;
use ulid::Ulid;
pub const VIEW_ANGLE: f32 = std::f32::consts::FRAC_PI_4;
#[derive(Debug)]
pub struct PhysicalObject(Option<Ulid>);
pub struct PhysicsSystem {}
impl PhysicsSystem {
pub fn new() -> Self {
Self {}
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PhysicsState {
objects: HashMap<Ulid, PhysicsObject>,
}
impl PhysicsState {
pub fn lerp(&self, from: &Self, delta: f32) -> Self {
let both_exist = self
.objects
.iter()
.filter_map(|(id, oobj)| from.objects.get(id).map(|obj| (id, oobj, obj)));
let only_self = self
.objects
.iter()
.filter(|(id, _)| !from.objects.contains_key(*id));
let mut new_objects = HashMap::new();
for (id, oobj, obj) in both_exist {
new_objects.insert(*id, oobj.lerp(obj, delta));
}
for (id, obj) in only_self {
new_objects.insert(*id, obj.clone());
}
Self {
objects: new_objects,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PhysicsObject {
position: glam::Vec3,
}
impl PhysicsObject {
fn lerp(&self, from: &Self, delta: f32) -> Self {
let position = self.position * delta + from.position * (1.0 - delta);
Self { position }
}
fn position(&self) -> glam::Vec2 {
glam::Vec2::new(
self.position.x,
self.position.y - VIEW_ANGLE.tan() * self.position.z,
)
}
}
#[cfg(test)]
mod tests;