Khaotic Engine Reborn
Loading...
Searching...
No Matches
transform_component.h
1#pragma once
2#include "../component.h"
3#include <DirectXMath.h>
4
5using namespace DirectX;
6
7namespace ecs {
8
10public:
11
17 m_ScaleMatrix = XMMatrixIdentity();
18 m_RotateMatrix = XMMatrixIdentity();
19 m_TranslateMatrix = XMMatrixIdentity();
20 m_WorldMatrix = XMMatrixIdentity();
21 }
22
23 ~TransformComponent() = default;
24
29 void SetPosition(XMVECTOR position) {
30 XMFLOAT4X4 matrix;
31 XMStoreFloat4x4(&matrix, m_TranslateMatrix);
32 matrix._41 = XMVectorGetX(position);
33 matrix._42 = XMVectorGetY(position);
34 matrix._43 = XMVectorGetZ(position);
35 m_TranslateMatrix = XMLoadFloat4x4(&matrix);
37 }
38
43 void SetRotation(XMVECTOR rotation) {
44 m_RotateMatrix = XMMatrixRotationRollPitchYawFromVector(rotation);
46 }
47
52 void SetScale(XMVECTOR scale) {
53 XMFLOAT4X4 matrix;
54 XMStoreFloat4x4(&matrix, m_ScaleMatrix);
55 matrix._11 = XMVectorGetX(scale);
56 matrix._22 = XMVectorGetY(scale);
57 matrix._33 = XMVectorGetZ(scale);
58 m_ScaleMatrix = XMLoadFloat4x4(&matrix);
60 }
61
66 XMVECTOR GetPosition() const {
67 XMFLOAT4X4 matrix;
68 XMStoreFloat4x4(&matrix, m_TranslateMatrix);
69 return XMVectorSet(matrix._41, matrix._42, matrix._43, 0.0f);
70 }
71
76 XMVECTOR GetRotation() const {
77 XMFLOAT4X4 matrix;
78 XMStoreFloat4x4(&matrix, m_RotateMatrix);
79 float rotationX = atan2f(matrix._32, matrix._33);
80 float rotationY = atan2f(-matrix._31, sqrtf(matrix._32 * matrix._32 + matrix._33 * matrix._33));
81 float rotationZ = atan2f(matrix._21, matrix._11);
82 return XMVectorSet(rotationX, rotationY, rotationZ, 0.0f);
83 }
84
89 XMVECTOR GetScale() const {
90 XMFLOAT4X4 matrix;
91 XMStoreFloat4x4(&matrix, m_ScaleMatrix);
92
93 XMVECTOR row1 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._11));
94 XMVECTOR row2 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._21));
95 XMVECTOR row3 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._31));
96
97 XMVECTOR scale = XMVectorSet(
98 XMVectorGetX(XMVector3Length(row1)),
99 XMVectorGetX(XMVector3Length(row2)),
100 XMVectorGetX(XMVector3Length(row3)),
101 0.0f
102 );
103
104 return scale;
105 }
106
112 m_WorldMatrix = m_ScaleMatrix * m_RotateMatrix * m_TranslateMatrix;
113 }
114
119 XMMATRIX GetScaleMatrix() const { return m_ScaleMatrix; }
124 XMMATRIX GetRotateMatrix() const { return m_RotateMatrix; }
129 XMMATRIX GetTranslateMatrix() const { return m_TranslateMatrix; }
135 XMMATRIX GetWorldMatrix() const { return m_WorldMatrix; }
136
142 void SetScaleMatrix(XMMATRIX matrix) { m_ScaleMatrix = matrix; UpdateWorldMatrix(); }
148 void SetRotateMatrix(XMMATRIX matrix) { m_RotateMatrix = matrix; UpdateWorldMatrix(); }
154 void SetTranslateMatrix(XMMATRIX matrix) { m_TranslateMatrix = matrix; UpdateWorldMatrix(); }
155
156private:
157 XMMATRIX m_ScaleMatrix;
158 XMMATRIX m_RotateMatrix;
159 XMMATRIX m_TranslateMatrix;
160 XMMATRIX m_WorldMatrix;
161};
162
163} // namespace ecs
XMMATRIX GetTranslateMatrix() const
void SetRotateMatrix(XMMATRIX matrix)
void SetRotation(XMVECTOR rotation)
void SetScaleMatrix(XMMATRIX matrix)
void SetScale(XMVECTOR scale)
void SetPosition(XMVECTOR position)
void SetTranslateMatrix(XMMATRIX matrix)
Definition component.h:9