gwnavruntime/collision/heightfieldtile.h Source File

heightfieldtile.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 #ifndef Navigation_HeightFieldTile_H
8 #define Navigation_HeightFieldTile_H
9 
14 
15 namespace Kaim
16 {
17 
18 struct HeightFieldTile
19 {
20  enum TwoDSide
21  {
22  LeftOfPoint = 0,
23  RightOfPoint = 1,
24  };
25 
26  HeightFieldTile() : m_entrantDirection(CardinalDir_INVALID) {}
27 
28  KY_INLINE KyUInt32 GetSide(const Vec2f& a, const Vec2f& b, const Vec3f& c) const
29  {
30  return (b.x - a.x)*(c.y - a.y) - (b.y - a.y)*(c.x - a.x) <= 0.f ? RightOfPoint : LeftOfPoint;
31  }
32 
33  KY_INLINE Triangle3f GetFirstTriangle() const
34  {
35  Triangle3f tri;
36  tri.Set(m_vertices[0], m_vertices[2], m_vertices[3]);
37  return tri;
38  }
39 
40  KY_INLINE Triangle3f GetSecondTriangle() const
41  {
42  Triangle3f tri;
43  tri.Set(m_vertices[0], m_vertices[1], m_vertices[2]);
44  return tri;
45  }
46 
47  // This function assumes that m_entrantDirection == CardinalDir_INVALID
48  // In other words, we began from within this tile and need to determine the direction in which we exit.
49  CardinalDir GetFirstExitDirection(const Vec2f& a, const Vec2f& b) const;
50 
51  // This function assumes that m_entrantDirection != CardinalDir_INVALID
52  // In other words, we entered the tile from a given direction and know that we cannot "go backwards".
53 
54  /*
55  B A
56  v2--e1--v1 // CCW order in Navigation.
57  | | // This means that each vertex is numbered such that vN will be the "right" vertex of eN
58  e2 e0
59  | /|\ |
60  v3--|--v0
61  |
62  r
63  */
64  KY_INLINE CardinalDir GetExitDirectionFromEntryDirection(const Vec2f& a, const Vec2f& b) const
65  {
66  const CardinalDir facingDir = GetOppositeCardinalDir(m_entrantDirection);
67  const KyUInt32 rightVertexSide = GetSide(a, b, m_vertices[facingDir]); // rightVertexSide == 0 if A is to the left of the segment
68  const KyUInt32 leftVertexSide = GetSide(a, b, m_vertices[ClampVertexIndex(facingDir + 1)]); // leftVertexSide == 1 if A to to the right of the segment
69  const KyUInt32 exitDirOffset = (rightVertexSide + leftVertexSide * 2 - (rightVertexSide & leftVertexSide)) + 1;
70 
71  return ClampVertexIndex(m_entrantDirection + exitDirOffset);
72  }
73 
74  // Some functions require vertex indices to "wrap" (e.g. 4 => 0 as there is no index '4').
75  KY_INLINE KyUInt32 ClampVertexIndex(const KyUInt32 index) const { return index & 3 /* equivalent to % 4 */; }
76 
77  CellPos m_cellPos;
78  Vec3f m_vertices[4];
79  CardinalDir m_entrantDirection;
80 };
81 
82 }
83 
84 #endif //Navigation_HeightField_H
CardinalDir GetOppositeCardinalDir(const CardinalDir dir)
Returns the CardinalDir that lies in the opposite direction from the specified CardinalDir.
Definition: cardinaldir.h:35
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:33
KyUInt32 CardinalDir
Defines a type that refers to one of the cardinal points on the compass:
Definition: cardinaldir.h:23
static const CardinalDir CardinalDir_INVALID
Identifies an invalid cardinal direction.
Definition: cardinaldir.h:29
Definition: gamekitcrowddispersion.h:20
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36