From 695e3db5f9b24ad46d236a28f9896dc9e5a619f6 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Tue, 6 Feb 2018 16:47:21 +0500 Subject: Simplified raycast --- src/GameState.cpp | 4 ++- src/World.cpp | 93 +++++++++---------------------------------------------- src/World.hpp | 8 ++++- 3 files changed, 25 insertions(+), 80 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 650cd6f..180db98 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -36,7 +36,9 @@ void GameState::Update(float deltaTime) { direction.y = sin(glm::radians(playerPitch)); direction.z = sin(glm::radians(playerYaw)) * cos(glm::radians(playerPitch)); - selectedBlock = world.Raycast(player->pos + player->EyeOffset, direction, distanceToSelectedBlock); + RaycastResult raycast = world.Raycast(player->pos + player->EyeOffset, direction); + selectedBlock = raycast.isHit ? raycast.hitBlock : Vector(0,0,0); + distanceToSelectedBlock = raycast.isHit ? (player->pos - raycast.hitPos).GetLength() : 0.0f; } } diff --git a/src/World.cpp b/src/World.cpp index e4c2a1b..4678964 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -135,86 +135,23 @@ const Section &World::GetSection(Vector sectionPos) { } } -Vector World::Raycast(glm::vec3 position, glm::vec3 direction, float &distance) { - auto triangle_intersection = [&] (const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2) -> bool { - glm::vec3 e1 = v1 - v0; - glm::vec3 e2 = v2 - v0; - - glm::vec3 pvec = glm::cross(direction, e2); - - float det = glm::dot(e1, pvec); - if (det < 1e-8 && det > -1e-8) { - return 0; - } - - float inv_det = 1 / det; - glm::vec3 tvec = position - v0; - float u = dot(tvec, pvec) * inv_det; - if (u < 0 || u > 1) { - return 0; - } - - glm::vec3 qvec = cross(tvec, e1); - float v = dot(direction, qvec) * inv_det; - if (v < 0 || u + v > 1) { - return 0; - } - return dot(e2, qvec) * inv_det; - }; - - float minDistance = 1000000; - Vector minBlock; - - for (int y = position.y-5; y