Coordinates in a tiled world

Hello,

I’ve tried out a couple of coordinate systems in a tiled world.
So far I’m not convinced that any of them is really what I’m looking for.

I’m working with a pacman clone (The level is tiled, but entities should be able to move freely).
The level is tiled from a 2d array. Each tile is 16x16.

My first attempt was just to use x:y coords for both objects and tiles.
(position and index in the tile-map). This resulted in pacman jumping from tile to tile
and not moving smoothly… clearly not what I was looking for.

My second attempt was to add delta coords for objects. This allowed me to
move the objects smoothly, but I feel using a delta is error prone. And should the major coord
be changed at the start/end/middle of a move?

Now I’m thinking about using different coordinates for objects and scenery.
And add a translation method. Does this sound like a good idea?

I would also like to have objects with larger than 1x1 tile sizes in the future.
Does any of the proposed coordinate systems limit this?

Thanks!

You can use 2 different coordinate systems. The first you have already thought of based on the tiles index in the 2d array. The other system would be based on the pixel location. Have the character store its exact pixel location.

When you need to know what tile something is on just divide pixel location by the tiles size in pixels, 16 in your case.

Typically for collisions I calculate the index location of the 4 corners of the object, but for movement I use pixel coordinates.

Ok,
Makes sense.

Should the position of the objects be based on their center or top-left corner?
Will either affect the ability to have objects spanning several tiles?

Thanks!

Base the position off the top left for a rectangular shaped object like your tiles. Use the center for circular shaped collision like on your creatures.

You can use java.awt.geom shapes to do a lot of the collision for you.

Well as long as you’re consistent using the center versus the top left corner (or any corner) makes no difference. So do whatever is easiest for you. Typically the only time you want the top left corner is for drawing images - otherwise the center is more convenient.

Although geom can indeed do collision algorithms, it is by no means efficient about it. If you’re interested in scaling your project to have a lot of collision going on or you’re only going to be doing square and circle collisions, I wouldn’t bother with geom.