# Khaotic Engine Reborn: enginecustom/src/inc/system/ecs/components/transform_component.h Source File

Loading...
Searching...
No Matches
transform_component.h
#pragma once
#include "../component.h"
#include <DirectXMath.h>
using namespace DirectX;
namespace ecs {
class TransformComponent : public Component {
public:
TransformComponent() {
m_ScaleMatrix = XMMatrixIdentity();
m_RotateMatrix = XMMatrixIdentity();
m_TranslateMatrix = XMMatrixIdentity();
m_WorldMatrix = XMMatrixIdentity();
}
~TransformComponent() = default;
void SetPosition(XMVECTOR position) {
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, m_TranslateMatrix);
matrix._41 = XMVectorGetX(position);
matrix._42 = XMVectorGetY(position);
matrix._43 = XMVectorGetZ(position);
m_TranslateMatrix = XMLoadFloat4x4(&matrix);
UpdateWorldMatrix();
}
void SetRotation(XMVECTOR rotation) {
m_RotateMatrix = XMMatrixRotationRollPitchYawFromVector(rotation);
UpdateWorldMatrix();
}
void SetScale(XMVECTOR scale) {
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, m_ScaleMatrix);
matrix._11 = XMVectorGetX(scale);
matrix._22 = XMVectorGetY(scale);
matrix._33 = XMVectorGetZ(scale);
m_ScaleMatrix = XMLoadFloat4x4(&matrix);
UpdateWorldMatrix();
}
XMVECTOR GetPosition() const {
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, m_TranslateMatrix);
return XMVectorSet(matrix._41, matrix._42, matrix._43, 0.0f);
}
XMVECTOR GetRotation() const {
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, m_RotateMatrix);
float rotationX = atan2f(matrix._32, matrix._33);
float rotationY = atan2f(-matrix._31, sqrtf(matrix._32 * matrix._32 + matrix._33 * matrix._33));
float rotationZ = atan2f(matrix._21, matrix._11);
return XMVectorSet(rotationX, rotationY, rotationZ, 0.0f);
}
XMVECTOR GetScale() const {
XMFLOAT4X4 matrix;
XMStoreFloat4x4(&matrix, m_ScaleMatrix);
XMVECTOR row1 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._11));
XMVECTOR row2 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._21));
XMVECTOR row3 = XMLoadFloat3(reinterpret_cast<XMFLOAT3*>(&matrix._31));
XMVECTOR scale = XMVectorSet(
XMVectorGetX(XMVector3Length(row1)),
XMVectorGetX(XMVector3Length(row2)),
XMVectorGetX(XMVector3Length(row3)),
0.0f
);
return scale;
}
void UpdateWorldMatrix() {
m_WorldMatrix = m_ScaleMatrix * m_RotateMatrix * m_TranslateMatrix;
}
XMMATRIX GetScaleMatrix() const { return m_ScaleMatrix; }
XMMATRIX GetRotateMatrix() const { return m_RotateMatrix; }
XMMATRIX GetTranslateMatrix() const { return m_TranslateMatrix; }
XMMATRIX GetWorldMatrix() const { return m_WorldMatrix; }
void SetScaleMatrix(XMMATRIX matrix) { m_ScaleMatrix = matrix; UpdateWorldMatrix(); }
void SetRotateMatrix(XMMATRIX matrix) { m_RotateMatrix = matrix; UpdateWorldMatrix(); }
void SetTranslateMatrix(XMMATRIX matrix) { m_TranslateMatrix = matrix; UpdateWorldMatrix(); }
private:
XMMATRIX m_ScaleMatrix;
XMMATRIX m_RotateMatrix;
XMMATRIX m_TranslateMatrix;
XMMATRIX m_WorldMatrix;
};
} // namespace ecs
ecs::Component
Definition component.h:12
ecs::TransformComponent
Definition transform_component.h:9
ecs::TransformComponent::GetWorldMatrix
XMMATRIX GetWorldMatrix() const
Definition transform_component.h:135
ecs::TransformComponent::UpdateWorldMatrix
void UpdateWorldMatrix()
Definition transform_component.h:111
ecs::TransformComponent::GetPosition
XMVECTOR GetPosition() const
Definition transform_component.h:66
ecs::TransformComponent::GetTranslateMatrix
XMMATRIX GetTranslateMatrix() const
Definition transform_component.h:129
ecs::TransformComponent::GetRotation
XMVECTOR GetRotation() const
Definition transform_component.h:76
ecs::TransformComponent::SetRotateMatrix
void SetRotateMatrix(XMMATRIX matrix)
Definition transform_component.h:148
ecs::TransformComponent::TransformComponent
TransformComponent()
Definition transform_component.h:16
ecs::TransformComponent::SetRotation
void SetRotation(XMVECTOR rotation)
Definition transform_component.h:43
ecs::TransformComponent::SetScaleMatrix
void SetScaleMatrix(XMMATRIX matrix)
Definition transform_component.h:142
ecs::TransformComponent::SetScale
void SetScale(XMVECTOR scale)
Definition transform_component.h:52
ecs::TransformComponent::GetScaleMatrix
XMMATRIX GetScaleMatrix() const
Definition transform_component.h:119
ecs::TransformComponent::SetPosition
void SetPosition(XMVECTOR position)
Definition transform_component.h:29
ecs::TransformComponent::SetTranslateMatrix
void SetTranslateMatrix(XMMATRIX matrix)
Definition transform_component.h:154
ecs::TransformComponent::GetRotateMatrix
XMMATRIX GetRotateMatrix() const
Definition transform_component.h:124
ecs::TransformComponent::GetScale
XMVECTOR GetScale() const
Definition transform_component.h:89
ecs
Definition component.h:9