galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Transform.cpp
Go to the documentation of this file.
1
7
8#include <glm/gtc/type_ptr.hpp>
9#include <nlohmann/json.hpp>
10
11#include "galaxy/utils/Globals.hpp"
12
13#include "Transform.hpp"
14
15const constexpr auto identity_matrix = glm::mat4 {GALAXY_IDENTITY_MATRIX};
16const constexpr auto rotation_vec = glm::vec3 {0.0f, 0.0f, 1.0f};
17
18namespace galaxy
19{
20 namespace graphics
21 {
23 : m_pos {0.0f, 0.0f}
24 , m_rotation {0.0f}
25 , m_scale {1.0f, 1.0f}
26 , m_origin {0.0f, 0.0f}
27 , m_dirty {true}
28 , m_transform {GALAXY_IDENTITY_MATRIX}
29 {
30 }
31
33 {
34 this->m_pos = std::move(t.m_pos);
35 this->m_rotation = t.m_rotation;
36 this->m_scale = std::move(t.m_scale);
37 this->m_origin = std::move(t.m_origin);
38 this->m_dirty = t.m_dirty;
39 this->m_transform = std::move(t.m_transform);
40 }
41
43 {
44 if (this != &t)
45 {
46 this->m_pos = std::move(t.m_pos);
47 this->m_rotation = t.m_rotation;
48 this->m_scale = std::move(t.m_scale);
49 this->m_origin = std::move(t.m_origin);
50 this->m_dirty = t.m_dirty;
51 this->m_transform = std::move(t.m_transform);
52 }
53
54 return *this;
55 }
56
58 {
59 this->m_pos = t.m_pos;
60 this->m_rotation = t.m_rotation;
61 this->m_scale = t.m_scale;
62 this->m_origin = t.m_origin;
63 this->m_dirty = t.m_dirty;
64 this->m_transform = t.m_transform;
65 }
66
68 {
69 if (this != &t)
70 {
71 this->m_pos = t.m_pos;
72 this->m_rotation = t.m_rotation;
73 this->m_scale = t.m_scale;
74 this->m_origin = t.m_origin;
75 this->m_dirty = t.m_dirty;
76 this->m_transform = t.m_transform;
77 }
78
79 return *this;
80 }
81
85
86 void Transform::translate(const float x, const float y)
87 {
88 m_pos.x += x;
89 m_pos.y += y;
90
91 m_dirty = true;
92 }
93
94 void Transform::rotate(const float degrees)
95 {
96 m_rotation += degrees;
97 m_rotation = std::clamp(m_rotation, -360.0f, 360.0f);
98
99 m_dirty = true;
100 }
101
102 void Transform::scale(const float scale)
103 {
104 m_scale.x = scale;
105 m_scale.y = scale;
106
107 m_dirty = true;
108 }
109
111 {
112 m_scale.x = x;
113 m_dirty = true;
114 }
115
117 {
118 m_scale.y = y;
119 m_dirty = true;
120 }
121
122 void Transform::set_pos(const float x, const float y)
123 {
124 m_pos.x = x;
125 m_pos.y = y;
126
127 m_dirty = true;
128 }
129
130 void Transform::set_rotation(const float degrees)
131 {
132 m_rotation = std::clamp(degrees, -360.0f, 360.0f);
133 m_dirty = true;
134 }
135
136 void Transform::set_origin(const float x, const float y)
137 {
138 m_origin.x = x;
139 m_origin.y = y;
140
141 m_dirty = true;
142 }
143
145 {
146 m_pos = {0.0f, 0.0f};
147 m_rotation = 0.0f;
148 m_scale = {1.0f, 1.0f};
149
150 m_dirty = true;
151 }
152
153 const glm::vec2& Transform::get_pos() const
154 {
155 return m_pos;
156 }
157
159 {
160 return m_rotation;
161 }
162
163 const glm::vec2& Transform::get_scale() const
164 {
165 return m_scale;
166 }
167
168 const glm::vec2& Transform::get_origin() const
169 {
170 return m_origin;
171 }
172
174 {
175 if (m_dirty)
176 {
177 m_dirty = false;
178
179 const auto origin = glm::vec3 {m_origin, 0.0f};
180
181 auto rotation = glm::translate(identity_matrix, origin);
182 rotation = glm::rotate(rotation, glm::radians(m_rotation), rotation_vec);
183 rotation = glm::translate(rotation, -origin);
184
185 auto scale = glm::translate(identity_matrix, origin);
186 scale = glm::scale(scale, {m_scale, 1.0f});
187 scale = glm::translate(scale, -origin);
188
189 m_transform = glm::translate(identity_matrix, {m_pos, 0.0f}) * rotation * scale;
190 }
191
192 return m_transform;
193 }
194 } // namespace graphics
195} // namespace galaxy
Defines the 2D transformation of an entity.
Definition Transform.hpp:21
const glm::vec2 & get_origin() const
Get origin point.
void set_scale_vertical(const float y)
Set entity scale.
virtual void rotate(const float degrees)
Rotate entity.
Definition Transform.cpp:94
glm::vec2 m_origin
Transform origin point.
void set_pos(const float x, const float y)
Sets position without moving the entity.
void set_origin(const float x, const float y)
Set the origin point.
Transform & operator=(Transform &&)
Move assignment operator.
Definition Transform.cpp:42
const glm::vec2 & get_pos() const
Get stored position.
virtual void translate(const float x, const float y)
Translate (move) position.
Definition Transform.cpp:86
bool m_dirty
Flag to see if transform needs to be recalculated.
const glm::vec2 & get_scale() const
Get stored scale.
float get_rotation() const
Get stored rotation.
void set_rotation(const float degrees)
Set the entity rotation.
float m_rotation
Cached for easy retrieval. Rotation.
virtual ~Transform()
Destructor.
Definition Transform.cpp:82
void reset()
Reset transform.
glm::vec2 m_scale
Cached for easy retrieval. Scale.
void set_scale_horizontal(const float x)
Set entity scale.
void scale(const float scale)
Set entity scale.
glm::mat4 & get_transform()
Retrieve internal transformation matrix.
glm::vec2 m_pos
Cached for easy retrieval. Pos.
glm::mat4 m_transform
Combined transform.
const constexpr auto identity_matrix
Transform.cpp galaxy.
Definition Transform.cpp:15
const constexpr auto rotation_vec
Definition Transform.cpp:16
Animated.cpp galaxy.
Definition Animated.cpp:16