2026-02-11 20:04:35 -05:00
|
|
|
use std::ops::DerefMut;
|
|
|
|
|
|
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())
|
2026-02-11 20:04:35 -05:00
|
|
|
.add_systems(Update, (tick_physics, resolve_aabb_collisions).chain());
|
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
|
|
|
|
2026-02-11 20:04:35 -05:00
|
|
|
fn resolve_aabb_collisions(mut query: Query<(&mut PhysicsBody2D, &AABBCollider)>) {
|
|
|
|
|
let bcs: Vec<_> = query.iter_mut().collect();
|
|
|
|
|
|
|
|
|
|
let len = bcs.len();
|
|
|
|
|
|
|
|
|
|
let mut updated_bcs: Vec<(usize, PhysicsBody2D)> = Vec::new();
|
|
|
|
|
|
|
|
|
|
for i in 0..len {
|
|
|
|
|
let (b, c) = &bcs[i];
|
|
|
|
|
|
|
|
|
|
// don't bother collision checking immovables,
|
|
|
|
|
// instead movable entities will check against them
|
|
|
|
|
if b.immovable {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for j in 0..len {
|
|
|
|
|
if i == j {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let (other_b, other_c) = &bcs[j];
|
|
|
|
|
|
|
|
|
|
let bminx = b.pos.x;
|
|
|
|
|
let bmaxx = b.pos.x + c.width as i64;
|
|
|
|
|
let bminy = b.pos.y;
|
|
|
|
|
let bmaxy = b.pos.y + c.height as i64;
|
|
|
|
|
|
|
|
|
|
let obminx = other_b.pos.x;
|
|
|
|
|
let obmaxx = other_b.pos.x + other_c.width as i64;
|
|
|
|
|
let obminy = other_b.pos.y;
|
|
|
|
|
let obmaxy = other_b.pos.y + other_c.height as i64;
|
|
|
|
|
|
|
|
|
|
if bminx >= obmaxx || bminy >= obmaxy || bmaxx <= obminx || bmaxy <= obminy {
|
|
|
|
|
// no collision
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info!("Collide!");
|
|
|
|
|
|
|
|
|
|
let dx = b.pos.x - other_b.pos.x;
|
|
|
|
|
let dy = b.pos.y - other_b.pos.y;
|
|
|
|
|
|
|
|
|
|
if other_b.immovable {
|
|
|
|
|
let new_x = match dx {
|
|
|
|
|
..0 => other_b.pos.x - (c.width / 2 + other_c.width / 2) as i64,
|
|
|
|
|
0 => b.pos.x,
|
|
|
|
|
1.. => other_b.pos.x + (c.width / 2 + other_c.width / 2) as i64,
|
|
|
|
|
};
|
|
|
|
|
let new_y = match dy {
|
|
|
|
|
..0 => other_b.pos.y - (c.height / 2 + other_c.height / 2) as i64,
|
|
|
|
|
0 => b.pos.y,
|
|
|
|
|
1.. => other_b.pos.y + (c.height / 2 + other_c.height / 2) as i64,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let updated_b = PhysicsBody2D {
|
|
|
|
|
pos: I64Vec2 { x: new_x, y: new_y },
|
|
|
|
|
vel: b.vel,
|
|
|
|
|
immovable: false,
|
|
|
|
|
};
|
|
|
|
|
updated_bcs.push((i, updated_b));
|
|
|
|
|
} else {
|
|
|
|
|
let updated_b = PhysicsBody2D {
|
|
|
|
|
pos: b.pos - I64Vec2::new(dx / 2, dy / 2),
|
|
|
|
|
vel: b.vel,
|
|
|
|
|
immovable: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let other_updated_b = PhysicsBody2D {
|
|
|
|
|
pos: other_b.pos + I64Vec2::new(dx / 2, dy / 2),
|
|
|
|
|
vel: other_b.vel,
|
|
|
|
|
immovable: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updated_bcs.push((i, updated_b));
|
|
|
|
|
|
|
|
|
|
updated_bcs.push((i, other_updated_b));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i, (mut b, c)) in query.iter_mut().enumerate() {
|
|
|
|
|
let Some((_, updateb)) = updated_bcs.iter().find(|(j, _)| i == *j) else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
*b = updateb.clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Component, Clone, Copy)]
|
2026-02-07 19:09:58 -05:00
|
|
|
pub struct PhysicsBody2D {
|
|
|
|
|
pub pos: I64Vec2,
|
|
|
|
|
pub vel: I64Vec2,
|
2026-02-11 20:04:35 -05:00
|
|
|
pub immovable: bool,
|
2026-02-07 19:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-11 20:04:35 -05:00
|
|
|
|
|
|
|
|
#[derive(Default, Component)]
|
|
|
|
|
pub struct Collideable;
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Component)]
|
|
|
|
|
#[require(Collideable)]
|
|
|
|
|
pub struct AABBCollider {
|
|
|
|
|
pub width: u32,
|
|
|
|
|
pub height: u32,
|
|
|
|
|
pub offset: IVec2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AABBCollider {
|
|
|
|
|
pub fn new(w: u32, h: u32) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
width: w,
|
|
|
|
|
height: h,
|
|
|
|
|
offset: IVec2::ZERO,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|