Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Transform.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17#include "../../core/Types.hpp"
19#include "../../math/Math.hpp"
20
21#include <cstring>
22#include <limits>
23
24namespace esengine::ecs {
25
44ES_COMPONENT(stability=public)
45struct Transform {
46 ES_PROPERTY(animatable, anim_override, replicated, tooltip="Local position in world units, relative to the parent.")
47 glm::vec3 position{0.0f, 0.0f, 0.0f};
48
49 ES_PROPERTY(animatable, anim_override, replicated, tooltip="Local rotation in degrees, applied X then Y then Z. 2D content turns about Z alone.")
50 glm::quat rotation{1.0f, 0.0f, 0.0f, 0.0f};
51
52 ES_PROPERTY(animatable, anim_override, replicated, tooltip="Local scale per axis (1 = original size; negative flips).")
53 glm::vec3 scale{1.0f, 1.0f, 1.0f};
54
55 ES_PROPERTY(readonly)
56 glm::vec3 worldPosition{0.0f, 0.0f, 0.0f};
57
58 ES_PROPERTY(readonly)
59 glm::quat worldRotation{1.0f, 0.0f, 0.0f, 0.0f};
60
61 ES_PROPERTY(readonly)
62 glm::vec3 worldScale{1.0f, 1.0f, 1.0f};
63
64 // Beside the world fields, ahead of the matrix: composition sets it with every world transform it writes, and here it sits
65 // in the cache line it already wrote, not in a line of the matrix that a root never reads.
66 bool decomposed_ = true;
67 glm::mat4 cachedMatrix_{std::numeric_limits<f32>::quiet_NaN()};
68
75
82 bool auditSame(const Transform& kept) const {
83 const auto same = [](const auto& a, const auto& b) { return std::memcmp(&a, &b, sizeof(a)) == 0; };
84 if (!same(position, kept.position) || !same(rotation, kept.rotation) || !same(scale, kept.scale)
85 || !same(cachedMatrix_, kept.cachedMatrix_)) {
86 return false;
87 }
88 if (!decomposed_ || !kept.decomposed_) return true;
89 return same(worldPosition, kept.worldPosition) && same(worldRotation, kept.worldRotation)
90 && same(worldScale, kept.worldScale);
91 }
92
93 Transform() = default;
94
95 explicit Transform(const glm::vec3& pos) : position(pos) {}
96
97 Transform(const glm::vec3& pos, const glm::quat& rot)
98 : position(pos), rotation(rot) {}
99
100 Transform(const glm::vec3& pos, const glm::quat& rot, const glm::vec3& scl)
101 : position(pos), rotation(rot), scale(scl) {}
102};
103
105
107
108} // namespace esengine::ecs
Math utilities and GLM configuration for ESEngine.
Reflection and metadata system for automatic binding generation.
#define ES_COMPONENT(...)
Mark a struct/class as a component that should be exposed to scripts.
#define ES_PROPERTY(...)
Mark a field as a property that should be exposed to scripts.
Core type definitions and utilities for ESEngine.
void decompose(const glm::mat4 &matrix, glm::vec3 &position, glm::vec3 &rotation, glm::vec3 &scale)
Decomposes a transformation matrix into components (Euler angles).
Definition Math.hpp:248
Transform(const glm::vec3 &pos, const glm::quat &rot, const glm::vec3 &scl)
bool auditSame(const Transform &kept) const
What the change audit takes as unchanged since kept (Registry::auditChanges).
Definition Transform.hpp:82
Transform(const glm::vec3 &pos, const glm::quat &rot)
Definition Transform.hpp:97
Transform(const glm::vec3 &pos)
Definition Transform.hpp:95