added collider interface

This commit is contained in:
2025-12-06 13:34:34 +03:00
parent e97be8c142
commit e05eba42c3
9 changed files with 118 additions and 43 deletions

View File

@@ -99,10 +99,10 @@ namespace omath::primitives
}
[[nodiscard]]
VectorType vertex_to_world_space(const Vector3<float>& vertex_position) const
VectorType vertex_position_to_world_space(const Vector3<float>& vertex_position) const
requires HasPosition<VertexType>
{
auto abs_vec = get_to_world_matrix() * mat_column_from_vector(vertex_position);
auto abs_vec = get_to_world_matrix() * mat_column_from_vector<typename Mat4X4::ContainedType, Mat4X4::get_store_ordering()>(vertex_position);
return {abs_vec.at(0, 0), abs_vec.at(1, 0), abs_vec.at(2, 0)};
}
@@ -111,9 +111,9 @@ namespace omath::primitives
Triangle<VectorType> make_face_in_world_space(const Ebo::const_iterator vao_iterator) const
requires HasPosition<VertexType>
{
return {vertex_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
}
private:

View File

@@ -0,0 +1,23 @@
//
// Created by Vladislav on 06.12.2025.
//
#pragma once
namespace omath::collision
{
template<class VecType = Vector3<float>>
class ColliderInterface
{
public:
using VectorType = VecType;
virtual ~ColliderInterface() = default;
[[nodiscard]]
virtual VectorType find_abs_furthest_vertex_position(const VectorType& direction) const = 0;
[[nodiscard]]
virtual const VectorType& get_origin() const = 0;
virtual void set_origin(const VectorType& new_origin) = 0;
};
}

View File

@@ -101,10 +101,7 @@ namespace omath::collision
out.num_vertices = static_cast<int>(vertexes.size());
out.num_faces = static_cast<int>(faces.size());
const auto centers = b.get_origin() - a.get_origin();
const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1;
out.penetration_vector = out.normal * out.depth * sign;
out.penetration_vector = out.normal * out.depth;
return out;
}
@@ -163,10 +160,7 @@ namespace omath::collision
out.num_vertices = static_cast<int>(vertexes.size());
out.num_faces = static_cast<int>(faces.size());
const auto centers = b.get_origin() - a.get_origin();
const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1;
out.penetration_vector = out.normal * out.depth * sign;
out.penetration_vector = out.normal * out.depth;
return out;
}
@@ -253,7 +247,7 @@ namespace omath::collision
[[nodiscard]]
static VectorType support_point(const ColliderType& a, const ColliderType& b, const VectorType& dir)
{
return a.find_abs_furthest_vertex(dir).position - b.find_abs_furthest_vertex(-dir).position;
return a.find_abs_furthest_vertex_position(dir) - b.find_abs_furthest_vertex_position(-dir);
}
template<class V>

View File

@@ -17,14 +17,15 @@ namespace omath::collision
template<class ColliderType>
class GjkAlgorithm final
{
using VertexType = ColliderType::VertexType;
using VectorType = VertexType::VectorType;
using VectorType = ColliderType::VectorType;
public:
[[nodiscard]]
static VectorType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b,
const VectorType& direction)
{
return collider_a.find_abs_furthest_vertex(direction).position - collider_b.find_abs_furthest_vertex(-direction).position;
return collider_a.find_abs_furthest_vertex_position(direction)
- collider_b.find_abs_furthest_vertex_position(-direction);
}
[[nodiscard]]

View File

@@ -3,13 +3,22 @@
//
#pragma once
#include "collider_interface.hpp"
#include "omath/linear_algebra/vector3.hpp"
#ifdef OMATH_BUILD_TESTS
// ReSharper disable once CppInconsistentNaming
class UnitTestColider_FindFurthestVertex_Test;
#endif
namespace omath::collision
{
template<class MeshType>
class MeshCollider
class MeshCollider final : public ColliderInterface<omath::Vector3<float>>
{
#ifdef OMATH_BUILD_TESTS
friend UnitTestColider_FindFurthestVertex_Test;
#endif
public:
using VertexType = MeshType::VertexType;
using VectorType = VertexType::VectorType;
@@ -17,6 +26,23 @@ namespace omath::collision
{
}
[[nodiscard]]
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
{
return m_mesh.vertex_position_to_world_space(find_furthest_vertex(direction).position);
}
[[nodiscard]]
const VectorType& get_origin() const override
{
return m_mesh.get_origin();
}
void set_origin(const VectorType& new_origin) override
{
m_mesh.set_origin(new_origin);
}
private:
[[nodiscard]]
const VertexType& find_furthest_vertex(const VectorType& direction) const
{
@@ -24,23 +50,6 @@ namespace omath::collision
m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
{ return first.position.dot(direction) < second.position.dot(direction); });
}
[[nodiscard]]
VertexType find_abs_furthest_vertex(const VectorType& direction) const
{
const auto& vertex = find_furthest_vertex(direction);
auto new_vertex = vertex;
new_vertex.position = m_mesh.vertex_to_world_space(find_furthest_vertex(direction).position);
return new_vertex;
}
[[nodiscard]]
const VectorType& get_origin() const
{
return m_mesh.get_origin();
}
private:
MeshType m_mesh;
};
} // namespace omath::collision

View File

@@ -0,0 +1,52 @@
//
// Created by Vlad on 11/9/2025.
//
#pragma once
#include "omath/linear_algebra/vector3.hpp"
#include <omath/linear_algebra/triangle.hpp>
namespace omath::collision
{
struct TriangleCollider
{
Vector3<float> position;
};
class TriangleMeshCollider final : public ColliderInterface<Vector3<float>>
{
public:
using VectorType = Vector3<float>;
using VertexType = TriangleCollider;
explicit TriangleMeshCollider(const Triangle<Vector3<float>>& triangle)
: m_mesh({triangle.m_vertex1, triangle.m_vertex2, triangle.m_vertex3})
{
}
[[nodiscard]]
VectorType find_abs_furthest_vertex_position(const VectorType& direction) const override
{
return find_furthest_vertex(direction).position;
}
[[nodiscard]]
const VectorType& get_origin() const override
{
return m_origin;
}
void set_origin(const VectorType&) override
{
}
private:
[[nodiscard]]
const VertexType& find_furthest_vertex(const VectorType& direction) const
{
return *std::ranges::max_element(
m_mesh, [&direction](const auto& first, const auto& second)
{ return first.position.dot(direction) < second.position.dot(direction); });
}
Vector3<float> m_origin{};
std::array<VertexType, 3> m_mesh;
};
} // namespace omath::collision

View File

@@ -46,7 +46,7 @@ namespace omath
}
[[nodiscard]]
constexpr static MatStoreType get_store_ordering() noexcept
consteval static MatStoreType get_store_ordering() noexcept
{
return StoreType;
}