From b372d928580589b3d1d655072fdc36574e98e47f Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Fri, 6 Mar 2026 23:06:19 +0800 Subject: [PATCH] fix: movement speed tied to frame rate --- include/Cubed/config.hpp | 3 +++ include/Cubed/gameplay/player.hpp | 7 ++++++- src/gameplay/player.cpp | 20 ++++++++++++++++++-- src/main.cpp | 3 +-- 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 include/Cubed/config.hpp diff --git a/include/Cubed/config.hpp b/include/Cubed/config.hpp new file mode 100644 index 0000000..70e4efc --- /dev/null +++ b/include/Cubed/config.hpp @@ -0,0 +1,3 @@ +#pragma once +constexpr int WORLD_SIZE_X = 32; +constexpr int WORLD_SIZE_Z = 32; \ No newline at end of file diff --git a/include/Cubed/gameplay/player.hpp b/include/Cubed/gameplay/player.hpp index baa958e..4b4e7a9 100644 --- a/include/Cubed/gameplay/player.hpp +++ b/include/Cubed/gameplay/player.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include struct MoveState { bool forward = false; bool back = false; @@ -14,6 +15,8 @@ struct MoveState { class Player { private: + bool** m_blockPresent; + float m_yaw; float m_pitch; @@ -21,7 +24,7 @@ private: float m_speed = 0.1f; // player is tow block tall, the pos is the lower pos - glm::vec3 m_playerPos; + glm::vec3 m_playerPos = glm::vec3(0.0f, 5.0f, 0.0f); glm::vec3 m_front = glm::vec3(0, 0, -1); glm::vec3 m_right; MoveState m_moveState; @@ -32,6 +35,8 @@ public: const glm::vec3& getFront() const; const glm::vec3& getPlayerPos() const; const MoveState& getMoveState() const; + + void init(bool** blockPresent); void setPlayerPos(const glm::vec3& pos); void update(float deltaTime); void updateFrontVec(float offsetX, float offsetY); diff --git a/src/gameplay/player.cpp b/src/gameplay/player.cpp index 7d1946a..d5ef788 100644 --- a/src/gameplay/player.cpp +++ b/src/gameplay/player.cpp @@ -1,5 +1,10 @@ #include #include + +static constexpr int UPDATE_TIME = 0.1f; +static float currentTime = 0.0f; + + Player::Player() { } @@ -16,11 +21,21 @@ const MoveState& Player::getMoveState() const { return m_moveState; } +void Player::init(bool** blockPresent) { + m_blockPresent = blockPresent; +} + void Player::setPlayerPos(const glm::vec3& pos) { m_playerPos = pos; } void Player::update(float deltaTime) { + + currentTime += deltaTime; + if (currentTime < UPDATE_TIME) { + return; + } + currentTime = 0.0f; m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f))); if (m_moveState.forward) { m_playerPos += glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed; @@ -35,11 +50,12 @@ void Player::update(float deltaTime) { m_playerPos += glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed; } if (m_moveState.up) { - m_playerPos += glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;; + m_playerPos += glm::vec3(0.0f, 1.0f, 0.0f) * m_speed; } if (m_moveState.down) { - m_playerPos -= glm::vec3(0.0f, 1.0f, 0.0f) * m_speed;; + m_playerPos -= glm::vec3(0.0f, 1.0f, 0.0f) * m_speed; } + } void Player::updatePlayerMoveState(int key, int action) { diff --git a/src/main.cpp b/src/main.cpp index a5a625a..8b1952d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -19,8 +20,6 @@ constexpr int numVAOs = 1; constexpr int numVBOs = 3; -constexpr int WORLD_SIZE_X = 32; -constexpr int WORLD_SIZE_Z = 32; bool blockPresent[WORLD_SIZE_X][WORLD_SIZE_Z] = {false}; GLuint renderingProgram; GLuint vao[numVAOs];