worked on navMesh stuff
- creation - walking - helper
This commit is contained in:
@@ -5,46 +5,66 @@
|
||||
#include <vector>
|
||||
#include "../math/DrawList.h"
|
||||
#include "../geo/Point3.h"
|
||||
|
||||
#include "NavMeshLocation.h"
|
||||
|
||||
template <typename Tria> class NavMeshRandom {
|
||||
namespace NM {
|
||||
|
||||
std::minstd_rand gen;
|
||||
std::uniform_real_distribution<float> dOnTriangle = std::uniform_real_distribution<float>(0.0f, 1.0f);
|
||||
const std::vector<Tria*>& triangles;
|
||||
DrawList<size_t> lst;
|
||||
/**
|
||||
* randomly pick points within the area of the nav-mesh.
|
||||
* points are picked evenly:
|
||||
* bigger triangles are used more often
|
||||
*
|
||||
*/
|
||||
template <typename Tria> class NavMeshRandom {
|
||||
|
||||
public:
|
||||
DrawList<size_t> lst;
|
||||
std::minstd_rand gen;
|
||||
std::uniform_real_distribution<float> dOnTriangle = std::uniform_real_distribution<float>(0.0f, 1.0f);
|
||||
std::vector<const Tria*> triangles;
|
||||
|
||||
|
||||
uint32_t nextSeed() {
|
||||
static uint32_t seed = 0;
|
||||
return ++seed;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/** ctor (const/non-const using T) */
|
||||
template <typename T> NavMeshRandom(const std::vector<T*>& srcTriangles) : lst(nextSeed()), gen(nextSeed()) {
|
||||
|
||||
// almost always the same number?!
|
||||
gen();
|
||||
|
||||
// construct a DrawList (probability = size[area] of the triangle
|
||||
// bigger triangles must be choosen more often
|
||||
for (size_t idx = 0; idx < srcTriangles.size(); ++idx) {
|
||||
this->triangles.push_back(srcTriangles[idx]);
|
||||
this->lst.add(idx, srcTriangles[idx]->getArea());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** draw a random point */
|
||||
NavMeshLocation<Tria> draw() {
|
||||
|
||||
// pick a random triangle to draw from
|
||||
const size_t idx = lst.get();
|
||||
const Tria* tria = triangles[idx];
|
||||
|
||||
while (true) {
|
||||
const float u = dOnTriangle(gen);
|
||||
const float v = dOnTriangle(gen);
|
||||
if ((u+v) > 1) {continue;}
|
||||
const Point3 pos = tria->getPoint(u,v); //tria->getA() + (tria.getAB() * u) + (tria.getAC() * v);
|
||||
return NavMeshLocation<Tria>(pos, tria);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Result {
|
||||
Point3 pos;
|
||||
size_t triaIdx;
|
||||
Result(const Point3 pos, const size_t triaIdx) : pos(pos), triaIdx(triaIdx) {;}
|
||||
};
|
||||
|
||||
/** ctor */
|
||||
NavMeshRandom(const std::vector<Tria*>& triangles) : triangles(triangles) {
|
||||
for (size_t idx = 0; idx < triangles.size(); ++idx) {
|
||||
lst.add(idx, triangles[idx]->getArea());
|
||||
}
|
||||
}
|
||||
|
||||
/** draw a random point within the map */
|
||||
NavMeshLocation<Tria> draw() {
|
||||
|
||||
const size_t idx = lst.get();
|
||||
const Tria* tria = triangles[idx];
|
||||
|
||||
while (true) {
|
||||
const float u = dOnTriangle(gen);
|
||||
const float v = dOnTriangle(gen);
|
||||
if (u+v > 1) {continue;}
|
||||
const Point3 pos = tria.getA() + (tria.getAB() * u) + (tria.getAC() * v);
|
||||
return NavMeshLocation<Tria>(pos, tria);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAVMESHRANDOM_H
|
||||
|
||||
Reference in New Issue
Block a user