nngn
Loading...
Searching...
No Matches
collision.h
Go to the documentation of this file.
1
12#ifndef NNGN_COLLISION_H
13#define NNGN_COLLISION_H
14
15#include <memory>
16#include <vector>
17
18#include "lua/table.h"
19#include "timing/stats.h"
20#include "utils/utils.h"
21
22#include "colliders.h"
23
24struct lua_State;
25struct Entity;
26
27namespace nngn {
28
29struct Compute;
30struct Timing;
31
32struct Collision {
33 Entity *entity0 = nullptr, *entity1 = nullptr;
34 float mass0 = 0, mass1 = 0;
36 vec3 force = {};
37};
38
39struct CollisionStats : StatsBase<CollisionStats, 4> {
40 std::array<uint64_t, 4>
54 static constexpr std::array names = {
55 "counters",
56 "aabb_copy", "aabb_exec_barrier", "aabb_exec",
57 "bb_copy", "bb_exec_barrier", "bb_exec",
58 "sphere_pos", "sphere_vel", "sphere_mass", "sphere_radius",
59 "sphere_grid_count", "sphere_exec_grid_barrier",
60 "sphere_exec_grid", "sphere_exec_barrier", "sphere_exec",
61 "plane",
62 "gravity_pos", "gravity_mass", "gravity_max_distance2",
63 "aabb_bb_exec_barrier", "aabb_bb_exec",
64 "aabb_sphere_exec_barrier", "aabb_sphere_exec",
65 "bb_sphere_exec_barrier", "bb_sphere_exec",
66 "sphere_plane_exec_barrier", "sphere_plane_exec",
67 "sphere_gravity_exec_barrier", "sphere_gravity_exec"};
68 const uint64_t *to_u64(void) const { return this->counters.data(); }
69 uint64_t *to_u64(void) { return this->counters.data(); }
70};
71
72struct Colliders {
73 struct Backend {
74 struct Input {
75 std::vector<AABBCollider> aabb = {};
76 std::vector<BBCollider> bb = {};
77 std::vector<SphereCollider> sphere = {};
78 std::vector<PlaneCollider> plane = {};
79 std::vector<GravityCollider> gravity = {};
80 };
81 struct Output {
83 std::vector<Collision> collisions = {};
84 };
86 virtual bool init(void) { return true; }
87 virtual bool set_max_colliders(std::size_t) { return true; }
88 virtual bool set_max_collisions(std::size_t) { return true; }
89 virtual bool check(const Timing&, Input*, Output*)
90 { return true; }
91 };
92private:
93 enum Flag : uint8_t {
94 CHECK = 1u << 0, RESOLVE = 1u << 1,
96 };
97 Flags<Flag> m_flags = {static_cast<Flag>(Flag::CHECK | Flag::RESOLVE)};
98 std::size_t m_max_colliders = 0;
101 std::unique_ptr<Backend> backend = {};
102public:
104 static constexpr std::size_t STATS_IDX = 1;
105 static std::unique_ptr<Backend> native_backend();
106 static std::unique_ptr<Backend> compute_backend(Compute *c);
108 Colliders(void);
109 ~Colliders(void);
110 auto &aabb(void) const { return this->input.aabb; }
111 auto &bb(void) const { return this->input.bb; }
112 auto &sphere(void) const { return this->input.sphere; }
113 auto &plane(void) const { return this->input.plane; }
114 auto &gravity(void) const { return this->input.gravity; }
115 std::size_t max_colliders(void) const { return this->m_max_colliders; }
116 std::size_t max_collisions(void) const;
117 auto &collisions(void) const { return this->output.collisions; }
118 bool check(void) const { return this->m_flags.is_set(Flag::CHECK); }
119 bool resolve(void) const { return this->m_flags.is_set(Flag::RESOLVE); }
120 bool has_backend(void) const { return static_cast<bool>(this->backend); }
121 void set_check(bool b) { this->m_flags.set(Flag::CHECK, b); }
122 void set_resolve(bool b) { this->m_flags.set(Flag::RESOLVE, b); }
123 bool set_max_colliders(std::size_t n);
124 bool set_max_collisions(std::size_t n);
125 bool set_backend(std::unique_ptr<Backend> p);
127 BBCollider *add(const BBCollider &c);
132 void remove(Collider *p);
133 void clear(void);
134 bool check_collisions(const Timing &t);
135 void resolve_collisions(void) const;
137};
138
139inline std::size_t Colliders::max_collisions(void) const {
140 return this->output.collisions.capacity();
141}
142
143}
144
145#endif
Definition state.h:88
set_max_colliders
Definition collision.lua:14
local n
Definition dump_lights.lua:5
local c
Definition gamma.lua:11
Definition audio.cpp:7
load
Definition entity.lua:7
#define NNGN_VIRTUAL(x)
Definition utils.h:27
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
Definition entity.h:24
Definition colliders.h:34
Definition colliders.h:44
Definition colliders.h:15
Definition collision.h:74
std::vector< GravityCollider > gravity
Definition collision.h:79
std::vector< AABBCollider > aabb
Definition collision.h:75
std::vector< SphereCollider > sphere
Definition collision.h:77
std::vector< PlaneCollider > plane
Definition collision.h:78
std::vector< BBCollider > bb
Definition collision.h:76
Definition collision.h:81
std::vector< Collision > collisions
Definition collision.h:83
CollisionStats stats
Definition collision.h:82
Definition collision.h:73
virtual bool set_max_collisions(std::size_t)
Definition collision.h:88
virtual bool check(const Timing &, Input *, Output *)
Definition collision.h:89
virtual bool set_max_colliders(std::size_t)
Definition collision.h:87
virtual bool init(void)
Definition collision.h:86
Definition collision.h:72
Flag
Definition collision.h:93
@ CHECK
Definition collision.h:94
@ MAX_COLLIDERS_UPDATED
Definition collision.h:95
@ RESOLVE
Definition collision.h:94
@ MAX_COLLISIONS_UPDATED
Definition collision.h:95
auto & aabb(void) const
Definition collision.h:110
bool resolve(void) const
Definition collision.h:119
auto & collisions(void) const
Definition collision.h:117
Backend::Input input
Definition collision.h:99
bool check_collisions(const Timing &t)
Definition collision.cpp:76
bool set_max_collisions(std::size_t n)
Definition collision.cpp:37
void set_check(bool b)
Definition collision.h:121
void set_resolve(bool b)
Definition collision.h:122
auto & sphere(void) const
Definition collision.h:112
auto & bb(void) const
Definition collision.h:111
std::size_t max_colliders(void) const
Definition collision.h:115
static std::unique_ptr< Backend > native_backend()
Definition native.cpp:440
auto & plane(void) const
Definition collision.h:113
Flags< Flag > m_flags
Definition collision.h:97
void clear(void)
Definition collision.cpp:68
bool lua_on_collision(nngn::lua::state_view lua)
Definition collision.cpp:113
static constexpr std::size_t STATS_IDX
Definition collision.h:104
bool has_backend(void) const
Definition collision.h:120
std::size_t max_collisions(void) const
Definition collision.h:139
Colliders(void)
Definition collision.cpp:18
Backend::Output output
Definition collision.h:100
std::size_t m_max_colliders
Definition collision.h:98
~Colliders(void)
Definition collision.cpp:22
static std::unique_ptr< Backend > compute_backend(Compute *c)
Definition compute.cpp:677
void remove(Collider *p)
Definition collision.cpp:50
std::unique_ptr< Backend > backend
Definition collision.h:101
void resolve_collisions(void) const
Definition collision.cpp:96
auto & gravity(void) const
Definition collision.h:114
AABBCollider * add(const AABBCollider &c)
Definition collision.cpp:145
bool set_backend(std::unique_ptr< Backend > p)
Definition collision.cpp:43
bool check(void) const
Definition collision.h:118
Definition collision.h:39
std::array< uint64_t, 4 > sphere_gravity_exec_barrier
Definition collision.h:53
std::array< uint64_t, 4 > sphere_plane_exec_barrier
Definition collision.h:52
std::array< uint64_t, 4 > sphere_exec
Definition collision.h:46
std::array< uint64_t, 4 > aabb_sphere_exec_barrier
Definition collision.h:50
std::array< uint64_t, 4 > sphere_vel
Definition collision.h:44
std::array< uint64_t, 4 > bb_exec
Definition collision.h:43
std::array< uint64_t, 4 > gravity_mass
Definition collision.h:48
std::array< uint64_t, 4 > sphere_exec_grid
Definition collision.h:46
std::array< uint64_t, 4 > bb_copy
Definition collision.h:43
std::array< uint64_t, 4 > sphere_plane_exec
Definition collision.h:52
std::array< uint64_t, 4 > bb_sphere_exec
Definition collision.h:51
std::array< uint64_t, 4 > aabb_bb_exec_barrier
Definition collision.h:49
std::array< uint64_t, 4 > plane
Definition collision.h:47
std::array< uint64_t, 4 > counters
Definition collision.h:41
static constexpr std::array names
Definition collision.h:54
const uint64_t * to_u64(void) const
Definition collision.h:68
std::array< uint64_t, 4 > bb_sphere_exec_barrier
Definition collision.h:51
std::array< uint64_t, 4 > sphere_radius
Definition collision.h:44
std::array< uint64_t, 4 > sphere_grid_count
Definition collision.h:45
std::array< uint64_t, 4 > bb_exec_barrier
Definition collision.h:43
std::array< uint64_t, 4 > sphere_exec_grid_barrier
Definition collision.h:45
std::array< uint64_t, 4 > gravity_max_distance2
Definition collision.h:48
std::array< uint64_t, 4 > aabb_exec_barrier
Definition collision.h:42
std::array< uint64_t, 4 > sphere_pos
Definition collision.h:44
std::array< uint64_t, 4 > gravity_pos
Definition collision.h:48
std::array< uint64_t, 4 > aabb_copy
Definition collision.h:42
std::array< uint64_t, 4 > aabb_bb_exec
Definition collision.h:49
std::array< uint64_t, 4 > sphere_gravity_exec
Definition collision.h:53
std::array< uint64_t, 4 > aabb_sphere_exec
Definition collision.h:50
std::array< uint64_t, 4 > sphere_exec_barrier
Definition collision.h:46
std::array< uint64_t, 4 > sphere_mass
Definition collision.h:44
uint64_t * to_u64(void)
Definition collision.h:69
std::array< uint64_t, 4 > aabb_exec
Definition collision.h:42
Definition collision.h:32
Entity * entity0
Definition collision.h:33
Entity * entity1
Definition collision.h:33
float mass0
Definition collision.h:34
vec3 force
Definition collision.h:36
float mass1
Definition collision.h:34
Flags< Collider::Flag > flags1
Definition collision.h:35
Flags< Collider::Flag > flags0
Definition collision.h:35
Base class for computation back ends, which may be hardware-accelerated.
Definition compute.h:108
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
constexpr bool is_set(AT a) const
Definition flags.h:45
constexpr Flags & set(AT a)
Definition flags.h:47
Definition colliders.h:69
Definition colliders.h:61
Definition colliders.h:53
Definition stats.h:15
Definition timing.h:20
Non-owning reference to a table on the stack.
Definition table.h:166
Operations on table values.