I've come across a C++ Sample of from Programming Game AI by Example. For the most part it is fairly understandable, the only thing confusing me is the reference to "m_pVehicle->Heading()" I'm not sure what it means by heading. If anyone could clarify this, I would really appreciate it.

 Vector2D SteeringBehaviors::Pursuit(const Vehicle* evader)
      //if the evader is ahead and facing the agent then we can just seek
      //for the evader's current position.
      Vector2D ToEvader = evader->Pos() - m_pVehicle->Pos();

      double RelativeHeading = m_pVehicle->Heading().Dot(evader->Heading());

      if ((ToEvader.Dot(m_pVehicle->Heading()) > 0) &&
          (RelativeHeading < -0.95))  //acos(0.95)=18 degs
        return Seek(evader->Pos());

      //Not considered ahead so we predict where the evader will be.

      //the look-ahead time is proportional to the distance between the evader
      //and the pursuer; and is inversely proportional to the sum of the
      //agents' velocities
      double LookAheadTime = ToEvader.Length() /
                            (m_pVehicle->MaxSpeed() + evader->Speed());

      //now seek to the predicted future position of the evader
      return Seek(evader->Pos() + evader->Velocity() * LookAheadTime);

On p.87 of the book, you will find the definition of m_vHeading as:

//a normalized vector pointing in the direction the entity is heading.
SVector2D m_vHeading;

If you look through the source, you will find:

Vector2D  Heading()const{return m_vHeading;}

Right before your code snippet he explains that if the target being pursued is ahead and almost directly facing the agent, it should just seek directly towards the target's position.

A way to easily determine if the target is ahead, or "in front", of the agent is by using the dot product of the vector from the agent's position to the target's position ToEvader and the agent's heading vector m_pVehicle->Heading(). If the dot product is greater than 0, the target is in front of the agent:

(ToEvader.Dot(m_pVehicle->Heading()) > 0)

You can read more on the dot product and it's uses on pages 23-26.

| improve this answer | |
  • \$\begingroup\$ Thanks for that, I did read in the first chapter that heading was meant to represent direction, but I thought it was strange that the direction vector wasn't normalized \$\endgroup\$ – dbomb101 Aug 12 '11 at 15:04
  • 1
    \$\begingroup\$ @dbomb101 - What makes you think it is not normalized. Actually your code snippet only works if its normalized, otherwise comparing the dot product with -0.95 wouldnt work, so they must be normalized \$\endgroup\$ – Maik Semder Aug 12 '11 at 17:29
  • \$\begingroup\$ Just the fact that some of the other steering behavior methods in the book use a normalized method in them and that one had no mention of it, thus leading me to think something was off about it in the first place. \$\endgroup\$ – dbomb101 Aug 13 '11 at 7:07
  • \$\begingroup\$ dot product ftw. It's often the quick and simple answer to a compex question. \$\endgroup\$ – tenpn Aug 13 '11 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.