#include "GridSystem.hpp" #include 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( std::floor( localX ) ); outGridZ = static_cast( 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