From 27ba0701dcb7ce441cb91a10a570e4f4b05db5c4 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Sun, 14 Feb 2021 13:19:37 +0000
Subject: Support getting the position at a point along a link

---
 game/network/link.h   |  3 +++
 game/network/rail.cpp | 26 ++++++++++++++++++++++++++
 game/network/rail.h   |  2 ++
 3 files changed, 31 insertions(+)

diff --git a/game/network/link.h b/game/network/link.h
index c9fbedb..3fabf05 100644
--- a/game/network/link.h
+++ b/game/network/link.h
@@ -2,6 +2,7 @@
 #define LINK_H
 
 #include <array>
+#include <gfx/gl/transform.h>
 #include <glm/glm.hpp>
 #include <memory>
 #include <special_members.hpp>
@@ -36,6 +37,8 @@ public:
 	NO_COPY(Link);
 	NO_MOVE(Link);
 
+	virtual Transform positionAt(float dist, unsigned char start) const = 0;
+
 	std::array<End, 2> ends;
 	float length;
 	using Next = std::pair<LinkPtr, unsigned char /*end*/>;
diff --git a/game/network/rail.cpp b/game/network/rail.cpp
index 4e09e29..a765a4f 100644
--- a/game/network/rail.cpp
+++ b/game/network/rail.cpp
@@ -8,6 +8,7 @@
 #include <gfx/models/texture.h>
 #include <gfx/models/vertex.hpp>
 #include <glm/gtx/transform.hpp>
+#include <glm/gtx/vector_angle.hpp>
 #include <maths.h>
 #include <type_traits>
 #include <utility>
@@ -95,6 +96,15 @@ RailLinkStraight::RailLinkStraight(NodePtr a, NodePtr b, const glm::vec3 & diff)
 	defaultMesh();
 }
 
+Transform
+RailLinkStraight::positionAt(float dist, unsigned char start) const
+{
+	const auto es {std::make_pair(ends[start].first, ends[1 - start].first)};
+	const auto diff {es.second->pos - es.first->pos};
+	const auto dir {glm::normalize(diff)};
+	return Transform {es.first->pos + dir * dist, {0, flat_angle(diff) /*, std::atan2(diff.x, -diff.z)*/, 0}};
+}
+
 RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec2 c) :
 	RailLinkCurve(a, b, {c.x, a->pos.y, c.y}, {!c, a->pos, b->pos})
 {
@@ -124,3 +134,19 @@ RailLinkCurve::RailLinkCurve(const NodePtr & a, const NodePtr & b, glm::vec3 c,
 	}
 	defaultMesh();
 }
+
+Transform
+RailLinkCurve::positionAt(float dist, unsigned char start) const
+{
+	static constexpr std::array<float, 2> dirOffset {half_pi, -half_pi};
+	const auto frac {dist / length};
+	const auto es {std::make_pair(ends[start].first, ends[1 - start].first)};
+	const auto as {std::make_pair(arc[start], arc[1 - start])};
+	const auto ang {as.first + ((as.second - as.first) * frac)};
+	const auto angArc {ang - half_pi};
+	const auto relPos {glm::vec3 {std::cos(angArc), 0, -std::sin(angArc)} * radius};
+	const auto relClimb {
+			glm::vec3 {0, -centreBase.y + es.first->pos.y + ((es.second->pos.y - es.first->pos.y) * frac), 0}};
+
+	return Transform {relPos + relClimb + centreBase, {0, normalize(ang + dirOffset[start]), 0}};
+}
diff --git a/game/network/rail.h b/game/network/rail.h
index f50ef3c..58c26c3 100644
--- a/game/network/rail.h
+++ b/game/network/rail.h
@@ -36,6 +36,7 @@ protected:
 class RailLinkStraight : public RailLink {
 public:
 	RailLinkStraight(const NodePtr &, const NodePtr &);
+	Transform positionAt(float dist, unsigned char start) const override;
 
 private:
 	RailLinkStraight(NodePtr, NodePtr, const glm::vec3 & diff);
@@ -44,6 +45,7 @@ private:
 class RailLinkCurve : public RailLink {
 public:
 	RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec2);
+	Transform positionAt(float dist, unsigned char start) const override;
 
 private:
 	RailLinkCurve(const NodePtr &, const NodePtr &, glm::vec3, const Arc);
-- 
cgit v1.2.3