From 1ab1fd8cc03678ac180673884eff098821bcecd2 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 21 Jun 2024 12:39:50 +0200 Subject: refactor movement --- server/src/customer/movement.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'server/src') diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs index 0ddabd0b..b45c7931 100644 --- a/server/src/customer/movement.rs +++ b/server/src/customer/movement.rs @@ -2,6 +2,9 @@ use crate::protocol::PacketS; use glam::{IVec2, Vec2}; use std::collections::HashSet; +const PLAYER_SIZE: f32 = 0.4; +const PLAYER_SPEED: f32 = 25.; + pub struct MovementBase { pub position: Vec2, pub facing: Vec2, @@ -15,10 +18,11 @@ impl MovementBase { self.facing = direction + (self.facing - direction) * (-dt * 10.).exp(); } let rot = self.facing.x.atan2(self.facing.y); - self.vel += direction * dt * 0.5; - self.position += self.vel; + self.vel += direction * dt * PLAYER_SPEED; + self.position += self.vel * dt; self.vel = self.vel * (-dt * 5.).exp(); collide_player(self, map); + PacketS::Position { pos: self.position, rot, @@ -26,7 +30,6 @@ impl MovementBase { } } -const PLAYER_SIZE: f32 = 0.4; pub fn collide_player(p: &mut MovementBase, map: &HashSet) { for xo in -1..=1 { for yo in -1..=1 { @@ -35,15 +38,15 @@ pub fn collide_player(p: &mut MovementBase, map: &HashSet) { continue; } let tile = tile.as_vec2(); - let d = aabb_circle_distance(tile, tile + Vec2::ONE, p.position); + let d = aabb_point_distance(tile, tile + Vec2::ONE, p.position); if d > PLAYER_SIZE { continue; } let h = 0.01; let d_sample_x = - aabb_circle_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(h, 0.)); let d_sample_y = - aabb_circle_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); + aabb_point_distance(tile, tile + Vec2::ONE, p.position + Vec2::new(0., h)); let grad = (Vec2::new(d_sample_x, d_sample_y) - d) / h; p.position += (PLAYER_SIZE - d) * grad; @@ -51,6 +54,7 @@ pub fn collide_player(p: &mut MovementBase, map: &HashSet) { } } } -pub fn aabb_circle_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { + +pub fn aabb_point_distance(min: Vec2, max: Vec2, p: Vec2) -> f32 { (p - p.clamp(min, max)).length() } -- cgit v1.2.3-70-g09d2