fix: movement speed tied to frame rate

This commit is contained in:
2026-03-06 23:06:19 +08:00
parent d73d8e592d
commit b372d92858
4 changed files with 28 additions and 5 deletions

3
include/Cubed/config.hpp Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
constexpr int WORLD_SIZE_X = 32;
constexpr int WORLD_SIZE_Z = 32;

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <Cubed/config.hpp>
struct MoveState { struct MoveState {
bool forward = false; bool forward = false;
bool back = false; bool back = false;
@@ -14,6 +15,8 @@ struct MoveState {
class Player { class Player {
private: private:
bool** m_blockPresent;
float m_yaw; float m_yaw;
float m_pitch; float m_pitch;
@@ -21,7 +24,7 @@ private:
float m_speed = 0.1f; float m_speed = 0.1f;
// player is tow block tall, the pos is the lower pos // 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_front = glm::vec3(0, 0, -1);
glm::vec3 m_right; glm::vec3 m_right;
MoveState m_moveState; MoveState m_moveState;
@@ -32,6 +35,8 @@ public:
const glm::vec3& getFront() const; const glm::vec3& getFront() const;
const glm::vec3& getPlayerPos() const; const glm::vec3& getPlayerPos() const;
const MoveState& getMoveState() const; const MoveState& getMoveState() const;
void init(bool** blockPresent);
void setPlayerPos(const glm::vec3& pos); void setPlayerPos(const glm::vec3& pos);
void update(float deltaTime); void update(float deltaTime);
void updateFrontVec(float offsetX, float offsetY); void updateFrontVec(float offsetX, float offsetY);

View File

@@ -1,5 +1,10 @@
#include <Cubed/gameplay/player.hpp> #include <Cubed/gameplay/player.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
static constexpr int UPDATE_TIME = 0.1f;
static float currentTime = 0.0f;
Player::Player() { Player::Player() {
} }
@@ -16,11 +21,21 @@ const MoveState& Player::getMoveState() const {
return m_moveState; return m_moveState;
} }
void Player::init(bool** blockPresent) {
m_blockPresent = blockPresent;
}
void Player::setPlayerPos(const glm::vec3& pos) { void Player::setPlayerPos(const glm::vec3& pos) {
m_playerPos = pos; m_playerPos = pos;
} }
void Player::update(float deltaTime) { 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))); m_right = glm::normalize(glm::cross(m_front, glm::vec3(0.0f, 1.0f, 0.0f)));
if (m_moveState.forward) { if (m_moveState.forward) {
m_playerPos += glm::vec3(m_front.x, 0.0f, m_front.z) * m_speed; 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; m_playerPos += glm::vec3(m_right.x, 0.0f, m_right.z) * m_speed;
} }
if (m_moveState.up) { 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) { 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) { void Player::updatePlayerMoveState(int key, int action) {

View File

@@ -12,6 +12,7 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <Cubed/camera.hpp> #include <Cubed/camera.hpp>
#include <Cubed/config.hpp>
#include <Cubed/gameplay/player.hpp> #include <Cubed/gameplay/player.hpp>
#include <Cubed/tools/cubed_assert.hpp> #include <Cubed/tools/cubed_assert.hpp>
#include <Cubed/tools/log.hpp> #include <Cubed/tools/log.hpp>
@@ -19,8 +20,6 @@
constexpr int numVAOs = 1; constexpr int numVAOs = 1;
constexpr int numVBOs = 3; 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}; bool blockPresent[WORLD_SIZE_X][WORLD_SIZE_Z] = {false};
GLuint renderingProgram; GLuint renderingProgram;
GLuint vao[numVAOs]; GLuint vao[numVAOs];