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

What is the easiest way to implement DPad style movement (No diagonal) for AI without using and A* algorithm? I thought about having the enemy catch up to the player in the Y axis first then the X axis (or vice versa) but then it would be too easy to evade the enemy. If it matters I'm doing this in Objective-C using Cocos2d. Any input would be appreciated.

share|improve this question
up vote 0 down vote accepted

For Orthogonal movement, you can choose randomly between the two directions you need to go.

enter image description here

The red player and blue enemy are in the world. They are separated by 2x and 3y or (2,3). So we know we need to move in both the X plus direction and the Y plus direction. So we choose randomly between the two. Say we choose the X plus direction, now they're separated by (1,3). Now say the random choice again chooses X plus direction. Now they're separated by (0,3). Now the only choice is to move in the Y plus direction.

Otherwise, move in the longest direction first, then the shortest. If you're worried about the player being able to too easily avoid the enemy make the enemy move slightly more often than the player. Say 1.1 moves per the player's 1 move.

share|improve this answer
This make a lot of sense but what do you mean by having the enemy move more often? Do you mean turn based? – Slayter Feb 4 '13 at 14:23
Moving more often or moving faster. However you have the game set up. If the player were to hold the up key, they would move N spaces per second, the enemy would move N*1.1 spaces in the same time. – Byte56 Feb 4 '13 at 14:44
Ah, ok now I understand. Thanks! – Slayter Feb 4 '13 at 16:10
Excellent answer @Byte56 though I would add a small modification to it. I would suggest biasing the random selection of which direction, and using the remaining distances as the bias. In your example, I would have the chance of choosing to move in the Y plus direction 3/2 more likely than moving in the X plus direction for the first move. If the X plus direction is chosen, I would then have the Y plus direction be 3 times more likely to be chosen than the X plus direction. This will make the movement more likely to be heading directly towards the target. – fnord Feb 4 '13 at 17:51
Thanks @fnord. Sounds like you're suggesting a combination of the two methods I suggest. I agree, I was trying to keep it simple, but maybe a weighted random chance would be good. – Byte56 Feb 4 '13 at 17:56

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.