You get a bonus - 1 coin for daily activity. Now you have 1 coin

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

Lecture



Obstacle traversal by enemies and non-player characters (going around, jumping over, jumping up onto, crawling, etc.) can be implemented using various algorithms and approaches, depending on the complexity of the AI and the environment. Here are the main algorithms and methods used for obstacle traversal:

1. FSM (Finite State Machine)

Each state describes a behavior (for example, "walk", "jump", "stand", "fall"). When an obstacle is detected, the character transitions to the appropriate state:

  • Walk → obstacle → Jump

  • Jump → reaching the platform → Walk

Simple, works well for patrolling enemies and NPCs.

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

2. Raycasting / Scanning the environment to find a movement path in 2D platformers

Scanning the space with rays pointing down, forward, and up makes it possible to:

  • detect ledges (if there is no ground underfoot);

  • see walls ahead;

  • understand whether it is possible to jump over. Then simple physics checks + conditions are used

Suitable for simple AI:

if (isGroundAhead() && !isWallAhead())
{
  moveRight();
}
   else
      if (isWallAhead() && canJump())
      {
          jump();
      } 

Used in Unity, Godot, Phaser, and other engines.

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

3. A* (A-star) — pathfinding

Applicable for more complex levels, where you need to reach a target while going around obstacles:

  • The world is divided into a grid (in 2D — a tile map);

  • A* finds the shortest path;

  • The sprite moves along the found coordinates.

Requires configuring the "cost" of tiles: whether you can jump there, how long the jump is, and so on.

4. Navigation Mesh / Reachability graphs

  • Used in cases where A* is not suitable.

  • The space is broken down into platforms and links between them (whether you can jump from one to another).

  • The character chooses the optimal route along the graph's vertices.

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

5. Behavior Trees (BT)

  • A structured form of AI behavior.

  • You can specify: "If there is an obstacle — jump", "If there is an enemy — attack", and so on.

  • A very flexible system, often used in large projects.

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

7. Machine Learning / Reinforcement learning for path traversal

For advanced AI:

  • The AI learns to overcome obstacles on its own;

  • Used in experiments (for example, in OpenAI Gym);

  • Impractical for simple games — expensive to develop and unstable in behavior.

example

Enemy Obstacle Traversal Algorithms in 2D and 3D Games

To traverse a path in a game using reinforcement learning (RL), the process can be represented as a series of stages that repeat in a loop. Here is what it would look like step by step:

The RL agent's operating loop

  1. Initialization:

    • The agent starts in an initial state.

    • The initial values of the Q-function (or the policy) are random or zero.

  2. Obtaining the current state of the environment:

    • For example: "I am standing in front of a pit", or "There is an enemy in front of me".

  3. Choosing an action:

    • A strategy is used (for example, ε-greedy): the agent chooses either the best known action or a random one for exploration.

    • For example: "Jump", "Attack", "Go right".

  4. Performing the action and transitioning:

    • The agent acts in the environment.

    • The environment updates: a new position, a new situation.

  5. Receiving a reward:

    • The agent gets feedback:

      • +1 for a successful jump,

      • –1 for falling into a pit,

      • +10 for reaching the goal.

  6. Updating the strategy (learning):

    • The Q-function or neural network is adjusted based on the new experience.

    • The agent "learns" so as to act better in the future.

  7. Transitioning to a new state and repeating:

    • The agent is now in a new state, and the loop starts over.

Additionally:

Obstacle Possible solution algorithm
Ledge Downward raycast check
Low passage "Crouch" state
Wall raycast + "jump"
Moving platform FSM + platform tracking
Enemy FSM / Behavior Tree

See also

  • [[b13196]]
  • [[b13197]]
  • [[b13198]]
  • [[b4134]]
  • [[b4399]]

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Computer games developming, game-design"

Terms: Computer games developming, game-design