0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This make a lot of sense but what do you mean by having the enemy move more often? Do you mean turn based? \$\endgroup\$ – Slayter Feb 4 '13 at 14:23
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Feb 4 '13 at 14:44
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – fnord Feb 4 '13 at 17:51
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Feb 4 '13 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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