From 033797264ff58a1994765d7b99cf07be657a226e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 6 Feb 2021 16:54:54 +0000 Subject: Fix up the maths in creating curved rails --- application/main.cpp | 23 ++++++++++++++++------- game/network/rail.cpp | 22 ++++++++++++++++------ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/application/main.cpp b/application/main.cpp index a723b20..73d4440 100644 --- a/application/main.cpp +++ b/application/main.cpp @@ -1,8 +1,6 @@ #include -#include #include #include -#include #include #include #include @@ -14,9 +12,9 @@ #include #include #include +#include #include #include -#include #include #include @@ -66,11 +64,22 @@ public: Worker w; world.create(); + auto rl = world.create(); - auto a = rl->addLink({-1190, 5, -1190}, {-1190, 6, -1180}); - auto b = rl->addLink(a->ends.back().first->pos, {-1180, 5, -1180}, {-1185, -1180}); - auto c = rl->addLink(b->ends.back().first->pos, {-1180, 4, -1190}); - rl->addLink(c->ends.back().first->pos, a->ends.front().first->pos, {-1185, -1190}); + { + const glm::vec3 j {-1100, 5, -1100}, k {-1100, 5, -1000}, l {-1150, 10, -1050}, m {-1050, 10, -1050}; + rl->addLink(j, k); + rl->addLink(k, l, {l.x, k.z}); + rl->addLink(l, m); + rl->addLink(j, m, {m.x, j.z}); + } + { + const glm::vec3 j {-1190, 5, -1190}, k {-1190, 6, -1180}, l {-1180, 4, -1180}, m {-1180, 4, -1190}; + rl->addLink(j, k); + rl->addLink(l, k, {-1185, k.z}); + rl->addLink(l, m); + rl->addLink(j, m, {-1185, m.z}); + } Shader shader; Camera camera({-1250.0F, 35.0F, -1250.0F}, 70.0F, (float)DISPLAY_WIDTH / (float)DISPLAY_HEIGHT, 0.1F, 10000.0F); diff --git a/game/network/rail.cpp b/game/network/rail.cpp index 2ae65f7..93fc9f6 100644 --- a/game/network/rail.cpp +++ b/game/network/rail.cpp @@ -81,6 +81,14 @@ flat_orientation(const V & diff) return (std::isnan(e[0][0])) ? oneeighty : e; } +template +auto +flat_angle(const V & diff) +{ + const auto flatdiff {glm::normalize(glm::vec3 {diff.x, 0, diff.z})}; + return glm::orientedAngle(flatdiff, north, up); +} + template constexpr auto round_frac(const T & v, const T & frac) @@ -123,18 +131,20 @@ RailLinkCurve::RailLinkCurve(End a, End b, glm::vec2 c) : RailLink(std::move(a), const glm::vec3 centre3 {centre.x, e0p.y, centre.y}; const auto diffa = centre3 - e0p; const auto diffb = centre3 - e1p; - const auto anga = glm::orientedAngle(glm::normalize(diffa), north, up) - half_pi; + const auto anga = flat_angle(diffa); const auto angb = [&diffb, &anga]() { - const auto angb = glm::orientedAngle(glm::normalize(diffb), north, up) - half_pi; - return (angb < anga) ? angb + glm::radians(360.f) : angb; + const auto angb = flat_angle(diffb); + return (angb < anga) ? angb + glm::two_pi() : angb; }(); const auto radius = glm::length(e0p - centre3); const auto length = round_sleepers(radius * (angb - anga) / 2.F); - const auto step {glm::vec3 {std::abs(angb - anga), e1p.y - e0p.y, length} / std::round(angb - anga) / 5.F}; + const auto segs = std::round(5.F * length / std::pow(radius, 0.7F)); + const auto step {glm::vec3 {angb - anga, e1p.y - e0p.y, length} / segs}; const auto trans {glm::translate(centre3)}; auto addRcs = [this, trans, radius](auto arc) { - const auto t {trans * glm::rotate(arc.x, up) * glm::translate(glm::vec3 {radius, arc.y, 0.F})}; + const auto t {trans * glm::rotate(glm::half_pi() - arc.x, up) + * glm::translate(glm::vec3 {radius, arc.y, 0.F})}; for (const auto & rcs : railCrossSection) { const glm::vec3 m {(t * glm::vec4 {rcs.first, 1})}; vertices.emplace_back(m, glm::vec2 {rcs.second, arc.z}, up); @@ -146,8 +156,8 @@ RailLinkCurve::RailLinkCurve(End a, End b, glm::vec2 c) : RailLink(std::move(a), addRcs(glm::vec3 {angb, e1p.y - e0p.y, length}); for (auto n = 4U; n < vertices.size(); n += 1) { - indices.push_back(n); indices.push_back(n - 4); + indices.push_back(n); } meshes.create(vertices, indices, GL_TRIANGLE_STRIP); } -- cgit v1.2.3