1
\$\begingroup\$

I understand how to move a square around a tile map: you have a coordinate of the square (say the center), and if they player press the "up" arrow, just increment the y-coordinate by 1.

Question: How should I think about tile based movement when the player is the size of 4 squares, and still moves in 1 square increment? E.g. if the player get into a tank, and the tank is now a 2 by 2 block. Would it be significantly different?

I suppose I would have to keep track of the 4 tiles my player's tank is in. And when he presses "up", I would have to check if the forward 2 tiles are empty, then increment all 4 tiles' y-coords by 1.

\$\endgroup\$
5
\$\begingroup\$

Yes, you'll want to define an "origin" tile for the player (for example, the top-left of his 2x2 set of tiles) and interpret the player's position as being the position of that origin tile.

You will then want to take the tile size of the player's avatar into account when determining if it can be moved to a partition position. For example, if you have a board like looks like

....#
.X..#
....#
#####

and the X represents the position of the player's origin in the world (which is the upper-left of his 2x2 sprite), then the player can't move down even though there is technically a one tile gap between the origin and the wall (represented by the # characters). On screen, the player might actually look like

....#
.XX.#
.XX.#
#####

when rendered.

The good news is that this is easily generalizable so a 1x1-sized entity can use the same code path as a NxM-sized entity: you only need to track the entity's position in the world and the size, in tiles, of the sprite.

If your system provides you the ability to specify entity positions in non-integer / non-tile values, you can simply use the exact center of the sprite as the origin, as it can simplify things just a bit.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.