mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
Merge pull request #109 from orange-cpp/feature/collider_interface
Feature/collider interface
This commit is contained in:
@@ -99,10 +99,10 @@ namespace omath::primitives
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[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>
|
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)};
|
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
|
Triangle<VectorType> make_face_in_world_space(const Ebo::const_iterator vao_iterator) const
|
||||||
requires HasPosition<VertexType>
|
requires HasPosition<VertexType>
|
||||||
{
|
{
|
||||||
return {vertex_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
|
return {vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->x).position),
|
||||||
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
|
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->y).position),
|
||||||
vertex_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
|
vertex_position_to_world_space(m_vertex_buffer.at(vao_iterator->z).position)};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
23
include/omath/collision/collider_interface.hpp
Normal file
23
include/omath/collision/collider_interface.hpp
Normal 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;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -6,10 +6,10 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <memory_resource>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory_resource>
|
|
||||||
|
|
||||||
namespace omath::collision
|
namespace omath::collision
|
||||||
{
|
{
|
||||||
@@ -23,16 +23,16 @@ namespace omath::collision
|
|||||||
{ a / s } -> std::same_as<V>;
|
{ a / s } -> std::same_as<V>;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class ColliderType>
|
template<class ColliderInterfaceType>
|
||||||
class Epa final
|
class Epa final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using VectorType = ColliderType::VectorType;
|
using VectorType = ColliderInterfaceType::VectorType;
|
||||||
static_assert(EpaVector<VectorType>, "VertexType must satisfy EpaVector concept");
|
static_assert(EpaVector<VectorType>, "VertexType must satisfy EpaVector concept");
|
||||||
|
|
||||||
struct Result final
|
struct Result final
|
||||||
{
|
{
|
||||||
VectorType normal{}; // outward normal (from B to A)
|
VectorType normal{}; // from A to B
|
||||||
VectorType penetration_vector;
|
VectorType penetration_vector;
|
||||||
float depth{0.0f};
|
float depth{0.0f};
|
||||||
int iterations{0};
|
int iterations{0};
|
||||||
@@ -48,7 +48,7 @@ namespace omath::collision
|
|||||||
|
|
||||||
// Precondition: simplex.size()==4 and contains the origin.
|
// Precondition: simplex.size()==4 and contains the origin.
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static std::optional<Result> solve(const ColliderType& a, const ColliderType& b,
|
static std::optional<Result> solve(const ColliderInterfaceType& a, const ColliderInterfaceType& b,
|
||||||
const Simplex<VectorType>& simplex, const Params params = {},
|
const Simplex<VectorType>& simplex, const Params params = {},
|
||||||
std::shared_ptr<std::pmr::memory_resource> mem_resource = {
|
std::shared_ptr<std::pmr::memory_resource> mem_resource = {
|
||||||
std::shared_ptr<void>{}, std::pmr::get_default_resource()})
|
std::shared_ptr<void>{}, std::pmr::get_default_resource()})
|
||||||
@@ -86,25 +86,22 @@ namespace omath::collision
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
const int fidx = heap.top().idx;
|
const int fidx = heap.top().idx;
|
||||||
const Face f = faces[fidx];
|
const Face face = faces[fidx];
|
||||||
|
|
||||||
// Get the furthest point in face normal direction
|
// Get the furthest point in face normal direction
|
||||||
const VectorType p = support_point(a, b, f.n);
|
const VectorType p = support_point(a, b, face.n);
|
||||||
const float p_dist = f.n.dot(p);
|
const float p_dist = face.n.dot(p);
|
||||||
|
|
||||||
// Converged if we can’t push the face closer than tolerance
|
// Converged if we can’t push the face closer than tolerance
|
||||||
if (p_dist - f.d <= params.tolerance)
|
if (p_dist - face.d <= params.tolerance)
|
||||||
{
|
{
|
||||||
out.normal = f.n;
|
out.normal = face.n;
|
||||||
out.depth = f.d; // along unit normal
|
out.depth = face.d; // along unit normal
|
||||||
out.iterations = it + 1;
|
out.iterations = it + 1;
|
||||||
out.num_vertices = static_cast<int>(vertexes.size());
|
out.num_vertices = static_cast<int>(vertexes.size());
|
||||||
out.num_faces = static_cast<int>(faces.size());
|
out.num_faces = static_cast<int>(faces.size());
|
||||||
|
|
||||||
const auto centers = b.get_origin() - a.get_origin();
|
out.penetration_vector = out.normal * out.depth;
|
||||||
const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1;
|
|
||||||
|
|
||||||
out.penetration_vector = out.normal * out.depth * sign;
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,10 +160,7 @@ namespace omath::collision
|
|||||||
out.num_vertices = static_cast<int>(vertexes.size());
|
out.num_vertices = static_cast<int>(vertexes.size());
|
||||||
out.num_faces = static_cast<int>(faces.size());
|
out.num_faces = static_cast<int>(faces.size());
|
||||||
|
|
||||||
const auto centers = b.get_origin() - a.get_origin();
|
out.penetration_vector = out.normal * out.depth;
|
||||||
const auto sign = out.normal.dot(centers) >= 0 ? 1 : -1;
|
|
||||||
|
|
||||||
out.penetration_vector = out.normal * out.depth * sign;
|
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -251,9 +245,10 @@ namespace omath::collision
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static VectorType support_point(const ColliderType& a, const ColliderType& b, const VectorType& dir)
|
static VectorType support_point(const ColliderInterfaceType& a, const ColliderInterfaceType& 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>
|
template<class V>
|
||||||
|
|||||||
@@ -14,28 +14,29 @@ namespace omath::collision
|
|||||||
Simplex<VertexType> simplex; // valid only if hit == true and size==4
|
Simplex<VertexType> simplex; // valid only if hit == true and size==4
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class ColliderType>
|
template<class ColliderInterfaceType>
|
||||||
class GjkAlgorithm final
|
class GjkAlgorithm final
|
||||||
{
|
{
|
||||||
using VertexType = ColliderType::VertexType;
|
using VectorType = ColliderInterfaceType::VectorType;
|
||||||
using VectorType = VertexType::VectorType;
|
|
||||||
public:
|
public:
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static VectorType find_support_vertex(const ColliderType& collider_a, const ColliderType& collider_b,
|
static VectorType find_support_vertex(const ColliderInterfaceType& collider_a,
|
||||||
const VectorType& direction)
|
const ColliderInterfaceType& 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]]
|
[[nodiscard]]
|
||||||
static bool is_collide(const ColliderType& collider_a, const ColliderType& collider_b)
|
static bool is_collide(const ColliderInterfaceType& collider_a, const ColliderInterfaceType& collider_b)
|
||||||
{
|
{
|
||||||
return is_collide_with_simplex_info(collider_a, collider_b).hit;
|
return is_collide_with_simplex_info(collider_a, collider_b).hit;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
static GjkHitInfo<VectorType> is_collide_with_simplex_info(const ColliderType& collider_a,
|
static GjkHitInfo<VectorType> is_collide_with_simplex_info(const ColliderInterfaceType& collider_a,
|
||||||
const ColliderType& collider_b)
|
const ColliderInterfaceType& collider_b)
|
||||||
{
|
{
|
||||||
auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});
|
auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});
|
||||||
|
|
||||||
|
|||||||
@@ -3,20 +3,46 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "collider_interface.hpp"
|
||||||
#include "omath/linear_algebra/vector3.hpp"
|
#include "omath/linear_algebra/vector3.hpp"
|
||||||
|
|
||||||
|
#ifdef OMATH_BUILD_TESTS
|
||||||
|
// ReSharper disable once CppInconsistentNaming
|
||||||
|
class UnitTestColider_FindFurthestVertex_Test;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace omath::collision
|
namespace omath::collision
|
||||||
{
|
{
|
||||||
template<class MeshType>
|
template<class MeshType>
|
||||||
class MeshCollider
|
class MeshCollider final : public ColliderInterface<typename MeshType::VertexType::VectorType>
|
||||||
{
|
{
|
||||||
|
#ifdef OMATH_BUILD_TESTS
|
||||||
|
friend UnitTestColider_FindFurthestVertex_Test;
|
||||||
|
#endif
|
||||||
public:
|
public:
|
||||||
using VertexType = MeshType::VertexType;
|
using VertexType = MeshType::VertexType;
|
||||||
using VectorType = VertexType::VectorType;
|
using VectorType = MeshType::VertexType::VectorType;
|
||||||
explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh))
|
explicit MeshCollider(MeshType mesh): m_mesh(std::move(mesh))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[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]]
|
[[nodiscard]]
|
||||||
const VertexType& find_furthest_vertex(const VectorType& direction) const
|
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)
|
m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
|
||||||
{ return first.position.dot(direction) < second.position.dot(direction); });
|
{ 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;
|
MeshType m_mesh;
|
||||||
};
|
};
|
||||||
} // namespace omath::collision
|
} // namespace omath::collision
|
||||||
@@ -46,7 +46,7 @@ namespace omath
|
|||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr static MatStoreType get_store_ordering() noexcept
|
consteval static MatStoreType get_store_ordering() noexcept
|
||||||
{
|
{
|
||||||
return StoreType;
|
return StoreType;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ TEST(UnitTestColider, CheckToWorld)
|
|||||||
mesh.set_origin({0, 2, 0});
|
mesh.set_origin({0, 2, 0});
|
||||||
const omath::source_engine::MeshCollider collider(mesh);
|
const omath::source_engine::MeshCollider collider(mesh);
|
||||||
|
|
||||||
const auto vertex = collider.find_abs_furthest_vertex({1.f, 0.f, 0.f}).position;
|
const auto vertex = collider.find_abs_furthest_vertex_position({1.f, 0.f, 0.f});
|
||||||
|
|
||||||
EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 3.f, 1.f));
|
EXPECT_EQ(vertex, omath::Vector3<float>(1.f, 3.f, 1.f));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ TEST(UnitTestEpa, TestCollisionTrue)
|
|||||||
|
|
||||||
// Try both signs with a tiny margin (avoid grazing contacts)
|
// Try both signs with a tiny margin (avoid grazing contacts)
|
||||||
const float margin = 1.0f + 1e-3f;
|
const float margin = 1.0f + 1e-3f;
|
||||||
const auto pen = epa->normal * epa->depth;
|
const auto pen = epa->penetration_vector;
|
||||||
|
|
||||||
Mesh b_plus = b;
|
Mesh b_plus = b;
|
||||||
b_plus.set_origin(b_plus.get_origin() + pen * margin);
|
b_plus.set_origin(b_plus.get_origin() + pen * margin);
|
||||||
@@ -133,12 +133,8 @@ TEST(UnitTestEpa, TestCollisionTrue2)
|
|||||||
EXPECT_NEAR(epa->normal.y, 0.0f, 1e-3f);
|
EXPECT_NEAR(epa->normal.y, 0.0f, 1e-3f);
|
||||||
EXPECT_NEAR(epa->normal.z, 0.0f, 1e-3f);
|
EXPECT_NEAR(epa->normal.z, 0.0f, 1e-3f);
|
||||||
|
|
||||||
// Choose a deterministic sign: orient penetration from A toward B
|
|
||||||
const auto centers = b.get_origin() - a.get_origin(); // (0.5, 0, 0)
|
|
||||||
float sign = (epa->normal.dot(centers) >= 0.0f) ? +1.0f : -1.0f;
|
|
||||||
|
|
||||||
constexpr float margin = 1.0f + 1e-3f; // tiny slack to avoid grazing
|
constexpr float margin = 1.0f + 1e-3f; // tiny slack to avoid grazing
|
||||||
const auto pen = epa->normal * epa->depth * sign;
|
const auto pen = epa->normal * epa->depth;
|
||||||
|
|
||||||
// Apply once: B + pen must separate; the opposite must still collide
|
// Apply once: B + pen must separate; the opposite must still collide
|
||||||
Mesh b_resolved = b;
|
Mesh b_resolved = b;
|
||||||
|
|||||||
Reference in New Issue
Block a user