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)

  • \$\begingroup\$ 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. \$\endgroup\$
    – Kikaimaru
    Sep 20, 2013 at 11:13

2 Answers 2


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.


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.

  • \$\begingroup\$ Can you give some pointers to the other answers? \$\endgroup\$
    – Jakub M.
    Sep 20, 2013 at 11:29
  • \$\begingroup\$ i'm looking for them, and of course when your trying to find them you dont ... i'll keep on checking \$\endgroup\$ Sep 20, 2013 at 12:57
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – House
    Sep 20, 2013 at 13:16
  • \$\begingroup\$ 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. \$\endgroup\$ Sep 20, 2013 at 15:28

You must log in to answer this question.

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