Sunday, June 12, 2016

collision detection - Error in Finding Nearest Hexagonal Tile


Using Openfl for a game using hexagonal tiles


Working out the problem of walkability in a game, and I've run into an issue with my Hexagonal tiles. I think it is an odd rounding error of sorts. When heading down on the left side of tile, for a moment the nearest returns the coordinates of the tile on the right side. So the coordinates I get from the bellow nearest causes the "no walk" area for each unwalkable (water in this case) tile to be the area in red on this image.


Hex Map showing problem area



public function nearest(pos:Point):Point
{
var real_x:Float = pos.x + character.xOffset;
var real_y:Float = pos.y + character.yOffset;
var pos_y:Float = ((real_y + yOffset) / tileHeight)+1;
var pos_x:Float = ((real_x + xOffset - ( pos_y%2 * tileWidth/2) ) / tileWidth);
return new Point(Math.round(pos_x),Std.int(pos_y));
}

For reference, this is how my map is laid out, later the map data will be read in from a file and not hard coded into the application.



public function createMap()
{
map.push([0,0,0,0,0,0,0,3,0,0]);
map.push([ 0,0,0,0,0,0,3,0,0,0]);
map.push([0,0,0,0,0,0,3,0,0,0]);
map.push([ 0,0,0,0,0,3,0,0,0,0]);
map.push([0,0,0,0,0,3,0,0,0,0]);
map.push([ 0,0,0,0,3,0,1,1,0,0]);
map.push([0,0,0,0,3,0,1,2,1,0]);
map.push([ 0,0,0,3,0,0,1,1,0,0]);

map.push([0,0,0,3,0,0,0,0,0,0]);
map.push([ 0,0,3,0,0,0,0,0,0,0]);

walkMap.push([true,true,true,true,true,true,true,false,true,true]);
walkMap.push([true,true,true,true,true,true,false,true,true,true]);
walkMap.push([true,true,true,true,true,true,false,true,true,true]);
walkMap.push([true,true,true,true,true,false,true,true,true,true]);
walkMap.push([true,true,true,true,true,false,true,true,true,true]);
walkMap.push([true,true,true,true,false,true,true,true,true,true]);
walkMap.push([true,true,true,true,false,true,true,true,true,true]);

walkMap.push([true,true,true,false,true,true,true,true,true,true]);
walkMap.push([true,true,true,false,true,true,true,true,true,true]);
walkMap.push([true,true,false,true,true,true,true,true,true,true]);

tileData = [];
for (row in 0...map.length)
{
for (cell in 0...map[row].length)
{
tileData = tileData.concat(

[
(tileWidth * cell + ((row%2) * (tileWidth/2))) - xOffset,
(tileHeight * row) - yOffset,
map[row][cell]
]
);
}
}
}


What is causing the issue, where is my math off that I'm getting this odd effect?


Update:


The tile mapping from the above code should look like (realizing that I haven't taken the corners into account yet):


Should look like


But instead look like:


enter image description here


They are not diagonal at all (like DMGregory suggests in his answer), the the top of them where the corners live is shifted by 1/2 a tile width, and I don't see why.


I could force the the tops to the right half a tile after (and that might be what I end up doing), but the code above should work, and I don't see where it is off by half a tile.


The bit DMGregory is doing at the bottom of his answer is what I was starting to work on to try and solve the corner problem (except the corners are at the top, not the bottom), when I realized what the shifting bug that's really going on really is.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...