diff --git a/engine/physics/HitBox.cpp b/engine/physics/HitBox.cpp index 8bdfa4d..64564c1 100644 --- a/engine/physics/HitBox.cpp +++ b/engine/physics/HitBox.cpp @@ -2,28 +2,34 @@ // Created by Иван Ильин on 04.11.2021. // -#include +#include #include "HitBox.h" +#include "../Consts.h" -HitBox::HitBox(const Mesh &mesh) { - _hitBox.reserve(mesh.triangles().size() * 3); - for(const auto& t : mesh.triangles()) { - for(int i = 0; i < 3; i++) { - // we dont need to add the same points in hit box - _addIfUnique(Vec3D(t[i])); - } - } - _hitBox.shrink_to_fit(); +bool HitBox::Vec3DLess::operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept { + if (fabs(lhs.x() - rhs.x()) >= Consts::EPS) + return lhs.x() < rhs.x(); + else if (fabs(lhs.y() - rhs.y()) >= Consts::EPS) + return lhs.y() < rhs.y(); + else if (fabs(lhs.z() - rhs.z()) >= Consts::EPS) + return lhs.z() < rhs.z(); + else + 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 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.push_back(point); - } + _hitBox.reserve(points.size()); + for (const auto& it : points) + _hitBox.push_back(it); + _hitBox.shrink_to_fit(); } HitBox HitBox::Box(const Mesh &mesh) { diff --git a/engine/physics/HitBox.h b/engine/physics/HitBox.h index eda1e6e..c48fdcf 100644 --- a/engine/physics/HitBox.h +++ b/engine/physics/HitBox.h @@ -9,9 +9,11 @@ class HitBox final { private: - std::vector _hitBox; + struct Vec3DLess { + bool operator()(const Vec3D& lhs, const Vec3D& rhs) const noexcept; + }; - void _addIfUnique(Vec3D &&point); + std::vector _hitBox; public: HitBox() = default;