From b628ce1d6847edd16816891ef29464fa803a336e Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Sun, 8 Feb 2026 08:54:48 -0500 Subject: [PATCH] more physics systems stuff --- assets/greembyguy.png | Bin 0 -> 790 bytes assets/test.ldtk | 9 ++++++--- src/main.rs | 18 ++++++++++-------- src/physics.rs | 26 ++++++++++++++++++++++++-- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 assets/greembyguy.png diff --git a/assets/greembyguy.png b/assets/greembyguy.png new file mode 100644 index 0000000000000000000000000000000000000000..718717138769bda7ec3ac23ffc02e18cd31314df GIT binary patch literal 790 zcmV+x1L^#UP)Px%&`Cr=RA_E;tzGW)`6gl0j|Nv|5E|= zl|bV0gc5W*y64)E#sMBz4}*%3ivgRFHU*^nel_JDo8MOgb=}KB$&=h93UPmTjpgV& z03eA%0KjyyIq*apo)qCXaT8TTH@hZP$aT^0(nBgayMs47dhBw{qBYh;u4@Lf80}9sCZNu$lw{!1L2;uwDB|;{a(K zsHGo(DhNKuG3>)YQ|JPq>%6)G;+=SGKG0A6OdVX8KmrxPcJ0HP`(z)kDMb-Ua?_%{D_8?&zQvvn|WO$p>E@(e|u;VpSpzmq6L zk!RH+W$?(r7_(g(my_GIU)eb}F`xnf5diE1z%@;L4(Z`f@Vof$BYva-Ky@&o$TNJJ z|FrT=DDupDU7Z5FNv^6y8t^80rTS%s_7Q-x1O8rq9|=^+N&w(h&5!&G098M2F!C=B z-iM$HpggMSe{)bU)5`c~hJ@@W&ZpgUlTF$w@=2rX z`@6>9h40Cm090O#Fut7ac-8WI%4<+vV}i*b>Du`G*|K|%&2RetCcd0+8-Ex6FO0~n UFl^`J3IG5A07*qoM6N<$f`W>58~^|S literal 0 HcmV?d00001 diff --git a/assets/test.ldtk b/assets/test.ldtk index 683ec37..1675c52 100644 --- a/assets/test.ldtk +++ b/assets/test.ldtk @@ -1416,6 +1416,7 @@ { "px": [193,255], "src": [80,32], "f": 0, "t": 29, "d": [42,380], "a": 1 }, { "px": [175,272], "src": [80,0], "f": 0, "t": 5, "d": [42,402], "a": 1 }, { "px": [112,96], "src": [160,16], "f": 0, "t": 22, "d": [66,145], "a": 1 }, + { "px": [112,112], "src": [160,16], "f": 0, "t": 22, "d": [66,168], "a": 1 }, { "px": [0,224], "src": [160,16], "f": 0, "t": 22, "d": [66,322], "a": 1 }, { "px": [16,224], "src": [160,16], "f": 0, "t": 22, "d": [66,323], "a": 1 }, { "px": [32,224], "src": [160,16], "f": 0, "t": 22, "d": [66,324], "a": 1 }, @@ -1428,16 +1429,18 @@ { "px": [16,256], "src": [160,16], "f": 0, "t": 22, "d": [66,369], "a": 1 }, { "px": [32,256], "src": [160,16], "f": 0, "t": 22, "d": [66,370], "a": 1 }, { "px": [96,96], "src": [144,16], "f": 0, "t": 21, "d": [69,144], "a": 1 }, + { "px": [96,112], "src": [144,16], "f": 0, "t": 21, "d": [69,167], "a": 1 }, { "px": [112,80], "src": [160,0], "f": 0, "t": 10, "d": [68,122], "a": 1 }, { "px": [0,208], "src": [160,0], "f": 0, "t": 10, "d": [68,299], "a": 1 }, { "px": [16,208], "src": [160,0], "f": 0, "t": 10, "d": [68,300], "a": 1 }, { "px": [32,208], "src": [160,0], "f": 0, "t": 10, "d": [68,301], "a": 1 }, { "px": [48,208], "src": [160,0], "f": 0, "t": 10, "d": [68,302], "a": 1 }, - { "px": [112,112], "src": [160,32], "f": 0, "t": 34, "d": [65,168], "a": 1 }, + { "px": [112,128], "src": [160,32], "f": 0, "t": 34, "d": [65,191], "a": 1 }, { "px": [0,272], "src": [160,32], "f": 0, "t": 34, "d": [65,391], "a": 1 }, { "px": [16,272], "src": [160,32], "f": 0, "t": 34, "d": [65,392], "a": 1 }, { "px": [32,272], "src": [160,32], "f": 0, "t": 34, "d": [65,393], "a": 1 }, { "px": [128,96], "src": [176,16], "f": 0, "t": 23, "d": [70,146], "a": 1 }, + { "px": [128,112], "src": [176,16], "f": 0, "t": 23, "d": [70,169], "a": 1 }, { "px": [64,224], "src": [176,16], "f": 0, "t": 23, "d": [70,326], "a": 1 }, { "px": [64,240], "src": [176,16], "f": 0, "t": 23, "d": [70,349], "a": 1 }, { "px": [48,256], "src": [176,16], "f": 0, "t": 23, "d": [70,371], "a": 1 }, @@ -1489,7 +1492,7 @@ 1,1,1,1,1,1,3,1,1,1,1,4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,4, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,4,1,1,1,1,1,2,2,2,1,1,1,1, 1,1,1,1,1,3,1,1,1,1,4,1,1,1,1,2,2,2,2,2,2,1,1,1,1,1,1,1,3,1,1,1,1,1,1, - 1,1,1,2,2,5,2,2,2,1,1,3,3,3,3,3,3,1,1,1,1,1,1,2,2,1,1,2,2,2,1,1,1,1,3, + 1,1,1,2,2,5,2,2,2,1,1,3,3,3,3,3,3,1,1,1,1,1,1,2,2,1,1,2,5,2,1,1,1,1,3, 1,1,1,1,1,1,1,1,1,1,2,2,2,2,1,2,2,2,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1, 1,2,2,2,2,2,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,2,2,2,2,1,3,1, 1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,2, @@ -1501,7 +1504,6 @@ ], "autoLayerTiles": [ { "px": [112,80], "src": [16,32], "f": 0, "t": 25, "d": [37,122], "a": 1 }, - { "px": [112,112], "src": [16,32], "f": 0, "t": 25, "d": [37,168], "a": 1 }, { "px": [112,128], "src": [16,32], "f": 0, "t": 25, "d": [37,191], "a": 1 }, { "px": [96,96], "src": [96,48], "f": 0, "t": 42, "d": [36,144], "a": 1 }, { "px": [128,96], "src": [80,48], "f": 0, "t": 41, "d": [35,146], "a": 1 }, @@ -1560,6 +1562,7 @@ { "px": [16,128], "src": [0,16], "f": 0, "t": 12, "d": [25,185], "a": 1 }, { "px": [48,192], "src": [0,16], "f": 0, "t": 12, "d": [25,279], "a": 1 }, { "px": [112,96], "src": [16,32], "f": 0, "t": 25, "d": [67,145], "a": 1 }, + { "px": [112,112], "src": [16,32], "f": 0, "t": 25, "d": [67,168], "a": 1 }, { "px": [0,224], "src": [16,32], "f": 0, "t": 25, "d": [67,322], "a": 1 }, { "px": [16,224], "src": [16,32], "f": 0, "t": 25, "d": [67,323], "a": 1 }, { "px": [32,224], "src": [16,32], "f": 0, "t": 25, "d": [67,324], "a": 1 }, diff --git a/src/main.rs b/src/main.rs index d059038..1667b0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,9 +4,12 @@ mod player; use bevy::{image::Image, math::I64Vec2, prelude::*}; -use crate::{physics::PhysicsBody2D, player::Player}; +use crate::{ + physics::{Physics2DPlugin, PhysicsBody2D}, + player::Player, +}; -const UNITS_TO_PX_SCALE: i64 = 256; +const UNITS_TO_PX_SCALE: i64 = 1000; #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, States)] enum AppState { @@ -37,6 +40,7 @@ fn main() { let mut app = App::new(); app.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) + .add_plugins(Physics2DPlugin::default()) .init_state::() .insert_resource(LevelData { ldtk_proj }) .insert_resource(TilemapStuff::default()) @@ -114,8 +118,8 @@ fn levelload( let parsnip = assets.load("parsnip.png"); // create map from lvl data - let flowerguy = assets.load("flowerfriend.png"); - let fl_ta_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 5, 1, None, None); + let flowerguy = assets.load("greembyguy.png"); + let fl_ta_layout = TextureAtlasLayout::from_grid(uvec2(16, 16), 8, 1, None, None); let fl_ta_handle = ta_layouts.add(fl_ta_layout); let lvl = &lvl.ldtk_proj.levels[0]; @@ -218,8 +222,6 @@ fn gameloop( play_phys.add_vel(dir * UNITS_TO_PX_SCALE); } - play_phys.tick(); - *player_sing.1 = Transform::from_xyz( (play_phys.pos.x / UNITS_TO_PX_SCALE) as f32, (play_phys.pos.y / UNITS_TO_PX_SCALE) as f32, @@ -228,11 +230,11 @@ fn gameloop( let plyr = player_sing.2.as_mut(); - if time.elapsed_secs_f64() - plyr.last_frame > 0.1 { + if time.elapsed_secs_f64() - plyr.last_frame > 0.01 { plyr.last_frame = time.elapsed_secs_f64(); if dir.length_squared() > 0 { - plyr.anim_index = (plyr.anim_index + 1) % 5; + plyr.anim_index = (plyr.anim_index + 1) % 8; } else { plyr.anim_index = 0; } diff --git a/src/physics.rs b/src/physics.rs index e476b64..962c33e 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -1,6 +1,28 @@ -use bevy::{ecs::component::Component, math::I64Vec2}; +/// 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, +}; -const MAX_SPEED: i64 = 256; +const MAX_SPEED: i64 = 400; + +#[derive(Default, Debug)] +pub struct Physics2DPlugin {} + +impl Plugin for Physics2DPlugin { + fn build(&self, app: &mut bevy::app::App) { + app.add_systems(Update, tick_physics); + } +} + +fn tick_physics(mut query: Query<&mut PhysicsBody2D>) { + query.iter_mut().for_each(|mut pb| { + pb.tick(); + }); +} #[derive(Default, Debug, Component)] pub struct PhysicsBody2D {