added mesh class, added mesh trair

This commit is contained in:
2025-11-09 22:10:56 +03:00
parent e5d8e1c953
commit 6da44a5a51
8 changed files with 102 additions and 60 deletions

View File

@@ -5,52 +5,39 @@
#pragma once
#include "omath/engines/source_engine/traits/pred_engine_trait.hpp"
#include "omath/linear_algebra/vector3.hpp"
#include <omath/3d_primitives/mesh.hpp>
#include <vector>
namespace omath::collision
{
template<class NumericType = float>
template<class MeshType>
class MeshCollider
{
public:
using NumericType = MeshType::NumericType;
using VertexType = Vector3<NumericType>;
MeshCollider(const std::vector<VertexType>& vertexes, const VertexType& origin,
const VertexType& scale = {1.f, 1.f, 1.f})
: m_vertexes(vertexes), m_scale(scale), m_origin(origin)
explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh))
{
if (m_vertexes.empty())
throw std::runtime_error("Collider cannot have 0 vertexes");
}
std::vector<Vector3<float>> m_vertexes;
Vector3<float> m_scale;
Vector3<float> m_origin;
source_engine::ViewAngles m_rotation;
[[nodiscard]]
source_engine::Mat4X4 to_world() const
{
return mat_translation(m_origin) * mat_scale(m_scale) * source_engine::rotation_matrix(m_rotation);
}
[[nodiscard]]
const Vector3<float>& find_furthest_vertex(const Vector3<float>& direction) const
{
return *std::ranges::max_element(m_vertexes, [&direction](const auto& first, const auto& second)
{ return first.dot(direction) < second.dot(direction); });
return *std::ranges::max_element(m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
{
return first.dot(direction) < second.dot(direction);
});
}
[[nodiscard]]
Vector3<float> find_abs_furthest_vertex(const Vector3<float>& direction) const
{
return vertex_to_world_space(find_furthest_vertex(direction));
return m_mesh.vertex_to_world_space(find_furthest_vertex(direction));
}
[[nodiscard]] Vector3<float> vertex_to_world_space(const Vector3<float>& local_vertex) const
{
auto abs_vec = to_world() * mat_column_from_vector(local_vertex);
return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
}
private:
MeshType m_mesh;
};
} // namespace omath::collision