Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a tile-based game (in this case MMO), should an actor (e.g., player) know its position, or should only the map know its position?

If an actor is placed on a grid map, it is placed on a specific position (x, y) and assigned to a cell in the map. Now, should I also store the actor's position inside the actor's data structure? It would allow to check the actor's position easily (just check it's data) but at the same time, I duplicate the same information in two places which is bad and could introduce a whole spectrum of bugs (e.g., if those two pieces of data are inconsistent)

share|improve this question
I dont think you should store actor in 2d array on map, you gain little performance and issues with deleting where he was, updating new tile. – Kikaimaru Sep 20 '13 at 11:13

In order to achieve better performance when testing for collisions, checking for surroundings and nearby agents, you have to keep track of an agent's location on the map (so you don't get to check each cell to see who's in it, you just pick an agent and check the other cells surrounding him up to a given range).

Unless you have a pre-processed list of agents to work with and their location, it will be handy to keep the location at agent-level too. You don't want to lessen the advantage you've got in the previous paragraph by going through the grid to see where your agent you want to check the surroundings for might be positioned.

You don't necessarily have to store raw positions in both places. The map could simply use pointers to agents, which store their own location. This way there's just one position, and the map references it.

class Cell
      private Agent[] agentsInside; // Keeps track of all the agents in this 
                                    // cell. If one moves away, it gets
                                    // deleted, and the new cell starts keeping
                                    // track of it.

Since you're on a 2D grid dealing with moving agents that can collide, you might be curious to also read more on the topic. With the fixed-size cell approach, Bin Partitioning could be a good place to start.

share|improve this answer

Yup, store in on both. There's a couple of answer about this already ...

Its really convenient to have an actor know its own location.

Its also convenient to have each tile knows which actor is on to affect specific status, traps, events, etc, instead of always going threw the npcs list. you wont have performance issue by storing 1 pointer in your class ...

its also easy to create a moveActor() method as the only way to handle movement, and have this method update both values.

share|improve this answer
Can you give some pointers to the other answers? – Jakub M. Sep 20 '13 at 11:29
i'm looking for them, and of course when your trying to find them you dont ... i'll keep on checking – Thierry Savard Sep 20 '13 at 12:57
I would not store it in both, do as Alex's answer suggests, store it in the actor, and have a pointer to the actor in the map data structure. – Byte56 Sep 20 '13 at 13:16
yea, that is what I meant by storing 1 pointer. I wouldve up his answer myself as it's more clear, but I cant log in from work. as for the other topic, I've check a bit and never found it, it was probably on another subject. – Thierry Savard Sep 20 '13 at 15:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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