gwnavruntime/queries/utils/capsuleintersector.h Source File

capsuleintersector.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 
9 
10 
11 // Primary contact: JUBA - secondary contact: NOBODY
12 #ifndef Navigation_CapsuleIntersector_H
13 #define Navigation_CapsuleIntersector_H
14 
20 
21 
22 namespace Kaim
23 {
24 
25 /*
26 class CapsuleIntersector
27 */
28 class CapsuleIntersector
29 {
30  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
31 
32 public:
33  CapsuleIntersector(const Vec3f& startPos, const Vec3f& endPos, KyFloat32 radius, KyFloat32 integerPrecision)
34  : m_integerPrecision(integerPrecision)
35  , m_startCenter(startPos)
36  , m_endCenter(endPos)
37  , m_radius(radius)
38  {
39 
40  Vec3f dir(m_endCenter - m_startCenter);
41  dir.z = 0.f;
42  m_dist = dir.Normalize();
43 
44  const Vec3f normalizedDir3D_ortho(-dir.y, dir.x, 0.f);
45  const Vec3f box_a(m_startCenter - (normalizedDir3D_ortho * m_radius));
46 
47  m_orientedBox2d.Set(box_a, Vec2f(dir.x, dir.y), m_dist, m_radius * 2.f, 0.f);
48  }
49 
50  CapsuleIntersector(const Vec3f& startPos, const Vec2f& normalizedDir2d, KyFloat32 dist, KyFloat32 radius, KyFloat32 integerPrecision)
51  : m_integerPrecision(integerPrecision)
52  , m_startCenter(startPos)
53  , m_normalizedDir2d(normalizedDir2d)
54  , m_dist(dist)
55  , m_radius(radius)
56  {
57  const Vec3f normalizedDir3D_ortho(-m_normalizedDir2d.y, m_normalizedDir2d.x, 0.f);
58  const Vec3f box_a(m_startCenter - (normalizedDir3D_ortho * m_radius));
59  m_orientedBox2d.Set(box_a, m_normalizedDir2d, m_dist, m_radius * 2.f, 0.f);
60 
61  const Vec3f normalizedDir3D(m_normalizedDir2d.x, m_normalizedDir2d.y, 0.f);
62  m_endCenter = m_startCenter + (normalizedDir3D * m_dist);
63  }
64 
65  bool DoesIntersectEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos)
66  {
67  const Vec3f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision, 0.f);
68  const Vec3f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision, 0.f);
69 
70  return Intersections::SegmentVsCapsule2d(startEdgePos, endEdgePos, m_orientedBox2d, m_startCenter, m_endCenter, m_radius);
71  }
72 
73  void ComputeCollisionPosOnEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos, Vec3f& collisionPos, KyFloat32& squareDistToCollisionPos)
74  {
75  const Vec3f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision, 0.f);
76  const Vec3f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision, 0.f);
77 
78  ClosestPoint::OnSegmentVsDiskCast2d(startEdgePos, endEdgePos, m_startCenter, m_radius, m_normalizedDir2d, m_dist, collisionPos, squareDistToCollisionPos);
79  }
80 
81  void ComputeTriangleCost(const CoordPos64& v0CoordPos, const CoordPos64& v1CoordPos, const CoordPos64& v2CoordPos, KyFloat32& cost)
82  {
83  const Vec3f v0(v0CoordPos.x * m_integerPrecision, v0CoordPos.y * m_integerPrecision, 0.f);
84  const Vec3f v1(v1CoordPos.x * m_integerPrecision, v1CoordPos.y * m_integerPrecision, 0.f);
85  const Vec3f v2(v2CoordPos.x * m_integerPrecision, v2CoordPos.y * m_integerPrecision, 0.f);
86 
87  Vec3f unused;
88  ClosestPoint::OnTriangleVsDiskCast2d(v0, v1, v2, m_startCenter, m_radius, m_normalizedDir2d, m_dist, unused, cost);
89  }
90 
91 private:
92  KyFloat32 m_integerPrecision;
93 
94  Vec3f m_startCenter;
95  Vec3f m_endCenter;
96  Vec2f m_normalizedDir2d;
97  KyFloat32 m_dist;
98  KyFloat32 m_radius; //< the radius of the extrimity disks
99 
100  OrientedBox2d m_orientedBox2d;
101 };
102 
103 
104 }
105 
106 
107 #endif //Navigation_CapsuleIntersector_H
108 
Vec2LL CoordPos64
A type that represents the position of a point within the 2D integer grid.
Definition: navmeshtypes.h:19
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43