Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qspatialhashing.h
1 
2 /************************************************************************************
3  * MIT License
4  *
5  * Copyright (c) 2023 Eray Zesen
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * https://github.com/erayzesen/QuarkPhysics
25  *
26 **************************************************************************************/
27 
28 
29 
30 
31 #ifndef QSPATIALHASHING_H
32 #define QSPATIALHASHING_H
33 
34 #include <unordered_map>
35 #include <unordered_set>
36 #include <cmath>
37 #include <vector>
38 #include "../qbroadphase.h"
39 
40 
41 
42 
43 class QSpatialHashing : public QBroadPhase {
44 private:
45  float cellSize=128.0f;
46 
47  float cellSizeFactor=1/cellSize;
48 
49  struct PairHash {
50  template <class T1, class T2>
51  std::size_t operator()(const std::pair<T1, T2>& p) const {
52  return std::hash<T1>()(p.first) ^ 0x100000001b3 ^std::hash<T2>()(p.second);
53  };
54  };
55 
56  struct CellAABB{
57  CellAABB(int minimumX,int minimumY,int maximumX,int maximumY){
58  minX=minimumX;
59  minY=minimumY;
60  maxX=maximumX;
61  maxY=maximumY;
62  };
63  CellAABB(){
64  minX=0;
65  minY=0;
66  maxX=0;
67  maxY=0;
68  }
69  int minX;
70  int minY;
71  int maxX;
72  int maxY;
73 
74  bool operator==(const CellAABB &other) const {
75  return (other.minX == minX && other.minY == minY &&
76  other.maxX == maxX && other.maxY == maxY);
77  }
78 
79 
80  };
81 
82 
83  std::unordered_map<pair<int,int>,vector<QBody*>,PairHash> cells;
84 
85  static bool SortBodiesHorizontal(const QBody *bodyA,const QBody *bodyB){
86  if(bodyA->GetAABB().GetMin().x==bodyB->GetAABB().GetMin().x){
87  return bodyA->GetAABB().GetMax().y>bodyB->GetAABB().GetMax().y;
88  }
89  return bodyA->GetAABB().GetMin().x<bodyB->GetAABB().GetMin().x;
90  };
91 
92  std::unordered_map<QBody*,CellAABB> bodyOldCells;
93 
94  void RemoveBodyFromCells(QBody *body,CellAABB &cellAABB);
95 
96 public:
97  QSpatialHashing(vector<QBody*>& worldBodies, float sizeOfCells=128.0f);
98 
99  void Clear();
100 
101  void Insert(QBody* body);
102  void Remove(QBody* body);
103 
104  void SetCellSize(float size);
105 
106  std::unordered_set<std::pair<QBody*, QBody*>,QBody::BodyPairHash,QBody::BodyPairEqual> & GetPairs();
107 
108 
109 };
110 
111 
112 #endif // QSPATIALHASHING_H
QBody objects are the base class for all types of bodies. Any class derived from QBody shares these m...
Definition: qbody.h:43
QAABB GetAABB() const
Definition: qbody.h:218
Definition: qbroadphase.h:44
Definition: qspatialhashing.h:43
Definition: qbody.h:77
Definition: qbody.h:69