Plasma Engine  2.0
Loading...
Searching...
No Matches
GameGrid.h
1#pragma once
2
3#include <Foundation/Containers/DynamicArray.h>
4#include <Foundation/Math/Mat3.h>
5#include <Foundation/Math/Vec2.h>
6#include <Foundation/Math/Vec3.h>
7#include <Utilities/UtilitiesDLL.h>
8
11template <class CellData>
13{
14public:
21
22 plGameGrid();
23
25 void CreateGrid(plUInt16 uiSizeX, plUInt16 uiSizeY);
26
33 void SetWorldSpaceDimensions(const plVec3& vLowerLeftCorner, const plVec3& vCellSize, Orientation ori = InPlaneXZ);
34
41 void SetWorldSpaceDimensions(const plVec3& vLowerLeftCorner, const plVec3& vCellSize, const plMat3& mRotation);
42
44 plVec3 GetCellSize() const { return m_vLocalSpaceCellSize; }
45
48 plVec2I32 GetCellAtWorldPosition(const plVec3& vWorldSpacePos) const;
49
51 plUInt16 GetGridSizeX() const { return m_uiGridSizeX; }
52
54 plUInt16 GetGridSizeY() const { return m_uiGridSizeY; }
55
58
60 plUInt32 GetNumCells() const { return m_uiGridSizeX * m_uiGridSizeY; }
61
63 CellData& GetCell(plUInt32 uiIndex) { return m_Cells[uiIndex]; }
64
66 const CellData& GetCell(plUInt32 uiIndex) const { return m_Cells[uiIndex]; }
67
69 CellData& GetCell(const plVec2I32& vCoord) { return m_Cells[ConvertCellCoordinateToIndex(vCoord)]; }
70
72 const CellData& GetCell(const plVec2I32& vCoord) const { return m_Cells[ConvertCellCoordinateToIndex(vCoord)]; }
73
75 plVec2I32 ConvertCellIndexToCoordinate(plUInt32 uiIndex) const { return plVec2I32(uiIndex % m_uiGridSizeX, uiIndex / m_uiGridSizeX); }
76
78 plUInt32 ConvertCellCoordinateToIndex(const plVec2I32& vCoord) const { return vCoord.y * m_uiGridSizeX + vCoord.x; }
79
81 plVec3 GetCellWorldSpaceOrigin(const plVec2I32& vCoord) const;
82 plVec3 GetCellLocalSpaceOrigin(const plVec2I32& vCoord) const;
83
85 plVec3 GetCellWorldSpaceCenter(const plVec2I32& vCoord) const;
86 plVec3 GetCellLocalSpaceCenter(const plVec2I32& vCoord) const;
87
89 bool IsValidCellCoordinate(const plVec2I32& vCoord) const;
90
94 bool PickCell(const plVec3& vRayStartPos, const plVec3& vRayDirNorm, plVec2I32* out_pCellCoord, plVec3* out_pIntersection = nullptr) const;
95
97 const plVec3& GetWorldSpaceOrigin() const { return m_vWorldSpaceOrigin; }
98
100 const plMat3& GetRotationToWorldSpace() const { return m_mRotateToWorldspace; }
101
103 const plMat3& GetRotationToGridSpace() const { return m_mRotateToGridspace; }
104
106 bool GetRayIntersection(const plVec3& vRayStartWorldSpace, const plVec3& vRayDirNormalizedWorldSpace, float fMaxLength, float& out_fIntersection,
107 plVec2I32& out_vCellCoord) const;
108
110 bool GetRayIntersectionExpandedBBox(const plVec3& vRayStartWorldSpace, const plVec3& vRayDirNormalizedWorldSpace, float fMaxLength,
111 float& out_fIntersection, const plVec3& vExpandBBoxByThis) const;
112
113 void ComputeWorldSpaceCorners(plVec3* pCorners) const;
114
115 plResult Serialize(plStreamWriter& stream) const;
116 plResult Deserialize(plStreamReader& stream);
117
118private:
119 plUInt16 m_uiGridSizeX;
120 plUInt16 m_uiGridSizeY;
121
122 plMat3 m_mRotateToWorldspace;
123 plMat3 m_mRotateToGridspace;
124
125 plVec3 m_vWorldSpaceOrigin;
126 plVec3 m_vLocalSpaceCellSize;
127 plVec3 m_vInverseLocalSpaceCellSize;
128
130};
131
132#include <Utilities/DataStructures/Implementation/GameGrid_inl.h>
Definition DynamicArray.h:81
plGameGrid is a general purpose 2D grid structure that has several convenience functions which are of...
Definition GameGrid.h:13
plUInt16 GetGridSizeX() const
Returns the number of cells along the X axis.
Definition GameGrid.h:51
const CellData & GetCell(plUInt32 uiIndex) const
Gives access to a cell by cell index.
Definition GameGrid.h:66
const plMat3 & GetRotationToGridSpace() const
Returns the matrix used to rotate coordinates from world space to grid space.
Definition GameGrid.h:103
plUInt16 GetGridSizeY() const
Returns the number of cells along the Y axis.
Definition GameGrid.h:54
void SetWorldSpaceDimensions(const plVec3 &vLowerLeftCorner, const plVec3 &vCellSize, Orientation ori=InPlaneXZ)
Sets the lower left position of the grid in world space coordinates and the cell size.
Definition GameGrid_inl.h:29
const CellData & GetCell(const plVec2I32 &vCoord) const
Gives access to a cell by cell coordinates.
Definition GameGrid.h:72
plVec3 GetCellWorldSpaceOrigin(const plVec2I32 &vCoord) const
Returns the lower left world space position of the cell with the given coordinates.
Definition GameGrid_inl.h:70
plUInt32 GetNumCells() const
Returns the total number of cells.
Definition GameGrid.h:60
plUInt32 ConvertCellCoordinateToIndex(const plVec2I32 &vCoord) const
Converts a cell coordinate into a cell index.
Definition GameGrid.h:78
plVec3 GetCellSize() const
Returns the size of each cell.
Definition GameGrid.h:44
bool IsValidCellCoordinate(const plVec2I32 &vCoord) const
Checks whether the given cell coordinate is inside valid ranges.
Definition GameGrid_inl.h:94
bool PickCell(const plVec3 &vRayStartPos, const plVec3 &vRayDirNorm, plVec2I32 *out_pCellCoord, plVec3 *out_pIntersection=nullptr) const
Casts a world space ray through the grid and determines which cell is hit (if any).
Definition GameGrid_inl.h:100
bool GetRayIntersection(const plVec3 &vRayStartWorldSpace, const plVec3 &vRayDirNormalizedWorldSpace, float fMaxLength, float &out_fIntersection, plVec2I32 &out_vCellCoord) const
Tests where and at which cell the given world space ray intersects the grids bounding box.
Definition GameGrid_inl.h:130
plBoundingBox GetWorldBoundingBox() const
Returns the world-space bounding box of the grid, as specified via SetWorldDimensions.
Definition GameGrid_inl.h:120
CellData & GetCell(const plVec2I32 &vCoord)
Gives access to a cell by cell coordinates.
Definition GameGrid.h:69
plVec3 GetCellWorldSpaceCenter(const plVec2I32 &vCoord) const
Returns the center world space position of the cell with the given coordinates.
Definition GameGrid_inl.h:82
plVec2I32 ConvertCellIndexToCoordinate(plUInt32 uiIndex) const
Converts a cell index into a 2D cell coordinate.
Definition GameGrid.h:75
const plVec3 & GetWorldSpaceOrigin() const
Returns the lower left corner position in world space of the grid.
Definition GameGrid.h:97
CellData & GetCell(plUInt32 uiIndex)
Gives access to a cell by cell index.
Definition GameGrid.h:63
const plMat3 & GetRotationToWorldSpace() const
Returns the matrix used to rotate coordinates from grid space to world space.
Definition GameGrid.h:100
plVec2I32 GetCellAtWorldPosition(const plVec3 &vWorldSpacePos) const
Returns the coordinate of the cell at the given world-space position. The world space dimension must ...
Definition GameGrid_inl.h:61
void CreateGrid(plUInt16 uiSizeX, plUInt16 uiSizeY)
Clears all data and reallocates the grid with the given dimensions.
Definition GameGrid_inl.h:18
Orientation
Definition GameGrid.h:16
@ InPlaneXminusZ
The grid is expected to lie in the XZ plane in world-space (when Y is up, this is similar to a top do...
Definition GameGrid.h:19
@ InPlaneXZ
The grid is expected to lie in the XZ plane in world-space (when Y is up, this is similar to a top do...
Definition GameGrid.h:18
@ InPlaneXY
The grid is expected to lie in the XY plane in world-space (when Y is up, this is similar to a 2D sid...
Definition GameGrid.h:17
bool GetRayIntersectionExpandedBBox(const plVec3 &vRayStartWorldSpace, const plVec3 &vRayDirNormalizedWorldSpace, float fMaxLength, float &out_fIntersection, const plVec3 &vExpandBBoxByThis) const
Tests whether a ray would hit the grid bounding box, if it were expanded by a constant.
Definition GameGrid_inl.h:167
Interface for binary in (read) streams.
Definition Stream.h:22
Interface for binary out (write) streams.
Definition Stream.h:107
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54