diff options
Diffstat (limited to 'apps/openmb/scene/GridSystem.cpp')
| -rw-r--r-- | apps/openmb/scene/GridSystem.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/apps/openmb/scene/GridSystem.cpp b/apps/openmb/scene/GridSystem.cpp new file mode 100644 index 0000000..273cb02 --- /dev/null +++ b/apps/openmb/scene/GridSystem.cpp @@ -0,0 +1,121 @@ +#include "GridSystem.hpp" + +#include <cmath> + +namespace scene { +GridSystem::GridSystem () + : mCellSize( 1.0f ), mFloorY( 0.0f ), mGridWidth( 1000 ), mGridDepth( 1000 ), mWallHeight( 4 ) { +} + +float GridSystem::getCellSize () const { + return mCellSize; +} + +float GridSystem::getFloorY () const { + return mFloorY; +} + +int GridSystem::getGridWidth () const { + return mGridWidth; +} + +int GridSystem::getGridDepth () const { + return mGridDepth; +} + +int GridSystem::getWallHeight () const { + return mWallHeight; +} + +glm::vec3 GridSystem::gridToWorld ( int gridX, int gridZ, float y ) const { + float halfW = mGridWidth * 0.5f; + float halfD = mGridDepth * 0.5f; + + float worldX = ( gridX - halfW ) * mCellSize + mCellSize * 0.5f; + float worldZ = ( gridZ - halfD ) * mCellSize + mCellSize * 0.5f; + + return glm::vec3( worldX, y, worldZ ); +} + +glm::vec3 GridSystem::gridToWorldFloor ( int gridX, int gridZ ) const { + return gridToWorld( gridX, gridZ, mFloorY ); +} + +bool GridSystem::worldToGrid ( const glm::vec3& worldPos, int& outGridX, int& outGridZ ) const { + float halfW = mGridWidth * 0.5f; + float halfD = mGridDepth * 0.5f; + + float localX = worldPos.x / mCellSize + halfW - 0.5f; + float localZ = worldPos.z / mCellSize + halfD - 0.5f; + + outGridX = static_cast<int>( std::floor( localX ) ); + outGridZ = static_cast<int>( std::floor( localZ ) ); + + return ( outGridX >= 0 && outGridX < mGridWidth && outGridZ >= 0 && outGridZ < mGridDepth ); +} + +glm::vec3 GridSystem::getCellCenter ( int gridX, int gridZ, int cellY ) const { + float y = mFloorY + cellY * mCellSize + mCellSize * 0.5f; + return gridToWorld( gridX, gridZ, y ); +} + +float GridSystem::getMinWorldX () const { + float halfW = mGridWidth * 0.5f; + return ( 0 - halfW ) * mCellSize; +} + +float GridSystem::getMaxWorldX () const { + float halfW = mGridWidth * 0.5f; + return ( mGridWidth - 1 - halfW ) * mCellSize + mCellSize; +} + +float GridSystem::getMinWorldZ () const { + float halfD = mGridDepth * 0.5f; + return ( 0 - halfD ) * mCellSize; +} + +float GridSystem::getMaxWorldZ () const { + float halfD = mGridDepth * 0.5f; + return ( mGridDepth - 1 - halfD ) * mCellSize + mCellSize; +} + +float GridSystem::getHalfWidth () const { + return mGridWidth * 0.5f; +} + +float GridSystem::getHalfDepth () const { + return mGridDepth * 0.5f; +} + +float GridSystem::getFrontWallZ () const { + float halfD = mGridDepth * 0.5f; + return ( 0 - halfD ) * mCellSize + mCellSize * 0.5f; +} + +float GridSystem::getBackWallZ () const { + float halfD = mGridDepth * 0.5f; + return ( mGridDepth - 1 - halfD ) * mCellSize + mCellSize * 0.5f; +} + +float GridSystem::getLeftWallX () const { + float halfW = mGridWidth * 0.5f; + return ( 0 - halfW ) * mCellSize + mCellSize * 0.5f; +} + +float GridSystem::getRightWallX () const { + float halfW = mGridWidth * 0.5f; + return ( mGridWidth - 1 - halfW ) * mCellSize + mCellSize * 0.5f; +} + +float GridSystem::getWallMinY () const { + return mFloorY; +} + +float GridSystem::getWallMaxY () const { + return mFloorY + mWallHeight * mCellSize; +} + +float GridSystem::getWallBaseY () const { + return mFloorY + mCellSize; +} +} // namespace scene |