I'm really trying to up my game in game mathematics and now I'm working on enemy movement and a bit of AI. I have an enemy with two modes attack and avoid, based on an integer avoid variable that's either 1 or 0.

On avoid I want it to hover by the player, moving back and forth at a distance until it is set to avoid.

On attack I want to go close at a steady pace say an x and y velocity of 12, then once it reaches a certain distance to gain speed.

I have disx and disy variables that have the distance from each other for each coordinate. I also have a totDis variable with the normal a^2 + b^2 = c^2 formula. I'm pretty sure I'll need another variable that I'll have to change depending on the distance that effects the speed.

It kinda feels like I have all the pieces to the puzzle, I just don't know how to put them together though lol.

I'd like to be able to do this as mathematically as possible and I don't want anyone to give the the answer or do it for me. I was hoping if anyone could give me pointers or tips or just ideas on the directions I can go myself because I really want to have an understanding of this. I hope this is specific enough, if not I'll try to edit my question for you.


2 Answers 2


You're looking at programming Steering Behaviors - (see this also). Matt Buckland's book is good for this, but in my opinion the code he provides is a bit sticky and overdoes it with the C++ templates (source is on the website).


i think a good approach is Behaviour Trees




This way you can focus in each behaviour and you can add new behaviours easily.

You need nothing too complicated, so you only have to implement what you need.

In your case, you will need to define this or more behaviours: Avoiding, GoingToAvoid, AttackWithCare, AttackingCrazy...

  • Avoiding can have a time counter to determine how much time is avoiding.
  • GoingToAvoid can be similar, but moving enemy as you need.
  • AttackWithCare, moves the enemy towards the player at desired speed, when distance is short enough change the behaviour to AttackingCrazy
  • AttackingCrazy, moves the enemy at increased speed
  • \$\begingroup\$ Underlying this you still need a way to implement the behaviours (or "steering behaviours" if you will!) \$\endgroup\$
    – bobobobo
    Sep 7, 2011 at 14:16

You must log in to answer this question.

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