3#include <glm/gtx/transform.hpp>
4#include <glm/gtx/quaternion.hpp>
5#include "EngineCore/Reflection/ComponentReflection.hpp"
6#include "Common/Math.hpp"
8#include <EngineCore/ECS/Entity.hpp>
9#include <EngineCore/CoreComponents/Parent/ParentComponent.hpp>
13 Math::Quaternion rotation;
14 Math::Float3 position = Math::Float3(0.0f, 0.0f, 0.0f);
15 Math::Float3 scale = Math::Float3(1.f, 1.f, 1.f);
17 static Math::Matrix4 GetWorldTransformMatrix(ECS::Entity entity) {
18 return GetWorldTransformMatrix(entity.GetHandle(), entity.GetSceneEntityRegistry());
21 static Math::Matrix4 GetWorldTransformMatrix(entt::entity entity,
const entt::registry& registry) {
22 Math::Matrix4 matrix = Math::Matrix4(1.0f);
23 entt::entity currentEntity = entity;
24 while (currentEntity != entt::null) {
25 const TransformComponent& transformComp = registry.get<TransformComponent>(currentEntity);
27 matrix = transformComp.GetTransformMatrix() * matrix;
29 const ParentComponent& parentComp = registry.get<ParentComponent>(currentEntity);
30 currentEntity = parentComp.parentEntity;
36 static Math::Float3 GetWorldPosition(
const ECS::Entity entity) {
37 return GetWorldPosition(entity.GetHandle(), entity.GetSceneEntityRegistry());
40 static Math::Float3 GetWorldPosition(
const entt::entity entity,
const entt::registry& registry) {
41 Math::Matrix4 worldMatrix = GetWorldTransformMatrix(entity, registry);
43 return Math::Float3(worldMatrix[3][0], worldMatrix[3][1], worldMatrix[3][2]);
46 void SetLocalMatrix(Math::Matrix4 localMatrix) {
47 rotation = Math::Quaternion(localMatrix);
49 for (
int i = 0; i < 3; i++) {
50 position[i] = localMatrix[3][i];
53 localMatrix[i][0] * localMatrix[i][0]
54 + localMatrix[i][1] * localMatrix[i][1]
55 + localMatrix[i][2] * localMatrix[i][2]);
59 void SetWorldMatrixRelativeTo(Math::Matrix4 newWorldMatrix, Math::Matrix4 parentMatrix) {
60 Math::Matrix4 localMatrix = glm::inverse(parentMatrix) * newWorldMatrix;
61 SetLocalMatrix(localMatrix);
64 Math::Matrix4 GetTransformMatrix()
const {
65 return glm::translate(position) *
66 glm::toMat4(rotation) *
70 Math::Float3 GetForward()
const {
71 return rotation * Math::Float3(0.0f, 0.0f, 1.0f);
74 Math::Float3 GetRight()
const {
75 return rotation * Math::Float3(1.0f, 0.0f, 0.0f);
78 Math::Float3 GetUp()
const {
79 return rotation * Math::Float3(0.0f, 1.0f, 0.0f);