gwnavruntime/collision/heightfieldtile.h Source File

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