Lots of physics thinking
This commit is contained in:
40
Cargo.lock
generated
40
Cargo.lock
generated
@@ -1715,9 +1715,9 @@ checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.11.0"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3"
|
||||
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
|
||||
|
||||
[[package]]
|
||||
name = "calloop"
|
||||
@@ -2307,9 +2307,9 @@ checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.8"
|
||||
version = "1.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b375d6465b98090a5f25b1c7703f3859783755aa9a80433b36e0379a3ec2f369"
|
||||
checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
@@ -3036,9 +3036,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.6"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "memmap2"
|
||||
@@ -3947,9 +3947,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.12.2"
|
||||
version = "1.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4"
|
||||
checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
@@ -3959,9 +3959,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.13"
|
||||
version = "0.4.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
|
||||
checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
@@ -3970,9 +3970,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.8"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
|
||||
checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c"
|
||||
|
||||
[[package]]
|
||||
name = "renderdoc-sys"
|
||||
@@ -4618,9 +4618,9 @@ checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.22"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
|
||||
checksum = "537dd038a89878be9b64dd4bd1b260315c1bb94f4d784956b81e27a088d9a09e"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-linebreak"
|
||||
@@ -5673,18 +5673,18 @@ checksum = "6df3dc4292935e51816d896edcd52aa30bc297907c26167fec31e2b0c6a32524"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.37"
|
||||
version = "0.8.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7456cf00f0685ad319c5b1693f291a650eaf345e941d082fc4e03df8a03996ac"
|
||||
checksum = "db6d35d663eadb6c932438e763b262fe1a70987f9ae936e60158176d710cae4a"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.37"
|
||||
version = "0.8.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1328722bbf2115db7e19d69ebcc15e795719e2d66b60827c6a69a117365e37a0"
|
||||
checksum = "4122cd3169e94605190e77839c9a40d40ed048d305bfdc146e7df40ab0f3e517"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -5693,6 +5693,6 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.19"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ff05f8caa9038894637571ae6b9e29466c1f4f829d26c9b28f869a29cbe3445"
|
||||
checksum = "4de98dfa5d5b7fef4ee834d0073d560c9ca7b6c46a71d058c48db7960f8cfaf7"
|
||||
|
||||
@@ -219,7 +219,7 @@ fn gameloop(
|
||||
play_phys.vel *= 6;
|
||||
play_phys.vel /= 10;
|
||||
} else {
|
||||
play_phys.add_vel(dir * UNITS_TO_PX_SCALE);
|
||||
play_phys.apply_force(dir * UNITS_TO_PX_SCALE, Some(UNITS_TO_PX_SCALE * 10 / 100));
|
||||
}
|
||||
|
||||
*player_sing.1 = Transform::from_xyz(
|
||||
|
||||
@@ -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