I'm programming an AI agent and I want them to avoid the screen edges. I've been considering using either the obstacle avoidance or the fleeing behaviour. Do either of these make sense?
How should I approach this?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
First thing first, creatures should not avoid the "edges of the screen" because that is part of the View and not the actual Game Model. Game Entities should interact with the Game Model and not the View. The view is only the way you present the game to your game's players eyes.
Secondly, what you are asking of has little to do with Obstacle Avoidance steering behavior because these techniques were mostly designed to help finding a none-collision path through obstacles or avoiding moving obstacles (see this for more info).
It also should have little to do with fleeing because that is conceptually useful in building distance from moving creatures. If something is trying to get away from the edges of the screen as much as possible, it may just stay in the center of the screen.
If the game type is a brawler (beat'em up) or a platformer, you could make it so that if the distance from the camera is greater than
Width / 2 on axis-x or
Height / 2, on axis-y, the creature will move towards the camera until it is close enough. Alternatively if there are obstacles, the creature could find a path to some spot close enough to the camera and stop once it is near enough.
Normally a creature follows a path. Sometimes the path is generated with a path finding algorithm such as A*. Other times the path is picked in advance by the games' creators. If the path is picked in advance and the edges of the screen don't move, it could be selected so it doesn't move through obstacles or outside the edges of the screen.
If the path is created by a path finding algorithm, the edges of the screen could be marked as an impassable obstacle and avoided.
How close to an edge do we care about?
For a fairly thin threshold, avoiding the edges is quite closely approximated by finding the closest point on the edge and avoiding that:
For a thick threshold, it's a good approximation to just steer toward the center of the screen: