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 +++++++++++++++++++++++++++++++++++++++++++ game/vehicles/railVehicle.h | 5 ++++- game/vehicles/train.cpp | 6 ++++++ game/vehicles/train.h | 3 +++ game/vehicles/vehicle.h | 3 ++- 5 files changed, 59 insertions(+), 2 deletions(-) (limited to 'game/vehicles') 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); + }); +} diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index a089f1d..55cb6a2 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -4,6 +4,8 @@ #include "gfx/renderable.h" #include "railVehicleClass.h" #include +#include +#include #include #include #include @@ -11,13 +13,14 @@ class Shader; class Train; -class RailVehicle : public Renderable { +class RailVehicle : public Renderable, Selectable { public: explicit RailVehicle(RailVehicleClassPtr rvc) : rvClass {std::move(rvc)} { } void move(const Train *, float & trailBy); void render(const Shader & shader) const override; + [[nodiscard]] bool intersectRay(const glm::vec3 &, const glm::vec3 &, glm::vec2 *, float *) const override; Location location; diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 14753c0..4f19bed 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -23,6 +23,12 @@ Train::getBogiePosition(float linkDist, float dist) const return b2Link.first->positionAt(b2linkDist, b2Link.second); } +bool +Train::intersectRay(const glm::vec3 & pos, const glm::vec3 & dir, glm::vec2 * baryPos, float * eh) const +{ + return applyOne(&RailVehicle::intersectRay, pos, dir, baryPos, eh) != end(); +} + void Train::tick(TickDuration dur) { diff --git a/game/vehicles/train.h b/game/vehicles/train.h index ef49209..c823aed 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -9,6 +9,7 @@ #include "railVehicle.h" #include "vehicle.h" #include +#include #include #include #include @@ -27,6 +28,8 @@ public: void render(const Shader & shader) const override; + [[nodiscard]] bool intersectRay(const glm::vec3 &, const glm::vec3 &, glm::vec2 *, float *) const override; + void tick(TickDuration elapsed) override; void doActivity(Go *, TickDuration) override; void doActivity(Idle *, TickDuration) override; diff --git a/game/vehicles/vehicle.h b/game/vehicles/vehicle.h index 07bd492..3ddeac5 100644 --- a/game/vehicles/vehicle.h +++ b/game/vehicles/vehicle.h @@ -6,13 +6,14 @@ #include #include #include +#include #include #include #include class Location; -class Vehicle : public WorldObject, public Renderable { +class Vehicle : public WorldObject, public Renderable, public Selectable { public: explicit Vehicle(const LinkPtr & link, float linkDist = 0); float linkDist; // distance along current link -- cgit v1.2.3