CSEngine
Loading...
Searching...
No Matches
TransformComponent.cpp
1#include "TransformComponent.h"
2
3using namespace CSE;
4
5COMPONENT_CONSTRUCTOR(TransformComponent) {
6 m_position = vec3{ 0, 0, 0 };
7 m_rotation = Quaternion();
8 m_scale = vec3{ 1, 1, 1 };
9}
10
11
12TransformComponent::~TransformComponent() = default;
13
14
15void TransformComponent::Init() {
16}
17
18
19void TransformComponent::Tick(float elapsedTime) {
20
21 m_f_matrix = GetFinalMatrix();
22 m_f_position = vec3{ m_f_matrix.w.x, m_f_matrix.w.y, m_f_matrix.w.z };
23 m_f_scale = vec3{ m_f_matrix.x.x, m_f_matrix.y.y, m_f_matrix.z.z };
24
25}
26
27
28void TransformComponent::Exterminate() {
29}
30
31mat4 TransformComponent::GetMatrix() const {
32 return m_f_matrix;
33}
34
35vec3* TransformComponent::GetPosition() {
36 return &m_f_position;
37}
38
39vec3 TransformComponent::GetScale() const {
40 return m_f_scale;
41}
42
43Quaternion TransformComponent::GetRotation() const {
44
45 Quaternion parent = Quaternion();
46 if (gameObject->GetParent() != nullptr) {
47 parent = static_cast<TransformComponent*>(gameObject->GetParent()->GetTransform())->GetRotation();
48 }
49
50
51 return m_rotation.Multiplied(parent);
52}
53
54void TransformComponent::SetMatrix(const mat4& matrix) {
55 m_position = vec3{ matrix.w.x, matrix.w.y, matrix.w.z };
56 m_scale = vec3{ matrix.x.x, matrix.y.y, matrix.z.z };
57 m_rotation = Quaternion::ToQuaternion(matrix);
58
59}
60
61mat4 TransformComponent::GetFinalMatrix() const {
62 mat4 scale = mat4::Scale(m_scale.x, m_scale.y, m_scale.z);
63 mat4 translation = mat4::Translate(m_position.x, m_position.y, m_position.z);
64 //юс╫ц rotation
65 // mat4 rotationY = mat4::RotateY(m_rotation.y);
66 mat4 rotation = mat4(m_rotation.ToMatrix3());
67 // vec3 rotation_vec = m_rotation.ToEulerAngle();
68 // mat4 rotation = mat4::RotateX(rotation_vec.x) * mat4::RotateY(rotation_vec.y) * mat4::RotateZ(rotation_vec.z);
69
70 mat4 parentMatrix = mat4::Identity();
71 if (gameObject->GetParent() != nullptr) {
72 parentMatrix = static_cast<TransformComponent*>(gameObject->GetParent()->GetTransform())->GetMatrix();
73 }
74
75
76 return scale * rotation * translation * parentMatrix;
77}
78
79SComponent* TransformComponent::Clone(SGameObject* object) {
80 INIT_COMPONENT_CLONE(TransformComponent, clone);
81
82 clone->m_position = vec3(m_position);
83 clone->m_scale = vec3(m_scale);
84 clone->m_rotation = Quaternion(m_rotation);
85
86 return clone;
87}
88
89void TransformComponent::SetValue(std::string name_str, Arguments value) {
90 if (name_str == "m_position") {
91 m_position = vec3(std::stof(value[0]), std::stof(value[1]), std::stof(value[2]));
92 } else if (name_str == "m_scale") {
93 m_scale = vec3(std::stof(value[0]), std::stof(value[1]), std::stof(value[2]));
94 } else if (name_str == "m_rotation") {
95 m_rotation = Quaternion(std::stof(value[0]), std::stof(value[1]), std::stof(value[2]), std::stof(value[3]));
96 }
97}
98
99std::string TransformComponent::PrintValue() const {
100 PRINT_START("component");
101
102 PRINT_VALUE(m_position, m_position.x, ' ', m_position.y, ' ', m_position.z);
103 PRINT_VALUE(m_scale, m_scale.x, ' ', m_scale.y, ' ', m_scale.z);
104 PRINT_VALUE(m_rotation, m_rotation.x, ' ', m_rotation.y, ' ', m_rotation.z, ' ', m_rotation.w);
105
106 PRINT_END("component");
107}