home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 05 Hancock / NodeAndLink.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-09  |  1.8 KB  |  50 lines

  1. #include "NodeAndLink.h"
  2.  
  3. bool PathNode::PathExists(const PathNode *n2, const Character *pChar) const{
  4.     bool anyPath = NodeMap::RegionConnection(GetRegion(Connected), n2->GetRegion(Connected));
  5.     if (!anyPath || !pChar) //if there's not a path, or no character to check further, return the result
  6.         return anyPath;
  7.     if (!ReachableWithoutJumping(n2) && !pChar->CanJump())
  8.         return false;
  9.     if (!NoDoorsBetween(n2) && !pChar->CanUseDoors())
  10.         return false;
  11.     if (!NoElevatorsBetween(n2) && pChar->CanUseElevators())
  12.         return false;
  13.     return true;
  14. }
  15.  
  16. bool PathNode::NoDoorsBetween(const PathNode *n2) const{
  17.     return NodeMap::DoorRegionConnection(GetRegion(NoDoor), n2->GetRegion(NoDoor));
  18. }
  19.  
  20. bool PathNode::NoElevatorsBetween(const PathNode *n2) const{
  21.     return NodeMap::ElevatorRegionConnection(GetRegion(NoElevator), n2->GetRegion(NoElevator));
  22. }
  23.  
  24. bool PathNode::ReachableWithoutForceJumping(const PathNode *n2) const {
  25.     return NodeMap::JumpRegionConnection(GetRegion(NoJump), n2->GetRegion(NoJump));
  26. }
  27.  
  28.  
  29. Vector3 PathLink::ClosestPointOnLinkLine(const Vector3& q, float &fraction) const{
  30.     Vector3 dir = next->pos - origin->pos;
  31.     fraction = dir.dot(q - origin->pos) / dir.dot(dir);
  32.     Vector3 a = origin->pos + fraction * dir;
  33.     return a;
  34. }
  35.  
  36. bool PathLink::InsideFatLink(const Vector3& p) const{
  37.  
  38.     float fraction;
  39.     Vector3 a = ClosestPointOnLinkLine(p, fraction);
  40.     Vector3 q = p;
  41.     q.y = a.y; //set the vertical position to be the same as the projection
  42.     if (fraction < 0.0f)
  43.         return (q.distance2(origin->pos) < (origin->radius * origin->radius));
  44.     else if (fraction > 1.0f)
  45.         return (q.distance2(next->pos) < (next->radius * next->radius));
  46.     float localWidth = fraction * next->radius + (1.0f - fraction) * origin->radius;
  47.     CVector diff = q;
  48.     diff -= a;
  49.     return (diff.length2() < Sqr(localWidth));
  50. }