5
\$\begingroup\$

I'm setting up a tile system where every tile has x and y coordinates. Right now I assume that the top left corner of the tile is positioned on it's coordinate on the screen, x = tileX * tileWidth and y = tileY x tileWidth.

However, it seems strange that the tile with coordinate (0, 0) is completely drawn in the 'positive' side of the coordinate system as opposed to in the center of the origin.

Is it common practice to assume that a coordinate lays in the center of a tile or at the top left corner of a tile? So basically x = tileX x tileWidth or x = tileX x tilewidth - ( tileWidth / 2 )?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

I would argue that it's common to work from the top left of the tile (assuming your coordinate system works with 0,0 at the top left).

This works well for several reasons:

  • typical rendering routines take a position and dimensions as arguments. If you used the centre point, you'd have to subtract half the width and height to calculate the position each time.
  • typical collision detection routines also take a position and dimensions as arguments, and the same principle above applies.
  • converting from pixel coordinates to tile coordinates can be done with integer division (ie. rounding down). Divide the pixel position by the tile size to get the tile index in a zero-based array. If you use the centre point you have to add in an offset to compensate.
  • You need never work with negative numbers. The extreme corner of your map is typically 0,0 whether you count in pixels or in tiles. But if your tiles had their origin at the middle then you'd have to accommodate at least half a tile's worth of negative numbers for the 0,0 tile.
  • The top left pixel of a graphic is unambiguous and invariant over images of different sizes. However the mid point may be less simple - if a tile has an even width, there is no middle row of pixels, and rounding or truncating this calculation could cause off-by-one errors.

Of course, all these issues are counterbalanced by some problems caused by not using the centre-point, such as counting a character as being on tile 0 when only 1 pixel is on that tile. But you can easily have a GetCenter() member for each tile or object which you can use for such logic calculations, and leave all the rendering to work via the top left corner.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, you covered exactly the aspects I was wondering about :) \$\endgroup\$
    – soimon
    Dec 13, 2012 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .