**Win-Vector Blog » R**, and kindly contributed to R-bloggers)

Hollywood movies are obsessed with outrunning explosions and outrunning crashing alien spaceships. For explosions the movies give the optimal (but unusable) solution: run straight away. For crashing alien spaceships they give the same advice, but in this case it is wrong. We demonstrate the correct angle to flee.

However, for slower explosions this is useful advice.

But what about outrunning a crashing alien spaceship? Since the spaceship is not expanding the situation is no longer isotropic. Some directions may be better than others. In fact if the spaceship is moving faster than you, getting out of its way may be better than trying to outrun it. So we consider the following diagram of the situation:

If the crashing spaceship is approximated as a large red circle moving towards our astronaut (green circle) with a velocity “u” and our astronaut starts with a head start of “d” and a running speed of “v” at an angle “a” from the direction of motion of the spaceship. An angle of zero means the astronaut is running straight away (and hoping the spaceship will give up). Lets give quantities values. Let us say our olympic star astronaut can run (in space suit) at 6 meters per second, the crashing ship is moving at a cinematic 12 meters per second and our astronaut has a 240 meter head start. We can code up in R these values and a function that computes the distance between the two centers (spaceship and astronaut) as a function of time t and fleeing angle a:

```
```v <- 6
u <- 12
d <- 240
distsq <- function(t,a) { (d+v*cos(a)*t - u*t)^2 + (v*sin(a)*t)^2 }

A little calculus and algebra lets us define a function that gives the time of closest approach for each angle of retreat:

```
```topt <- function(a) { -d*(v*cos(a)-u)/((v*cos(a)-u)^2+(v*sin(a))^2) }

Plugging in the time of closest approach back into our distance function gives us the nearest distance encountered for each angle of retreat:

```
```dmin <- function(a) { sqrt(distsq(topt(a),a)) }

We can plot this to get a glimpse of the best angle (notice both running away and running towards end up with the astronaut getting smushed since the astronaut is slower than the crashing alien spaceship):

```
```df <- data.frame(angle=seq(from=0,to=pi,length.out=200))
df$mindist <- sapply(df$angle,dmin)
library(ggplot2)
ggplot(df,aes(x=angle,y=mindist)) + geom_line()

This graph suggests that for this combination of initial distance and velocities the optimal fleeing angle is around 1 radian. We can solve for the optimal angle:

```
```optimize(dmin,lower=0,upper=2*pi,maximum=T)

Which turns out to be:

```
```$maximum
[1] 1.047206
$objective
[1] 120

Or 1.047 radians with a closest approach of 120 meters. This already is an angle of fleeing that is more across than away. For even faster ships we would have optimal fleeing plans that are closer and closer to being at right angles to the direction of spaceship motion. For example if we had u=24 the optimal fleeing angle is 1.32 radians with a closest approach of 60 meters.

We sincerely hope that more movies will feature cybernetic characters capable of on the fly calculation and execution optimal fleeing (or pursuit) solutions.

All images copyright their respective holders, included under fair use doctrine.

Related posts:

- Kernel Methods and Support Vector Machines de-Mystified
- The cranky guide to trying R packages
- Why I don’t like Dynamic Typing

**leave a comment**for the author, please follow the link and comment on their blog:

**Win-Vector Blog » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...