Storing a 2d topdown map as integer or float matrix

Hi.

Im implementing a 2d topdown strategy game and consider storing the map as either a integer or floating matrix.
Storing it as an integer matrix would be easier for pathfinding and collision detection:

example (1 beeing walls and 0 being walkable path of tiles 32x32 pixels):


1,1,1,1,
1,0,0,1,
0,1,0,0

The drawback with integer is that the movement of the characters won’t be that smooth as with floating point. With floating point I could move the creatures 1-2 pixels, but using int, it would move 1 tile at a time which is 32 pixels)
Preferably I want to use float for the movement of the creatures but I think it will be more complex with both pathfinding and collision detection using float for the coordinates.

What do you suggest. Is it possible to mix the integer coordinate system for pathfinding but still use the float for smooth movement and positioning?

What you use to store the tile data has no bearing on how you represent screen and world coordinates. You can work in three coordinate systems with your tile map. Assuming as an example a 10x10 map of 32x32 tiles:

  • world coordinates extend from (0,0) to (319, 319)
  • tile coordiantes (inside an individual tile) extend from (0,0) to (31,31)
  • array coordinates, representing the index into the tile array. Assuming a one dimensional array, these extend from [0 … 99].

Given world coordinates, you can work out which index to look for in the array like so:

int x = worldX / tileWidth;
int y = worldY / tileHeight;

Then you can pick your tile in a one dimensional array like:


tiles[x + (y * mapWidth)];

Or in a 2D array with:

tiles[x][y]

To get from world coordinates to tile coordinates:

auto tileX = worldX % tileWidth;
auto tileY = worldY % tileHeight;

Store your entity coordinates as world coordinates, not array coordinates. Then you can move as many pixels as you need.

Thank you for the tips.

But should the pathfinding (a* algorithm) be implemented based on the world coordinates or array coordinates? If the creature is rendered at world coordinates that could be between two tiles in the array?

An entity’s position is never between two tiles. It is either in a tile, or it isn’t. Graphically it can be, of course. You have to decide what a position represents for your entity sprites. Is it the top left corner or bottom row? The center pixel? Some arbitrary point that is set in a tool and loaded from a file? Working with a corner or a center pixel is probably easiest.

It doesn’t make much sense to use world coordinates for A*. The map data is an array, you need to deal with elements of that array for your algorithm, so you use array indices. Convert the world coordinates of the entity to an array index to get the starting tile, convert the world coordinates of the destination to an array index to get the target tile, and run your A*.

Ok I understand, but what happens if the entity sprite got position 0,0 in the array but is “walking” to the next tile 1,0 pixel by pixel and also another entity sprite is walking from the other side 2,0 to the same tile 1,0, so a collision will be detected.

Then none of the entity sprites got the new position 1,0 in the array?

Collision detection should happen in world coordinates. It’s up to you to decide if you want to allow multiple entities per tile or not.

Look, you aren’t going to learn every detail you need about implementing tile maps from people answering your questions in forum posts. I suggest you hit google up for some tile map tutorials. There are a great many of them out there. Read as many as you need to until you understand the principles behind them (whether they use Java or C or something else) and can adapt them to your own project. Then, when you actually encounter specific problems during the implementation, come here and ask for help.

Basically, how it goes, is you have 9 patches or two-4 patches of land rendered at once. You will need to store information in a way where you can load these patches/chunks and render them. The tiles should be a 2d byte array. I assure you, you are not going to reach 256 textures to need a short array.

Same goes for collision, but i’d use a boolean for easier checks and small data size (obviously not for loading) :point:

BUT an alternative would be to dynamically disallow tiles, not statically disallow them. That means, when the scene loads, you iterate through everything and say whether they can go somewhere or not.

I’m assuming this is a pokemon-like game.

So you would tween to different tiles in the game, but yes it would be one tile at a time.

If you are going to want free-roam, you can’t use this method.

I have googled and read some tutorials, but I haven’t found one that describes both how to handle world coordinates for smooth pixel movement and combining it with A* pathfinding based on 2d array. Thats why I needed some starting help. Anyway thank you for the tips. Probably I will combine both of them.

Yes my game will be like pokemon-game, it will only allow one sprite entity to be placed at a tile. Thank you for your tips, I think about dynamically disallow tiles when my level loads.