Dura2D  v0.1.0
An educational 2D game physics library
Loading...
Searching...
No Matches
d2AABB.h
Go to the documentation of this file.
1#ifndef D2AABB_H
2#define D2AABB_H
3
4#include "d2api.h"
5#include "d2Math.h"
6
7class d2Body;
8
10{
11 d2Vec2 lowerBound{};
12 d2Vec2 upperBound{};
13
14 void *userData{};
15
16 d2Body *Collider{};
17
18 d2AABB() = default;
19
20 d2AABB(const d2Vec2& lower, const d2Vec2& upper) : lowerBound(lower), upperBound(upper) {}
21
23 {
24 return 0.5f * (lowerBound + upperBound);
25 }
26
28 {
29 return 0.5f * (upperBound - lowerBound);
30 }
31
32 float GetPerimeter() const
33 {
34 float wx = upperBound.x - lowerBound.x;
35 float wy = upperBound.y - lowerBound.y;
36 return 2.0f * (wx + wy);
37 }
38
39 void Combine(const d2AABB& aabb)
40 {
41 lowerBound = d2Min(lowerBound, aabb.lowerBound);
42 upperBound = d2Max(upperBound, aabb.upperBound);
43 }
44
45 void Combine(const d2AABB& aabb1, const d2AABB& aabb2)
46 {
47 lowerBound = d2Min(aabb1.lowerBound, aabb2.lowerBound);
48 upperBound = d2Max(aabb1.upperBound, aabb2.upperBound);
49 }
50
51 bool Overlaps(const d2AABB& aabb) const
52 {
53 bool xOverlap = lowerBound.x <= aabb.upperBound.x && upperBound.x >= aabb.lowerBound.x;
54 bool yOverlap = lowerBound.y <= aabb.upperBound.y && upperBound.y >= aabb.lowerBound.y;
55
56 return xOverlap && yOverlap;
57 }
58
59 bool Contains(const d2AABB& aabb) const
60 {
61 bool xContains = lowerBound.x <= aabb.lowerBound.x && aabb.upperBound.x <= upperBound.x;
62 bool yContains = lowerBound.y <= aabb.lowerBound.y && aabb.upperBound.y <= upperBound.y;
63
64 return xContains && yContains;
65 }
66
67 bool Contains(const d2Vec2& point) const
68 {
69 bool xContains = lowerBound.x <= point.x && point.x <= upperBound.x;
70 bool yContains = lowerBound.y <= point.y && point.y <= upperBound.y;
71
72 return xContains && yContains;
73 }
74};
75
76inline d2AABB Union(const d2AABB& aabb1, const d2AABB& aabb2)
77{
78 d2AABB aabb;
79 aabb.Combine(aabb1, aabb2);
80 return aabb;
81}
82
83#endif //D2AABB_H
A class representing a 2D rigid body.
Definition d2Body.h:28
d2AABB Union(const d2AABB &aabb1, const d2AABB &aabb2)
Definition d2AABB.h:76
T d2Min(T a, T b)
Definition d2Math.h:353
T d2Max(T a, T b)
Definition d2Math.h:365
#define D2_API
Definition d2api.h:27
Definition d2AABB.h:10
d2Vec2 GetExtents() const
Definition d2AABB.h:27
bool Contains(const d2AABB &aabb) const
Definition d2AABB.h:59
d2Vec2 upperBound
Definition d2AABB.h:12
d2Vec2 lowerBound
Definition d2AABB.h:11
d2AABB()=default
d2AABB(const d2Vec2 &lower, const d2Vec2 &upper)
Definition d2AABB.h:20
void Combine(const d2AABB &aabb)
Definition d2AABB.h:39
d2Vec2 GetCenter() const
Definition d2AABB.h:22
float GetPerimeter() const
Definition d2AABB.h:32
void Combine(const d2AABB &aabb1, const d2AABB &aabb2)
Definition d2AABB.h:45
bool Contains(const d2Vec2 &point) const
Definition d2AABB.h:67
bool Overlaps(const d2AABB &aabb) const
Definition d2AABB.h:51
Represents a 2D vector.
Definition d2Math.h:22
real y
Definition d2Math.h:106
real x
Definition d2Math.h:106