SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
TransformComponent.hpp
Go to the documentation of this file.
1#ifndef _TRANSFORM_COMPONENT_H_
2#define _TRANSFORM_COMPONENT_H_
3
4#include <ECS/Component.hpp>
5#include "Math/Vector.hpp"
6#include "Math/Quaternion.hpp"
7#include "Math/Matrix.hpp"
8#include <Memory/RefPtr.hpp>
9
10namespace Sleak {
11
12 using namespace Sleak::Math;
13
14 namespace RenderEngine { class BufferBase; };
15
16 /**
17 * @class TransformComponent
18 * @brief Represents the position, rotation, and scale of an entity in 3D space.
19 *
20 * This component is essential for positioning and orienting entities
21 * within the game world. It provides functionalities for setting and
22 * retrieving the transform properties, as well as calculating transformation
23 * matrices for rendering and physics calculations.
24 * @ingroup scene
25 */
26 class ENGINE_API TransformComponent : public Component {
27 public:
28 // Constructors
31 const Quaternion& rotation);
33 const Quaternion& rotation, const Vector3D& scale);
34 ~TransformComponent() override;
35
36 /// Allocates the transform's GPU constant buffer.
37 bool Initialize() override;
38 /// Refreshes and rebinds the transform constant buffer for this frame.
39 void Update(float DeltaTime) override;
40
41 // Transformation matrix calculation
43
44 // Transformations
45 /// Adds translation to position and refreshes the cached matrix.
46 void Translate(const Vector3D& translation);
47 void Translate(float x, float y, float z);
48 /// Composes rotation onto the current orientation.
49 void Rotate(const Quaternion& rotation);
50 /// Uniformly scales by amount along all axes.
51 void Scale(float amount);
52 /// Multiplies the current scale componentwise by scale.
53 void Scale(const Vector3D& scale);
54
55 // Setters
56 void SetPosition(const Vector3D& position);
57 void SetRotation(const Quaternion& rotation);
58 void SetScale(const Vector3D& scale);
59
60 // Direction vectors
61 Vector3D Forward() const;
62 Vector3D Right() const;
63 Vector3D Up() const;
64
65 // Rotation utilities
66 /// Rotates by angle around axis in world space.
67 void RotateAround(const Vector3D& axis, float angle);
68 /// Rotates by angle around axis expressed in the object's local space.
69 void RotateAroundLocal(const Vector3D& axis, float angle);
70 /// Orients the transform to face target, using world up as reference.
71 void LookAt(const Vector3D& target);
72
73 // Getters
76 Vector3D GetWorldScale() const;
77
78 /// Cached translation/rotation/scale matrices combined into the final transform matrix.
92
93 protected:
94 Vector3D position = Vector3D(0.0f, 0.0f, 0.0f);
96 Vector3D scale = Vector3D(1.0f, 1.0f, 1.0f);
98
99 private:
101
102 /// Rebuilds the transform matrix and pushes it to the GPU constant buffer.
103 void UpdateConstantBuffer();
104 /// Recomputes the cached Translation/Rotation/Scaling matrices from position/rotation/scale.
105 void UpdateTransform();
106 /// Combines the transform with the main camera's view and projection matrices.
107 Math::Matrix4 GetMVP();
108 };
109
110} // namespace Sleak
111
112#endif // _TRANSFORM_COMPONENT_H_
113
Component(GameObject *object)
Definition Component.hpp:61
Represents a quaternion for 3D rotations.
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
void SetRotation(const Quaternion &rotation)
void RotateAround(const Vector3D &axis, float angle)
Rotates by angle around axis in world space.
void SetPosition(const Vector3D &position)
TransformComponent(GameObject *object, const Vector3D &position)
void RotateAroundLocal(const Vector3D &axis, float angle)
Rotates by angle around axis expressed in the object's local space.
void Translate(const Vector3D &translation)
Adds translation to position and refreshes the cached matrix.
void Update(float DeltaTime) override
Refreshes and rebinds the transform constant buffer for this frame.
void SetScale(const Vector3D &scale)
bool Initialize() override
Allocates the transform's GPU constant buffer.
void Scale(float amount)
Uniformly scales by amount along all axes.
void Rotate(const Quaternion &rotation)
Composes rotation onto the current orientation.
Quaternion GetWorldRotation() const
void LookAt(const Vector3D &target)
Orients the transform to face target, using world up as reference.
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:413
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
Cached translation/rotation/scale matrices combined into the final transform matrix.