From 54409423f767d8b1cf30cb7d0efca6b4ca138823 Mon Sep 17 00:00:00 2001 From: Ethan Morgan Date: Sat, 14 Feb 2026 16:44:06 +0000 Subject: move to own git server --- apps/openmb/scene/GridSystem.cpp | 121 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 apps/openmb/scene/GridSystem.cpp (limited to 'apps/openmb/scene/GridSystem.cpp') 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 + +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 -- cgit v1.2.3