Quark Physics  1.0
2D Rigid and Soft Body Physics Engine
qaabb.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 #ifndef QAABB_H
29 #define QAABB_H
30 #include "qvector.h"
31 
32 
33 class QAABB
34 {
35  QVector minPos;
36  QVector maxPos;
37  QVector size;
38  float volume;
39  static float multiplier;
40 public:
41 
42 
43  QAABB(){
44  QAABB(minPos,maxPos);
45  }
46 
47  QAABB(QVector minPosition, QVector maxPosition ){
48  minPos=minPosition;
49  maxPos=maxPosition;
50  size=maxPos-minPos;
51  volume=size.x*size.y;
52  };
53 
54  //General Get Methods
55  QVector GetMin(){
56  return minPos;
57  }
58  QVector GetMax(){
59  return maxPos;
60  }
61  QVector GetSize(){
62  return size;
63  }
64 
65 
66  //General Set Methods
67 
68 
69 
70  QAABB * SetMinMax(QVector minPosition,QVector maxPosition){
71  minPos=minPosition;
72  maxPos=maxPosition;
73  size=maxPos-minPos;
74  volume=size.x*size.y;
75  return this;
76  }
77 
78 
79  float GetPerimeter() const{
80  return 2.0f*(size.x+size.y);
81  }
82 
83  float GetArea()const{
84  return size.x*size.y;
85  }
86 
87  QVector GetCenterPosition()const {
88  return (minPos+maxPos)*0.5f;
89  }
90 
91  float GetVolume() const{
92  return volume;
93  }
94 
95  double isContain(const QAABB& otherAABB) const {
96  return (minPos.x <= otherAABB.minPos.x) &&
97  (minPos.y <= otherAABB.minPos.y) &&
98  (maxPos.x >= otherAABB.maxPos.x) &&
99  (maxPos.y >= otherAABB.maxPos.y);
100  }
101  static QAABB Combine( QAABB &b1, QAABB &b2){
102  QAABB output=QAABB();
103  float minX=b1.minPos.x<b2.minPos.x ? b1.minPos.x : b2.minPos.x;
104  float minY=b1.minPos.y<b2.minPos.y ? b1.minPos.y : b2.minPos.y;
105 
106  float maxX=b1.maxPos.x>b2.maxPos.x ? b1.maxPos.x : b2.maxPos.x;
107  float maxY=b1.maxPos.y>b2.maxPos.y ? b1.maxPos.y : b2.maxPos.y;
108  output.SetMinMax(QVector(minX,minY),QVector(maxX,maxY) );
109  return output;
110  };
111 
112  void Fatten(float amount){
113  QVector amountVec=QVector(amount,amount);
114  SetMinMax(minPos-amountVec,maxPos+amountVec);
115  }
116 
117  QAABB Fattened(float amount)const{
118  QVector amountVec=QVector(amount,amount);
119  return QAABB(minPos-amountVec,maxPos+amountVec);
120 
121  }
122 
123  QAABB FattenedWithRate(float rate)const{
124  QVector ratedSize=size*rate*0.5f;
125  return QAABB(minPos-ratedSize,maxPos+ratedSize);
126  }
127 
128  bool isCollidingWith(const QAABB& otherAABB) const {
129  return maxPos.x >= otherAABB.minPos.x && minPos.x <= otherAABB.maxPos.x &&
130  maxPos.y >= otherAABB.minPos.y && minPos.y <= otherAABB.maxPos.y;
131  }
132 
133 
134 
135 };
136 
137 #endif // QAABB_H
Definition: qaabb.h:34
Definition: qvector.h:44