Research Article

Artificial Intelligence in Video Games: Towards a Unified Framework

Algorithm 7

Dodging a straight line projectile. This function assumes that the projectile is not penetrating.
void  dodge_projectile(Unit  u, Projectile  p)
{
 //Create a ray for the projectile course
Ray r(p->position(), p->velocity());
 //Get a list of objects intersecting the ray
ObjectList is = intersection(r, p->radius());
 //Only dodge if u is the first object to intersect the ray
if  (is.front() == u)
 {
  //Is u exactly on the projectile course?
  if  (r.passthru(u->position()))
  {
   //Move perpendicularly by a distance equal to the sum of bounding radiuses
   u->move(u->position() + r.normal()(p->radius() + u->radius()));
   return;
  }
  //Project the unit position on the projectile course
  Vector pr = r->project(u->position());
  //Get a normal to the projectile course with a norm equal to the distance between
   the unit position and its projection
  Vector mv = u->position() − pr;
  //Rescale it to the width of the intersection
  mv  = (p->radius()   (mv.norm()   u->radius()))/mv.norm();
  //Follow the normal to avoid collision
  u->move(u->position() + mv);
 }
}