feat: add collision detection

This commit is contained in:
2026-03-29 20:19:08 +08:00
parent 01d6e91da9
commit fb11fee955
8 changed files with 276 additions and 168 deletions

19
include/Cubed/AABB.hpp Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <glm/glm.hpp>
struct AABB {
glm::vec3 min{0.0f, 0.0f, 0.0f};
glm::vec3 max{0.0f, 0.0f, 0.0f};
AABB(glm::vec3 min_point, glm::vec3 max_point):
min(min_point),
max(max_point)
{
}
bool intersects(const AABB& other) const {
return (min.x <= other.max.x && max.x >= other.min.x) &&
(min.y <= other.max.y && max.y >= other.min.y) &&
(min.z <= other.max.z && max.z >= other.min.z);
}
};

View File

@@ -30,14 +30,14 @@ private:
Window m_window{m_renderer};
GLuint m_texture_array;
GLuint m_texture_array = 0;
inline static double last_time = glfwGetTime();
inline static double current_time = glfwGetTime();
inline static double delta_time = 0.0f;
inline static double fps_time_count = 0.0f;
inline static int frame_count = 0;
inline static int fps;
inline static int fps = 0;
void init();

View File

@@ -103,13 +103,13 @@ constexpr float SQUARE_TEXTURE_POS[6][2] = {
};
struct Vertex {
float x, y, z;
float s, t;
float layer;
float x = 0.0f, y = 0.0f, z = 0.0f;
float s = 0.0f, t = 0.0f;
float layer = 0.0f;
};
struct Vertex2D {
float x, y;
float s, t;
float layer;
float x = 0.0f, y = 0.0f;
float s = 0.0f, t = 0.0f;
float layer = 0.0f;
};

View File

@@ -1,6 +1,7 @@
#pragma once
#include <glm/glm.hpp>
#include <Cubed/AABB.hpp>
#include <Cubed/config.hpp>
#include <Cubed/gameplay/block.hpp>
#include <Cubed/input.hpp>
@@ -21,22 +22,35 @@ private:
float m_sensitivity = 0.15f;
float m_speed = 4.5f;
float down_speed = 0.0f;
bool can_up = true;
float speed = 0;
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 move_distance {0.0f, 0.0f, 0.0f};
// player is tow block tall, the pos is the lower pos
glm::vec3 m_player_pos = glm::vec3(0.0f, 15.0f, 0.0f);
glm::vec3 m_front = glm::vec3(0, 0, -1);
glm::vec3 m_right;
MoveState m_move_state;
glm::vec3 m_player_pos {0.0f, 15.0f, 0.0f};
glm::vec3 m_front {0, 0, -1};
glm::vec3 m_right {0, 0, 0};
glm::vec3 m_size {0.6f, 1.8f, 0.6f};
MoveState m_move_state {};
std::optional<LookBlock> m_look_block = std::nullopt;
std::string m_name;
std::string m_name {};
World& m_world;
bool ray_cast(const glm::vec3& start, const glm::vec3& dir, glm::ivec3& block_pos, glm::vec3& normal, float distance = 4.0f);
void update_direction();
void update_lookup_block();
void update_move(float delta_time);
void update_x_move();
void update_y_move();
void update_z_move();
public:
Player(World& world, const std::string& name);
~Player();
AABB get_aabb() const;
const glm::vec3& get_front() const;
const std::optional<LookBlock>& get_look_block_pos() const;
const glm::vec3& get_player_pos() const;

View File

@@ -2,6 +2,7 @@
#include <optional>
#include <unordered_map>
#include <Cubed/AABB.hpp>
#include <Cubed/gameplay/chunk.hpp>
class Player;
@@ -16,7 +17,7 @@ public:
World();
~World();
bool can_move(const glm::vec3& new_pos) const;
bool can_move(const AABB& player_box) const;
const BlockRenderData& get_block_render_data(int x, int y ,int z);
const std::optional<LookBlock>& get_look_block_pos(const std::string& name) const;
Player& get_player(const std::string& name);