From 381caebb524a5bb8f091d1aa2ca02d384e010961 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 7 Jan 2024 17:13:38 +0000 Subject: Global positions in network data --- game/network/link.cpp | 28 ++++++++++++------------ game/network/link.h | 21 +++++++++--------- game/network/rail.cpp | 59 ++++++++++++++++++++++++--------------------------- game/network/rail.h | 10 ++++----- 4 files changed, 57 insertions(+), 61 deletions(-) (limited to 'game/network') diff --git a/game/network/link.cpp b/game/network/link.cpp index e8eaea2..dc9aecc 100644 --- a/game/network/link.cpp +++ b/game/network/link.cpp @@ -1,5 +1,4 @@ #include "link.h" -#include #include #include #include @@ -8,10 +7,10 @@ Link::Link(End a, End b, float l) : ends {{std::move(a), std::move(b)}}, length {l} { } -LinkCurve::LinkCurve(Position3D c, float r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { } +LinkCurve::LinkCurve(GlobalPosition3D c, RelativeDistance r, Arc a) : centreBase {c}, radius {r}, arc {std::move(a)} { } bool -operator<(const Position3D & a, const Position3D & b) +operator<(const GlobalPosition3D & a, const GlobalPosition3D & b) { // NOLINTNEXTLINE(hicpp-use-nullptr,modernize-use-nullptr) return std::tie(a.x, a.y, a.z) < std::tie(b.x, b.y, b.z); @@ -24,12 +23,13 @@ operator<(const Node & a, const Node & b) } Location -LinkStraight::positionAt(float dist, unsigned char start) const +LinkStraight::positionAt(RelativeDistance dist, unsigned char start) const { const auto es {std::make_pair(ends[start].node.get(), ends[1 - start].node.get())}; - const auto diff {es.second->pos - es.first->pos}; + const RelativePosition3D diff {es.second->pos - es.first->pos}; const auto dir {glm::normalize(diff)}; - return Location {es.first->pos + vehiclePositionOffset() + dir * dist, {vector_pitch(dir), vector_yaw(dir), 0}}; + return Location {es.first->pos + GlobalPosition3D(vehiclePositionOffset() + dir * dist), + {vector_pitch(dir), vector_yaw(dir), 0}}; } bool @@ -49,9 +49,11 @@ LinkCurve::positionAt(float dist, unsigned char start) const const auto ang {as.first + ((as.second - as.first) * frac)}; const auto relPos {(sincosf(ang) || 0.F) * radius}; const auto relClimb {vehiclePositionOffset() - + Position3D {0, 0, es.first->pos.z - centreBase.z + ((es.second->pos.z - es.first->pos.z) * frac)}}; - const auto pitch {vector_pitch({0, 0, (es.second->pos.z - es.first->pos.z) / length})}; - return Location {relPos + relClimb + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; + + RelativePosition3D {0, 0, + static_cast(es.first->pos.z - centreBase.z) + + (static_cast(es.second->pos.z - es.first->pos.z) * frac)}}; + const auto pitch {vector_pitch({0, 0, static_cast(es.second->pos.z - es.first->pos.z) / length})}; + return Location {GlobalPosition3D(relPos + relClimb) + centreBase, {pitch, normalize(ang + dirOffset[start]), 0}}; } bool @@ -61,15 +63,13 @@ LinkCurve::intersectRay(const Ray & ray) const const auto & e1p {ends[1].node->pos}; const auto slength = round_frac(length / 2.F, 5.F); const auto segs = std::round(15.F * slength / std::pow(radius, 0.7F)); - const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength} / segs}; - const auto trans {glm::translate(centreBase)}; + const auto step {Position2D {arc_length(arc), e1p.z - e0p.z} / segs}; auto segCount = static_cast(std::lround(segs)) + 1; std::vector points; points.reserve(segCount); - for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { - const auto t {trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; - points.emplace_back(t * glm::vec4 {0, 0, 0, 1}); + for (Position2D swing = {arc.first, centreBase.z - e0p.z}; segCount; swing += step, --segCount) { + points.emplace_back(centreBase + GlobalPosition3D((sincosf(swing.x) * radius) || swing.y)); } return ray.passesCloseToEdges(points, 1.F); } diff --git a/game/network/link.h b/game/network/link.h index 95c141e..725e023 100644 --- a/game/network/link.h +++ b/game/network/link.h @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -17,12 +16,12 @@ template class Ray; // it has location class Node : public StdTypeDefs { public: - explicit Node(Position3D p) noexcept : pos(p) {}; + explicit Node(GlobalPosition3D p) noexcept : pos(p) {}; virtual ~Node() noexcept = default; NO_COPY(Node); NO_MOVE(Node); - Position3D pos; + GlobalPosition3D pos; }; // Generic network link @@ -44,21 +43,21 @@ public: NO_COPY(Link); NO_MOVE(Link); - [[nodiscard]] virtual Location positionAt(float dist, unsigned char start) const = 0; + [[nodiscard]] virtual Location positionAt(RelativeDistance dist, unsigned char start) const = 0; [[nodiscard]] virtual bool intersectRay(const Ray &) const = 0; std::array ends; float length; protected: - [[nodiscard]] virtual Position3D + [[nodiscard]] virtual RelativePosition3D vehiclePositionOffset() const { return {}; } }; -bool operator<(const Position3D & a, const Position3D & b); +bool operator<(const GlobalPosition3D & a, const GlobalPosition3D & b); bool operator<(const Node & a, const Node & b); class LinkStraight : public virtual Link { @@ -68,7 +67,7 @@ public: NO_COPY(LinkStraight); NO_MOVE(LinkStraight); - [[nodiscard]] Location positionAt(float dist, unsigned char start) const override; + [[nodiscard]] Location positionAt(RelativeDistance dist, unsigned char start) const override; [[nodiscard]] bool intersectRay(const Ray &) const override; }; @@ -77,15 +76,15 @@ LinkStraight::~LinkStraight() = default; class LinkCurve : public virtual Link { public: inline ~LinkCurve() override = 0; - LinkCurve(Position3D, float, Arc); + LinkCurve(GlobalPosition3D, RelativeDistance, Arc); NO_COPY(LinkCurve); NO_MOVE(LinkCurve); - [[nodiscard]] Location positionAt(float dist, unsigned char start) const override; + [[nodiscard]] Location positionAt(RelativeDistance dist, unsigned char start) const override; [[nodiscard]] bool intersectRay(const Ray &) const override; - Position3D centreBase; - float radius; + GlobalPosition3D centreBase; + RelativeDistance radius; Arc arc; }; diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 303f1c8..79aaf97 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -9,9 +9,7 @@ #include #include #include -#include #include -#include #include #include @@ -28,7 +26,7 @@ RailLinks::tick(TickDuration) } std::shared_ptr -RailLinks::addLinksBetween(Position3D start, Position3D end) +RailLinks::addLinksBetween(GlobalPosition3D start, GlobalPosition3D end) { auto node1ins = newNodeAt(start), node2ins = newNodeAt(end); if (node1ins.second == NodeIs::NotInNetwork && node2ins.second == NodeIs::NotInNetwork) { @@ -41,31 +39,32 @@ RailLinks::addLinksBetween(Position3D start, Position3D end) std::swap(start, end); } // Find start link/end - opposite entry dir to existing link; so pi +... - const float dir = pi + findNodeDirection(node1ins.first); + const Angle dir = pi + findNodeDirection(node1ins.first); if (dir == vector_yaw(end - start)) { return addLink(start, end); } - const Position2D flatStart {start.xy()}, flatEnd {end.xy()}; + const auto flatStart {start.xy()}, flatEnd {end.xy()}; if (node2ins.second == NodeIs::InNetwork) { auto midheight = [&](auto mid) { - const auto sm = glm::distance(flatStart, mid), em = glm::distance(flatEnd, mid); - return start.z + ((end.z - start.z) * (sm / (sm + em))); + const auto sm = glm::length(RelativePosition2D(flatStart - mid)), + em = glm::length(RelativePosition2D(flatEnd - mid)); + return start.z + GlobalDistance(RelativeDistance(end.z - start.z) * (sm / (sm + em))); }; const float dir2 = pi + findNodeDirection(node2ins.first); if (const auto radii = find_arcs_radius(flatStart, dir, flatEnd, dir2); radii.first < radii.second) { const auto radius {radii.first}; - const auto c1 = flatStart + sincosf(dir + half_pi) * radius; - const auto c2 = flatEnd + sincosf(dir2 + half_pi) * radius; - const auto mid = (c1 + c2) / 2.F; + const auto c1 = flatStart + GlobalPosition2D(sincosf(dir + half_pi) * radius); + const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 + half_pi) * radius); + const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink(start, midh, c1); return addLink(end, midh, c2); } else { const auto radius {radii.second}; - const auto c1 = flatStart + sincosf(dir - half_pi) * radius; - const auto c2 = flatEnd + sincosf(dir2 - half_pi) * radius; - const auto mid = (c1 + c2) / 2.F; + const auto c1 = flatStart + GlobalPosition2D(sincosf(dir - half_pi) * radius); + const auto c2 = flatEnd + GlobalPosition2D(sincosf(dir2 - half_pi) * radius); + const auto mid = (c1 + c2) / 2; const auto midh = mid || midheight(mid); addLink(midh, start, c1); return addLink(midh, end, c2); @@ -100,7 +99,7 @@ RailLink::render(const SceneShader &) const mesh->Draw(); } -constexpr const std::array, RAIL_CROSSSECTION_VERTICES> railCrossSection {{ +constexpr const std::array, RAIL_CROSSSECTION_VERTICES> railCrossSection {{ // ___________ // _/ \_ // left to right @@ -122,59 +121,57 @@ RailLinkStraight::RailLinkStraight(const Node::Ptr & a, const Node::Ptr & b) : R { } -RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const Position3D & diff) : +RailLinkStraight::RailLinkStraight(Node::Ptr a, Node::Ptr b, const RelativePosition3D & diff) : Link({std::move(a), vector_yaw(diff)}, {std::move(b), vector_yaw(-diff)}, glm::length(diff)) { if (glGenVertexArrays) { std::vector vertices; vertices.reserve(2 * railCrossSection.size()); const auto len = round_sleepers(length / 2000.F); - const auto e {flat_orientation(diff)}; + const glm::mat3 trans {flat_orientation(diff)}; for (auto ei : {1U, 0U}) { - const auto trans {glm::translate(ends[ei].node->pos) * e}; for (const auto & rcs : railCrossSection) { - const Position3D m {(trans * (rcs.first || 1.F))}; - vertices.emplace_back(m, Position2D {rcs.second, len * static_cast(ei)}, up); + const auto m {ends[ei].node->pos + GlobalPosition3D(trans * rcs.first)}; + vertices.emplace_back(m, TextureRelCoord {rcs.second, len * static_cast(ei)}, up); } } mesh = defaultMesh(vertices); } } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position2D c) : - RailLinkCurve(a, b, c || a->pos.z, {c || 0.F, a->pos, b->pos}) +RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition2D c) : + RailLinkCurve(a, b, c || a->pos.z, {c || 0, a->pos, b->pos}) { } -RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, Position3D c, const Arc arc) : +RailLinkCurve::RailLinkCurve(const Node::Ptr & a, const Node::Ptr & b, GlobalPosition3D c, const Arc arc) : Link({a, normalize(arc.first + half_pi)}, {b, normalize(arc.second - half_pi)}, - (glm::length(a->pos - c)) * arc_length(arc)), - LinkCurve {c, glm::length(ends[0].node->pos - c), arc} + glm::length(RelativePosition3D(a->pos - c)) * arc_length(arc)), + LinkCurve {c, glm::length(RelativePosition3D(ends[0].node->pos - c)), arc} { if (glGenVertexArrays) { const auto & e0p {ends[0].node->pos}; const auto & e1p {ends[1].node->pos}; const auto slength = round_sleepers(length / 2.F); const auto segs = std::round(slength / std::pow(radius, 0.7F)); - const auto step {Position3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs}; - const auto trans {glm::translate(centreBase)}; + const auto step {RelativePosition3D {arc_length(arc), e1p.z - e0p.z, slength / 1000.F} / segs}; auto segCount = static_cast(std::lround(segs)) + 1; std::vector vertices; vertices.reserve(segCount * railCrossSection.size()); - for (Position3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { + for (RelativePosition3D swing = {arc.first, centreBase.z - e0p.z, 0.F}; segCount; swing += step, --segCount) { const auto t { - trans * glm::rotate(half_pi - swing.x, up) * glm::translate(Position3D {radius, 0.F, swing.y})}; + glm::rotate(half_pi - swing.x, up) * glm::translate(RelativePosition3D {radius, 0.F, swing.y})}; for (const auto & rcs : railCrossSection) { - const Position3D m {(t * (rcs.first || 1.F))}; - vertices.emplace_back(m, Position2D {rcs.second, swing.z}, up); + const auto m {centreBase + GlobalPosition3D(t * (rcs.first || 1.F))}; + vertices.emplace_back(m, TextureRelCoord {rcs.second, swing.z}, up); } } mesh = defaultMesh(vertices); } } -Position3D +RelativePosition3D RailLink::vehiclePositionOffset() const { return RAIL_HEIGHT; diff --git a/game/network/rail.h b/game/network/rail.h index 4a1932f..986b0aa 100644 --- a/game/network/rail.h +++ b/game/network/rail.h @@ -32,7 +32,7 @@ public: NO_MOVE(RailLink); protected: - [[nodiscard]] Position3D vehiclePositionOffset() const override; + [[nodiscard]] RelativePosition3D vehiclePositionOffset() const override; [[nodiscard]] static Mesh::Ptr defaultMesh(const std::span vertices); Mesh::Ptr mesh; @@ -45,22 +45,22 @@ public: RailLinkStraight(const Node::Ptr &, const Node::Ptr &); private: - RailLinkStraight(Node::Ptr, Node::Ptr, const Position3D & diff); + RailLinkStraight(Node::Ptr, Node::Ptr, const RelativePosition3D & diff); }; class RailLinkCurve : public RailLink, public LinkCurve { public: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position2D); + RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition2D); private: - RailLinkCurve(const Node::Ptr &, const Node::Ptr &, Position3D, const Arc); + RailLinkCurve(const Node::Ptr &, const Node::Ptr &, GlobalPosition3D, const Arc); }; class RailLinks : public NetworkOf, public WorldObject { public: RailLinks(); - std::shared_ptr addLinksBetween(Position3D start, Position3D end); + std::shared_ptr addLinksBetween(GlobalPosition3D start, GlobalPosition3D end); private: void tick(TickDuration elapsed) override; -- cgit v1.2.3