BVB Source Codes

CRYENGINE Show CoverPath.cpp Source code

Return Download CRYENGINE: download CoverPath.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "CoverPath.h"
  5.  
  6. #include "DebugDrawContext.h"
  7.  
  8. CoverPath::CoverPath()
  9.         : m_flags(0)
  10. {
  11. }
  12.  
  13. const CoverPath::Points& CoverPath::GetPoints() const
  14. {
  15.         return m_points;
  16. }
  17.  
  18. bool CoverPath::Empty() const
  19. {
  20.         return m_points.size() < 2;
  21. }
  22.  
  23. void CoverPath::Clear()
  24. {
  25.         m_flags = 0;
  26.         m_points.clear();
  27. }
  28.  
  29. void CoverPath::Reserve(uint32 pointCount)
  30. {
  31.         m_points.reserve(pointCount);
  32. }
  33.  
  34. void CoverPath::AddPoint(const Vec3& point)
  35. {
  36.         if (!m_points.empty())
  37.         {
  38.                 float distance = m_points.back().distance + (m_points.back().position - point).len();
  39.                 m_points.push_back(Point(point, distance));
  40.         }
  41.         else
  42.                 m_points.push_back(Point(point, 0.0f));
  43. }
  44.  
  45. Vec3 CoverPath::GetPointAt(float distance) const
  46. {
  47.         uint32 pointCount = m_points.size();
  48.         uint32 current = 0;
  49.  
  50.         if ((m_flags & Looped) && (distance > GetLength()))
  51.                 distance -= GetLength();
  52.  
  53.         while (current < pointCount - 1)
  54.         {
  55.                 const Point& left = m_points[current];
  56.                 const Point& right = m_points[current + 1];
  57.  
  58.                 if ((distance >= left.distance) && (distance < right.distance))
  59.                         break;
  60.  
  61.                 ++current;
  62.         }
  63.  
  64.         if (current != pointCount)
  65.         {
  66.                 const Point& left = m_points[current];
  67.                 const Point& right = m_points[current + 1];
  68.  
  69.                 float frac = (distance - left.distance) / (right.distance - left.distance);
  70.  
  71.                 return left.position + (right.position - left.position) * frac;
  72.         }
  73.  
  74.         return Vec3(ZERO);
  75. }
  76.  
  77. Vec3 CoverPath::GetClosestPoint(const Vec3& point, float* distanceToPath, float* distanceOnPath) const
  78. {
  79.         uint32 pointCount = m_points.size();
  80.  
  81.         float shortestSq = FLT_MAX;
  82.         float shortestT = FLT_MAX;
  83.         uint32 shortestLeft = -1;
  84.  
  85.         for (uint32 i = 0; i < pointCount - 1; ++i)
  86.         {
  87.                 const Point& left = m_points[i];
  88.                 const Point& right = m_points[i + 1];
  89.  
  90.                 Lineseg pathSegment(left.position, right.position);
  91.  
  92.                 float t;
  93.                 float distToSegmentSq = Distance::Point_Lineseg2DSq(point, pathSegment, t);
  94.  
  95.                 if (distToSegmentSq <= shortestSq)
  96.                 {
  97.                         shortestSq = distToSegmentSq;
  98.                         shortestT = t;
  99.                         shortestLeft = i;
  100.                 }
  101.         }
  102.  
  103.         const Point& left = m_points[shortestLeft];
  104.         const Point& right = m_points[shortestLeft + 1];
  105.  
  106.         if (distanceToPath)
  107.                 *distanceToPath = sqrt_tpl(shortestSq);
  108.  
  109.         if (distanceOnPath)
  110.                 *distanceOnPath = left.distance + shortestT * (right.distance - left.distance);
  111.  
  112.         return left.position + (right.position - left.position) * shortestT;
  113. }
  114.  
  115. float CoverPath::GetDistanceAt(const Vec3& point, float tolerance) const
  116. {
  117.         uint32 pointCount = m_points.size();
  118.  
  119.         float lowestDistToSegmentSq = FLT_MAX;
  120.         float best = -1.0f;
  121.  
  122.         for (uint32 i = 0; i < pointCount - 1; ++i)
  123.         {
  124.                 const Point& left = m_points[i];
  125.                 const Point& right = m_points[i + 1];
  126.  
  127.                 Lineseg pathSegment(left.position, right.position);
  128.  
  129.                 float t;
  130.                 float distToSegmentSq = Distance::Point_Lineseg2DSq(point, pathSegment, t);
  131.  
  132.                 if (distToSegmentSq <= sqr(tolerance))
  133.                 {
  134.                         if (distToSegmentSq < lowestDistToSegmentSq)
  135.                         {
  136.                                 best = left.distance + t * (right.distance - left.distance);
  137.                                 lowestDistToSegmentSq = distToSegmentSq;
  138.                         }
  139.                 }
  140.         }
  141.  
  142.         return best;
  143. }
  144.  
  145. Vec3 CoverPath::GetNormalAt(const Vec3& point) const
  146. {
  147.         uint32 pointCount = m_points.size();
  148.         if (pointCount < 2)
  149.                 return ZERO;
  150.  
  151.         float lowestDistToSegmentSq = FLT_MAX;
  152.         uint32 best = ~0u;
  153.  
  154.         for (uint32 i = 0; i < pointCount - 1; ++i)
  155.         {
  156.                 const Point& left = m_points[i];
  157.                 const Point& right = m_points[i + 1];
  158.  
  159.                 Lineseg pathSegment(left.position, right.position);
  160.  
  161.                 float t;
  162.                 float distToSegmentSq = Distance::Point_Lineseg2DSq(point, pathSegment, t);
  163.  
  164.                 if (distToSegmentSq < lowestDistToSegmentSq)
  165.                 {
  166.                         best = i;
  167.                         lowestDistToSegmentSq = distToSegmentSq;
  168.                 }
  169.         }
  170.  
  171.         const Point& left = m_points[best];
  172.         const Point& right = m_points[best + 1];
  173.  
  174.         return (left.position - right.position).Cross(CoverUp).normalized();
  175. }
  176.  
  177. float CoverPath::GetLength() const
  178. {
  179.         return !m_points.empty() ? m_points.back().distance : 0.0f;
  180. }
  181.  
  182. uint32 CoverPath::GetFlags() const
  183. {
  184.         return m_flags;
  185. }
  186.  
  187. void CoverPath::SetFlags(uint32 flags)
  188. {
  189.         m_flags = flags;
  190. }
  191.  
  192. bool CoverPath::Intersect(const Vec3& origin, const Vec3& dir, float* distance, Vec3* point) const
  193. {
  194.         uint32 pointCount = m_points.size();
  195.  
  196.         Lineseg ray(origin, origin + dir);
  197.  
  198.         int closestLeftIdx = -1;
  199.         float closestA = FLT_MAX;
  200.         float closestB = FLT_MAX;
  201.  
  202.         for (uint32 i = 0; i < pointCount - 1; ++i)
  203.         {
  204.                 const Point& left = m_points[i];
  205.                 const Point& right = m_points[i + 1];
  206.  
  207.                 Lineseg pathSegment(left.position, right.position);
  208.  
  209.                 float a, b;
  210.  
  211.                 Vec3 delta = ray.start - pathSegment.start;
  212.                 Vec3 segmentDir = pathSegment.end - pathSegment.start;
  213.  
  214.                 float crossD = segmentDir.x * dir.y - segmentDir.y * dir.x;
  215.                 float crossDelta1 = delta.x * dir.y - delta.y * dir.x;
  216.                 float crossDelta2 = delta.x * segmentDir.y - delta.y * segmentDir.x;
  217.  
  218.                 if (fabs_tpl(crossD) > 0.0001f)
  219.                 {
  220.                         a = crossDelta1 / crossD;
  221.                         b = crossDelta2 / crossD;
  222.  
  223.                         if (a > 1.0f || a < 0.0f || b > 1.0f || b < 0.0f)
  224.                                 continue;
  225.  
  226.                         if (b < closestB)
  227.                         {
  228.                                 closestA = a;
  229.                                 closestB = b;
  230.                                 closestLeftIdx = i;
  231.                         }
  232.                 }
  233.         }
  234.  
  235.         if (closestLeftIdx != -1)
  236.         {
  237.                 const Point& left = m_points[closestLeftIdx];
  238.                 const Point& right = m_points[closestLeftIdx + 1];
  239.  
  240.                 if (distance)
  241.                         *distance = left.distance + closestA * (right.distance - left.distance);
  242.  
  243.                 if (point)
  244.                         *point = left.position + closestA * (right.position - left.position);
  245.  
  246.                 return true;
  247.         }
  248.  
  249.         return false;
  250. }
  251.  
  252. void CoverPath::DebugDraw()
  253. {
  254.         uint32 pointCount = m_points.size();
  255.  
  256.         if (pointCount < 2)
  257.                 return;
  258.  
  259.         CDebugDrawContext dc;
  260.         dc->SetDepthWrite(false);
  261.  
  262.         Points::const_iterator rightIt = m_points.begin();
  263.         Points::const_iterator leftIt = rightIt++;
  264.  
  265.         bool looped = (m_flags & Looped) != 0;
  266.  
  267.         const Vec3 floorOffset = (CoverUp * 0.005f);
  268.  
  269.         for (uint32 i = 0; i < pointCount - 1; ++i)
  270.         {
  271.                 const Vec3& left = floorOffset + (*leftIt++).position;
  272.                 const Vec3& right = floorOffset + (*rightIt++).position;
  273.  
  274.                 bool loopSegment = looped && (i == (pointCount - 2));
  275.  
  276.                 dc->DrawLine(left, loopSegment ? Col_SlateBlue : Col_White, right, loopSegment ? Col_SlateBlue : Col_White, 10.0f);
  277.         }
  278.  
  279.         Points::const_iterator pointIt = m_points.begin();
  280.         for (uint32 i = 0; i < pointCount; ++i)
  281.                 dc->DrawSphere((CoverUp * 0.025f) + (*pointIt++).position, 0.035f, ColorB(Col_Black), true);
  282. }
  283.  
downloadCoverPath.cpp 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