13 Math::Quaternion rotation = Math::Quaternion(0.0f, 0.0f, 0.0f, 1.0f);
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) {
26 if (transformComp ==
nullptr) {
30 matrix = transformComp->GetTransformMatrix() * matrix;
33 if (parentComp ==
nullptr) {
37 currentEntity = parentComp->parentEntity;
43 static Math::Float3 GetWorldPosition(
const ECS::Entity entity) {
44 return GetWorldPosition(entity.GetHandle(), entity.GetSceneEntityRegistry());
47 static Math::Float3 GetWorldPosition(
const entt::entity entity,
const entt::registry& registry) {
48 Math::Matrix4 worldMatrix = GetWorldTransformMatrix(entity, registry);
50 return Math::Float3(worldMatrix[3][0], worldMatrix[3][1], worldMatrix[3][2]);
53 void SetLocalMatrix(Math::Matrix4 localMatrix) {
54 rotation = Math::Quaternion(localMatrix);
56 for (
int i = 0; i < 3; i++) {
57 position[i] = localMatrix[3][i];
60 localMatrix[i][0] * localMatrix[i][0]
61 + localMatrix[i][1] * localMatrix[i][1]
62 + localMatrix[i][2] * localMatrix[i][2]);
66 void SetWorldMatrixRelativeTo(Math::Matrix4 newWorldMatrix, Math::Matrix4 parentMatrix) {
67 Math::Matrix4 localMatrix = glm::inverse(parentMatrix) * newWorldMatrix;
68 SetLocalMatrix(localMatrix);
71 Math::Matrix4 GetTransformMatrix()
const {
72 return glm::translate(position) *
73 glm::toMat4(rotation) *
77 Math::Float3 GetForward()
const {
78 return rotation * Math::Float3(0.0f, 0.0f, 1.0f);
81 Math::Float3 GetRight()
const {
82 return rotation * Math::Float3(1.0f, 0.0f, 0.0f);
85 Math::Float3 GetUp()
const {
86 return rotation * Math::Float3(0.0f, 1.0f, 0.0f);