50 lines
994 B
Rust
50 lines
994 B
Rust
use super::Pfloat;
|
|
use cgmath::{Angle, Deg, Point2, Vector2};
|
|
|
|
pub struct PhysBody {
|
|
pub pos: Point2<Pfloat>,
|
|
pub vel: Vector2<Pfloat>,
|
|
pub mass: Pfloat,
|
|
pub angle: Deg<Pfloat>,
|
|
}
|
|
|
|
impl PhysBody {
|
|
pub fn new(pos: Point2<Pfloat>) -> 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<Pfloat>) {
|
|
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<Pfloat>) {
|
|
self.angle -= a;
|
|
|
|
// Wrap angles
|
|
if self.angle.0.abs() > 180.0 {
|
|
self.angle -= Deg(self.angle.0.signum() * 360.0);
|
|
}
|
|
}
|
|
}
|