-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNavGrid.cpp
More file actions
87 lines (69 loc) · 2.05 KB
/
NavGrid.cpp
File metadata and controls
87 lines (69 loc) · 2.05 KB
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
#include "stdafx.h"
#include "NavGrid.h"
#include "NavNode.h"
namespace KE
{
NavGrid::NavGrid(Vector2i aGridSize, float aCellSize) : myGridSize(aGridSize), myCellSize(aCellSize)
{
}
bool NavGrid::Init(float aCellSize, Vector3f aMin, Vector3f aMax, std::vector<KE::NavNode>& someNodes)
{
myCellSize = aCellSize;
myMin = { aMin.x, aMin.z };
myMax = { aMax.x, aMax.z };
myGridSize = { (int)(abs(aMax.x - aMin.x) / myCellSize) + 1, (int)(abs(aMax.z - aMin.z) / myCellSize) + 1 };
myNavGrid.resize(myGridSize.x * myGridSize.y);
for (int i = 0; i < someNodes.size(); i++)
{
KE::NavNode& node = someNodes[i];
Vector3f min = { 1000000, 0.0f, 1000000 };
Vector3f max = { -1000000, 0.0f, -1000000 };
for (auto& vertex : node.vertices)
{
if (vertex->x < min.x) {
min.x = vertex->x;
}
if (vertex->z < min.z) {
min.z = vertex->z;
}
if (vertex->x > max.x) {
max.x = vertex->x;
}
if (vertex->z > max.z) {
max.z = vertex->z;
}
}
Vector2i minCord = GetCoordinate(min);
Vector2i maxCord = GetCoordinate(max);
int xSize = abs(maxCord.x - minCord.x) + 1;
int ySize = abs(maxCord.y - minCord.y) + 1;
for (int y = 0; y < ySize; y++)
{
for (int x = 0; x < xSize; x++)
{
int index = (minCord.x + x) + (minCord.y + y) * myGridSize.x;
myNavGrid[index].myData.push_back(&node);
}
}
}
return true;
}
Vector2i NavGrid::GetCoordinate(Vector3f aPos)
{
float relativeX = aPos.x - myMin.x;
float relativeY = aPos.z - myMin.y;
return Vector2i((int)(relativeX / myCellSize), (int)(relativeY / myCellSize));
}
int NavGrid::GetIndexByPos(Vector3f aPos)
{
Vector2i coordinate = GetCoordinate(aPos);
return coordinate.x + coordinate.y * myGridSize.x;
}
std::vector<KE::NavNode*> NavGrid::GetNodesByPos(Vector3f aPos)
{
int index = GetIndexByPos(aPos);
if (index < 0 || index >= myNavGrid.size())
return std::vector<KE::NavNode*>();
return myNavGrid[index].myData;
}
}