From 7c8b8a39fd36e190578587d8d92f28f460a1fc4b Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 2 Jan 2022 21:07:37 +0000 Subject: Encapsulate Ray --- game/selectable.h | 4 +++- game/vehicles/railVehicle.cpp | 9 +++++---- game/vehicles/railVehicle.h | 3 ++- game/vehicles/train.cpp | 6 ++++-- game/vehicles/train.h | 3 ++- 5 files changed, 16 insertions(+), 9 deletions(-) (limited to 'game') diff --git a/game/selectable.h b/game/selectable.h index 0104f3d..3d734f3 100644 --- a/game/selectable.h +++ b/game/selectable.h @@ -3,11 +3,13 @@ #include #include +class Ray; + class Selectable { public: Selectable() = default; virtual ~Selectable() = default; DEFAULT_MOVE_COPY(Selectable); - [[nodiscard]] virtual bool intersectRay(const glm::vec3 &, const glm::vec3 &, glm::vec2 *, float *) const = 0; + [[nodiscard]] virtual bool intersectRay(const Ray &, glm::vec2 *, float *) const = 0; }; diff --git a/game/vehicles/railVehicle.cpp b/game/vehicles/railVehicle.cpp index 8d7b06f..9ad1ea7 100644 --- a/game/vehicles/railVehicle.cpp +++ b/game/vehicles/railVehicle.cpp @@ -9,6 +9,7 @@ #include #include #include +#include void RailVehicle::render(const Shader & shader) const @@ -29,7 +30,7 @@ RailVehicle::move(const Train * t, float & trailBy) } bool -RailVehicle::intersectRay(const glm::vec3 & pos, const glm::vec3 & dir, glm::vec2 * baryPos, float * eh) const +RailVehicle::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * eh) const { constexpr const auto X = 1.35F; const auto Y = this->rvClass->length / 2.F; @@ -63,8 +64,8 @@ RailVehicle::intersectRay(const glm::vec3 & pos, const glm::vec3 & dir, glm::vec {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); + triangles.begin(), triangles.end(), [&cornerVertices, &ray, &baryPos, &eh](const glm::uvec3 idx) { + return glm::intersectRayTriangle(ray.start, ray.direction, cornerVertices[idx[0]], + cornerVertices[idx[1]], cornerVertices[idx[2]], *baryPos, *eh); }); } diff --git a/game/vehicles/railVehicle.h b/game/vehicles/railVehicle.h index f52b154..5fbe1ed 100644 --- a/game/vehicles/railVehicle.h +++ b/game/vehicles/railVehicle.h @@ -10,6 +10,7 @@ #include class Shader; +class Ray; class Train; class RailVehicle : public Renderable, Selectable { @@ -19,7 +20,7 @@ public: 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; + [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; Location location; diff --git a/game/vehicles/train.cpp b/game/vehicles/train.cpp index 4f19bed..05915c4 100644 --- a/game/vehicles/train.cpp +++ b/game/vehicles/train.cpp @@ -9,6 +9,8 @@ #include #include +class Ray; + void Train::render(const Shader & shader) const { @@ -24,9 +26,9 @@ Train::getBogiePosition(float linkDist, float dist) const } bool -Train::intersectRay(const glm::vec3 & pos, const glm::vec3 & dir, glm::vec2 * baryPos, float * eh) const +Train::intersectRay(const Ray & ray, glm::vec2 * baryPos, float * eh) const { - return applyOne(&RailVehicle::intersectRay, pos, dir, baryPos, eh) != end(); + return applyOne(&RailVehicle::intersectRay, ray, baryPos, eh) != end(); } void diff --git a/game/vehicles/train.h b/game/vehicles/train.h index 68f9fbf..0c6a741 100644 --- a/game/vehicles/train.h +++ b/game/vehicles/train.h @@ -14,6 +14,7 @@ #include class Shader; +class Ray; class Train : public Vehicle, public Collection, public Can, public Can { public: @@ -27,7 +28,7 @@ public: void render(const Shader & shader) const override; - [[nodiscard]] bool intersectRay(const glm::vec3 &, const glm::vec3 &, glm::vec2 *, float *) const override; + [[nodiscard]] bool intersectRay(const Ray &, glm::vec2 *, float *) const override; void tick(TickDuration elapsed) override; void doActivity(Go *, TickDuration) override; -- cgit v1.2.3