Hitbox creation through set

master
Neirokan 2021-11-06 00:28:55 +03:00
parent 556ca037ec
commit f07f9a6638
2 changed files with 25 additions and 17 deletions

View File

@ -2,28 +2,34 @@
// Created by Иван Ильин on 04.11.2021. // Created by Иван Ильин on 04.11.2021.
// //
#include <algorithm> #include <set>
#include "HitBox.h" #include "HitBox.h"
#include "../Consts.h"
HitBox::HitBox(const Mesh &mesh) { bool HitBox::Vec3DLess::operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept {
_hitBox.reserve(mesh.triangles().size() * 3); if (fabs(lhs.x() - rhs.x()) >= Consts::EPS)
for(const auto& t : mesh.triangles()) { return lhs.x() < rhs.x();
for(int i = 0; i < 3; i++) { else if (fabs(lhs.y() - rhs.y()) >= Consts::EPS)
// we dont need to add the same points in hit box return lhs.y() < rhs.y();
_addIfUnique(Vec3D(t[i])); else if (fabs(lhs.z() - rhs.z()) >= Consts::EPS)
} return lhs.z() < rhs.z();
} else
_hitBox.shrink_to_fit(); return false;
} }
void HitBox::_addIfUnique(Vec3D &&point) { HitBox::HitBox(const Mesh& mesh) {
// we dont need to add the same points in hit box
std::set<Vec3D, HitBox::Vec3DLess> points;
auto check = [&point](const auto& p) { return p == point; }; for (const auto& t : mesh.triangles())
for (int i = 0; i < 3; i++)
points.insert(Vec3D(t[i]));
if (std::find_if(_hitBox.rbegin(), _hitBox.rend(), check) == _hitBox.rend()) { _hitBox.reserve(points.size());
_hitBox.push_back(point); for (const auto& it : points)
} _hitBox.push_back(it);
_hitBox.shrink_to_fit();
} }
HitBox HitBox::Box(const Mesh &mesh) { HitBox HitBox::Box(const Mesh &mesh) {

View File

@ -9,9 +9,11 @@
class HitBox final { class HitBox final {
private: private:
std::vector<Vec3D> _hitBox; struct Vec3DLess {
bool operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept;
};
void _addIfUnique(Vec3D &&point); std::vector<Vec3D> _hitBox;
public: public:
HitBox() = default; HitBox() = default;