I have a question that is driving me crazy because I think it should be fairly easy to find a tutorial on it. I'm currently using XNA and Farseer to create a simple platformer game. I'm about to be done with it but I want my monsters to be smarter. In this case that means that I don't want my monsters to fall off cliffs.

To put it simply, I want something that checks the area in front of my "monster" in order to determine when the monster should change direction. Could you guys point me in the right direction?

What am I supposed to use, there should exist a simple solution or simple algorithm to check this, no?


5 Answers 5


I've never worked with XNA before and i think it will appear better solutions then mine. But a workaround that might work is that you could put a invisible object on the extremes of the cliffs and the monster could check for collision with those object. If it collides, then changes direction.

  • 2
    \$\begingroup\$ Simple and elegant, +1 \$\endgroup\$
    – Valmond
    Commented Oct 18, 2011 at 14:23
  • \$\begingroup\$ That's a good solution. Farseer supports "sensors", so use these at the cliffs. \$\endgroup\$
    – bummzack
    Commented Oct 18, 2011 at 16:39
  • \$\begingroup\$ I really like your answer so I don't want to contend it. He could also use the bottom-left/right tile (depending on the movement direction) - which is a more 'obvious' solution. \$\endgroup\$ Commented Oct 18, 2011 at 17:48
  • 1
    \$\begingroup\$ The main issue with a solution like this is that you have to place an obstacle for every cliff. If there are few in the game, then it may be ok, but otherwise, it can take a long time, and you can miss some cliffs. \$\endgroup\$ Commented Oct 19, 2011 at 2:12
  • 4
    \$\begingroup\$ @AlbeyAmakiir Well, you could also swap this around and put a sensor at the bottom ahead of the player (that moves with the player). As soon as this sensor stops registering collisions, you're at a cliff. \$\endgroup\$
    – bummzack
    Commented Oct 19, 2011 at 6:44

One naive way of implementing it is by using raycasting.

Calculate where your monster will be in some small chunk of time (probably a frame). From that position (or if you want to be more robust, that position +/- the bounding area of your enemy) do a raycast down. If it doesn't hit something within an appropriate range (depending on where you start your raycast vertically, the height of the character, your desired "step size", etc) you can figure that there's a cliff where the character wants to be, and decide to change directions appropriately.


With my AI, I simply check if there's a platform below and int the direction of its travel (not using a tilesystem, just a groovy collision system). If there is not- or if it hits a solid block, it gets a confused look and turns around.

This way you can have dynamic maps and still have a working(term used loosely) AI.


I suspect you are using a tile map, in which case on of the solution would be to do it based on tile indices. You need to find on what tile your monster currently is on, with the following formulas:

int tileX = (int)(monster.Position.X / TILE_WIDTH);
int tileY = (int(monster.Position.Y / TILE_HEIGHT);

Once you know these, you have to find in which X direction you're going (in order to check for blocks ahead):

int direction = 0;
if (monster.Velocity > 0f)
    direction = 1;
if (monster.Velocity < 0f)
    direction = -1;

Now you have a normalized X direction to work with.

Next, you'll want to check the blocks around to see if you should change direction:

if (GetTileCollision(tileX + direction, tileY) == TileCollision.Impassable || // going into a wall
    GetTileCollision(tileX + direction, tileY) == TIleCollision.Passable &&
    GetTileCollision(tileX + direction, tileY + 1) == TileCollision.Passable)) // we're going to fall down a block
    ChangeDirection(); // we change direction

You get the general idea. Make sure that you return TileCollision.Impassable (or whatever) if the tile indices are outside the map, to prevent your program from crashing with an invalid index, and it also prevents monsters from going outside of the map.

I guess it really depends on the way you handle tiles but this is the solution that worked for me.

Based on the tutorials off http://www.sgtconker.com/ (although the website is down as I write this)


Set a box on the outside of the platform that is on a separate layer, and have the monsters check for that, so they won't fall off but the player still can.

  • \$\begingroup\$ I think this is the same answer that Gilson gave. \$\endgroup\$ Commented May 9, 2012 at 3:21
  • \$\begingroup\$ ya looking properly it is \$\endgroup\$
    – Darren
    Commented May 9, 2012 at 8:22

You must log in to answer this question.

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