FabGL
ESP32 VGA 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 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
26 
34 #include <stdint.h>
35 #include <stddef.h>
36 
37 #include "fabglconf.h"
38 #include "vgacontroller.h"
39 
40 
41 
42 namespace fabgl {
43 
44 
45 
46 class CollisionDetector;
47 
48 class QuadTree;
49 
50 
51 struct QuadTreeObject {
52  QuadTree * owner;
53  QuadTreeObject * next;
54  Sprite * sprite;
55 
56  QuadTreeObject(QuadTreeObject * next_, Sprite * sprite_)
57  : owner(nullptr), next(next_), sprite(sprite_)
58  {
59  }
60 };
61 
62 
63 #define QUADTREE_LEVEL_SPLIT_THRESHOLD 3
64 
65 
66 enum QuadTreeQuadrant {
67  TopLeft,
68  TopRight,
69  BottomLeft,
70  BottomRight,
71  None,
72 };
73 
74 
75 typedef void (*CollisionDetectionCallback)(void * callbackObj, Sprite * spriteA, Sprite * spriteB, Point collisionPoint);
76 
77 
78 class QuadTree {
79 
80 public:
81 
82  QuadTree(CollisionDetector * collisionDetector, QuadTree * parent, QuadTreeQuadrant quadrant, int x, int y, int width, int height);
83 
84  void insert(QuadTreeObject * object);
85 
86  static void remove(QuadTreeObject * object);
87 
88  //void dump(int level = 0);
89 
90  QuadTreeObject * detectCollision(QuadTreeObject * object, CollisionDetectionCallback callbackFunc = nullptr, void * callbackObj = nullptr);
91 
92  bool isEmpty();
93 
94  void detachFromParent();
95 
96  static void update(QuadTreeObject * object);
97 
98 private:
99 
100  QuadTreeQuadrant getQuadrant(QuadTreeObject * object);
101  void createQuadrant(QuadTreeQuadrant quadrant);
102  bool objectsIntersect(QuadTreeObject * objectA, QuadTreeObject * objectB);
103  bool objectIntersectsQuadTree(QuadTreeObject * object, QuadTree * quadTree);
104  bool checkMaskCollision(QuadTreeObject * objectA, QuadTreeObject * objectB, Point * collisionPoint);
105 
106  static bool objectInRect(QuadTreeObject * object, int x, int y, int width, int height);
107 
108  CollisionDetector * m_collisionDetector;
109  QuadTree * m_parent;
110  QuadTreeQuadrant m_quadrant;
111  int m_x;
112  int m_y;
113  int m_width;
114  int m_height;
115  QuadTreeObject * m_objects;
116  int m_objectsCount;
117  QuadTree * m_children[4]; // index is Quadrant
118 
119 };
120 
121 
122 
130 
131 friend class QuadTree;
132 
133 public:
134 
144  CollisionDetector(int maxObjectsCount, int width, int height);
145 
147 
156  void addSprite(Sprite * sprite);
157 
163  void removeSprite(Sprite * sprite);
164 
175  Sprite * detectCollision(Sprite * sprite, bool removeCollidingSprites = true);
176 
186  void detectCollision(Sprite * sprite, CollisionDetectionCallback callbackFunc, void * callbackObj);
187 
196  void update(Sprite * sprite);
197 
209  Sprite * updateAndDetectCollision(Sprite * sprite, bool removeCollidingSprites = true);
210 
221  void updateAndDetectCollision(Sprite * sprite, CollisionDetectionCallback callbackFunc, void * callbackObj);
222 
223  //void dump();
224 
225 
226 private:
227 
228  QuadTree * initEmptyQuadTree(QuadTree * parent, QuadTreeQuadrant quadrant, int x, int y, int width, int height);
229 
230 
231  QuadTree * m_rootQuadTree;
232  QuadTree * m_quadTreePool;
233  int m_quadTreePoolSize;
234  QuadTreeObject * m_objectPool;
235  int m_objectPoolSize;
236 };
237 
238 
239 
240 
241 
242 } // end of namespace
243 
244 
245 
246 
Represents a sprite.
Definition: vgacontroller.h:454
Sprite * updateAndDetectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Updates collision detector and detect collision with the specified sprite.
Definition: collisiondetector.cpp:428
This file contains fabgl::VGAControllerClass definition and the VGAController instance.
CollisionDetector(int maxObjectsCount, int width, int height)
Creates an instance of CollisionDetector.
Definition: collisiondetector.cpp:324
A class to detect sprites collisions.
Definition: collisiondetector.h:129
void update(Sprite *sprite)
Updates collision detector.
Definition: collisiondetector.cpp:422
Definition: canvas.cpp:47
Sprite * detectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Detects first collision with the specified sprite.
Definition: collisiondetector.cpp:403
This file contains FabGL library configuration settings, like number of supported colors...
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
Definition: collisiondetector.cpp:373
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: collisiondetector.cpp:388