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