From 3bb238d41ca4f3e4a40f4ee3700b27ca7bf03b56 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 28 Oct 2022 01:08:28 +0100 Subject: Implement extending a network to join link ends --- game/network/network.cpp | 28 ++++++++++++++++++++++++++++ game/network/network.h | 2 ++ game/network/network.impl.h | 21 +++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) (limited to 'game') diff --git a/game/network/network.cpp b/game/network/network.cpp index 7724c20..dd61f9b 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -109,3 +109,31 @@ Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float start } return {start, end, centre.first}; } + +std::pair +Network::genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir, float endDir) +{ + startDir += pi; + endDir += pi; + const glm::vec2 flatStart {!start}, flatEnd {!end}; + 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))); + }; + if (const auto radii = find_arcs_radius(flatStart, startDir, flatEnd, endDir); radii.first < radii.second) { + const auto radius {radii.first}; + const auto c1 = flatStart + sincosf(startDir + half_pi) * radius; + const auto c2 = flatEnd + sincosf(endDir + half_pi) * radius; + const auto mid = (c1 + c2) / 2.F; + const auto midh = mid ^ midheight(mid); + return {{start, midh, c1}, {end, midh, c2}}; + } + else { + const auto radius {radii.second}; + const auto c1 = flatStart + sincosf(startDir - half_pi) * radius; + const auto c2 = flatEnd + sincosf(endDir - half_pi) * radius; + const auto mid = (c1 + c2) / 2.F; + const auto midh = mid ^ midheight(mid); + return {{midh, start, c1}, {midh, end, c2}}; + } +} diff --git a/game/network/network.h b/game/network/network.h index f37f335..4a2f56a 100644 --- a/game/network/network.h +++ b/game/network/network.h @@ -48,6 +48,8 @@ public: protected: static void joinLinks(const Link::Ptr & l, const Link::Ptr & ol); static GenCurveDef genCurveDef(const glm::vec3 & start, const glm::vec3 & end, float startDir); + static std::pair genCurveDef( + const glm::vec3 & start, const glm::vec3 & end, float startDir, float endDir); using Nodes = std::set>; Nodes nodes; diff --git a/game/network/network.impl.h b/game/network/network.impl.h index 859d1ac..5b1649d 100644 --- a/game/network/network.impl.h +++ b/game/network/network.impl.h @@ -61,9 +61,16 @@ NetworkOf::candidateStraight(glm::vec3 n1, glm::vec3 n2) template Link::CCollection -NetworkOf::candidateJoins(glm::vec3 n1, glm::vec3 n2) +NetworkOf::candidateJoins(glm::vec3 start, glm::vec3 end) { - return {candidateLink(n1, n2)}; + if (glm::distance(start, end) < 2.F) { + return {}; + } + const auto defs = genCurveDef( + start, end, findNodeDirection(candidateNodeAt(start).first), findNodeDirection(candidateNodeAt(end).first)); + const auto & [c1s, c1e, c1c] = defs.first; + const auto & [c2s, c2e, c2c] = defs.second; + return {candidateLink(c1s, c1e, c1c), candidateLink(c2s, c2e, c2c)}; } template @@ -83,9 +90,15 @@ NetworkOf::addStraight(glm::vec3 n1, glm::vec3 n2) template Link::CCollection -NetworkOf::addJoins(glm::vec3 n1, glm::vec3 n2) +NetworkOf::addJoins(glm::vec3 start, glm::vec3 end) { - return {addLink(n1, n2)}; + if (glm::distance(start, end) < 2.F) { + return {}; + } + const auto defs = genCurveDef(start, end, findNodeDirection(nodeAt(start)), findNodeDirection(nodeAt(end))); + const auto & [c1s, c1e, c1c] = defs.first; + const auto & [c2s, c2e, c2c] = defs.second; + return {addLink(c1s, c1e, c1c), addLink(c2s, c2e, c2c)}; } template -- cgit v1.2.3