Lots of physics thinking
This commit is contained in:
@@ -1,26 +1,41 @@
|
||||
/// 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.
|
||||
use bevy::{
|
||||
app::{Plugin, Update},
|
||||
ecs::{component::Component, system::Query},
|
||||
math::I64Vec2,
|
||||
};
|
||||
use bevy::{math::I64Vec2, prelude::*};
|
||||
|
||||
const MAX_SPEED: i64 = 400;
|
||||
const FIXED_UPDATE_INTERVAL_MS: u128 = 10; // 100 fps physics
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Physics2DPlugin {}
|
||||
pub struct Physics2DPlugin;
|
||||
|
||||
#[derive(Default, Debug, Resource)]
|
||||
pub struct Physics2DWorld {
|
||||
pub last_update: u128,
|
||||
}
|
||||
|
||||
impl Plugin for Physics2DPlugin {
|
||||
fn build(&self, app: &mut bevy::app::App) {
|
||||
app.add_systems(Update, tick_physics);
|
||||
app.insert_resource(Physics2DWorld::default())
|
||||
.add_systems(Update, tick_physics);
|
||||
}
|
||||
}
|
||||
|
||||
fn tick_physics(mut query: Query<&mut PhysicsBody2D>) {
|
||||
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;
|
||||
|
||||
query.iter_mut().for_each(|mut pb| {
|
||||
pb.tick();
|
||||
pb.tick(delta_t as i64);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,24 +46,27 @@ pub struct PhysicsBody2D {
|
||||
}
|
||||
|
||||
impl PhysicsBody2D {
|
||||
pub fn tick(&mut self) {
|
||||
self.pos += self.vel;
|
||||
pub fn tick(&mut self, delta_t: i64) {
|
||||
self.pos += self.vel * delta_t;
|
||||
}
|
||||
|
||||
pub fn add_vel(&mut self, force: I64Vec2) {
|
||||
pub fn apply_force(&mut self, force: I64Vec2, term_speed: Option<i64>) {
|
||||
let mut vel = self.vel + force;
|
||||
if vel.length_squared() >= MAX_SPEED.pow(2) {
|
||||
let Some(max_speed) = term_speed else {
|
||||
return;
|
||||
};
|
||||
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,
|
||||
..0 => (x * -max_speed) / l,
|
||||
_ => (x * max_speed) / l,
|
||||
};
|
||||
vel.y = match vel.y {
|
||||
..0 => (y * -MAX_SPEED) / l,
|
||||
_ => (y * MAX_SPEED) / l,
|
||||
..0 => (y * -max_speed) / l,
|
||||
_ => (y * max_speed) / l,
|
||||
};
|
||||
}
|
||||
self.vel = vel;
|
||||
|
||||
Reference in New Issue
Block a user