neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
collision_detector.hpp
Go to the documentation of this file.
1
// collision_detector.hpp
2
/*
3
neogfx C++ App/Game Engine
4
Copyright (c) 2020 Leigh Johnston. All Rights Reserved.
5
6
This program 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
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18
*/
19
#pragma once
20
21
#include <
neogfx/neogfx.hpp
>
22
#include <
neogfx/core/event.hpp
>
23
#include <
neogfx/game/system.hpp
>
24
#include <
neogfx/game/aabb_quadtree.hpp
>
25
#include <
neogfx/game/aabb_octree.hpp
>
26
#include <
neogfx/game/box_collider.hpp
>
27
28
namespace
neogfx::game
29
{
30
enum class
collision_detection_cycle
: uint32_t
31
{
32
None
= 0x00000000,
33
34
UpdateColliders
= 0x00000001,
35
UpdateTrees
= 0x00000002,
36
DetectCollisions
= 0x00000004,
37
38
Default
=
UpdateColliders
|
UpdateTrees
|
DetectCollisions
,
39
Detect
=
UpdateTrees
|
DetectCollisions
,
40
Test
=
UpdateColliders
|
UpdateTrees
41
};
42
43
inline
constexpr
collision_detection_cycle
operator|
(
collision_detection_cycle
aLhs,
collision_detection_cycle
aRhs)
44
{
45
return
static_cast<
collision_detection_cycle
>
(
static_cast<
uint32_t
>
(aLhs) |
static_cast<
uint32_t
>
(aRhs));
46
}
47
48
inline
constexpr
collision_detection_cycle
operator&
(
collision_detection_cycle
aLhs,
collision_detection_cycle
aRhs)
49
{
50
return
static_cast<
collision_detection_cycle
>
(
static_cast<
uint32_t
>
(aLhs) &
static_cast<
uint32_t
>
(aRhs));
51
}
52
53
class
collision_detector
:
public
game::system
<entity_info, box_collider, box_collider_2d>
54
{
55
public
:
56
define_event
(Collision, collision,
entity_id
,
entity_id
)
57
public
:
58
collision_detector
(
i_ecs
& aEcs);
59
~collision_detector
();
60
public
:
61
const
system_id
&
id
()
const override
;
62
const
i_string
&
name
()
const override
;
63
public
:
64
std::optional<entity_id>
entity_at
(
const
vec3
& aPoint)
const
;
65
public
:
66
bool
apply
()
override
;
67
public
:
68
void
run_cycle
(
collision_detection_cycle
aCycle =
collision_detection_cycle::Default
);
69
template
<
typename
Visitor>
70
void
visit_aabbs
(
const
Visitor& aVisitor)
const
71
{
72
iBroadphaseTree.visit_aabbs(aVisitor);
73
}
74
template
<
typename
Visitor>
75
void
visit_aabbs_2d
(
const
Visitor& aVisitor)
const
76
{
77
iBroadphase2dTree.visit_aabbs(aVisitor);
78
}
79
public
:
80
const
aabb_octree<box_collider>
&
broadphase_tree
()
const
;
81
const
aabb_quadtree<box_collider_2d>
&
broadphase_2d_tree
()
const
;
82
private
:
83
void
update_colliders();
84
void
update_trees();
85
void
detect_collisions();
86
public
:
87
struct
meta
88
{
89
static
const
neolib::uuid
&
id
()
90
{
91
static
const
neolib::uuid
sId = { 0xdb9c2033, 0xae26, 0x463e, 0x9100,{ 0xd5, 0x41, 0xfe, 0x4a, 0xd, 0xae } };
92
return
sId;
93
}
94
static
const
i_string
&
name
()
95
{
96
static
const
string
sName =
"Collision Detector"
;
97
return
sName;
98
}
99
};
100
private
:
101
aabb_octree<box_collider>
iBroadphaseTree;
102
aabb_quadtree<box_collider_2d>
iBroadphase2dTree;
103
std::atomic<bool> iCollidersUpdated;
104
};
105
}
aabb_octree.hpp
aabb_quadtree.hpp
box_collider.hpp
neogfx::game::aabb_octree
Definition
aabb_octree.hpp:34
neogfx::game::aabb_quadtree
Definition
aabb_quadtree.hpp:34
neogfx::game::collision_detector
Definition
collision_detector.hpp:54
neogfx::game::collision_detector::visit_aabbs
void visit_aabbs(const Visitor &aVisitor) const
Definition
collision_detector.hpp:70
neogfx::game::collision_detector::entity_at
std::optional< entity_id > entity_at(const vec3 &aPoint) const
neogfx::game::collision_detector::~collision_detector
define_event(Collision, collision, entity_id, entity_id) public ~collision_detector()
neogfx::game::collision_detector::name
const i_string & name() const override
neogfx::game::collision_detector::broadphase_2d_tree
const aabb_quadtree< box_collider_2d > & broadphase_2d_tree() const
neogfx::game::collision_detector::run_cycle
void run_cycle(collision_detection_cycle aCycle=collision_detection_cycle::Default)
neogfx::game::collision_detector::visit_aabbs_2d
void visit_aabbs_2d(const Visitor &aVisitor) const
Definition
collision_detector.hpp:75
neogfx::game::collision_detector::broadphase_tree
const aabb_octree< box_collider > & broadphase_tree() const
neogfx::game::collision_detector::apply
bool apply() override
neogfx::game::collision_detector::id
const system_id & id() const override
neolib::ecs::i_ecs
Definition
i_ecs.hpp:85
neolib::ecs::system
Definition
system.hpp:58
neolib::i_string
Definition
i_string.hpp:49
neolib::math::basic_vector
Definition
numerical.hpp:112
neogfx::game
Definition
aabb_octree.hpp:31
neogfx::game::operator|
constexpr collision_detection_cycle operator|(collision_detection_cycle aLhs, collision_detection_cycle aRhs)
Definition
collision_detector.hpp:43
neogfx::game::operator&
constexpr collision_detection_cycle operator&(collision_detection_cycle aLhs, collision_detection_cycle aRhs)
Definition
collision_detector.hpp:48
neogfx::game::collision_detection_cycle
collision_detection_cycle
Definition
collision_detector.hpp:31
neogfx::game::collision_detection_cycle::Test
@ Test
neogfx::game::collision_detection_cycle::UpdateTrees
@ UpdateTrees
neogfx::game::collision_detection_cycle::Detect
@ Detect
neogfx::game::collision_detection_cycle::UpdateColliders
@ UpdateColliders
neogfx::game::collision_detection_cycle::DetectCollisions
@ DetectCollisions
neogfx::game::collision_detection_cycle::None
@ None
neogfx::game::collision_detection_cycle::Default
@ Default
neolib::ecs::entity_id
id_t entity_id
Definition
ecs_ids.hpp:51
event.hpp
system.hpp
neogfx.hpp
define_event
#define define_event(name, declName,...)
Definition
event.hpp:200
neogfx::game::collision_detector::meta
Definition
collision_detector.hpp:88
neogfx::game::collision_detector::meta::name
static const i_string & name()
Definition
collision_detector.hpp:94
neogfx::game::collision_detector::meta::id
static const neolib::uuid & id()
Definition
collision_detector.hpp:89
neolib::uuid
Definition
uuid.hpp:51
include
neogfx
game
collision_detector.hpp
Generated by
1.9.8