worked on grid-generation added helper library for nav-meshes started working on nav meshes
50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
#ifndef NAVMESHRANDOM_H
|
|
#define NAVMESHRANDOM_H
|
|
|
|
#include <random>
|
|
#include <vector>
|
|
#include "../math/DrawList.h"
|
|
#include "../geo/Point3.h"
|
|
|
|
template <typename Tria> class NavMeshRandom {
|
|
|
|
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;
|
|
|
|
public:
|
|
|
|
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 */
|
|
Result 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 Result(pos, idx);
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif // NAVMESHRANDOM_H
|