AI, with improved collision avoidance

I’ve rewritten the “AI”- at least, the part of it that determines, millisecond by millisecond, what direction the ship should be applying its thrust.  The problem is pretty complex.  First, I’m not realistically able to run the AI by pre-computing a path to a given destination, because over long distances, the path changes.  Planets move in their orbit, and a path that’s clear now might not be clear by the time a ship gets there.  Taking planetary motion and their gravity into account makes the problem complicated to the point where it’s more practical to use a moment-to-moment approach, instead.

So then, every ship needs to be able to look at the universe around it, and use that information to decide what direction it wants to be going in.  The following factors are important: the gravitational force being experienced, proximity to collidable objects, current velocity, engine power, distance to target location, whether all of that adds up to a likelihood of a collision in the immediate future, and what direction that collision is coming from.

My current solution uses a sort of priority system- the first priority is how imminent a collision is if the ship maintains its current velocity.  The more imminent that collision, the higher percentage of total engine output is reserved for avoiding it.  Second priority is gravity- every ship needs to strike a balance between not overreacting to weak gravity wells and not allowing itself deep enough into stronger ones that it is beyond the point of no return.  So a certain amount of thrust is reserved for counteracting gravity, depending on velocity towards or away from the source of the well and the total force, taken as a percentage of total engine thrust.  The final priority is getting closer to the target, without overshooting it.  Again, current velocity is taken into account, and whatever engine power is left over is devoted to getting to the target position without more velocity than can be burned off on arrival.

It’s a fair amount of math, and there are a several constants that need to be tuned.  Make one number too high, and ships will be over-cautious about gravity wells, and the player will be relatively safe near a large planet’s surface.  Make another number too low, and ships with strong engines might overestimate their stopping power, and collide with heavy planets if they have too much distance to get up a high speed.

Overall, I’m pleased with how it’s turned out.  Having a general solution for getting from A to B without making obviously bad decisions is a big step for a game like this.

This entry was posted in Design. Bookmark the permalink.

Comments are closed.