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/GameState.cpp | 13 +++++++-- src/GameState.hpp | 1 + src/Render.cpp | 8 ++++- src/World.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- src/World.hpp | 2 +- 5 files changed, 103 insertions(+), 8 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index 20926e0..650cd6f 100644 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -27,7 +27,16 @@ void GameState::Update(float deltaTime) { PUSH_EVENT("SendPacket",packet); } - selectedBlock = Vector(Vector(player->pos.x,player->pos.y,player->pos.z) - Vector(-1,0,0)); + + double playerYaw = Entity::DecodeYaw(player->yaw); + double playerPitch = Entity::DecodePitch(player->pitch); + + glm::vec3 direction; + direction.x = cos(glm::radians(playerYaw)) * cos(glm::radians(playerPitch)); + 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); } } @@ -404,7 +413,7 @@ void GameState::HandleMovement(GameState::Direction direction, float deltaTime) const double playerSpeed = 43; float velocity = playerSpeed * deltaTime; - + double playerYaw = Entity::DecodeYaw(player->yaw); double playerPitch = Entity::DecodePitch(player->pitch); diff --git a/src/GameState.hpp b/src/GameState.hpp index 0bc8cfd..5d3d36a 100644 --- a/src/GameState.hpp +++ b/src/GameState.hpp @@ -62,4 +62,5 @@ public: std::vector openedWindows; Vector selectedBlock; + float distanceToSelectedBlock; }; diff --git a/src/Render.cpp b/src/Render.cpp index f53cc91..534782e 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -291,7 +291,7 @@ void Render::RenderGui() { ImGui::Text("Player pos: %.1f %.1f %.1f OnGround=%d", world->GameStatePtr()->player->pos.x, world->GameStatePtr()->player->pos.y, world->GameStatePtr()->player->pos.z, world->GameStatePtr()->player->onGround); ImGui::Text("Player vel: %.1f %.1f %.1f", world->GameStatePtr()->player->vel.x, world->GameStatePtr()->player->vel.y, world->GameStatePtr()->player->vel.z); ImGui::Text("Player health: %.1f/%.1f", world->GameStatePtr()->g_PlayerHealth, 20.0f); - ImGui::Text("Selected block: %d %d %d",world->GameStatePtr()->selectedBlock.x,world->GameStatePtr()->selectedBlock.y,world->GameStatePtr()->selectedBlock.z); + ImGui::Text("Selected block: %d %d %d : %.1f",world->GameStatePtr()->selectedBlock.x,world->GameStatePtr()->selectedBlock.y,world->GameStatePtr()->selectedBlock.z,world->GameStatePtr()->distanceToSelectedBlock); } ImGui::End(); @@ -476,6 +476,12 @@ void Render::RenderGui() { } case State::InitialLoading: break; + case State::Playing: { + ImGui::SetNextWindowPosCenter(); + ImGui::Begin("",0,windowFlags); + ImGui::End(); + break; + } } ImGui::Render(); 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) { diff --git a/src/World.hpp b/src/World.hpp index 6d10491..50dfa31 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -55,7 +55,7 @@ public: const Section &GetSection(Vector sectionPos); - glm::vec3 Raycast(glm::vec3 position, glm::vec3 direction, float maxLength = 1000.0f, float minPrecision = 0.01f); + Vector Raycast(glm::vec3 position, glm::vec3 direction, float &distance); void UpdatePhysics(float delta); -- cgit v1.2.3