From 2d30ce1cb98f46a2c33338470ce600e116c48176 Mon Sep 17 00:00:00 2001 From: 0x01FE <0x01fe@0x01fe.net> Date: Tue, 18 Nov 2025 19:51:09 -0600 Subject: [PATCH] fixed collision --- src/Camera.h | 15 ++++++++++++--- src/World.cpp | 2 +- src/main.cpp | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Camera.h b/src/Camera.h index d6d6a7d..063a7ab 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -6,7 +6,7 @@ #include "World.h" -const float CAMERA_HEIGHT = 0.0f; +const float CAMERA_HEIGHT = 1.8f; const float GRAVITY = 18.6f; // Should be in m/s^2 but it doesn't really work out with 9.8 so we double it class Camera @@ -22,6 +22,8 @@ class Camera glm::vec3 forward_velocity; glm::vec3 vertical_velocity; + bool grounded; + float speed; glm::vec3 worldUp; @@ -42,6 +44,8 @@ class Camera glm::vec3 right = glm::normalize(glm::cross(up, this->direction)); this->up = glm::cross(this->direction, right); + + this->grounded = false; } Camera(glm::vec3 pos, glm::vec3 target, float speed) @@ -60,6 +64,8 @@ class Camera glm::vec3 right = glm::normalize(glm::cross(up, this->direction)); this->up = glm::cross(this->direction, right); + + this->grounded = false; } glm::mat4 getView() @@ -100,8 +106,9 @@ class Camera target_position[axis] += position_delta[axis]; glm::vec3 aabb_center = target_position + aabb_center_offset; - glm::vec3 min = glm::floor(aabb_center - aabb_half_dims); - glm::vec3 max = glm::ceil(aabb_center + aabb_half_dims); + glm::vec3 adjusted_center = aabb_center + glm::vec3(0.5f, 0.5f, 0.5f); + glm::vec3 min = glm::floor(adjusted_center - aabb_half_dims); + glm::vec3 max = glm::ceil(adjusted_center + aabb_half_dims); bool collided = false; for (int x = min.x; x < max.x && !collided; x++) { @@ -120,12 +127,14 @@ class Camera this->position[axis] = target_position[axis]; } else if (axis == 1) { y_axis_collided = true; + grounded = true; this->vertical_velocity.y = 0.0f; } } if (!y_axis_collided) { this->vertical_velocity.y -= GRAVITY * time; + grounded = false; } diff --git a/src/World.cpp b/src/World.cpp index c66ff9f..3552ec5 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -77,7 +77,7 @@ std::optional> World::raycast_voxel(glm::vec3 // Adjust start position to account for voxels being centered at integer + 0.5 // (voxel at pos (0,0,0) occupies space from (-0.5,-0.5,-0.5) to (0.5,0.5,0.5)) glm::vec3 adjusted_start = start + glm::vec3(0.5f, 0.5f, 0.5f); - + glm::ivec3 pos = glm::ivec3(std::floor(adjusted_start.x), std::floor(adjusted_start.y), std::floor(adjusted_start.z)); // std::cout << "[RAYCAST] Starting voxel: (" << pos.x << ", " << pos.y << ", " << pos.z << ")" << std::endl; diff --git a/src/main.cpp b/src/main.cpp index 26f1665..3a26a97 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ const int EDIT_RANGE = 5; // Jump const float JUMP_COOLDOWN = 0.0f; -const float JUMP_POWER = 10.0f; // Initial Jump Speed +const float JUMP_POWER = 7.5f; // Initial Jump Speed float last_jump = 0.0f; // Camera @@ -193,7 +193,7 @@ void processInput(GLFWwindow *window) if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) // camera.velocity -= glm::normalize(glm::cross(camera.target, glm::normalize(glm::cross(camera.target, camera.worldUp)))) * camera.speed * 0.5f; { - if (current_time - last_jump > JUMP_COOLDOWN && camera.position.y == 0) + if (current_time - last_jump > JUMP_COOLDOWN && camera.grounded) { camera.vertical_velocity.y = JUMP_POWER; last_jump = current_time;