Explanation of Coordinate Systems
Understanding this, one must see there are three different coordinate systems at work here.Hex Space
First comes the hex space. This is the game world's coordinate system. It is the one which all the game mechanics rely upon. It looks like :Hex Space __ 5 __/D \__ 4 __/ \__/ \__ 3 "Y" coord __/ \__/ \__/ \__ 2 __/A \__/ \__/ \__/ \__ 1 __/ \__/ \__/E \__/B \__/ \__ 0 / \__/G \__/ \__/ \__/F \__/C \ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ 5 \__/ \__/ \__/ \__/ 4 \__/ \__/ \__/ 3 \__/ \__/ 2 "X" coord \__/ 1 0 Such that A =(2,5); B =(4,2); C =(5,0); D =(5,5); E =(3,3); F =(4,1); G =(1,4)
Array Space
Next is the array space. This is a translation of the hex space into an orthogonal space. I've made it the same as the original hex space I was using. It begins with (0,0) in the top left and extends positively to the right and down. It looks like :Array Space __ __ __ __ /M \__/O \__/ \__/ \__ \__/N \__/ \__/ \__/ \ / \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ \ /P \__/R \__/ \__/ \__/ \__/Q \__/ \__/ \__/ \ / \__/ \__/ \__/ \__/ \__/ \__/ \__/ \__/ Such that M =(0,0); N =(1,0); O =(2,0); P =(0,3); Q =(1,3); R =(2,3)The reason for this space is that computer screens are square, not diamond shaped. The hex space as described above would be displayed such that a large percentage of the screen would be unused, or a large percentage of world would be unseen.
Screen Space
Finally is the screen space. This represents the pixels on the screen. This could also be a viewport space if I need to implement scrolling, but I currently want the entire map to appear on screen at once. This starts at (0,0) for the pixel in the top left and proceeds positively to the right and down.Converting Between Coordinate Systems
Between Array and Hex Coordinates
Between Screen and Array Coords
Calculating the distance between two hexes
Finally for the thing that I did the switch for. This hex coordinate system allows a quick (compared to walking the whole map) calculation of distance between two hexes. It also allows for some Line-of-Site calculations, but I don't currently need those. Anyways, the code :Oh well, that's it for now!