Fixed raycasting

This commit is contained in:
JISAUAY 2025-11-17 10:54:49 -06:00
parent bee594d4b9
commit 68acdc5183
5 changed files with 44 additions and 54 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -35,3 +35,9 @@ Different voxel types are now supported in the data structures and passed to the
When placing a block, the program now raycasts to place where the camera is looking.
![a place of brown dirt with word Hi built out of stone.](.attachments/raycasting_test.png)
### 0.6.1 : Raycast Bug Fixes - 11/17/25
It took me a couple of days to fix the ray casting. I forgot that my mesh is offset by 0.5, so 3/4 raycasts looked at least a block off of what the camera was looking at.
![Hello, written in blocks on a dirt plane](.attachments/fixed-raycast.png)

1
run.ps1 Normal file
View File

@ -0,0 +1 @@
.\build\Debug\VoxelEngine.exe

View File

@ -74,7 +74,11 @@ std::optional<std::pair<glm::ivec3, glm::ivec3>> World::raycast_voxel(glm::vec3
// std::cout << "[RAYCAST] Start: (" << start.x << ", " << start.y << ", " << start.z << ")" << std::endl;
// std::cout << "[RAYCAST] Direction: (" << direction.x << ", " << direction.y << ", " << direction.z << ")" << std::endl;
glm::ivec3 pos = glm::ivec3(std::floor(start.x), std::floor(start.y), std::floor(start.z));
// 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;
// (+1, -1, or 0 for each axis)
@ -94,7 +98,7 @@ std::optional<std::pair<glm::ivec3, glm::ivec3>> World::raycast_voxel(glm::vec3
// Calculate t_max: distance along ray to next voxel boundary for each axis
glm::vec3 t_max;
glm::vec3 fract = start - glm::vec3(pos);
glm::vec3 fract = adjusted_start - glm::vec3(pos);
// For each axis, calculate distance to the next boundary
if (step_dir.x > 0)

View File

@ -13,6 +13,8 @@
unsigned int SCR_WIDTH = 1920;
unsigned int SCR_HEIGHT = 1080;
const int EDIT_RANGE = 5;
// Jump
const float JUMP_COOLDOWN = 0.0f;
const float JUMP_POWER = 10.0f; // Initial Jump Speed
@ -96,9 +98,19 @@ void processInput(GLFWwindow *window)
{
float current_time = glfwGetTime();
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
glm::vec3 eye = camera.getEyePosition();
glm::mat4 view = camera.getView();
glm::vec3 forward = -glm::vec3(view[0][2], view[1][2], view[2][2]); // Extract forward from view matrix
std::cout << "Camera target (from pitch/yaw): (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
std::cout << "Camera forward (from view matrix): (" << forward.x << ", " << forward.y << ", " << forward.z << ")" << std::endl;
std::cout << "Match: " << (glm::length(camera.target - forward) < 0.01f ? "YES" : "NO") << std::endl;
}
// Update targeted block every frame for highlight
std::optional<std::pair<glm::ivec3, glm::ivec3>> raycast_result =
world.raycast_voxel(camera.getEyePosition(), camera.target, 5);
world.raycast_voxel(camera.getEyePosition(), camera.target, EDIT_RANGE);
if (raycast_result.has_value()) {
targeted_block = raycast_result.value().first;
@ -205,7 +217,7 @@ void processInput(GLFWwindow *window)
std::cout << "Camera target: (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
std::optional<std::pair<glm::ivec3, glm::ivec3>> raycast_result =
world.raycast_voxel(camera.getEyePosition(), camera.target, 3);
world.raycast_voxel(camera.getEyePosition(), camera.target, EDIT_RANGE);
if (raycast_result.has_value()) {
auto [target_block, normal] = raycast_result.value();
@ -230,7 +242,7 @@ void processInput(GLFWwindow *window)
std::cout << "Camera target: (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
std::optional<std::pair<glm::ivec3, glm::ivec3>> raycast_result =
world.raycast_voxel(camera.getEyePosition(), camera.target, 3);
world.raycast_voxel(camera.getEyePosition(), camera.target, EDIT_RANGE);
if (raycast_result.has_value()) {
auto [target_block, normal] = raycast_result.value();
@ -257,7 +269,7 @@ int main() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// 2. --- Create a Window ---
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Voxel Engine 0.6.0", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Voxel Engine 0.6.1", NULL, NULL);
if (window == NULL) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
@ -490,39 +502,6 @@ int main() {
glBindVertexArray(0);
}
// ---- Draw Debug Ray ----
{
shader.use();
glm::vec3 ray_start = camera.getEyePosition();
static bool printed_debug = false;
// Draw multiple small cubes along the ray path to make it visible
for (int i = 1; i <= 10; i++) {
float distance = i * 0.5f;
glm::vec3 point = ray_start + camera.target * distance;
if (!printed_debug && i == 1) {
std::cout << "\n[DEBUG RAY]" << std::endl;
std::cout << "Ray start: (" << ray_start.x << ", " << ray_start.y << ", " << ray_start.z << ")" << std::endl;
std::cout << "Ray direction: (" << camera.target.x << ", " << camera.target.y << ", " << camera.target.z << ")" << std::endl;
std::cout << "First marker at: (" << point.x << ", " << point.y << ", " << point.z << ")" << std::endl;
printed_debug = true;
}
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, point);
model = glm::scale(model, glm::vec3(0.1f)); // Small marker
glm::mat4 mvp = projection * view * model;
shader.setMat4("u_mvp", mvp);
shader.setVec3("u_Color", glm::vec3(1.0f, 0.0f, 0.0f)); // Red markers
glBindVertexArray(highlightVAO);
glDrawElements(GL_LINES, 24, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
}
// ---- Draw Crosshair ----
glDisable(GL_DEPTH_TEST); // Draw on top of everything
shader.use();