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>
27
28namespace 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
41 };
42
44 {
45 return static_cast<collision_detection_cycle>(static_cast<uint32_t>(aLhs) | static_cast<uint32_t>(aRhs));
46 }
47
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:
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:
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:
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}
void visit_aabbs(const Visitor &aVisitor) const
std::optional< entity_id > entity_at(const vec3 &aPoint) const
define_event(Collision, collision, entity_id, entity_id) public ~collision_detector()
const i_string & name() const override
const aabb_quadtree< box_collider_2d > & broadphase_2d_tree() const
void run_cycle(collision_detection_cycle aCycle=collision_detection_cycle::Default)
void visit_aabbs_2d(const Visitor &aVisitor) const
const aabb_octree< box_collider > & broadphase_tree() const
const system_id & id() const override
constexpr collision_detection_cycle operator|(collision_detection_cycle aLhs, collision_detection_cycle aRhs)
constexpr collision_detection_cycle operator&(collision_detection_cycle aLhs, collision_detection_cycle aRhs)
id_t entity_id
Definition ecs_ids.hpp:51
#define define_event(name, declName,...)
Definition event.hpp:200