worked on navMesh stuff
- creation - walking - helper
This commit is contained in:
@@ -9,101 +9,113 @@
|
||||
#include "NavMeshRandom.h"
|
||||
#include "NavMeshLocation.h"
|
||||
|
||||
template <typename Tria> class NavMesh {
|
||||
namespace NM {
|
||||
|
||||
/** all triangles within the mesh */
|
||||
std::vector<Tria*> triangles;
|
||||
template <typename Tria> class NavMesh {
|
||||
|
||||
BBox3 bbox;
|
||||
/** all triangles within the mesh */
|
||||
std::vector<Tria*> triangles;
|
||||
|
||||
public:
|
||||
BBox3 bbox;
|
||||
|
||||
NavMesh() {
|
||||
public:
|
||||
|
||||
}
|
||||
/** ctor */
|
||||
NavMesh() {
|
||||
|
||||
/** the overall bounding-box */
|
||||
const BBox3 getBBox() const {
|
||||
return bbox;
|
||||
}
|
||||
|
||||
/** add a new triangle */
|
||||
void add(const Point3 p1, const Point3 p2, const Point3 p3, const uint8_t type) {
|
||||
triangles.push_back(new Tria(p1,p2,p3,type));
|
||||
bbox.add(p1);
|
||||
bbox.add(p2);
|
||||
bbox.add(p3);
|
||||
}
|
||||
|
||||
NavMeshLocation<Tria> getLocation(const Point3 pos) {
|
||||
for (const Tria* tria : triangles) {
|
||||
if (tria->contains(pos)) {
|
||||
return NavMeshLocation<Tria>(pos, tria);
|
||||
}
|
||||
}
|
||||
throw Exception("location not found");
|
||||
}
|
||||
|
||||
/** connect both triangles */
|
||||
void connectBiDir(int idx1, int idx2) {
|
||||
connectUniDir(idx1,idx2);
|
||||
connectUniDir(idx2,idx1);
|
||||
}
|
||||
/** dtor */
|
||||
~NavMesh() {
|
||||
for (const Tria* t : triangles) {delete t;}
|
||||
triangles.clear();
|
||||
}
|
||||
|
||||
/** connect both triangles */
|
||||
void connectUniDir(int idxFrom, int idxTo) {
|
||||
NavMeshTriangle* tria = triangles[idxFrom];
|
||||
tria->_neighbors[tria->_numNeighbors] = triangles[idxTo];
|
||||
}
|
||||
/** the overall bounding-box */
|
||||
const BBox3 getBBox() const {
|
||||
return bbox;
|
||||
}
|
||||
|
||||
/** allows for-each iteration over all included triangles */
|
||||
decltype(triangles.begin()) begin() {return triangles.begin();}
|
||||
/** add a new triangle */
|
||||
void add(const Point3 p1, const Point3 p2, const Point3 p3, const uint8_t type) {
|
||||
triangles.push_back(new Tria(p1,p2,p3,type));
|
||||
bbox.add(p1);
|
||||
bbox.add(p2);
|
||||
bbox.add(p3);
|
||||
}
|
||||
|
||||
/** allows for-each iteration over all included triangles */
|
||||
decltype(triangles.end()) end() {return triangles.end();}
|
||||
/** get the triangle this point belongs to (if any) */
|
||||
NavMeshLocation<Tria> getLocation(const Point3 pos) {
|
||||
for (const Tria* tria : triangles) {
|
||||
if (tria->contains(pos)) {
|
||||
return NavMeshLocation<Tria>(pos, tria);
|
||||
}
|
||||
}
|
||||
throw Exception("location not found within NavMesh: " + pos.asString());
|
||||
}
|
||||
|
||||
/** array access */
|
||||
Tria* operator [] (const size_t idx) {
|
||||
Assert::isBetween(idx, (size_t)0, getNumTriangles()-1, "index out of bounds");
|
||||
return triangles[idx];
|
||||
}
|
||||
/** connect both triangles */
|
||||
void connectBiDir(int idx1, int idx2) {
|
||||
connectUniDir(idx1,idx2);
|
||||
connectUniDir(idx2,idx1);
|
||||
}
|
||||
|
||||
/** get the number of triangles used */
|
||||
size_t getNumTriangles() const {
|
||||
return triangles.size();
|
||||
}
|
||||
/** connect both triangles */
|
||||
void connectUniDir(int idxFrom, int idxTo) {
|
||||
Tria* tria = triangles[idxFrom];
|
||||
tria->addNeighbor(triangles[idxTo]);
|
||||
}
|
||||
|
||||
/** ---------------- MISC ---------------- */
|
||||
/** allows for-each iteration over all included triangles */
|
||||
decltype(triangles.begin()) begin() {return triangles.begin();}
|
||||
|
||||
/** allows for-each iteration over all included triangles */
|
||||
decltype(triangles.end()) end() {return triangles.end();}
|
||||
|
||||
/** array access */
|
||||
Tria* operator [] (const size_t idx) {
|
||||
Assert::isBetween(idx, (size_t)0, getNumTriangles()-1, "index out of bounds");
|
||||
return triangles[idx];
|
||||
}
|
||||
|
||||
/** get the number of triangles used */
|
||||
size_t getNumTriangles() const {
|
||||
return triangles.size();
|
||||
}
|
||||
|
||||
/** ---------------- MISC ---------------- */
|
||||
|
||||
|
||||
NavMeshRandom<Tria> getRandomizer() {
|
||||
return NavMeshRandom<Tria>(triangles);
|
||||
}
|
||||
NavMeshRandom<Tria> getRandom() {
|
||||
return NavMeshRandom<Tria>(triangles);
|
||||
}
|
||||
|
||||
// /** ---------------- NEIGHBORS ---------------- */
|
||||
// /** ---------------- NEIGHBORS ---------------- */
|
||||
|
||||
// /** get the number of neighbors for the given element */
|
||||
// int getNumNeighbors(const size_t idx) const {
|
||||
// return getNumNeighbors(triangles[idx]);
|
||||
// }
|
||||
// /** get the number of neighbors for the given element */
|
||||
// int getNumNeighbors(const size_t idx) const {
|
||||
// return getNumNeighbors(triangles[idx]);
|
||||
// }
|
||||
|
||||
// /** get the number of neighbors for the given element */
|
||||
// int getNumNeighbors(const Tria& e) const {
|
||||
// return e._numNeighbors;
|
||||
// }
|
||||
// /** get the number of neighbors for the given element */
|
||||
// int getNumNeighbors(const Tria& e) const {
|
||||
// return e._numNeighbors;
|
||||
// }
|
||||
|
||||
// /** get the n-th neighbor for the given node */
|
||||
// Tria& getNeighbor(const size_t nodeIdx, const size_t nth) const {
|
||||
// const Tria& node = triangles[nodeIdx];
|
||||
// return getNeighbor(node, nth);
|
||||
// }
|
||||
// /** get the n-th neighbor for the given node */
|
||||
// Tria& getNeighbor(const size_t nodeIdx, const size_t nth) const {
|
||||
// const Tria& node = triangles[nodeIdx];
|
||||
// return getNeighbor(node, nth);
|
||||
// }
|
||||
|
||||
// /** get the n-th neighbor for the given node */
|
||||
// Tria& getNeighbor(const Tria& tria, const size_t nth) const {
|
||||
// const Tria& neighbor = triangles[tria._neighbors[nth]];
|
||||
// return (Tria&) neighbor;
|
||||
// }
|
||||
// /** get the n-th neighbor for the given node */
|
||||
// Tria& getNeighbor(const Tria& tria, const size_t nth) const {
|
||||
// const Tria& neighbor = triangles[tria._neighbors[nth]];
|
||||
// return (Tria&) neighbor;
|
||||
// }
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user