From 66b2e1c903bee5ca731fe82121c7582c90ec416e Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 6 Jan 2022 20:31:14 +0000 Subject: GeoData::quad works in world coordinates --- game/geoData.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'game/geoData.cpp') diff --git a/game/geoData.cpp b/game/geoData.cpp index c4a75f9..c3fb5dc 100644 --- a/game/geoData.cpp +++ b/game/geoData.cpp @@ -69,25 +69,27 @@ GeoData::loadFromImages(const std::filesystem::path & fileName, float scale_) } GeoData::Quad -GeoData::quad(glm::vec2 coord) const +GeoData::quad(glm::vec2 wcoord) const { - const std::array gridCorner { - {{std::floor(coord.x), std::floor(coord.y)}, {std::floor(coord.x), std::ceil(coord.y)}, - {std::ceil(coord.x), std::floor(coord.y)}, {std::ceil(coord.x), std::ceil(coord.y)}}}; - - return transform_array(gridCorner, [this](const auto c) { - return c || nodes[at(c)].height; - }); + constexpr static const std::array corners {{{0, 0}, {0, 1}, {1, 0}, {1, 1}}}; + return transform_array(transform_array(corners, + [coord = (wcoord / scale)](const auto c) { + return glm::vec2 {std::floor(coord.x), std::floor(coord.y)} + c; + }), + [this](const auto c) { + return (c * scale) || nodes[at(c)].height; + }); } glm::vec3 GeoData::positionAt(const glm::vec2 wcoord) const { - const auto coord {wcoord / scale}; - const auto point {quad(coord)}; - const glm::vec2 frac = coord - !point.front(); - const auto heightFloor = point[0].z + ((point[2].z - point[0].z) * frac.x), - heightCeil = point[1].z + ((point[3].z - point[1].z) * frac.x), + const auto point {quad(wcoord)}; + const glm::vec2 frac = (wcoord - !point.front()) / scale; + auto edge = [&point, &frac](auto offset) { + return point[offset].z + ((point[offset + 2].z - point[offset].z) * frac.x); + }; + const auto heightFloor = edge(0U), heightCeil = edge(1U), heightMid = heightFloor + ((heightCeil - heightFloor) * frac.y); return wcoord || heightMid; -- cgit v1.2.3