From ca1d515282c295192b0b419e481ab4f8a0b3a45d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 27 Feb 2021 20:25:43 +0000 Subject: Implement joining two existing links/nodes This will likely make stupid connections in some circumstances, but they will be mechanically sound --- game/network/rail.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'game/network') diff --git a/game/network/rail.cpp b/game/network/rail.cpp index c1a72fb..55d57e4 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -52,7 +52,7 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) std::swap(start, end); } // Find start link/end - opposite entry dir to existing link; so pi +... - float dir = pi + [this](const auto & n) { + const auto findDir = [this](const auto & n) { for (const auto & l : links.objects) { for (const auto & e : l->ends) { if (e.first == n) { @@ -61,9 +61,28 @@ RailLinks::addLinksBetween(glm::vec3 start, glm::vec3 end) } } throw std::runtime_error("Node exists but couldn't find it"); - }(*node1ins.first); + }; + float dir = pi + findDir(*node1ins.first); const glm::vec2 flatStart {start.x, start.z}, flatEnd {end.x, end.z}; - // if (node2ins.second) { // Unimplemented second arc/stright required + if (!node2ins.second) { + float dir2 = pi + findDir(*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 = start + glm::vec3 {std::sin(dir + half_pi), 0, std::cos(dir + half_pi)} * radius; + const auto c2 = end + glm::vec3 {std::sin(dir2 + half_pi), 0, std::cos(dir2 + half_pi)} * radius; + const auto mid = (c1 + c2) / 2.F; + addLink(start, mid, !c1); + return addLink(end, mid, !c2); + } + else { + const auto radius {radii.second}; + const auto c1 = start + glm::vec3 {std::sin(dir - half_pi), 0, std::cos(dir - half_pi)} * radius; + const auto c2 = end + glm::vec3 {std::sin(dir2 - half_pi), 0, std::cos(dir2 - half_pi)} * radius; + const auto mid = (c1 + c2) / 2.F; + addLink(mid, start, !(c1)); + return addLink(mid, end, !c2); + } + } const auto diff {end - start}; const auto vy {vector_yaw(diff)}; const auto n2ed {(vy * 2) - dir - pi}; -- cgit v1.2.3