3#include "structure/geometry/spk_mesh.hpp"
4#include "structure/math/spk_bounding_box_2d.hpp"
5#include "structure/math/spk_matrix.hpp"
6#include "structure/math/spk_vector2.hpp"
7#include "structure/opengl/spk_vertex_buffer_object.hpp"
59 template <
typename TMesh>
60 explicit CollisionMesh2D(
const TMesh &p_mesh,
const std::function<spk::Vector2(
const typename TMesh::Vertex &)> &p_positionExtractor) :
61 spk::
IMesh<spk::Vector2>(),
62 _boundingBox([this]() {
63 return _computeBoundingBox();
66 _subscribeBoundingBoxUpdates();
68 std::vector<spk::Vector2> points;
69 for (
const auto &polygon : p_mesh.polygons())
71 for (
const auto &vertex : polygon.points)
73 points.push_back(p_positionExtractor(vertex));
77 auto hull = _convexHull(std::move(points));
84 collisionPolygon.order = PolygonOrder::TriangleFanFromFirst;
85 collisionPolygon.points.reserve(hull.size());
86 for (
const auto &point : hull)
88 collisionPolygon.points.push_back(point);
100 bool collideWith(
const CollisionMesh2D &p_other,
const Transform2D &p_selfTransform,
const Transform2D &p_otherTransform)
const;
107 bool collideWith(
const spk::Vector2 &p_worldPoint,
const Transform2D &p_selfTransform)
const;
115 bool collideWith(
const CollisionMesh2D &p_other,
const spk::Matrix4x4 &p_selfTransform,
const spk::Matrix4x4 &p_otherTransform)
const;
122 bool collideWith(
const spk::Vector2 &p_worldPoint,
const spk::Matrix4x4 &p_selfTransform)
const;
135 layout.addAttribute({0, Attribute::Type::Vector2});
139 static float _cross(
const spk::Vector2 &p_origin,
const spk::Vector2 &p_a,
const spk::Vector2 &p_b);
140 static std::vector<spk::Vector2> _convexHull(std::vector<spk::Vector2> p_points);
143 void _subscribeBoundingBoxUpdates();
Lazily generates and caches a value with optional custom destructor.
Definition spk_cached_data.hpp:26
const spk::BoundingBox2D & boundingBox() const
Returns the axis-aligned bounding box in local space.
Definition spk_collision_mesh_2d.cpp:323
spk::IMesh< spk::Vector2 >::Polygon Polygon
Polygon type used for collision triangles.
Definition spk_collision_mesh_2d.hpp:24
void _configureBufferSet() const override
Configures the vertex buffer layout for this mesh.
Definition spk_collision_mesh_2d.hpp:131
CollisionMesh2D(const TMesh &p_mesh, const std::function< spk::Vector2(const typename TMesh::Vertex &)> &p_positionExtractor)
Builds a convex collision mesh from another mesh type.
Definition spk_collision_mesh_2d.hpp:60
CollisionMesh2D & operator=(const CollisionMesh2D &p_other)
Copy-assigns another collision mesh.
Definition spk_collision_mesh_2d.cpp:28
spk::IMesh< spk::Vector2 >::EditionContract EditionContract
Contract type for mesh edition notifications.
Definition spk_collision_mesh_2d.hpp:26
CollisionMesh2D()
Creates an empty collision mesh.
Definition spk_collision_mesh_2d.cpp:10
bool collideWith(const CollisionMesh2D &p_other, const Transform2D &p_selfTransform, const Transform2D &p_otherTransform) const
Tests collision against another mesh using transform components.
Definition spk_collision_mesh_2d.cpp:259
Stores polygon data and exposes GPU-ready vertex/index buffers.
Definition spk_mesh.hpp:29
const std::shared_ptr< spk::OpenGL::BufferSetObject > & bufferSet() const
Definition spk_mesh.hpp:338
IMesh()
Definition spk_mesh.hpp:170
void append(const Polygon &p_shape)
Definition spk_mesh.hpp:270
ContractProvider::Contract EditionContract
Contract type returned by edition subscriptions.
Definition spk_mesh.hpp:38
IPolygon< TVertex > Polygon
Polygon type stored by the mesh.
Definition spk_mesh.hpp:34
VertexBufferObject & vbo()
Access mutable VBO.
Definition spk_buffer_set_object.cpp:76
Layout & layout()
Access layout descriptor.
Definition spk_vertex_buffer_object.cpp:253
Axis-aligned 2D bounding box defined by center, size, and extents.
Definition spk_bounding_box_2d.hpp:19
Describes a vertex attribute entry in the layout.
Definition spk_vertex_buffer_object.hpp:38