1
\$\begingroup\$

I am making a tile based game, in which also the player movement is completely snapped to the grid. Imagine sokoban.

I basically have a 2d array for the layout of the level (walls / not walls), and the player can check is there's a wall where he is going.

My question is, when the player is trying to move into a wall, should I:

  1. Let it move into the wall, then later check if it's in a wall and if needed revert it's movement?
  2. Check whether it is going to move into a wall and prevent it beforehand?

I understand the answer may heavily depend on the structure of my code, but I'd still like to hear some opinions about how it was done in the past. What do you think? which of the two is better? (or is there a third option)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Both options sound viable — you can make either one work. Which one you prefer to work with in your code is really up to you. \$\endgroup\$
    – DMGregory
    Jan 14, 2020 at 22:07
  • \$\begingroup\$ I have used both approaches and you can make any of them work. General consensus is option 2, read the answer to learn why. I'm now using 3D physics and coordinate system with 2D graphics, but I'm firmly believe that tile based games from the age of 8/16 bits consoles used a 2D grid to keep track of tiles and moving characters positions, or at least if a tile is occupied by a character or not. Possibly, JRPGs from the age used a similar strategy. \$\endgroup\$ Jan 15, 2020 at 21:48

1 Answer 1

1
\$\begingroup\$

It doesn't matter that much for collisions with static objects. It gets more relevant when you have collisions between moving entities.

Personally I prefer to check first and move when the destination is valid, because it prevents two problems:

  1. You don't have to remember where the entity moved from
  2. You never have an entity in an illegal position
\$\endgroup\$

You must log in to answer this question.

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