From fd5314a1d1f159170a3da595921c763abdb8cd75 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Sat, 14 Mar 2026 16:32:41 +0800 Subject: [PATCH] feat: add block detection while walking --- include/Cubed/config.hpp | 89 ++++++++++++++++---------------- include/Cubed/gameplay/world.hpp | 2 +- src/camera.cpp | 5 +- src/gameplay/player.cpp | 41 ++++++++++++--- src/gameplay/world.cpp | 11 ++++ 5 files changed, 93 insertions(+), 55 deletions(-) diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp index a7b8d73..c991874 100644 --- a/include/Cubed/config.hpp +++ b/include/Cubed/config.hpp @@ -9,47 +9,47 @@ constexpr int MAX_BLOCK_STATUS = 1; constexpr float FOV = 70.0f; constexpr float VERTICES_POS[6][6][3] = { // ===== front (z = +1) ===== - -0.5f, -0.5f, 0.5f, // bottom left - -0.5f, 0.5f, 0.5f, // top left - 0.5f, 0.5f, 0.5f, // top right - 0.5f, 0.5f, 0.5f, // top right - 0.5f, -0.5f, 0.5f, // bottom right - -0.5f, -0.5f, 0.5f, // bottom left + 0.0f, 0.0f, 1.0f, // bottom left + 0.0f, 1.0f, 1.0f, // top left + 1.0f, 1.0f, 1.0f, // top right + 1.0f, 1.0f, 1.0f, // top right + 1.0f, 0.0f, 1.0f, // bottom right + 0.0f, 0.0f, 1.0f, // bottom left // ===== right (x = +1) ===== - 0.5f, -0.5f, 0.5f, // bottom front - 0.5f, -0.5f, -0.5f, // bottom back - 0.5f, 0.5f, -0.5f, // top back - 0.5f, 0.5f, -0.5f, // top back - 0.5f, 0.5f, 0.5f, // top front - 0.5f, -0.5f, 0.5f, // bottom front + 1.0f, 0.0f, 1.0f, // bottom front + 1.0f, 0.0f, 0.0f, // bottom back + 1.0f, 1.0f, 0.0f, // top back + 1.0f, 1.0f, 0.0f, // top back + 1.0f, 1.0f, 1.0f, // top front + 1.0f, 0.0f, 1.0f, // bottom front // ===== back (z = -1) ===== - -0.5f, -0.5f, -0.5f, // bottom left - 0.5f, -0.5f, -0.5f, // bottom right - 0.5f, 0.5f, -0.5f, // top right - 0.5f, 0.5f, -0.5f, // top right - -0.5f, 0.5f, -0.5f, // top left - -0.5f, -0.5f, -0.5f, // bottom left + 0.0f, 0.0f, 0.0f, // bottom left + 1.0f, 0.0f, 0.0f, // bottom right + 1.0f, 1.0f, 0.0f, // top right + 1.0f, 1.0f, 0.0f, // top right + 0.0f, 1.0f, 0.0f, // top left + 0.0f, 0.0f, 0.0f, // bottom left // ===== left (x = -1) ===== - -0.5f, -0.5f, -0.5f, // bottom back - -0.5f, -0.5f, 0.5f, // bottom front - -0.5f, 0.5f, 0.5f, // top front - -0.5f, 0.5f, 0.5f, // top front - -0.5f, 0.5f, -0.5f, // top back - -0.5f, -0.5f, -0.5f, // bottom back + 0.0f, 0.0f, 0.0f, // bottom back + 0.0f, 0.0f, 1.0f, // bottom front + 0.0f, 1.0f, 1.0f, // top front + 0.0f, 1.0f, 1.0f, // top front + 0.0f, 1.0f, 0.0f, // top back + 0.0f, 0.0f, 0.0f, // bottom back // ===== top (y = +1) ===== - -0.5f, 0.5f, -0.5f, // back left - 0.5f, 0.5f, -0.5f, // back right - 0.5f, 0.5f, 0.5f, // front right - 0.5f, 0.5f, 0.5f, // front right - -0.5f, 0.5f, 0.5f, // front left - -0.5f, 0.5f, -0.5f, // back left + 0.0f, 1.0f, 0.0f, // back left + 1.0f, 1.0f, 0.0f, // back right + 1.0f, 1.0f, 1.0f, // front right + 1.0f, 1.0f, 1.0f, // front right + 0.0f, 1.0f, 1.0f, // front left + 0.0f, 1.0f, 0.0f, // back left // ===== bottom (y = -1) ===== - -0.5f, -0.5f, 0.5f, // front left - 0.5f, -0.5f, 0.5f, // front right - 0.5f, -0.5f, -0.5f, // back right - 0.5f, -0.5f, -0.5f, // back right - -0.5f, -0.5f, -0.5f, // back left - -0.5f, -0.5f, 0.5f // front left + 0.0f, 0.0f, 1.0f, // front left + 1.0f, 0.0f, 1.0f, // front right + 1.0f, 0.0f, 0.0f, // back right + 1.0f, 0.0f, 0.0f, // back right + 0.0f, 0.0f, 0.0f, // back left + 0.0f, 0.0f, 1.0f // front left }; constexpr float TEX_COORDS[6][6][2] = { @@ -63,16 +63,17 @@ constexpr float TEX_COORDS[6][6][2] = { }; constexpr float CUBE_VER[24] = { - -0.5, -0.5, -0.5, - 0.5, -0.5, -0.5, - 0.5, 0.5, -0.5, - -0.5, 0.5, -0.5, - -0.5, -0.5, 0.5, - 0.5, -0.5, 0.5, - 0.5, 0.5, 0.5, - -0.5, 0.5, 0.5 + 0.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 1.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 1.0, 1.0, + 0.0, 1.0, 1.0 }; + constexpr int OUTLINE_CUBE_INDICES[24] = { 0,1, 1,2, 2,3, 3,0, 4,5, 5,6, 6,7, 7,4, diff --git a/include/Cubed/gameplay/world.hpp b/include/Cubed/gameplay/world.hpp index 72b3ffc..5ccc5a0 100644 --- a/include/Cubed/gameplay/world.hpp +++ b/include/Cubed/gameplay/world.hpp @@ -15,7 +15,7 @@ public: World(); ~World(); - + bool can_move(const glm::vec3& new_pos) const; const BlockRenderData& get_block_render_data(int x, int y ,int z); const std::optional& get_look_block_pos(const std::string& name) const; Player& get_player(const std::string& name); diff --git a/src/camera.cpp b/src/camera.cpp index e49fdc3..f7ebfe1 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -9,13 +9,14 @@ Camera::Camera() { void Camera::update_move_camera() { CUBED_ASSERT_MSG(m_player, "nullptr"); auto pos = m_player->get_player_pos(); - m_camera_pos = glm::vec3(pos.x, pos.y + 1, pos.z); + // pos.y need to add 1.6f to center + m_camera_pos = glm::vec3(pos.x, pos.y + 1.6f, pos.z); } void Camera::camera_init(Player* player) { - m_camera_pos = glm::vec3(0.0f, 2.0f, 0.0f); m_player = player; + update_move_camera(); } void Camera::update_cursor_position_camera(double xpos, double ypos) { diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 610c823..106b380 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -113,19 +113,44 @@ void Player::update(float delta_time) { m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f))); float speed = m_speed * delta_time; if (m_move_state.forward) { - m_player_pos += glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)) * speed; + auto new_pos = m_player_pos + glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)) * speed; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } } if (m_move_state.back) { - m_player_pos -= glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)) * speed; + auto new_pos = m_player_pos - glm::normalize(glm::vec3(m_front.x, 0.0f, m_front.z)) * speed; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } } if (m_move_state.left) { - m_player_pos -= glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)) * speed; + auto new_pos = m_player_pos - glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)) * speed; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } } if (m_move_state.right) { - m_player_pos += glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)) * speed; + auto new_pos = m_player_pos + glm::normalize(glm::vec3(m_right.x, 0.0f, m_right.z)) * speed; + new_pos.y += 1.0f; + if (m_world.can_move(new_pos)) { + new_pos.y -= 1.0f; + m_player_pos = new_pos; + } } if (m_move_state.up) { - m_player_pos += glm::vec3(0.0f, 1.0f, 0.0f) * speed * 2.0f; + auto new_pos = m_player_pos + glm::vec3(0.0f, 1.0f, 0.0f) * speed * 2.0f; + new_pos.y += 2.0f; + if (!m_world.is_block(new_pos)) { + new_pos.y -= 2.0f; + m_player_pos = new_pos; + } } /* if (m_move_state.down) { @@ -135,7 +160,7 @@ void Player::update(float delta_time) { // calculate the block that is looked glm::ivec3 block_pos; glm::vec3 block_normal; - if(ray_cast(glm::vec3(m_player_pos.x + 0.5f, (m_player_pos.y + 1.0f), m_player_pos.z + 0.5f), m_front, block_pos, block_normal)) { + if(ray_cast(glm::vec3(m_player_pos.x, (m_player_pos.y + 1.0f), m_player_pos.z), m_front, block_pos, block_normal)) { m_look_block = std::move(LookBlock{block_pos, glm::floor(block_normal)}); } else { m_look_block = std::nullopt; @@ -160,12 +185,12 @@ void Player::update(float delta_time) { static bool should_ceil = true; - if (!m_world.is_block(m_player_pos)) { + if (!m_world.is_block(glm::floor(m_player_pos))) { m_player_pos -= glm::vec3(0.0f, 1.0f, 0.0f) * speed * 0.7f; } else if (should_ceil) { should_ceil = false; - m_player_pos.y = std::ceil(m_player_pos.y); + m_player_pos.y = std::floor(m_player_pos.y + 1.0f); } if (m_player_pos.y < -50.0f) { diff --git a/src/gameplay/world.cpp b/src/gameplay/world.cpp index 646747f..09dc845 100644 --- a/src/gameplay/world.cpp +++ b/src/gameplay/world.cpp @@ -11,6 +11,17 @@ World::~World() { } +bool World::can_move(const glm::vec3& pos) const{ + + if (is_block(glm::floor(pos))) { + return false; + } + if (is_block(glm::floor(glm::vec3(pos.x, pos.y + 1.0f, pos.z)))) { + return false; + } + return true; +} + const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) { int chunk_x, chunk_z; if (world_x < 0) {