Antkeeper  0.0.1
mesh-collider.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_PHYSICS_MESH_COLLIDER_HPP
21 #define ANTKEEPER_PHYSICS_MESH_COLLIDER_HPP
22 
25 #include <engine/geom/bvh/bvh.hpp>
26 #include <engine/math/vector.hpp>
28 #include <memory>
29 #include <optional>
30 
31 namespace physics {
32 
36 class mesh_collider: public collider
37 {
38 public:
41 
44 
45  [[nodiscard]] inline constexpr collider_type type() const noexcept override
46  {
47  return collider_type::mesh;
48  }
49 
58  explicit mesh_collider(std::shared_ptr<mesh_type> mesh);
59 
61  constexpr mesh_collider() noexcept = default;
62 
71  void set_mesh(std::shared_ptr<mesh_type> mesh);
72 
74  [[nodiscard]] inline constexpr const std::shared_ptr<mesh_type>& get_mesh() const noexcept
75  {
76  return m_mesh;
77  }
78 
80  [[nodiscard]] inline constexpr const bvh_type& get_bvh() const noexcept
81  {
82  return m_bvh;
83  }
84 
86  void rebuild_bvh();
87 
95  [[nodiscard]] std::optional<std::tuple<float, std::uint32_t, math::fvec3>> intersection(const geom::ray<float, 3>& ray) const;
96 
97 private:
98  std::shared_ptr<mesh_type> m_mesh;
99  const geom::brep_attribute<math::fvec3>* m_vertex_positions{};
100  const geom::brep_attribute<math::fvec3>* m_face_normals{};
101  bvh_type m_bvh;
102 };
103 
104 } // namespace physics
105 
106 #endif // ANTKEEPER_PHYSICS_MESH_COLLIDER_HPP
Per-element B-rep data.
Boundary representation (B-rep) of a mesh.
Definition: brep-mesh.hpp:34
Bounding volume hierarchy (BVH).
Definition: bvh.hpp:39
Abstract base class for collision objects.
Definition: collider.hpp:34
Mesh collision object.
void set_mesh(std::shared_ptr< mesh_type > mesh)
Sets the collider's mesh.
constexpr mesh_collider() noexcept=default
Constructs an empty mesh collider.
geom::bvh bvh_type
Bounding volume hierarchy type.
constexpr const bvh_type & get_bvh() const noexcept
Returns the BVH of the collision mesh faces.
std::optional< std::tuple< float, std::uint32_t, math::fvec3 > > intersection(const geom::ray< float, 3 > &ray) const
Finds the nearest point of intersection between a ray and this collision mesh.
void rebuild_bvh()
Rebuilds the BVH of the collision mesh faces.
constexpr collider_type type() const noexcept override
Returns the collider type.
constexpr const std::shared_ptr< mesh_type > & get_mesh() const noexcept
Returns the collision mesh.
Physics.
Definition: constants.hpp:23
collider_type
Collider types.
@ mesh
Mesh collider.
Half of a line proceeding from an initial point.
Definition: ray.hpp:38