aboutsummaryrefslogtreecommitdiff
path: root/apps/openmb/scene/GridSystem.cpp
blob: 273cb027c4647fb8bb283966c9c456906a770cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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