Files
parsnip_plucker/src/physics.rs

75 lines
1.9 KiB
Rust
Raw Normal View History

2026-02-08 08:54:48 -05:00
/// This module is intended to be used as a fixed-point physics system.
/// The hope is that this will make deterministic physics easier,
/// and therefore networking a bit less of a pain.
2026-02-08 21:10:43 -05:00
use bevy::{math::I64Vec2, prelude::*};
2026-02-07 19:09:58 -05:00
2026-02-08 21:10:43 -05:00
const FIXED_UPDATE_INTERVAL_MS: u128 = 10; // 100 fps physics
2026-02-08 08:54:48 -05:00
#[derive(Default, Debug)]
2026-02-08 21:10:43 -05:00
pub struct Physics2DPlugin;
#[derive(Default, Debug, Resource)]
pub struct Physics2DWorld {
pub last_update: u128,
}
2026-02-08 08:54:48 -05:00
impl Plugin for Physics2DPlugin {
fn build(&self, app: &mut bevy::app::App) {
2026-02-08 21:10:43 -05:00
app.insert_resource(Physics2DWorld::default())
.add_systems(Update, tick_physics);
2026-02-08 08:54:48 -05:00
}
}
2026-02-08 21:10:43 -05:00
fn tick_physics(
mut query: Query<&mut PhysicsBody2D>,
mut p_world: ResMut<Physics2DWorld>,
time: Res<Time>,
) {
let t = time.elapsed().as_millis();
let delta_t = t - p_world.last_update;
if delta_t < FIXED_UPDATE_INTERVAL_MS {
return;
}
p_world.last_update = t;
2026-02-08 08:54:48 -05:00
query.iter_mut().for_each(|mut pb| {
2026-02-08 21:10:43 -05:00
pb.tick(delta_t as i64);
2026-02-08 08:54:48 -05:00
});
}
2026-02-07 19:09:58 -05:00
#[derive(Default, Debug, Component)]
pub struct PhysicsBody2D {
pub pos: I64Vec2,
pub vel: I64Vec2,
}
impl PhysicsBody2D {
2026-02-08 21:10:43 -05:00
pub fn tick(&mut self, delta_t: i64) {
self.pos += self.vel * delta_t;
2026-02-07 19:09:58 -05:00
}
2026-02-08 21:10:43 -05:00
pub fn apply_force(&mut self, force: I64Vec2, term_speed: Option<i64>) {
2026-02-07 19:09:58 -05:00
let mut vel = self.vel + force;
2026-02-08 21:10:43 -05:00
let Some(max_speed) = term_speed else {
return;
};
if vel.length_squared() >= max_speed.pow(2) {
2026-02-07 19:09:58 -05:00
let l = vel.length_squared();
let x = vel.x * vel.x;
let y = vel.y * vel.y;
vel.x = match vel.x {
2026-02-08 21:10:43 -05:00
..0 => (x * -max_speed) / l,
_ => (x * max_speed) / l,
2026-02-07 19:09:58 -05:00
};
vel.y = match vel.y {
2026-02-08 21:10:43 -05:00
..0 => (y * -max_speed) / l,
_ => (y * max_speed) / l,
2026-02-07 19:09:58 -05:00
};
}
self.vel = vel;
}
}