How can a 3D-autopilot be implemented in a space opera game? How do you compute the optimal trajectory from A to B in a solar system using classical mechanics?

This has been one of the tougher problems I've encountered in the MMO game project I'm working on. No other space game I've played has had a solution for this. The problem is usually avoided by either inventing other physics or hiding or skipping the actual travel. But I aim higher than that. I want it to

Since it's a space opera game, the simulation primarily involves classical mechanics, i.e. Newtonian physics. (Einsteinian relativity I consider non-practical from an implementation point-of-view. I don't say this

This has been one of the tougher problems I've encountered in the MMO game project I'm working on. No other space game I've played has had a solution for this. The problem is usually avoided by either inventing other physics or hiding or skipping the actual travel. But I aim higher than that. I want it to

*feel*like piloting a ship in space.Since it's a space opera game, the simulation primarily involves classical mechanics, i.e. Newtonian physics. (Einsteinian relativity I consider non-practical from an implementation point-of-view. I don't say this

*only*because it's harder physics... and the light-speed limit would definitely limit game design!) In the game, celestial orbits, space ship movement, thrust engine power, fuel consumption etc are all derived with Newtonian physics.
A lot of such behavior can be computed using high school level physics. But planning an optimal (or at least fairly efficient) space trajectory path to reach a given destination proved to be a considerably more difficult problem. It took much longer to solve it than I had expected. In this post I thought I'd describe what I came up with. It is an interesting problem and the solution can perhaps be useful to others.

### Defining the Problem

The objective is to plan the space trajectory needed to rendezvous with a given destination within the solar system. It is needed to implement the game's autopilot which is used by both computer-controlled ships and players. The trajectory must be computed in advance since simply going in the direction of the destination would result in arriving with a speed very different from that of the destination object. We need to know beforehand when to 'gas' and when to 'break', and in what precise directions to do this.

The planned trajectory should be fairly efficient (primarily regarding travel time, but also regarding fuel consumption) and preferably not cheat by using different physics than the player experiences with the manual controls. It must also be fairly fast to compute - it's an MMO game and there can be many thousands of travelling ships.

The formal problem is: The travelling spaceship, at current position P in space and with current velocity V, is to travel and rendezvous with a destination object (a planet, another ship etc) which is at current position Q and with current velocity W. Since it's a rendezvous (e.g. docking or landing) the travelling spaceship must have the same velocity W as the destination upon arrival.

The above diagram is in absolute frame coordinates (sometimes called the inertial frame). However since we're adhering to Newtonian physics we can simplify the problem by defining it relative to the travelling ship's starting position and velocity instead. The next diagram shows the same case but in relative frame coordinates.

P

_{0 }=

**0**Q

_{0 }= Q - P ΔV = W - V

A game space ship travels using its thrust engine, which accelerates the vessel in whatever direction it is pointing. The planned trajectory should be expressed as a sequence of thrust applications, each describing a trajectory

*leg*:

- From time 0 to t1 apply thrust X
- From time t1 to t2 apply thrust Y
- ...

The technical problem is: Find the trajectory legs (each expressed as a thrust vector and a length of time) that as soon as possible will move the ship to the same position and velocity as the destination, so that:

P(t) = Q(t); velocity = ΔV; t as small as possible

### Solution Approach

This is not trivial. What is described here is actually my third approach. The first one was plain incorrect, and the second one was sub-optimal and missed the destination in certain cases. This one however I believe is fully correct and actually produces the optimal trajectory in all cases (in terms of shortest travel time). (If a physicist reads this I'd be happy for any comments!)

#### Two Solution Parts

Two things must happen in the trajectory: Our ship needs to change its speed to match that of the destination, and it must travel the distance to reach the destination.The naive approach would be to do one after the other. Simply first move to where the destination is and then adjust the speed! (Of course it takes time to adjust the speed and the destination will move in the mean time, but we can change the destination position to adjust for that.) Or first change the speed to match the destination, and then travel to it by applying an acceleration followed by an equal amount of deceleration?

There are three problems with this one-after-the-other approach. First, it results in a sub-optimal trajectory meaning it will take longer time and consume more fuel than necessary. Second, it will look weird to the player. And third and most importantly, it works poorly for destinations that change their velocity over time, as almost all destinations do. (Planets are in orbit, other spaceships are travelling somewhere - or even actively evading you if you're chasing them...)

To elaborate a little on this last point, it's true the problem definition above makes no mention of changes to the destination's velocity over time. It's been simplified to disregard the destination's own acceleration. In theory the change in velocity direction over time is possible to compute for orbiting celestial bodies, although difficult within the context of this problem. But other spaceships are impossible to predict because someone else is controlling them!

*Therefore the trajectory must be recomputed regularly during the journey - and we might as well disregard the complication of the destination's continuously changing acceleration in this solution.*

The naive approach is inadequate and the two solution parts must therefore be 'integrated' within each trajectory leg.

#### Impulse and Moment

To begin with, let's describe the solution in terms of impulse and moment. Δp is the change in an object's*moment*achieved by applying a certain

*impulse*I. Impulse is defined as applying a certain force F over a length of time Δt, and is also equal to the change in velocity ΔV multiplied by the object's mass m.

t

_{tot}expresses the total time the trajectory will take to traverse. We shall try to find this value.
We know that, over the course of the total trajectory, an impulse I

_{V}must have been applied to the ship that changes its velocity with ΔV. And since we know ΔV and the ship's mass m we can directly compute it via I_{V}= m ΔV.
We also know that, some impulses in addition to I

_{V}must have been applied that causes it to traverse the remaining distance to the destination so they rendezvous at the same point P(t_{tot}) = Q(t_{tot}). Furthermore, since I_{V}brings the ship to the correct arrival velocity, the sum of these impulses must be zero (otherwise the arrival velocity will be wrong). We therefore define these impulses as I_{D}and I_{D}', where the direction of I_{D}is such that it accelerates the ship towards the rendezvous point, and I_{D}' = -I_{D}so that the net velocity change from these two impulses is zero.The above diagram illustrates the impulses that must have been applied to the ship over the course of the trajectory.

In order to apply these impulses the trajectory needs to have two legs:

- the first trajectory leg applies I
_{D}and part of I_{V}; - the second trajectory leg applies I
_{D}' and the remaining part of I_{V}.

####
Finding I_{D}

We know I_{V}, and I

_{D}' is the inverse of I

_{D}. So the impulse we still need to find is I

_{D}.

The purpose of I

_{D}and I

_{D}' is to traverse the distance to the rendezvous point. How do we define this distance? Note that the purpose of I

_{D}and I

_{D}' is to traverse the distance that results after the destination's velocity ΔV and our own I

_{V}have been taken into account.

First, define the distance at time 0:

Then we add the destination's movement (t ΔV) and subtract our own movement caused by I

_{V:}

_{}

_{}

By our definition of I

_{D}, its direction is equal to the direction of D(t

_{tot}). Its magnitude also depends on t

_{tot}. To nail down I

_{D}we must thus find t

_{tot}.

### Algebraic Solution

There is an algebraic solution at this point. Assuming constant thrust F, the total time is bound by the following equation:### Practical Solution

Since short travel time is the top priority, we shall assume constant thrust at the maximum power level set by the player (or the computer-controlled pilot). At any moment the ship can exert this thrust in an arbitrary direction in 3D space.The radius represents the maximum thrust force. This scalar quantity will be referred to as F.

Remember that we're looking for two trajectory legs, each described by a thrust vector and the length of time it is to be applied. F

_{1}represents the first thrust vector and F

_{2}represents the second. As the diagram shows, they are resultant vectors of F

_{V}, F

_{D}and F

_{D}'. Their relationship with the impulse quantities is:

I

_{V}= t

_{tot}F

_{V}

I

_{D}= t

_{1}F

_{D}

I

_{D}' = t

_{2}F

_{D}'

t

_{tot }= t

_{1}+ t

_{2}

Since we know I

_{V}, we also know the direction of F

_{V}. However we don't know its magnitude, other than that it is in the interval (0, F).

If we

*did*know its magnitude, the rest would be relatively straight-forward to calculate. Applying the equations described above, knowing F

_{V }would yield:

t

_{tot}

D(t

_{tot})

R

_{D }(unit vector with same direction as D(t

_{tot}) and F

_{D})

Now we look closer at the force diagram:

Using the dot product between F

_{V }and R

_{D}we can write:

Using the law of sines we can write:

Now we have F

_{D}, which also gives us t

_{1}:

Having t

_{1}we can continue to calculate t

_{2}, F

_{D}'... and F

_{1}and F

_{2}.

**The solution thus consists of the two trajectory legs { F**

_{1}; t_{1 }} and { F_{2}; t_{2 }}!As stated earlier we merely assumed the knowledge of F

_{V}'s magnitude. In the next post I'll describe implementing this solution using the bisection method to iteratively find the greatest possible value for F

_{V}(the greatest value of F

_{V}yields the optimal trajectory). It turns out that the average number of iterations required is less than 10 which is fine performance-wise, and the resulting movement is, well, smooth!

## No comments:

## Post a Comment