-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform2d.cpp
114 lines (98 loc) · 2.71 KB
/
Transform2d.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "Transform2d.h"
bool Transform2D::collisionMatrix[4][4] = {
false, false, false, false,
false, false, true, false,
false, true, false, true,
false, false, true, false,
};
/// <summary>
/// Constructor
/// </summary>
/// <param name="x">Position on the x axis</param>
/// <param name="y">Position on the y axis</param>
/// <param name="width">Width of the object</param>
/// <param name="height">Height of the object</param>
Transform2D::Transform2D(const float x, const float y, const float width, const float height):
_x (x),
_y (y),
_w (width),
_h (height) {
}
/// <summary>
/// Destructor
/// </summary>
Transform2D::~Transform2D() = default;
/// <summary>
/// Returns the x position
/// </summary>
/// <returns></returns>
float Transform2D::getX() const { return _x; }
/// <summary>
/// Returns the y position
/// </summary>
/// <returns></returns>
float Transform2D::getY() const { return _y; }
/// <summary>
/// Returns the width
/// </summary>
/// <returns></returns>
float Transform2D::getWidth() const { return _w; }
/// <summary>
/// Returns the height
/// </summary>
/// <returns></returns>
float Transform2D::getHeight() const { return _h; }
/// <summary>
/// Returns the axis-aligned bounding box of this object
/// </summary>
/// <returns>Rect</returns>
D2D1_RECT_F Transform2D::getAABB() const {
const auto rect = D2D1::RectF(_x, _y - _h, _x + _w, _y);
return rect;
}
/// <summary>
/// Sets the size of this object
/// </summary>
/// <param name="width">Width of the object</param>
/// <param name="height">Height of the object</param>
void Transform2D::setSize(const float width, const float height) {
_w = width;
_h = height;
}
/// <summary>
/// Sets the position of this object
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
void Transform2D::setPos(const float x, const float y) {
_x = x;
_y = y;
}
/// <summary>
/// Returns true if the transforms are colliding, false if not
/// </summary>
/// <param name="other">Transform to check against</param>
/// <returns></returns>
bool Transform2D::isColliding(Transform2D* other) const {
if (other == nullptr) return false;
const auto otherAABB = other->getAABB();
const auto myAABB = getAABB();
//Collision tests
if (myAABB.right < otherAABB.left || myAABB.left > otherAABB.right) return false;
if (myAABB.top > otherAABB.bottom || myAABB.bottom < otherAABB.top) return false;
return true;
}
/// <summary>
/// Sets this object's layer
/// </summary>
/// <param name="layer">Layer to set</param>
void Transform2D::setLayer(const LAYER layer) {
_layer = layer;
}
/// <summary>
/// Returns this object's layer
/// </summary>
/// <returns>Layer of the object</returns>
Transform2D::LAYER Transform2D::getLayer() const {
return _layer;
}