Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
TransformComponent.cpp
Go to the documentation of this file.
2 #include <ThirdParty/glm/glm/gtc/matrix_transform.hpp>
3 #include <ThirdParty/glm/glm/gtx/euler_angles.hpp>
4 
5 namespace Engine
6 {
7  TransformComponent::TransformComponent() noexcept : TransformComponent(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0), glm::vec3(1, 1, 1), false)
8  {
9  }
10 
11  TransformComponent::TransformComponent(bool isStatic) noexcept : TransformComponent(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0), glm::vec3(1, 1, 1), isStatic)
12  {
13  }
14 
15  TransformComponent::TransformComponent(glm::vec3 position) noexcept : TransformComponent(position, glm::vec3(0, 0, 0), glm::vec3(1, 1, 1), false)
16  {
17  }
18 
19  TransformComponent::TransformComponent(glm::vec3 position, bool isStatic) noexcept : TransformComponent(position, glm::vec3(0, 0, 0), glm::vec3(1, 1, 1), isStatic)
20  {
21  }
22 
23  TransformComponent::TransformComponent(glm::vec3 position, glm::vec3 rotation) noexcept : TransformComponent(position, rotation, glm::vec3(1, 1, 1), false)
24  {
25  }
26 
27  TransformComponent::TransformComponent(glm::vec3 position, glm::vec3 rotation, bool isStatic) noexcept : TransformComponent(position, rotation, glm::vec3(1, 1, 1), isStatic)
28  {
29  }
30 
31  TransformComponent::TransformComponent(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale) noexcept : TransformComponent(position, rotation, scale, false)
32  {
33  }
34 
35  TransformComponent::TransformComponent(glm::vec3 position, glm::vec3 rotation, glm::vec3 scale, bool isStatic) noexcept : position(position), rotation(rotation), scale(scale), isStatic(isStatic)
36  {
38  }
39 
40  void TransformComponent::SetPosition(glm::vec3 position) noexcept
41  {
42  if (isStatic) return;
43 
44  this->position = position;
45  shouldRecalculateModelMatrix = true;
46  }
47 
48  void TransformComponent::SetPosition(float x, float y, float z) noexcept
49  {
50  SetPosition(glm::vec3(x, y, z));
51  }
52 
53  glm::vec3 TransformComponent::GetPosition() noexcept
54  {
55  return position;
56  }
57 
58  void TransformComponent::SetRotation(glm::vec3 rotation) noexcept
59  {
60  if (isStatic) return;
61 
62  this->rotation = rotation;
63  shouldRecalculateModelMatrix = true;
64  }
65 
66  void TransformComponent::SetRotation(float x, float y, float z) noexcept
67  {
68  SetRotation(glm::vec3(x, y, z));
69  }
70 
71  glm::vec3 TransformComponent::GetRotation() noexcept
72  {
73  return rotation;
74  }
75 
76  void TransformComponent::SetScale(glm::vec3 scale) noexcept
77  {
78  if (isStatic) return;
79 
80  this->scale = scale;
81  shouldRecalculateModelMatrix = true;
82  }
83 
84  void TransformComponent::SetScale(float x, float y, float z) noexcept
85  {
86  SetScale(glm::vec3(x, y, z));
87  }
88 
89  glm::vec3 TransformComponent::GetScale() noexcept
90  {
91  return scale;
92  }
93 
94  void TransformComponent::SetModelMatrix(glm::mat4x4 modelMatrix) noexcept
95  {
96  if (isStatic) return;
97 
98  this->modelMatrix = modelMatrix;
99  shouldRecalculateModelMatrix = true;
100  }
101 
102  void TransformComponent::SetModelMatrix(float modelMatrix[16]) noexcept
103  {
104  SetModelMatrix(glm::mat4x4(modelMatrix[0], modelMatrix[1], modelMatrix[2], modelMatrix[3],
105  modelMatrix[4], modelMatrix[5], modelMatrix[6], modelMatrix[7],
106  modelMatrix[8], modelMatrix[9], modelMatrix[10], modelMatrix[11],
107  modelMatrix[12], modelMatrix[13], modelMatrix[14], modelMatrix[15]));
108  }
109 
110  void TransformComponent::SetModelMatrix(glm::vec4 modelMatrix[4]) noexcept
111  {
112  SetModelMatrix(glm::mat4x4(modelMatrix[0].x, modelMatrix[0].y, modelMatrix[0].z, modelMatrix[0].w,
113  modelMatrix[1].x, modelMatrix[1].y, modelMatrix[1].z, modelMatrix[1].w,
114  modelMatrix[2].x, modelMatrix[2].y, modelMatrix[2].z, modelMatrix[2].w,
115  modelMatrix[3].x, modelMatrix[3].y, modelMatrix[3].z, modelMatrix[3].w));
116  }
117 
118  glm::mat4x4 TransformComponent::GetModelMatrix() noexcept
119  {
120  return modelMatrix;
121  }
122 
123  void TransformComponent::SetIsStatic(bool isStatic) noexcept
124  {
125  this->isStatic = isStatic;
126  }
127 
129  {
130  return isStatic;
131  }
132 
133  void TransformComponent::Translate(glm::vec3 positionToAdd) noexcept
134  {
135  if (isStatic) return;
136 
137  position += positionToAdd;
138  shouldRecalculateModelMatrix = true;
139  }
140 
141  void TransformComponent::Translate(float x) noexcept
142  {
143  Translate(glm::vec3(x, 0, 0));
144  }
145 
146  void TransformComponent::Translate(float x, float y) noexcept
147  {
148  Translate(glm::vec3(x, y, 0));
149  }
150 
151  void TransformComponent::Translate(float x, float y, float z) noexcept
152  {
153  Translate(glm::vec3(x, y, z));
154  }
155 
156  void TransformComponent::AddRotate(glm::vec3 rotationToAdd) noexcept
157  {
158  if (isStatic) return;
159 
160  rotation += rotationToAdd;
161  }
162 
163  void TransformComponent::AddRotate(float x) noexcept
164  {
165  AddRotate(glm::vec3(x, 0, 0));
166  }
167 
168  void TransformComponent::AddRotate(float x, float y) noexcept
169  {
170  AddRotate(glm::vec3(x, y, 0));
171  }
172 
173  void TransformComponent::AddRotate(float x, float y, float z) noexcept
174  {
175  AddRotate(glm::vec3(x, y, z));
176  }
177 
178  void TransformComponent::AddScale(glm::vec3 scaleToAdd) noexcept
179  {
180  if (isStatic) return;
181 
182  scale += scaleToAdd;
183  shouldRecalculateModelMatrix = true;
184  }
185 
186  void TransformComponent::AddScale(float x) noexcept
187  {
188  AddScale(glm::vec3(x, 0, 0));
189  }
190 
191  void TransformComponent::AddScale(float x, float y) noexcept
192  {
193  AddScale(glm::vec3(x, y, 0));
194  }
195 
196  void TransformComponent::AddScale(float x, float y, float z) noexcept
197  {
198  AddScale(glm::vec3(x, y, z));
199  }
200 
201  glm::vec3 TransformComponent::GetRight() noexcept
202  {
203  return glm::normalize(glm::vec3(modelMatrix[0]));
204  }
205 
206  glm::vec3 TransformComponent::GetUp() noexcept
207  {
208  return glm::normalize(glm::vec3(modelMatrix[1]));
209  }
210 
211  glm::vec3 TransformComponent::GetForward() noexcept
212  {
213  return glm::normalize(glm::vec3(modelMatrix[2]));
214  }
215 
217  {
218  LookAt(target.position);
219  }
220 
221  void TransformComponent::LookAt(glm::vec3 targetPosition) noexcept
222  {
223  if (isStatic) return;
224 
225  modelMatrix *= glm::lookAt(position, targetPosition, GetUp());
226  shouldRecalculateModelMatrix = true;
227  }
228 
229  void TransformComponent::RotateAround(float angle, glm::vec3 axis) noexcept
230  {
231  if (isStatic) return;
232 
233  glm::mat4x4 rotationMatrix;
234  glm::rotate(rotationMatrix, angle, axis);
235  modelMatrix *= rotationMatrix;
236 
237  shouldRecalculateModelMatrix = true;
238  }
239 
241  {
242  if (shouldRecalculateModelMatrix == false) return;
243 
245  }
246 
248  {
249  glm::mat4x4 rotationMatrix = glm::eulerAngleYXZ(rotation.y, rotation.x, rotation.z);
250  glm::mat4x4 rotationScaleMatrix = rotationMatrix * glm::scale(rotationMatrix, scale);
251  rotationScaleMatrix[3] = glm::vec4(position, 1);
252 
253  modelMatrix = rotationScaleMatrix;
254  shouldRecalculateModelMatrix = false;
255  }
256 
258  {
259  if (position != other.position) return true;
260  if (rotation != other.rotation) return true;
261  if (scale != other.scale) return true;
262  return false;
263  }
264 
266  {
267  if (position != other.position) return false;
268  if (rotation != other.rotation) return false;
269  if (scale != other.scale) return false;
270  return true;
271  }
272 } //namespace Engine
glm::mat4x4 GetModelMatrix() noexcept
void Translate(glm::vec3 positionToAdd) noexcept
void Update() override
The update method of this component. NOTE: This method will not be called in case the entity is disab...
glm::vec3 GetPosition() noexcept
void AddRotate(glm::vec3 rotationToAdd) noexcept
glm::vec3 GetScale() noexcept
glm::vec3 GetForward() noexcept
glm::vec3 GetRotation() noexcept
void SetIsStatic(bool isStatic) noexcept
void LookAt(TransformComponent target) noexcept
void RotateAround(float angle, glm::vec3 axis) noexcept
void SetScale(glm::vec3 scale) noexcept
void SetPosition(glm::vec3 position) noexcept
void AddScale(glm::vec3 scaleToAdd) noexcept
bool operator!=(TransformComponent &other)
void SetModelMatrix(glm::mat4x4 modelMatrix) noexcept
void SetRotation(glm::vec3 rotation) noexcept
bool operator==(TransformComponent &other)