The bugs you’ve seen crawling out of the walls in the Kickstarter video aren’t by any stretch the only creatures you’ll be running into in Eyes Open, but they’re interesting enough on their own to warrant talking about. Today’s post is all about monster AI and what goes on inside their brains.
What exactly is a bug thinking at any given point in time?
Our bug can always be described as being in one of two states : calm or attacking
If the bug is calm, it picks a random direction and turns to face that direction. Once it is looking in the designated direction, it moves in a straight line for a random amount of time. Then, it repeats the process.
If at any point in time the bug sees the player, it enters it’s attacking state. I immediately orient the bug to face the player and make it charge in a straight line. If it’s not directly facing the player (ie. the player moves slightly to the side), the bug will attempt to reorient itself by turning towards the player.
If the bug looses sight of the player, it stays in the attacking state and keeps charging forward until it hits a wall, then it re-enters the calm state and repeats the whole process.
And honestly, that’s about 90% of the behavior right there.
What are the results of the monsters behavior?
- Bugs in the calm state don’t turn while they’re walking. This allows the player to get around them while they’re turning, or back behind them while they’re walking.
- Bugs in the attacking state turn very slowly. This allows players to sidestep them as they charge, which is the best way of dealing with them.
- Bugs keep on charging even if they can’t see the player. This is both a liability and a resource when navigating through them. On one hand, if you’re attacked, the creature is likely to stick itself in an annoying spot and then re-attack you. On the other hand, if a bug is in a position you don’t like, you can actually bait it to go elsewhere. This is a particularly efficient way to get
What else does the monster do?
Even though our base AI is very simple, we introduce a couple of subtle tweaks on top of that to make monsters feel more natural. Each fix serves an important purpose.
- A bug in the calm state is more likely to choose a random direction to look that allows them to see farther. A bug is unlikely to turn to look directly at a wall, although it is possible for it to do so. This means that bugs are more likely to be looking into a room at any given moment, rather than just staring at a wall. But because of their turn-speed, turn-direction and the chance that they will prioritize staring at something close up, the player can still take advantage of an occasional stupid moment to get through a room.
- A bug that is in a hallway will only move in a direction that will take them out of a hallway, or if that’s not possible, in a direction that will not let them bump into a wall. This allows bugs to linger inside of hallways, but ensures that (short of being placed in a really awkward position), they’ll always find their way out relatively quickly. Which means that bugs going into hallways isn’t a fatal flaw that arrests player progression, but a feature that allows the player to exercise caution and patience to proceed. It also encourages bugs to, occasionally, migrate to different rooms, which is both fun and terrifying.
When you said the monster sees the player, what did you mean?
What an excellent question! A lot of AI comes down to finding ways to describe a complicated system in simple terms, then coming up with tricks or approximations to even further simplify the process you’re looking at. If you’re interested in AI, a good way to practice is to periodically stop yourself whenever you’re making a decision and ask yourself why you just made the decision you did. Ask yourself why you know the things you do.
How does your vision work? It’s less about the science of what’s happening when light strikes an object and more of you being able to describe a process in simple terms. So you might say that light travels in a straight line and hits your eye. If something is in the way, the light gets interrupted. Sometimes, only some of the light gets through, like with a piece of cloudy glass. Sometimes, the light even changes direction, like when you look into a mirror.
We use something called “ray-tracing” to simulate a line of sight. First, a monster checks to see whether or not a player is within it’s cone of vision by getting the angle to the player and how far away the player is. With your vision, everything throws off light and you collect all of it at once, but if our engine did that, it would be too CPU intensive and our game would stutter. So we eliminate some of the complexity to make our simulation easier on your hardware.
If the player isn’t within that cone of vision, there’s no way the monster could be seeing it. But even if it is inside that cone of vision, the player might be hiding behind a wall or other object. So we draw a line between the monster and the player, and check to see if it intersects any of the environment. If it does, the player isn’t visible. It’s very similar to how light works with your eye, but it only checks one beam of light – the one that would hit the player. It’s another way to reduce some complexity on the simulation.
There’s a lot more to talk about with AI, so we’ll get to that soon, but hopefully this is enough right now to get your imagination going.