From 1c34ba905863b7c6aa39e106fc937f10498f27d2 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sat, 24 Apr 2021 00:06:37 -0400 Subject: [PATCH 1/2] turns smoother --- src/logic/ingame/mod.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index b876de3..3cc48d6 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -10,6 +10,7 @@ use super::screen::Screen; const NORMAL_PLAYER_SPEED: i32 = 4; const BOOST_PLAYER_SPEED: i32 = NORMAL_PLAYER_SPEED * 2; const CAMERA_FOLLOW_SPEED: f32 = 0.7; +const TURN_SPEED: f32 = 0.05; pub enum InGameState { BUYING, @@ -41,7 +42,41 @@ impl InGameScreen { let raw_movement_direction = mouse_world_pose - game_core.player.position; let mut normalized_movement_direction = raw_movement_direction; normalized_movement_direction.normalize(); - game_core.player.direction = normalized_movement_direction; + + let tau: f32 = PI as f32 * 2.0; + let mut player_angle: f32 = Vector2::zero().angle_to(game_core.player.direction); + let mut desired_angle: f32 = Vector2::zero().angle_to(normalized_movement_direction); + + if desired_angle < 0.0 { + desired_angle += tau; + } + + if player_angle % tau > desired_angle { + if (player_angle % tau) - desired_angle > PI as f32 { + player_angle += TURN_SPEED; + } else { + player_angle -= TURN_SPEED; + } + } else { + if desired_angle - (player_angle % tau) > PI as f32 { + player_angle -= TURN_SPEED; + } else { + player_angle += TURN_SPEED; + } + } + + if f32::abs(player_angle - desired_angle) < (TURN_SPEED * 1.1) { + player_angle = desired_angle; + } + if player_angle > tau { + player_angle -= tau; + } + if player_angle < 0.0 { + player_angle += tau; + } + + + game_core.player.direction = Vector2::new(f32::cos(player_angle), f32::sin(player_angle)); // In the case the player is in "null", just jump the camera to them if game_core.player.position == Vector2::zero() { From e5a5fb86dce546b4d4abfb04baf79548e1f2bbc1 Mon Sep 17 00:00:00 2001 From: rsninja722 Date: Sat, 24 Apr 2021 00:09:36 -0400 Subject: [PATCH 2/2] forgot comments --- src/logic/ingame/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index 3cc48d6..df5ade2 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -44,13 +44,16 @@ impl InGameScreen { normalized_movement_direction.normalize(); let tau: f32 = PI as f32 * 2.0; + // get angles as floats let mut player_angle: f32 = Vector2::zero().angle_to(game_core.player.direction); let mut desired_angle: f32 = Vector2::zero().angle_to(normalized_movement_direction); + // make angle positive if desired_angle < 0.0 { desired_angle += tau; } + // turn towards mouse at turn speed if player_angle % tau > desired_angle { if (player_angle % tau) - desired_angle > PI as f32 { player_angle += TURN_SPEED; @@ -65,6 +68,7 @@ impl InGameScreen { } } + // snap to mouse if close enough if f32::abs(player_angle - desired_angle) < (TURN_SPEED * 1.1) { player_angle = desired_angle; } @@ -75,7 +79,7 @@ impl InGameScreen { player_angle += tau; } - + // set angle game_core.player.direction = Vector2::new(f32::cos(player_angle), f32::sin(player_angle)); // In the case the player is in "null", just jump the camera to them