Antkeeper  0.0.1
physics-system.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_GAME_PHYSICS_SYSTEM_HPP
21 #define ANTKEEPER_GAME_PHYSICS_SYSTEM_HPP
22 
24 #include <entt/entt.hpp>
25 #include <engine/math/vector.hpp>
26 #include <engine/entity/id.hpp>
30 #include <array>
31 #include <functional>
32 #include <optional>
33 #include <tuple>
34 
39  public updatable_system
40 {
41 public:
43  void update(float t, float dt) override;
44 
45  void interpolate(float alpha);
46 
52  inline void set_gravity(const math::fvec3& gravity) noexcept
53  {
54  this->gravity = gravity;
55  }
56 
66  [[nodiscard]] std::optional<std::tuple<entity::id, float, std::uint32_t, math::fvec3>> trace(const geom::ray<float, 3>& ray, entity::id ignore_eid = entt::null, std::uint32_t layer_mask = ~std::uint32_t{0}) const;
67 
68 private:
69  using collision_manifold_type = physics::collision_manifold<4>;
70 
71  void integrate(float dt);
72 
73  void solve_constraints(float dt);
74 
75  void detect_collisions_broad();
76  void detect_collisions_narrow();
77  void resolve_collisions();
78  void correct_positions();
79 
80  void narrow_phase_plane_plane(physics::rigid_body& body_a, physics::rigid_body& body_b);
81  void narrow_phase_plane_sphere(physics::rigid_body& body_a, physics::rigid_body& body_b);
82  void narrow_phase_plane_box(physics::rigid_body& body_a, physics::rigid_body& body_b);
83  void narrow_phase_plane_capsule(physics::rigid_body& body_a, physics::rigid_body& body_b);
84 
85  void narrow_phase_sphere_plane(physics::rigid_body& body_a, physics::rigid_body& body_b);
86  void narrow_phase_sphere_sphere(physics::rigid_body& body_a, physics::rigid_body& body_b);
87  void narrow_phase_sphere_box(physics::rigid_body& body_a, physics::rigid_body& body_b);
88  void narrow_phase_sphere_capsule(physics::rigid_body& body_a, physics::rigid_body& body_b);
89 
90  void narrow_phase_box_plane(physics::rigid_body& body_a, physics::rigid_body& body_b);
91  void narrow_phase_box_sphere(physics::rigid_body& body_a, physics::rigid_body& body_b);
92  void narrow_phase_box_box(physics::rigid_body& body_a, physics::rigid_body& body_b);
93  void narrow_phase_box_capsule(physics::rigid_body& body_a, physics::rigid_body& body_b);
94 
95  void narrow_phase_capsule_plane(physics::rigid_body& body_a, physics::rigid_body& body_b);
96  void narrow_phase_capsule_sphere(physics::rigid_body& body_a, physics::rigid_body& body_b);
97  void narrow_phase_capsule_box(physics::rigid_body& body_a, physics::rigid_body& body_b);
98  void narrow_phase_capsule_capsule(physics::rigid_body& body_a, physics::rigid_body& body_b);
99 
100  std::array<std::array<std::function<void(physics::rigid_body&, physics::rigid_body&)>, 4>, 4> narrow_phase_table;
101 
102  math::fvec3 gravity{0.0f, -9.80665f, 0.0f};
103 
104  std::vector<std::pair<physics::rigid_body*, physics::rigid_body*>> broad_phase_pairs;
105  std::vector<collision_manifold_type> narrow_phase_manifolds;
106 };
107 
108 #endif // ANTKEEPER_GAME_PHYSICS_SYSTEM_HPP
void set_gravity(const math::fvec3 &gravity) noexcept
Sets the gravity vector.
void interpolate(float alpha)
void update(float t, float dt) override
Perform's a system's update() function.
physics_system(entity::registry &registry)
std::optional< std::tuple< entity::id, float, std::uint32_t, math::fvec3 > > trace(const geom::ray< float, 3 > &ray, entity::id ignore_eid=entt::null, std::uint32_t layer_mask=~std::uint32_t{0}) const
Traces a ray to the nearest point of intersection.
Abstract base class for updatable systems.
entity::registry & registry
Registry on which the system operate.
entt::registry registry
Component registry type.
Definition: registry.hpp:28
entt::entity id
Entity ID type.
Definition: id.hpp:28
Half of a line proceeding from an initial point.
Definition: ray.hpp:38
n-dimensional vector.
Definition: vector.hpp:44
Collection of contact points between two colliding bodies.