Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
    }
share|improve this question

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
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 –  dbomb101 Aug 12 '11 at 15:04
1  
@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 –  Maik Semder Aug 12 '11 at 17:29
    
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. –  dbomb101 Aug 13 '11 at 7:07
    
dot product ftw. It's often the quick and simple answer to a compex question. –  tenpn Aug 13 '11 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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