From 838e5f77478e5648769439e191e3ff0a8e6405ab Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 22 Feb 2021 20:12:59 +0000 Subject: Add meandering support Add rail links between existing nodes and arbitrary points --- game/network/rail.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'game/network/rail.cpp') diff --git a/game/network/rail.cpp b/game/network/rail.cpp index bf1d71d..c1a72fb 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,44 @@ RailLinks::joinLinks(const LinkPtr & l) const } } +std::shared_ptr +RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) +{ + auto node1ins = nodes.insert(std::make_shared(start)); + auto node2ins = nodes.insert(std::make_shared(end)); + if (node1ins.second && node2ins.second) { + // Both nodes are new, direct link, easy + return addLink(start, end); + } + if (node1ins.second && !node2ins.second) { + // node1 is new, node2 exists, but we build from existing outwards + std::swap(node1ins, node2ins); + std::swap(start, end); + } + // Find start link/end - opposite entry dir to existing link; so pi +... + float dir = pi + [this](const auto & n) { + for (const auto & l : links.objects) { + for (const auto & e : l->ends) { + if (e.first == n) { + return e.second; + } + } + } + throw std::runtime_error("Node exists but couldn't find it"); + }(*node1ins.first); + const glm::vec2 flatStart {start.x, start.z}, flatEnd {end.x, end.z}; + // if (node2ins.second) { // Unimplemented second arc/stright required + const auto diff {end - start}; + const auto vy {vector_yaw(diff)}; + const auto n2ed {(vy * 2) - dir - pi}; + const auto centre {find_arc_centre(flatStart, dir, flatEnd, n2ed)}; + + if (centre.second) { // right hand arc + std::swap(start, end); + } + return addLink(start, end, centre.first); +} + void RailLinks::render(const Shader & shader) const { -- cgit v1.2.3