Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_collision_mesh_2d.hpp
1#pragma once
2
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"
8
9#include <functional>
10#include <vector>
11
12namespace spk
13{
14 class Transform2D;
15
20 class CollisionMesh2D : public spk::IMesh<spk::Vector2>
21 {
22 public:
27
34 CollisionMesh2D(const CollisionMesh2D &p_other);
45 CollisionMesh2D(CollisionMesh2D &&p_other) noexcept;
51 CollisionMesh2D &operator=(CollisionMesh2D &&p_other) noexcept;
52
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();
64 })
65 {
66 _subscribeBoundingBoxUpdates();
67
68 std::vector<spk::Vector2> points;
69 for (const auto &polygon : p_mesh.polygons())
70 {
71 for (const auto &vertex : polygon.points)
72 {
73 points.push_back(p_positionExtractor(vertex));
74 }
75 }
76
77 auto hull = _convexHull(std::move(points));
78 if (hull.size() < 3)
79 {
80 return;
81 }
82
83 Polygon collisionPolygon;
84 collisionPolygon.order = PolygonOrder::TriangleFanFromFirst;
85 collisionPolygon.points.reserve(hull.size());
86 for (const auto &point : hull)
87 {
88 collisionPolygon.points.push_back(point);
89 }
90 append(collisionPolygon);
91 }
92
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;
127 const spk::BoundingBox2D &boundingBox() const;
128
129 protected:
131 void _configureBufferSet() const override
132 {
133 auto &layout = bufferSet()->vbo().layout();
135 layout.addAttribute({0, Attribute::Type::Vector2});
136 }
137
138 private:
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);
141
142 spk::BoundingBox2D _computeBoundingBox() const;
143 void _subscribeBoundingBoxUpdates();
144
145 mutable spk::CachedData<spk::BoundingBox2D> _boundingBox;
146 EditionContract _onBoundingBoxEditionContract;
147 };
148}
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
Position, rotation, and scale component for 2D entities.
Definition spk_transform_2d.hpp:20
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