7 #ifndef Navigation_HeightField_H
8 #define Navigation_HeightField_H
24 class HeightFieldBlob;
32 struct HeightFieldConfig
34 HeightFieldConfig() : m_xAltitudeCount(0), m_yAltitudeCount(0), m_tileSize(0.f) {}
48 class HeightField :
public RefCountBase<HeightField, MemStat_CollisionData>
55 KyResult Initialize(
const HeightFieldConfig& config);
56 KyResult InitFromBlob(HeightFieldBlob* blob);
57 KyResult InitFromBlobHandler(Ptr<BlobHandler<HeightFieldBlob> > handler);
61 CollisionRayCastResult RayCast(
const Vec3f& startPoint,
const Vec3f& endPoint)
const;
63 KY_INLINE
KyUInt32 GetXTileCount()
const {
return m_tileBox.
CountX(); }
64 KY_INLINE
KyUInt32 GetYTileCount()
const {
return m_tileBox.
CountY(); }
65 KY_INLINE
KyUInt32 GetTileCount()
const {
return GetTileCount() * GetYTileCount(); }
67 KY_INLINE
KyUInt32 GetXAltitudeCount()
const {
return m_vertexBox.
CountX(); }
68 KY_INLINE
KyUInt32 GetYAltitudeCount()
const {
return m_vertexBox.
CountY(); }
69 KY_INLINE
KyUInt32 GetAltitudeCount()
const {
return GetTileCount() * GetYTileCount(); }
71 KY_INLINE
KyFloat32 GetXExtents()
const {
return GetXTileCount() * m_blob->m_tileSize; }
72 KY_INLINE
KyFloat32 GetYExtents()
const {
return GetYTileCount() * m_blob->m_tileSize; }
74 KY_INLINE
void SetAltitude(
const HeightfieldVertexPos& pixelPos,
KyFloat32 altitude)
79 KY_INLINE Vec3f GetVertex(
const HeightfieldVertexPos& vertexPos)
const
81 const KyFloat32 altitude = GetAltitude(vertexPos);
82 return m_blob->m_origin + Vec3f((
KyFloat32)vertexPos.x * m_blob->m_tileSize, (
KyFloat32)vertexPos.y * m_blob->m_tileSize, altitude);
85 KY_INLINE
KyResult GetTileAtPos(
const Vec3f& pos, HeightFieldTile& tile)
const
88 const Vec3f localPos = pos - m_blob->m_origin;
89 const CellPos cellPos(SnapFloat(localPos.x), SnapFloat(localPos.y));
90 return GetTileAtPos(cellPos, tile);
93 KY_INLINE
KyResult GetTileAtPos(
const HeightfieldTilePos& pixelPos, HeightFieldTile& tile)
const
95 if (m_tileBox.
IsInside(pixelPos) ==
false)
98 const KyFloat32 southEastX = ((
KyFloat32)pixelPos.x * m_blob->m_tileSize) + m_blob->m_origin.x + m_blob->m_tileSize;
99 const KyFloat32 southEastY = ((
KyFloat32)pixelPos.y * m_blob->m_tileSize) + m_blob->m_origin.y;
101 tile.m_vertices[0].x = southEastX;
102 tile.m_vertices[0].y = southEastY;
103 tile.m_vertices[0].z = GetAltitude(pixelPos.NeighborEast());
105 tile.m_vertices[1].x = southEastX;
106 tile.m_vertices[1].y = southEastY + m_blob->m_tileSize;
107 tile.m_vertices[1].z = GetAltitude(pixelPos.NeighborNorthEast());
109 tile.m_vertices[2].x = southEastX - m_blob->m_tileSize;
110 tile.m_vertices[2].y = southEastY + m_blob->m_tileSize;
111 tile.m_vertices[2].z = GetAltitude(pixelPos.NeighborNorth());
113 tile.m_vertices[3].x = southEastX - m_blob->m_tileSize;
114 tile.m_vertices[3].y = southEastY;
115 tile.m_vertices[3].z = GetAltitude(pixelPos);
117 tile.m_cellPos = pixelPos;
122 Box2f GetAABB2D()
const
124 return Box2f(m_blob->m_origin.Get2d(), m_blob->m_origin.Get2d() +
125 Vec2f(m_blob->m_xAltitudeCount * m_blob->m_tileSize, m_blob->m_yAltitudeCount * m_blob->m_tileSize));
128 void VisualDebug(World* world);
130 KyResult ConvertToIndexedMesh(IndexedMesh& mesh)
const;
132 KyResult WriteToObj(File* file)
const;
134 BlobHandler<HeightFieldBlob>* GetBlobHandler() {
return m_blobHandler; }
138 KY_INLINE
KyFloat32 GetAltitude(
const HeightfieldVertexPos& vertexPos)
const
143 KyResult GetStartTile(
const Vec3f& startPoint,
const Vec3f& endPoint, HeightFieldTile& tile)
const;
144 KyResult GetNextTile(
const HeightFieldTile& tile,
const Vec2f& rayA,
const Vec2f& rayB, HeightFieldTile& outTile,
const HeightFieldTile* destTile =
KY_NULL)
const;
145 CollisionRayCastResult RayVsTriangle(
const Triangle3f& tri,
const Vec3f& from,
const Vec3f& to)
const;
149 return KyUInt32(inputValue * m_tileSizeInv);
152 KY_INLINE Vec3f Interpolate(
const Vec3f& v0,
const Vec3f& v1,
KyFloat32 rt)
const
155 return Vec3f(s * v0[0] + rt * v1[0],
156 s * v0[1] + rt * v1[1],
157 s * v0[2] + rt * v1[2]);
162 Ptr<BlobHandler<HeightFieldBlob> > m_blobHandler;
163 HeightFieldBlob* m_blob;
166 HeightfieldTileBox m_tileBox;
167 HeightfieldVertexBox m_vertexBox;
173 #endif // Navigation_HeightField_H
This class represents a two-dimensional axis-aligned bounding box whose dimensions are stored using 3...
Definition: box2i.h:119
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
Vec2i CellPos
A type that represents the position of a cell within a 2D grid.
Definition: navmeshtypes.h:33
Box2i HeightfieldTileBox
A type that represents a bounding box around the Tiles of the Heigtfield.
Definition: heightfield.h:27
Box2i HeightfieldVertexBox
A type that represents a bounding box around the Vertices of the Heighfield.
Definition: heightfield.h:30
#define KY_NULL
Null value.
Definition: types.h:247
Vec2i HeightfieldVertexPos
A type that represents one of the vertices of a Tile within a 2D grid.
Definition: heightfield.h:29
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
Vec2i HeightfieldTilePos
A type that represents the position of a Tile within a 2D grid.
Definition: heightfield.h:24
KyInt32 GetRowMajorIndex(const Vec2i &pos) const
Retrieves the index of the grid cell at the specified (X,Y) position, counting cells row-by-row...
Definition: box2i.h:297
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
Heightfield with a uniform grid of sampled altitudes.
Definition: heightfield.h:51
This class defines a two-dimensional vector whose coordinates are stored using 32-bit integers...
Definition: vec2i.h:26
Definition: gamekitcrowddispersion.h:20
#define KY_SUCCESS
Shorthand for Kaim::Result::Success.
Definition: types.h:273
KyInt32 CountY() const
Retrieves the number of grid cells contained within this box along its Y axis.
Definition: box2i.h:210
#define KY_ERROR
Shorthand for Kaim::Result::Failure.
Definition: types.h:272
bool IsInside(const Vec2i &pos) const
Returns true if the specified position is contained within the extents of the bounding box or if the ...
Definition: box2i.h:214
KyInt32 CountX() const
Retrieves the number of grid cells contained within this box along its X axis.
Definition: box2i.h:207
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43