From de9909df038d1b6bb47bcc82fea5f8dc9dec2491 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 28 Dec 2021 15:42:50 +0000 Subject: Initial implementation for being able to click in the main window to select something --- game/vehicles/railVehicle.cpp | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'game/vehicles/railVehicle.cpp') diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index aea7f8c..8d7b06f 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -1,8 +1,11 @@ #include "railVehicle.h" #include "railVehicleClass.h" #include "train.h" +#include #include #include +#include +#include #include #include #include @@ -24,3 +27,44 @@ RailVehicle::move(const Train * t, float & trailBy) location.rot = {vector_pitch(diff), vector_yaw(diff), 0}; trailBy += 0.6F + overhang; } + +bool +RailVehicle::intersectRay(const glm::vec3 & pos, const glm::vec3 & dir, glm::vec2 * baryPos, float * eh) const +{ + constexpr const auto X = 1.35F; + const auto Y = this->rvClass->length / 2.F; + constexpr const auto Z = 3.9F; + const auto move = glm::translate(location.pos) * rotate_ypr(location.rot); + const std::array cornerVertices {{ + move * glm::vec4 {-X, Y, 0, 1}, // LFB + move * glm::vec4 {X, Y, 0, 1}, // RFB + move * glm::vec4 {-X, Y, Z, 1}, // LFT + move * glm::vec4 {X, Y, Z, 1}, // RFT + move * glm::vec4 {-X, -Y, 0, 1}, // LBB + move * glm::vec4 {X, -Y, 0, 1}, // RBB + move * glm::vec4 {-X, -Y, Z, 1}, // LBT + move * glm::vec4 {X, -Y, Z, 1}, // RBT + }}; + static constexpr const std::array triangles {{ + // Front + {0, 1, 2}, + {1, 2, 3}, + // Left + {0, 2, 4}, + {2, 4, 6}, + // Back + {4, 5, 6}, + {5, 6, 7}, + // Right + {1, 3, 5}, + {3, 5, 7}, + // Top + {2, 3, 6}, + {3, 6, 7}, + }}; + return std::any_of( + triangles.begin(), triangles.end(), [&cornerVertices, &pos, &dir, &baryPos, &eh](const glm::uvec3 idx) { + return glm::intersectRayTriangle(pos, dir, cornerVertices[idx[0]], cornerVertices[idx[1]], + cornerVertices[idx[2]], *baryPos, *eh); + }); +} -- cgit v1.2.3