From d07e3baed1e7a34db7d61cd80c2cf911b3c6d86b Mon Sep 17 00:00:00 2001 From: crodnu Date: Fri, 15 Feb 2019 13:38:11 -0300 Subject: [PATCH] Fixed the hex math (#734) * Fixed the hex math The issue was the hex code returning an Int2, which automatically rounded the hexes in some way before proper rounding could be done. Also the cube coordinates were wrong, making me wonder how stuff worked in the first place. * formatting stuff --- .../microbe_editor/microbe_editor.as | 30 ++----------------- src/general/hex.cpp | 30 +++++++++++++++---- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/scripts/microbe_stage/microbe_editor/microbe_editor.as b/scripts/microbe_stage/microbe_editor/microbe_editor.as index 79d4c320f65..a60c6007f10 100644 --- a/scripts/microbe_stage/microbe_editor/microbe_editor.as +++ b/scripts/microbe_stage/microbe_editor/microbe_editor.as @@ -544,34 +544,10 @@ class MicrobeEditor{ // Convert to the hex the cursor is currently located over. - //Negating X to compensate for the fact that we are looking at - //the opposite side of the normal coordinate system + const auto tmp1 = Hex::cartesianToAxial(rayPoint.x, rayPoint.z); - float hexOffsetX; - float hexOffsetY; - - if (rayPoint.z <0){ - hexOffsetY = -(HEX_SIZE/2); - } - else { - hexOffsetY = (HEX_SIZE/2); - } - if (rayPoint.x <0){ - hexOffsetX = -(HEX_SIZE/2); - } - else { - hexOffsetX = (HEX_SIZE/2); - } - - const auto tmp1 = Hex::cartesianToAxial(rayPoint.x+hexOffsetX, -1*(rayPoint.z+hexOffsetY)); - - // This requires a conversion to hex cube coordinates and back - // for proper rounding. - const auto qrrr = Hex::cubeToAxial(Hex::cubeHexRound( - Float3(Hex::axialToCube(tmp1.X, tmp1.Y)))); - - qr = qrrr.X; - rr = qrrr.Y; + qr = tmp1.X; + rr = tmp1.Y; // LOG_WRITE("Mouse hex: " + qr + ", " + rr); } diff --git a/src/general/hex.cpp b/src/general/hex.cpp index b72031979ac..9f49a265582 100644 --- a/src/general/hex.cpp +++ b/src/general/hex.cpp @@ -25,11 +25,30 @@ Float3 } Int2 - Hex::cartesianToAxial(double x, double z) + Hex::cartesianToAxial(double x, double y) { - double q = x * (2.0 / 3.0) / Hex::hexSize; - double r = z / (Hex::hexSize * std::sqrt(3)) - q / 2.0; - return Int2(q, r); + // Getting the cube coordinates. + double cx = x * (2.0 / 3.0) / Hex::hexSize; + double cy = y / (Hex::hexSize * std::sqrt(3)) - cx / 2.0; + double cz = -(cx + cy); + + // Rounding the result. + double rx = round(cx); + double ry = round(cy); + double rz = round(cz); + + double xDiff = std::abs(rx - cx); + double yDiff = std::abs(ry - cy); + double zDiff = std::abs(rz - cz); + + if(xDiff > yDiff && xDiff > zDiff) + rx = -(ry + rz); + + else if(yDiff > zDiff) + ry = -(rx + rz); + + // Returning the axial coordinates. + return cubeToAxial(rx, ry, rz); } Int3 @@ -41,7 +60,8 @@ Int3 Int2 Hex::cubeToAxial(double x, double y, double z) { - return Int2(x, z); + (void)z; + return Int2(x, y); } Int3