worked on grid-walking
worked on grid-generation added helper library for nav-meshes started working on nav meshes
This commit is contained in:
49
navMesh/NavMeshRandom.h
Normal file
49
navMesh/NavMeshRandom.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user