FabGL
ESP32 Display Controller and Graphics Library
collisiondetector.h
Go to the documentation of this file.
1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#pragma once
28
29
30
38#include <stdint.h>
39#include <stddef.h>
40
41#include "fabglconf.h"
42#include "displaycontroller.h"
43
44
45
46namespace fabgl {
47
48
49
50class CollisionDetector;
51
52class QuadTree;
53
54
55struct QuadTreeObject {
56 QuadTree * owner;
57 QuadTreeObject * next;
58 Sprite * sprite;
59
60 QuadTreeObject(QuadTreeObject * next_, Sprite * sprite_)
61 : owner(nullptr), next(next_), sprite(sprite_)
62 {
63 }
64};
65
66
67#define QUADTREE_LEVEL_SPLIT_THRESHOLD 3
68
69
70enum QuadTreeQuadrant {
71 TopLeft,
72 TopRight,
73 BottomLeft,
74 BottomRight,
75 None,
76};
77
78
79typedef void (*CollisionDetectionCallback)(void * callbackObj, Sprite * spriteA, Sprite * spriteB, Point collisionPoint);
80
81
82class QuadTree {
83
84public:
85
86 QuadTree(CollisionDetector * collisionDetector, QuadTree * parent, QuadTreeQuadrant quadrant, int x, int y, int width, int height);
87
88 void insert(QuadTreeObject * object);
89
90 static void remove(QuadTreeObject * object);
91
92 //void dump(int level = 0);
93
94 QuadTreeObject * detectCollision(QuadTreeObject * object, CollisionDetectionCallback callbackFunc = nullptr, void * callbackObj = nullptr);
95
96 bool isEmpty();
97
98 void detachFromParent();
99
100 static void update(QuadTreeObject * object);
101
102private:
103
104 QuadTreeQuadrant getQuadrant(QuadTreeObject * object);
105 void createQuadrant(QuadTreeQuadrant quadrant);
106 bool objectsIntersect(QuadTreeObject * objectA, QuadTreeObject * objectB);
107 bool objectIntersectsQuadTree(QuadTreeObject * object, QuadTree * quadTree);
108 bool checkMaskCollision(QuadTreeObject * objectA, QuadTreeObject * objectB, Point * collisionPoint);
109
110 static bool objectInRect(QuadTreeObject * object, int x, int y, int width, int height);
111
112 CollisionDetector * m_collisionDetector;
113 QuadTree * m_parent;
114 QuadTreeQuadrant m_quadrant;
115 int m_x;
116 int m_y;
117 int m_width;
118 int m_height;
119 QuadTreeObject * m_objects;
120 int m_objectsCount;
121 QuadTree * m_children[4]; // index is Quadrant
122
123};
124
125
126
134
135friend class QuadTree;
136
137public:
138
148 CollisionDetector(int maxObjectsCount, int width, int height);
149
151
160 void addSprite(Sprite * sprite);
161
167 void removeSprite(Sprite * sprite);
168
179 Sprite * detectCollision(Sprite * sprite, bool removeCollidingSprites = true);
180
190 void detectCollision(Sprite * sprite, CollisionDetectionCallback callbackFunc, void * callbackObj);
191
200 void update(Sprite * sprite);
201
213 Sprite * updateAndDetectCollision(Sprite * sprite, bool removeCollidingSprites = true);
214
225 void updateAndDetectCollision(Sprite * sprite, CollisionDetectionCallback callbackFunc, void * callbackObj);
226
227 //void dump();
228
229
230private:
231
232 QuadTree * initEmptyQuadTree(QuadTree * parent, QuadTreeQuadrant quadrant, int x, int y, int width, int height);
233
234
235 QuadTree * m_rootQuadTree;
236 QuadTree * m_quadTreePool;
237 int m_quadTreePoolSize;
238 QuadTreeObject * m_objectPool;
239 int m_objectPoolSize;
240};
241
242
243
244
245
246} // end of namespace
247
248
249
250
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
CollisionDetector(int maxObjectsCount, int width, int height)
Creates an instance of CollisionDetector.
void update(Sprite *sprite)
Updates collision detector.
Sprite * updateAndDetectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Updates collision detector and detect collision with the specified sprite.
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Sprite * detectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Detects first collision with the specified sprite.
A class to detect sprites collisions.
uint8_t width
uint8_t height
This file contains fabgl::BitmappedDisplayController definition.
This file contains FabGL library configuration settings, like number of supported colors,...
Represents a sprite.