From 7492187ad2e16c7398ca4ed812a57045c3d42d68 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 3 Feb 2018 22:42:25 +0500 Subject: Block selection implemented with mouse-look --- src/World.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 4 deletions(-) (limited to 'src/World.cpp') diff --git a/src/World.cpp b/src/World.cpp index 9df8889..e4c2a1b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -1,6 +1,7 @@ #include "World.hpp" #include +#include #include "Section.hpp" #include "Event.hpp" @@ -134,8 +135,86 @@ const Section &World::GetSection(Vector sectionPos) { } } -glm::vec3 World::Raycast(glm::vec3 position, glm::vec3 direction, float maxLength, float minPrecision) { - return glm::vec3(position * direction / maxLength * minPrecision); +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; yCollisionResult { - + int blockXBegin = pos.x - width - 1.0; int blockXEnd = pos.x + width + 0.5; int blockYBegin = pos.y - 0.5; @@ -324,9 +403,9 @@ BlockId World::GetBlockId(Vector pos) { void World::SetBlockId(Vector pos, BlockId block) { Vector sectionPos(std::floor(pos.x / 16.0), std::floor(pos.y / 16.0), std::floor(pos.z / 16.0)); - Section* section = GetSectionPtr(sectionPos); section->SetBlockId(pos - (sectionPos * 16), block); + PUSH_EVENT("ChunkChanged",sectionPos); } void World::SetBlockLight(Vector pos, unsigned char light) { -- cgit v1.2.3