Need help with obstacles and grid-based movement


I’ve become stuck with another seemingly simple task. My game is set up on a grid of 100 x 100px blocks and the player moves from the center of one grid block or “space” to another. The movement is only 4 directions. I am having trouble working with obstacles that would prevent the player from moving to that space.

I seemed to work at first… I tested if an obstacle object was within 101px of the player. For each obstacle object within range, I compared the position of the player to the obstacle. This determined whether the obstacle was above, below, left, or right of the player. This then set a variable which disabled movement in that direction.

For example,
if Player.Y() > Obstacle.Y(), Do =1 to variable Up_Blocked (The player can not move up)
if Player.X() > Obstacle.X(), Do =1 to variable Left_Blocked (The player can not move left)

This works perfectly for preventing movement. However, the problem I have is resetting the “(Direction)_Blocked” variable if there is no longer an obstacle in that direction. I can only make it work if the player is more than 101px from all obstacle objects. Then every “(Direction)_Blocked” variable is set to 0.

Basically, I just need to test if there is an Obstacle object in the space immediately above, below, left, and right of the player. If there is, prevent movement in that direction (set a variable). But if there is not, reset the variable and allow movement in that direction.

Hopefully that all makes sense.


Try to reset the blocking variables each frame, before checking the obstacles:

[code]Conditions: No conditions (always)
Actions: Do = 0 to variable Up_Blocked
Do = 0 to variable Left_Blocked

Conditions: Obstacle above
Actions: Do = 1 to variable Up_Blocked

Conditions: Obstacle at left
Actions: Do = 1 to variable Left_Blocked

… [/code]

I knew it was going to be something so simple! That makes perfect sense.

Thank you!