Skip to content

Commit

Permalink
Fixed the hex math (Revolutionary-Games#734)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
crodnu authored and hhyyrylainen committed Feb 15, 2019
1 parent 5feccfd commit d07e3ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
30 changes: 3 additions & 27 deletions scripts/microbe_stage/microbe_editor/microbe_editor.as
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
30 changes: 25 additions & 5 deletions src/general/hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d07e3ba

Please sign in to comment.