From 25c7fa695a858ee1450f9f461ed97796ad917ca4 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Sat, 7 Feb 2026 19:09:58 -0500 Subject: [PATCH] Physics refactored out --- assets/flowerfriend.png | Bin 0 -> 516 bytes src/main.rs | 61 ++++++++++------------------------------ src/physics.rs | 34 ++++++++++++++++++++++ src/player.rs | 10 ++----- 4 files changed, 51 insertions(+), 54 deletions(-) create mode 100644 assets/flowerfriend.png diff --git a/assets/flowerfriend.png b/assets/flowerfriend.png new file mode 100644 index 0000000000000000000000000000000000000000..bf9d0391aeb86352bd6f72be4c53a8bcde3c59f2 GIT binary patch literal 516 zcmV+f0{i`mP)Px$zDYzuR9J=GS21qGFbq5e&00V|$rPZ2y|;ti?tMYN(k*|;1Ee)b24(9H+D{}% zhYT^mP84X_X<##fjdC-^Bky#AAOkHCLLefdwtNVok^gVeNk0*hbB@wN4hfWTGXewS z)%v*sH<5+TIcfvTfm#A%!S~fa@!q2-iiV2-V66oJ?DzYa^WLK@OOw`00$G;DT6Uas zoF5BbtAA1yMQoBf$zUGPws~3tL*upjA9lN4Y!Zi6D^#Z?o@T&$a{wD=twjid@;o=r zIkMIw%Q6)KtpF`vtKR_iV~=VDGkwSTILGwy6X)X`>&*e@$w!SC50#r3*Is^5ot6Ml z!}5uc1ON<*&-2`9^cy0gOT#(Vn**L-zv4GLZj4bIP?n`ifEF)LUmyL(IY;si@!mHa ze0lqBI1T{D7_|XI`Pz_ctEE= zHgL|>9*c|ZjM9hYC9<+cK0@X9H3388HTsbb2Y|(P*7`cX2Z1r+`{-}{(#Qtfga_P% zz*z8o^*4gSvdLd@D+7kYr_oh^4VZ#DH_;GK&ehKEs{a>O>Fbe_z|oih0000 { - spawned_ent.insert(Player { - pos, - vel: I64Vec2::ZERO, - }); + spawned_ent.insert(Player {}); + spawned_ent.insert(PhysicsBody2D { pos, ..default() }); spawned_ent.insert(Sprite::from_color(Color::WHITE, vec2(16., 16.))); } "Parsnip" => { @@ -173,10 +173,8 @@ fn levelload( fn gameloop( keyb: Res>, - mut player_sing: Single<(&mut Player, &mut Transform)>, + mut player_sing: Single<(&mut PhysicsBody2D, &mut Transform), With>, ) { - const UNITS_TO_PX_SCALE: i64 = 100; - const MAX_SPEED: i64 = 2 * UNITS_TO_PX_SCALE; let mut dir = I64Vec2::ZERO; if keyb.pressed(KeyCode::KeyA) || keyb.pressed(KeyCode::ArrowLeft) { @@ -195,51 +193,22 @@ fn gameloop( dir.y = -1; } - let player = player_sing.0.as_mut(); + let play_phys = player_sing.0.as_mut(); - // player vel and pos are in px / 100 - let mut p_vel = player.vel + dir * UNITS_TO_PX_SCALE * MAX_SPEED; + // player vel and pos are in UNITS_TO_PX_SCALE if dir.length_squared() == 0 { - p_vel = (p_vel * 8) / 10; - info!("Slowing! {p_vel}"); + // very bare bones player friction + play_phys.vel *= 6; + play_phys.vel /= 10; } else { - info!("Changing vel: {p_vel}") + play_phys.add_vel(dir * UNITS_TO_PX_SCALE); } - // want unit vector with int math - // normally this would be x / len, y / len - - // eg (2,2) -> length of 4 -> (0.5, 0.5) - // how to do with squares? - - // (2, 2) -> sqr length of 8 -> sqr x / 8 -> 0.5... yes? - // but we can't get half units obvs so we need to work in centi-units - // 200, 200 -> should become 50, 50 - - // max speed is 3 px per update - if p_vel.length_squared() >= MAX_SPEED.pow(2) { - let l = p_vel.length_squared(); - let x = p_vel.x * p_vel.x; - let y = p_vel.y * p_vel.y; - - p_vel.x = match p_vel.x { - ..0 => (x * -MAX_SPEED) / l, - _ => (x * MAX_SPEED) / l, - }; - p_vel.y = match p_vel.y { - ..0 => (y * -MAX_SPEED) / l, - _ => (y * MAX_SPEED) / l, - }; - - info!("Nerfed speed {p_vel}"); - } - - player.vel = p_vel; - player.pos += p_vel; + play_phys.tick(); *player_sing.1 = Transform::from_xyz( - (player.pos.x as f32) / UNITS_TO_PX_SCALE as f32, - (player.pos.y as f32) / UNITS_TO_PX_SCALE as f32, + (play_phys.pos.x as f32) / UNITS_TO_PX_SCALE as f32, + (play_phys.pos.y as f32) / UNITS_TO_PX_SCALE as f32, 100., ); } diff --git a/src/physics.rs b/src/physics.rs index e69de29..e476b64 100644 --- a/src/physics.rs +++ b/src/physics.rs @@ -0,0 +1,34 @@ +use bevy::{ecs::component::Component, math::I64Vec2}; + +const MAX_SPEED: i64 = 256; + +#[derive(Default, Debug, Component)] +pub struct PhysicsBody2D { + pub pos: I64Vec2, + pub vel: I64Vec2, +} + +impl PhysicsBody2D { + pub fn tick(&mut self) { + self.pos += self.vel; + } + + pub fn add_vel(&mut self, force: I64Vec2) { + let mut vel = self.vel + force; + 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, + }; + vel.y = match vel.y { + ..0 => (y * -MAX_SPEED) / l, + _ => (y * MAX_SPEED) / l, + }; + } + self.vel = vel; + } +} diff --git a/src/player.rs b/src/player.rs index 9888ff3..b454ddc 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,10 +1,4 @@ -use bevy::{ - ecs::component::Component, - math::{I64Vec2, IVec2}, -}; +use bevy::ecs::component::Component; #[derive(Default, Debug, Component)] -pub struct Player { - pub pos: I64Vec2, - pub vel: I64Vec2, -} +pub struct Player {}