BVB Source Codes

CRYENGINE Show FlyHelpers_PathFollowerHelpers.h Source code

Return Download CRYENGINE: download FlyHelpers_PathFollowerHelpers.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef __FLY_HELPERS__PATH_FOLLOWER_HELPERS__H__
  4. #define __FLY_HELPERS__PATH_FOLLOWER_HELPERS__H__
  5.  
  6. #include <CryMath/Cry_Geo.h>
  7. #include <CryMath/Cry_GeoDistance.h>
  8.  
  9. namespace FlyHelpers
  10. {
  11. Path CreatePathSubset(const Path& sourcePath, const PathLocation& start, const PathLocation& end)
  12. {
  13.         assert(start.segmentIndex <= end.segmentIndex);
  14.  
  15.         Path result;
  16.  
  17.         result.AddPoint(start.CalculatePathPosition(sourcePath));
  18.         const size_t pointCount = end.segmentIndex - start.segmentIndex;
  19.         for (size_t i = 0; i < pointCount; ++i)
  20.         {
  21.                 const size_t index = start.segmentIndex + i + 1;
  22.                 const Vec3& point = sourcePath.GetPoint(index);
  23.                 result.AddPoint(point);
  24.         }
  25.         result.AddPoint(end.CalculatePathPosition(sourcePath));
  26.  
  27.         return result;
  28. }
  29.  
  30. PathLocation FindClosestPathLocation(const Path& path, const Vec3& position)
  31. {
  32.         PathLocation pathLocationOut;
  33.  
  34.         float closestDistanceSquared = FLT_MAX;
  35.  
  36.         const size_t segmentCount = path.GetSegmentCount();
  37.         for (size_t i = 0; i < segmentCount; ++i)
  38.         {
  39.                 const Lineseg segment = path.GetSegment(i);
  40.  
  41.                 float deltaSegment = 0;
  42.                 const float distanceSquared = Distance::Point_LinesegSq(position, segment, deltaSegment);
  43.                 if (distanceSquared < closestDistanceSquared)
  44.                 {
  45.                         pathLocationOut.segmentIndex = i;
  46.                         pathLocationOut.normalizedSegmentPosition = deltaSegment;
  47.                         closestDistanceSquared = distanceSquared;
  48.                 }
  49.         }
  50.  
  51.         return pathLocationOut;
  52. }
  53.  
  54. PathLocation TracePathForward(const Path& path, const PathLocation& start, const float distance)
  55. {
  56.         float remainingDistance = distance;
  57.         remainingDistance -= start.CalculateDistanceToNextPathPoint(path);
  58.         if (remainingDistance <= 0)
  59.         {
  60.                 PathLocation result;
  61.                 result.segmentIndex = start.segmentIndex;
  62.                 const float distanceAlongSegment = distance;
  63.                 const float normalizedDistanceDelta = distance / path.GetSegmentLength(start.segmentIndex);
  64.                 result.normalizedSegmentPosition = start.normalizedSegmentPosition + normalizedDistanceDelta;
  65.  
  66.                 return result;
  67.         }
  68.  
  69.         const size_t segmentCount = path.GetSegmentCount();
  70.         size_t currentSegment = start.segmentIndex + 1;
  71.         while (currentSegment < segmentCount)
  72.         {
  73.                 const float segmentLength = path.GetSegmentLength(currentSegment);
  74.                 if (remainingDistance < segmentLength)
  75.                 {
  76.                         break;
  77.                 }
  78.                 remainingDistance -= segmentLength;
  79.                 ++currentSegment;
  80.         }
  81.  
  82.         if (currentSegment < segmentCount)
  83.         {
  84.                 PathLocation result;
  85.                 result.segmentIndex = currentSegment;
  86.                 result.normalizedSegmentPosition = remainingDistance / path.GetSegmentLength(currentSegment);
  87.  
  88.                 return result;
  89.         }
  90.         else
  91.         {
  92.                 PathLocation result;
  93.                 result.segmentIndex = path.GetSegmentCount() - 1;
  94.                 result.normalizedSegmentPosition = 1.0f;
  95.  
  96.                 return result;
  97.         }
  98. }
  99.  
  100. PathLocation TracePathForwardLooping(const Path& path, const PathLocation& start, const float distance)
  101. {
  102.         float remainingDistance = distance;
  103.         remainingDistance -= start.CalculateDistanceToNextPathPoint(path);
  104.         if (remainingDistance <= 0)
  105.         {
  106.                 PathLocation result;
  107.                 result.segmentIndex = start.segmentIndex;
  108.                 const float distanceAlongSegment = distance;
  109.                 const float normalizedDistanceDelta = distance / path.GetSegmentLength(start.segmentIndex);
  110.                 result.normalizedSegmentPosition = start.normalizedSegmentPosition + normalizedDistanceDelta;
  111.  
  112.                 return result;
  113.         }
  114.  
  115.         const size_t pathSegmentCount = path.GetSegmentCount();
  116.         size_t currentSegment = (start.segmentIndex + 1) % pathSegmentCount;
  117.         while (true)
  118.         {
  119.                 const float segmentLength = path.GetSegmentLength(currentSegment);
  120.                 if (remainingDistance < segmentLength)
  121.                 {
  122.                         break;
  123.                 }
  124.                 remainingDistance -= segmentLength;
  125.                 currentSegment = (currentSegment + 1) % pathSegmentCount;
  126.         }
  127.  
  128.         PathLocation result;
  129.         result.segmentIndex = currentSegment;
  130.         result.normalizedSegmentPosition = remainingDistance / path.GetSegmentLength(currentSegment);
  131.  
  132.         return result;
  133. }
  134.  
  135. PathLocation GetReversePathLocation(const Path& path, const PathLocation& pathLocation)
  136. {
  137.         PathLocation reversedPathLocation;
  138.         const size_t pathSegmentCount = path.GetSegmentCount();
  139.         reversedPathLocation.segmentIndex = pathSegmentCount - 1 - pathLocation.segmentIndex;
  140.         reversedPathLocation.normalizedSegmentPosition = (1.0f - pathLocation.normalizedSegmentPosition);
  141.  
  142.         return reversedPathLocation;
  143. }
  144. }
  145.  
  146. #endif
  147.  
downloadFlyHelpers_PathFollowerHelpers.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