Physics refactored out

This commit is contained in:
JP Stringham
2026-02-07 19:09:58 -05:00
parent 01c5d76586
commit 25c7fa695a
4 changed files with 51 additions and 54 deletions

View File

@@ -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;
}
}