diff options
Diffstat (limited to 'game/vehicles')
-rw-r--r-- | game/vehicles/railVehicle.cpp | 44 | ||||
-rw-r--r-- | game/vehicles/railVehicle.h | 5 | ||||
-rw-r--r-- | game/vehicles/train.cpp | 6 | ||||
-rw-r--r-- | game/vehicles/train.h | 3 | ||||
-rw-r--r-- | game/vehicles/vehicle.h | 3 |
5 files changed, 59 insertions, 2 deletions
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 <algorithm> #include <array> #include <glm/glm.hpp> +#include <glm/gtx/intersect.hpp> +#include <glm/gtx/transform.hpp> #include <location.hpp> #include <maths.h> #include <memory> @@ -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<glm::vec3, 8> 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<glm::uvec3, 10> 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 <array> +#include <game/selectable.h> +#include <glm/glm.hpp> #include <location.hpp> #include <memory> #include <utility> @@ -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 <collection.hpp> +#include <glm/glm.hpp> #include <location.hpp> #include <memory> #include <vector> @@ -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 <game/activity.h> #include <game/network/link.h> #include <game/orders.h> +#include <game/selectable.h> #include <game/worldobject.h> #include <gfx/renderable.h> #include <memory> 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 |