-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
29 lines (26 loc) · 1.03 KB
/
point.cpp
File metadata and controls
29 lines (26 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "point.hpp"
// Converts a tileX, tileY to an X, Y relative to the texture's top left, where realX and realY is the center of tile (tileX, tileY)
point TileXYToTexXY(int tileX, int tileY) {
point retVal;
retVal.realX = (tileX*2 + tileY%2 + 1) * HALF_TILE_WIDTH;
retVal.realY = (tileY + 1) * HALF_TILE_HEIGHT;
retVal.tileX = tileX;
retVal.tileY = tileY;
return retVal;
}
point TexXYToTileXY(float texX, float texY) {
point retVal;
float realX = texX / TILE_WIDTH;
float realY = texY / TILE_HEIGHT;
bool oddY = (int(floor(realY + realX - 0.5) + floor(realY - realX - 0.5))) % 2 == 0;
retVal.realX = texX;
retVal.realY = texY;
retVal.tileX = (int) floor(realX - 0.5*oddY);
retVal.tileY = (int) (floor(realY - 0.5*oddY)*2 + oddY); // There is likely a more elegant way of stating this, but I just spent 5 hours getting the math right
return retVal;
}
float tileDistSq(point a, point b) {
float dx = (a.tileX + (a.tileY%2)/2) - (b.tileX + (b.tileY%2)/2);
float dy = a.tileY - b.tileY;
return dx*dx+dy*dy/TILE_DIST_DY_CONV_FACTOR;
}