summaryrefslogtreecommitdiff
path: root/game/network/rail.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-02-14 13:19:37 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-02-14 13:19:37 +0000
commit27ba0701dcb7ce441cb91a10a570e4f4b05db5c4 (patch)
tree604684712e372783974eff77b5497a5c0a28ecfe /game/network/rail.cpp
parentKeep radius and arc as part of rail curve (diff)
downloadilt-27ba0701dcb7ce441cb91a10a570e4f4b05db5c4.tar.bz2
ilt-27ba0701dcb7ce441cb91a10a570e4f4b05db5c4.tar.xz
ilt-27ba0701dcb7ce441cb91a10a570e4f4b05db5c4.zip
Support getting the position at a point along a link
Diffstat (limited to 'game/network/rail.cpp')
-rw-r--r--game/network/rail.cpp26
1 files changed, 26 insertions, 0 deletions
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}};
+}