12 struct TransformComponent {
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.try_get<TransformComponent>(currentEntity);
26 if (transformComp ==
nullptr) {
30 matrix = transformComp->GetTransformMatrix() * matrix;
33 if (parentComp ==
nullptr) {
37 currentEntity = parentComp->parentEntity;
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) {
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) *