some refactorings/fixes

worked on nav-mesh stuff
new tests
This commit is contained in:
k-a-z-u
2018-01-24 11:27:11 +01:00
parent c9d02d440a
commit 1a1f249e9b
10 changed files with 70 additions and 264 deletions

View File

@@ -45,7 +45,7 @@ namespace NM {
}
/** get the triangle this point belongs to (if any) */
NavMeshLocation<Tria> getLocation(const Point3 pos) {
NavMeshLocation<Tria> getLocation(const Point3 pos) const {
for (const Tria* tria : triangles) {
if (tria->contains(pos)) {
return NavMeshLocation<Tria>(pos, tria);
@@ -54,6 +54,19 @@ namespace NM {
throw Exception("location not found within NavMesh: " + pos.asString());
}
/** get the triangle/point on the mesh that is nearest to the given location */
NavMeshLocation<Tria> getLocationNearestTo(const Point3 pos) const {
auto comp = [pos] (const Tria* t1, const Tria* t2) {
//return t1->getCenter().getDistance(pos) < t2->getCenter().getDistance(pos);
return t1->getDistanceApx(pos) < t2->getDistanceApx(pos);
};
auto it = std::min_element(triangles.begin(), triangles.end(), comp);
const Tria* best = *it;
Point3 rPos = best->toPoint3Near(pos.xy());
return NavMeshLocation<Tria>(rPos, best);
}
/** connect both triangles */
void connectBiDir(int idx1, int idx2) {
connectUniDir(idx1,idx2);
@@ -86,7 +99,8 @@ namespace NM {
/** ---------------- MISC ---------------- */
NavMeshRandom<Tria> getRandom() {
/** get a random-generator for several mesh-actions */
NavMeshRandom<Tria> getRandom() const {
return NavMeshRandom<Tria>(triangles);
}