From 3298dde600f570b8e9d06a41100aaf3ff3d5d456 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 21 Mar 2026 16:14:09 +0800 Subject: [PATCH] fix: unable to move when in corner --- src/gameplay/player.cpp | 63 ++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 0e70a09..e9f59ea 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -126,31 +126,74 @@ void Player::update(float delta_time) { } m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f))); + glm::vec3 move_dir_front = glm::vec3(0.0f); + glm::vec3 move_dir_right = glm::vec3(0.0f); glm::vec3 move_dir = glm::vec3(0.0f); if (m_move_state.forward) { - move_dir += glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)); + move_dir_front += glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)); } if (m_move_state.back) { - move_dir -= glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)); + move_dir_front -= glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)); } if (m_move_state.left) { - move_dir -= glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)); + move_dir_right -= glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)); } if (m_move_state.right) { - move_dir += glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)); + move_dir_right += glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)); } + move_dir = move_dir_front + move_dir_right; + if (glm::length(move_dir) > 0.001f) { - direction = glm::normalize(move_dir); + direction = glm::normalize(move_dir); } - auto new_pos = m_player_pos + direction * speed * delta_time; - new_pos.y += 1.0f; - if (m_world.can_move(new_pos)) { - new_pos.y -= 1.0f; - m_player_pos = new_pos; + + if (glm::length(direction) > 0.001f) { + auto new_pos = m_player_pos + direction * speed * delta_time; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } else { + if (glm::length(move_dir_front) > 0.001f) { + if (std::abs(move_dir_front.x) > std::abs(move_dir_front.z)) { + move_dir_front.z = 0.0f; + } else { + move_dir_front.x = 0.0f; + } + direction = glm::normalize(move_dir_front); + auto new_pos = m_player_pos + direction * speed * delta_time; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } else { + direction = glm::vec3(0.0f, 0.0f, 0.0f); + } + } + if (glm::length(move_dir_right) > 0.001f) { + if (std::abs(move_dir_right.x) > std::abs(move_dir_right.z)) { + move_dir_right.z = 0.0f; + } else { + move_dir_right.x = 0.0f; + } + direction = glm::normalize(move_dir_right); + auto new_pos = m_player_pos + direction * speed * delta_time; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } else { + direction = glm::vec3(0.0f, 0.0f, 0.0f); + } + } + } } + + + if (m_move_state.up) { auto new_pos = m_player_pos + glm::vec3(0.0f, 1.0f, 0.0f) * speed * 2.0f * delta_time;