Simple AI Programming

Could anyone give me a basis in pseudo-code of setting up simple AI?

The AI entities only need to be able to move towards the player on a vector that points to the player location, and avoid obstacles in real time.

I’m just looking for an example algorithm to get me started.


double dx = player.x - enemy.x, dy = player.y - enemy.y;
double distance = Math.sqrt(dx*dx + dy*dy);

double multiplier = moveSpeedOfEnemy / distance;

double velocityX = dx * multiplier, velocityY = dy * multiplier;

How does velocity help me? (Thanks for the example though).

I’m more looking for real time path finding (assuming you have the coordinates and size of all obstacles in the area).

Position += Velocity

For path finding you should probably look up A*. There is this tutorial (for tile based maps though) on kevglass’s site http://cokeandcode.com/index.html?page=tutorials/tilemap2

Oh… I’ve just been increasing the position by a pace increment with each logic update. Seems to be working fine…

You can also use Point2D.distance() in place of Math.sqrt().

in 2d space I’ve been using Math.atan2() to get the angle (from -pi to pi) between 2 points, then multiply the x value by Math.sin(direction) and the y value by Math.sin(direction). Works like a charm, also for running away from something just use direction + pi. Ahhhh polar coordinates.

Angles are usually not needed. TheAgent method is fastest and simplest. But there is problem if distance = 0 so that need to be taken care of.

Bringing us back on topic here…

I didn’t ask for how to calculate the distance between to two entities. I’m looking for methods of implementing artificial intelligence and path-finding.

I’ve been considering implementing a concept similar to IR object detection:

  1. Scan 180 degree area (radius is distance from entity to player) for obstacles.
  2. Record where obstacles are and how far away they are, as well as where open spaces are that can be moved through.
  3. Path calculation can be based on open space > far object > near object > very near obstacle (can’t move here)

The question is how to write that in code and how to actually lay out the path. Should it be determined in segments? One segment=pace perhaps?

Note: the game and its map data is pixel based, not tile based.

How smart AI should be? allways found the shortest path or just wandering like zombies?

True, you need to first know what the AI should do.

If the purpose of the AI is to close to one (or many) Player-positions, then it needs 3 parts:
-recognize when to move (like seeing player)and where to (playerposition, or hidingspot)
-plan how to move (build a pathfinding-path, waypoints or just find a general direction)
-pursue the move according to plan and dynamic obstacles. (Step by step following the path, stop at and avoid dynamic obstacles)

Each part can be developed in a seperate step.
Where each generates the input of another part.

Once this simple “see-follow” works out, you might want to think about a “top level” AI, like a finite statemachine or “general” or script
that controls the AI in more complex situations.

Zombie-like AI, but the AI should always be able to find the shortest path to the player and navigate around obstacles.

That’s pretty much all I need. Shortest path from point A to B and ability to avoid obstacles.