diff options
-rw-r--r-- | application/main.cpp | 23 | ||||
-rw-r--r-- | 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 <SDL2/SDL.h> -#include <array> #include <chrono> #include <collection.hpp> -#include <game/network/link.h> #include <game/network/rail.h> #include <game/terrain.h> #include <game/world.h> @@ -14,9 +12,9 @@ #include <gfx/manualCameraController.h> #include <gfx/renderable.h> #include <gfx/window.h> +#include <glm/glm.hpp> #include <memory> #include <special_members.hpp> -#include <utility> #include <vector> #include <worker.h> @@ -66,11 +64,22 @@ public: Worker w; world.create<Terrain>(); + auto rl = world.create<RailLinks>(); - auto a = rl->addLink<RailLinkStraight>({-1190, 5, -1190}, {-1190, 6, -1180}); - auto b = rl->addLink<RailLinkCurve>(a->ends.back().first->pos, {-1180, 5, -1180}, {-1185, -1180}); - auto c = rl->addLink<RailLinkStraight>(b->ends.back().first->pos, {-1180, 4, -1190}); - rl->addLink<RailLinkCurve>(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<RailLinkStraight>(j, k); + rl->addLink<RailLinkCurve>(k, l, {l.x, k.z}); + rl->addLink<RailLinkStraight>(l, m); + rl->addLink<RailLinkCurve>(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<RailLinkStraight>(j, k); + rl->addLink<RailLinkCurve>(l, k, {-1185, k.z}); + rl->addLink<RailLinkStraight>(l, m); + rl->addLink<RailLinkCurve>(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<typename V> +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<typename T> 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<float>() : 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<float>() - 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<Mesh>(vertices, indices, GL_TRIANGLE_STRIP); } |