Physics refactored out
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
use bevy::{ecs::component::Component, math::I64Vec2};
|
||||
|
||||
const MAX_SPEED: i64 = 256;
|
||||
|
||||
#[derive(Default, Debug, Component)]
|
||||
pub struct PhysicsBody2D {
|
||||
pub pos: I64Vec2,
|
||||
pub vel: I64Vec2,
|
||||
}
|
||||
|
||||
impl PhysicsBody2D {
|
||||
pub fn tick(&mut self) {
|
||||
self.pos += self.vel;
|
||||
}
|
||||
|
||||
pub fn add_vel(&mut self, force: I64Vec2) {
|
||||
let mut vel = self.vel + force;
|
||||
if vel.length_squared() >= MAX_SPEED.pow(2) {
|
||||
let l = vel.length_squared();
|
||||
let x = vel.x * vel.x;
|
||||
let y = vel.y * vel.y;
|
||||
|
||||
vel.x = match vel.x {
|
||||
..0 => (x * -MAX_SPEED) / l,
|
||||
_ => (x * MAX_SPEED) / l,
|
||||
};
|
||||
vel.y = match vel.y {
|
||||
..0 => (y * -MAX_SPEED) / l,
|
||||
_ => (y * MAX_SPEED) / l,
|
||||
};
|
||||
}
|
||||
self.vel = vel;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user