BVB Source Codes

CRYENGINE Show PathFollower.h Source code

Return Download CRYENGINE: download PathFollower.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef PATHFOLLOWER_H
  4. #define PATHFOLLOWER_H
  5.  
  6. #include <CryAISystem/IPathfinder.h>
  7.  
  8. /// This implements path following in a rather simple - and therefore cheap way. One of the
  9. /// results is an ability to generate a pretty accurate prediction of the future entity position,
  10. /// assuming that the entity follows the output of this almost exactly.
  11. class CPathFollower : public IPathFollower
  12. {
  13.         /// The control points are attached to the original path, but can be dynamically adjusted
  14.         /// according to the pathRadius. Thus the offsetAmount is scaled by pathRadius when calculating
  15.         /// the actual position
  16.         struct SPathControlPoint
  17.         {
  18.                 SPathControlPoint()
  19.                         : navType(IAISystem::NAV_UNSET), pos(ZERO), offsetDir(ZERO), offsetAmount(ZERO), customId(0) {}
  20.                 SPathControlPoint(IAISystem::ENavigationType navType, const Vec3& pos, const Vec3& offsetDir, float offsetAmount)
  21.                         : navType(navType), pos(pos), offsetDir(offsetDir), offsetAmount(offsetAmount), customId(0) {}
  22.  
  23.                 IAISystem::ENavigationType navType;
  24.                 Vec3                       pos;
  25.                 Vec3                       offsetDir;
  26.                 float                      offsetAmount;
  27.                 uint16                     customId;
  28.         };
  29.         typedef std::vector<SPathControlPoint> TPathControlPoints;
  30.  
  31.         TPathControlPoints m_pathControlPoints;
  32.  
  33.         /// parameters describing how we follow the path - can be modified as we follow
  34.         PathFollowerParams m_params;
  35.  
  36.         /// segment that we're on - goes from this point index to the next
  37.         int m_curLASegmentIndex;
  38.  
  39.         /// position of our lookahead point - the thing we aim towards
  40.         Vec3 m_curLAPos;
  41.  
  42.         /// used for controlling the acceleration
  43.         float     m_lastOutputSpeed;
  44.  
  45.         INavPath* m_navPath;
  46.  
  47.         int       m_pathVersion;
  48.  
  49.         Vec3      m_CurPos;
  50.         uint32    m_CurIndex;
  51.  
  52.         Vec3 GetPathControlPoint(unsigned iPt) const
  53.         {
  54.                 return m_pathControlPoints[iPt].pos + m_pathControlPoints[iPt].offsetDir * (m_pathControlPoints[iPt].offsetAmount * m_params.pathRadius);
  55.         }
  56.  
  57.         /// calculates distance between two points, depending on m_params.use_2D
  58.         float DistancePointPoint(const Vec3 pt1, const Vec3 pt2) const;
  59.         /// calculates distance squared between two points, depending on m_params.use_2D
  60.         float DistancePointPointSq(const Vec3 pt1, const Vec3 pt2) const;
  61.  
  62.         /// Used internally to create our list of path control points
  63.         void ProcessPath();
  64.  
  65.         /// calculates a suitable starting state of the lookahead (etc) to match curPos
  66.         void StartFollowing(const Vec3& curPos, const Vec3& curVel);
  67.  
  68.         /// Used internally to calculate a new lookahead position. const so that it can be used in prediction
  69.         /// where we integrate forwards in time.
  70.         /// curPos and curVel are the position/velocity of the follower
  71.         void GetNewLookAheadPos(Vec3& newLAPos, int& newLASegIndex, const Vec3& curLAPos, int curLASegIndex,
  72.                                 const Vec3& curPos, const Vec3& curVel, float dt) const;
  73.  
  74.         /// uses the lookahead and current position to derive a velocity (ultimately using speed control etc) that
  75.         /// should be applied (i.e. passed to physics) to curPos.
  76.         void UseLookAheadPos(Vec3& velocity, bool& reachedEnd, const Vec3 LAPos, const Vec3 curPos, const Vec3 curVel,
  77.                              float dt, int curLASegmentIndex, float& lastOutputSpeed) const;
  78.  
  79.         /// As public version but you pass the current state in
  80.         Vec3 GetPathPointAhead(float dist, float& actualDist, int curLASegmentIndex, Vec3 curLAPos) const;
  81.  
  82.         /// As public version but you pass the current state in
  83.         float GetDistToEnd(const Vec3* pCurPos, int curLASegmentIndex, Vec3 curLAPos) const;
  84.  
  85.         // Hiding this virtual method here, so they are only accessible through the IPathFollower interface
  86.         // (i.e. Outside the AISystem module)
  87.         virtual void Release() override { delete this; }
  88.  
  89.         uint32       GetIndex(const Vec3& pos) const;
  90.  
  91. public:
  92.         CPathFollower(const PathFollowerParams& params = PathFollowerParams());
  93.         ~CPathFollower();
  94.  
  95.         virtual void Reset() override;
  96.  
  97.         /// This attaches us to a particular path (pass 0 to detach)
  98.         virtual void AttachToPath(INavPath* pNavPath) override;
  99.  
  100.         virtual void SetParams(const PathFollowerParams& params) override { m_params = params; }
  101.  
  102.         /// The params may be modified as the path is followed - bear in mind that doing so
  103.         /// will invalidate any predictions
  104.         virtual PathFollowerParams&       GetParams() override       { return m_params; }
  105.         /// Just view the params
  106.         virtual const PathFollowerParams& GetParams() const override { return m_params; }
  107.  
  108.         /// Updates the path following progress, resulting in a move instruction and prediction of future
  109.         /// states if desired.
  110.         virtual bool Update(PathFollowResult& result, const Vec3& curPos, const Vec3& curVel, float dt) override;
  111.  
  112.         /// Advances the current state in terms of position - effectively pretending that the follower
  113.         /// has gone further than it has.
  114.         virtual void Advance(float distance) override;
  115.  
  116.         /// Returns the distance from the lookahead to the end, plus the distance from the position passed in
  117.         /// to the LA if pCurPos != 0
  118.         virtual float GetDistToEnd(const Vec3* pCurPos) const override;
  119.  
  120.         /// Returns the distance along the path from the current look-ahead position to the
  121.         /// first smart object path segment. If there's no path, or no smart objects on the
  122.         /// path, then std::numeric_limits<float>::max() will be returned
  123.         virtual float GetDistToSmartObject() const override;
  124.         virtual float GetDistToNavType(IAISystem::ENavigationType navType) const override;
  125.         virtual float GetDistToCustomNav(const TPathControlPoints& controlPoints, uint32 curLASegmentIndex, const Vec3& curLAPos) const;
  126.         /// Returns a point on the path some distance ahead. actualDist is set according to
  127.         /// how far we looked - may be less than dist if we'd reach the end
  128.         virtual Vec3 GetPathPointAhead(float dist, float& actualDist) const override;
  129.  
  130.         virtual void Draw(const Vec3& drawOffset = ZERO) const override;
  131.  
  132.         virtual void Serialize(TSerialize ser) override;
  133.  
  134.         // Checks ability to walk along a piecewise linear path starting from the current position
  135.         // (useful for example when animation would like to deviate from the path)
  136.         virtual bool CheckWalkability(const Vec2* path, const size_t length) const override;
  137.  
  138.         // Can the pathfollower cut corners if there is space to do so? (default: true)
  139.         virtual bool GetAllowCuttingCorners() const override;
  140.  
  141.         // Sets whether or not the pathfollower is allowed to cut corners if there is space to do so. (default: true)
  142.         virtual void SetAllowCuttingCorners(const bool allowCuttingCorners) override;
  143.  
  144.         // Checks for whether the attached path is affected by a NavMesh change or whether it would be still be fully traversable from its current position.
  145.         virtual bool IsRemainingPathAffectedByNavMeshChange(const NavigationMeshID affectedMeshID, const MNM::TileID affectedTileID) const override;
  146. };
  147.  
  148. #endif
  149.  
downloadPathFollower.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top