mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-04-10 06:14:07 +08:00
feat: add block detection while walking
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<LookBlock>& get_look_block_pos(const std::string& name) const;
|
||||
Player& get_player(const std::string& name);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user