use super::Pfloat; use cgmath::{Angle, Deg, Point2, Vector2}; pub struct PhysBody { pub pos: Point2, pub vel: Vector2, pub mass: Pfloat, pub angle: Deg, } impl PhysBody { pub fn new(pos: Point2) -> Self { return PhysBody { pos, vel: (0.0, 0.0).into(), mass: 0.3, angle: Deg(0.0), }; } /// Calculate the position of this body after t seconds. pub fn tick(&mut self, t: Pfloat) { self.pos += self.vel * t; } /// Apply an instantaneous force to this object pub fn force(&mut self, v: Vector2) { self.vel += v / self.mass; } /// Apply a force in the direction this object is pointing. pub fn thrust(&mut self, f: Pfloat) { let l = Vector2 { x: -self.angle.sin(), y: self.angle.cos(), } * f; self.force(l); } // Rotate this object pub fn rot(&mut self, a: Deg) { self.angle -= a; // Wrap angles if self.angle.0.abs() > 180.0 { self.angle -= Deg(self.angle.0.signum() * 360.0); } } }