Getting Adjacent Tiles

Hi there,

I’m having trouble at the moment detecting the tiles around a specific tile. I’m doing this so that I can auto texture the tiles with their correct sides. Anyways, I have a get method in my level class that does the following:

	public Tile get(int x, int y){
		return tiles[x][y]; //Tiles is a Tile array.
	}

When printing out tile.getPosition().x, it returns a float value, which is correct due to it being a Vector. So, 1.0 for example. I need to check the tile beside it, so I’m assuming I would do the following:

int x = (int) tile.getPosition().x + 1;

Unfortunately, this equals = 1.01. Why is this the case? How can I achieve 1.0 + 1 = 2.0? If I do 1.0 + 1.0, it will equal 1.01.0 oddly enough…

Thanks!

  • A

I am a total idiot.
I was printing, so of course the + would print out as 1.01.0.

Anyways, I understand the issue now. Essentially it is looping through every tile and eventually checking for a tile that doesn’t exist (or, its hitting the edge). I need to make a better level generator.

This is part of my bitwise autotiler. I havent used bitwise or counted in bits before so it’s not perfect.

This is inside of a loop that checks every tile.


Tile temp;

				if (cleanMap[w - 1][h - 1] == 1)		temp.corner += 8;
				if (cleanMap[w - 1][h] == 1)			temp.side += 128;
				if (cleanMap[w - 1][h + 1] == 1)		temp.corner += 4;

				if (cleanMap[w][h - 1] == 1)			temp.side += 16;
				if (cleanMap[w][h + 1] == 1)			temp.side += 64;

				if (cleanMap[w + 1][h - 1] == 1)		temp.corner += 1;
				if (cleanMap[w + 1][h] == 1)			temp.side += 32;
				if (cleanMap[w + 1][h + 1] == 1)		temp.corner += 2;

				tilemap[w][h] = temp;


Corner and Side är chars that holds the values for the sides and corners. It checks all 8 directions and adds them. But you can use one char for this, as i said, i’m a noob and havent refactored this yet.

Then i loop though all of the tiles again when i draw and decide which sprite to draw (C++)


if (tilemap[w][h].side == 32 && (tilemap[w][h].corner < 4)) draw->drawSprite(map, w*tileSize, h*tileSize, 5, 2);

This checks if it has a blocked/water tile to the right and a blocked in upper and bottom right corners.

edit

I followed this guide:
http://www.angryfishstudios.com/2011/04/adventures-in-bitmasking/

It is far from the best method though. I’ll be changing to this method in the future:
http://www.gamedev.net/page/resources/_/technical/game-programming/tilemap-based-game-techniques-handling-terrai-r934

Using transparent tiles instead of tiles that contains everything.

You could extend the Tile class data so it maintains adjacent tile references. It drastically increases the size of the data but if you never plan to use huge maps it might be worth it.

Unlikely to be relevant I know - I only mention it because that’s what I do in my tile engine and it works. Effectively maps are held as graph data structures - tile faces, edges and vertexes. It was a solution to the problem of maps which aren’t made up of regular-shaped tiles, so a coordinate-based system of tile addresses was no good.

richierich , I was able to get it work by doing just as it sounds, check tileX and tileY surroundings for their types. The error I was having was because it was checking the edge tiles, which didn’t have adjacent tiles… because they were the edge, so it was returning null.

Now I’m trying to find an appropriate culling method. Mine isn’t working for some reason.

Anyways, thanks for the suggestion. :slight_smile:

My games all give each cell on the board an array of links to adjacent cells; and all routine navigation from cell to cell uses it instead of the coordinate system. This has lots of advantages. It’s efficient to use. All the standard overall geometries are obviously handled, but so are oddly shaped boards with cells missing, boards closed into a torus, boards with arbitrary networks of cells and so on.