SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
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
10
namespace
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
29
TransformComponent
(
GameObject
*
object
,
const
Vector3D
&
position
);
30
TransformComponent
(
GameObject
*
object
,
const
Vector3D
&
position
,
31
const
Quaternion
&
rotation
);
32
TransformComponent
(
GameObject
*
object
,
const
Vector3D
&
position
,
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
42
Matrix4
GetTransformMatrix
();
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
74
Vector3D
GetWorldPosition
()
const
;
75
Quaternion
GetWorldRotation
()
const
;
76
Vector3D
GetWorldScale
()
const
;
77
78
/// Cached translation/rotation/scale matrices combined into the final transform matrix.
79
struct
TransformMatrix
{
80
81
TransformMatrix
() :
Translation
(
Matrix4
::Identity()),
Rotation
(
Matrix4
::Identity()),
Scaling
(
Matrix4
::Identity()) {}
82
83
Math::Matrix4
CalculateTransformMatrix
()
const
{
84
return
(
Scaling
*
Rotation
) *
Translation
;
85
}
86
87
Math::Matrix4
Translation
;
88
Math::Matrix4
Rotation
;
89
Math::Matrix4
Scaling
;
90
91
};
92
93
protected
:
94
Vector3D
position
=
Vector3D
(0.0f, 0.0f, 0.0f);
95
Quaternion
rotation
=
Quaternion
();
96
Vector3D
scale
=
Vector3D
(1.0f, 1.0f, 1.0f);
97
TransformMatrix
*
Transform
=
nullptr
;
98
99
private
:
100
RefPtr<RenderEngine::BufferBase>
ConstantBuffer;
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.hpp
Matrix.hpp
Quaternion.hpp
RefPtr.hpp
Vector.hpp
Sleak::Component::GameObject
friend class GameObject
Definition
Component.hpp:88
Sleak::Component::Component
Component(GameObject *object)
Definition
Component.hpp:61
Sleak::Math::Quaternion
Represents a quaternion for 3D rotations.
Definition
Quaternion.hpp:26
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::RefPtr
Definition
RefPtr.hpp:62
Sleak::RenderEngine::BufferBase
Backend-agnostic GPU buffer: vertex, index, constant, or resource view target.
Definition
BufferBase.hpp:16
Sleak::TransformComponent::SetRotation
void SetRotation(const Quaternion &rotation)
Definition
TransformComponent.cpp:93
Sleak::TransformComponent::RotateAround
void RotateAround(const Vector3D &axis, float angle)
Rotates by angle around axis in world space.
Definition
TransformComponent.cpp:115
Sleak::TransformComponent::SetPosition
void SetPosition(const Vector3D &position)
Definition
TransformComponent.cpp:89
Sleak::TransformComponent::rotation
Quaternion rotation
Definition
TransformComponent.hpp:95
Sleak::TransformComponent::Up
Vector3D Up() const
Definition
TransformComponent.cpp:110
Sleak::TransformComponent::GetWorldScale
Vector3D GetWorldScale() const
Definition
TransformComponent.cpp:138
Sleak::TransformComponent::TransformComponent
TransformComponent(GameObject *object, const Vector3D &position)
Definition
TransformComponent.cpp:12
Sleak::TransformComponent::scale
Vector3D scale
Definition
TransformComponent.hpp:96
Sleak::TransformComponent::position
Vector3D position
Definition
TransformComponent.hpp:94
Sleak::TransformComponent::RotateAroundLocal
void RotateAroundLocal(const Vector3D &axis, float angle)
Rotates by angle around axis expressed in the object's local space.
Definition
TransformComponent.cpp:119
Sleak::TransformComponent::Transform
TransformMatrix * Transform
Definition
TransformComponent.hpp:97
Sleak::TransformComponent::Translate
void Translate(const Vector3D &translation)
Adds translation to position and refreshes the cached matrix.
Definition
TransformComponent.cpp:63
Sleak::TransformComponent::GetTransformMatrix
Matrix4 GetTransformMatrix()
Definition
TransformComponent.cpp:58
Sleak::TransformComponent::Forward
Vector3D Forward() const
Definition
TransformComponent.cpp:102
Sleak::TransformComponent::Update
void Update(float DeltaTime) override
Refreshes and rebinds the transform constant buffer for this frame.
Definition
TransformComponent.cpp:51
Sleak::TransformComponent::SetScale
void SetScale(const Vector3D &scale)
Definition
TransformComponent.cpp:97
Sleak::TransformComponent::Initialize
bool Initialize() override
Allocates the transform's GPU constant buffer.
Definition
TransformComponent.cpp:34
Sleak::TransformComponent::Scale
void Scale(float amount)
Uniformly scales by amount along all axes.
Definition
TransformComponent.cpp:79
Sleak::TransformComponent::Rotate
void Rotate(const Quaternion &rotation)
Composes rotation onto the current orientation.
Definition
TransformComponent.cpp:74
Sleak::TransformComponent::Right
Vector3D Right() const
Definition
TransformComponent.cpp:106
Sleak::TransformComponent::GetWorldPosition
Vector3D GetWorldPosition() const
Definition
TransformComponent.cpp:130
Sleak::TransformComponent::GetWorldRotation
Quaternion GetWorldRotation() const
Definition
TransformComponent.cpp:134
Sleak::TransformComponent::LookAt
void LookAt(const Vector3D &target)
Orients the transform to face target, using world up as reference.
Definition
TransformComponent.cpp:123
Sleak::Math::Matrix4
Matrix< float, 4, 4 > Matrix4
Definition
Matrix.hpp:413
Sleak::RenderEngine
Backend-facing rendering layer shared by the four graphics backends.
Definition
Application.hpp:60
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::TransformComponent::TransformMatrix
Cached translation/rotation/scale matrices combined into the final transform matrix.
Definition
TransformComponent.hpp:79
Sleak::TransformComponent::TransformMatrix::Scaling
Math::Matrix4 Scaling
Definition
TransformComponent.hpp:89
Sleak::TransformComponent::TransformMatrix::TransformMatrix
TransformMatrix()
Definition
TransformComponent.hpp:81
Sleak::TransformComponent::TransformMatrix::Rotation
Math::Matrix4 Rotation
Definition
TransformComponent.hpp:88
Sleak::TransformComponent::TransformMatrix::CalculateTransformMatrix
Math::Matrix4 CalculateTransformMatrix() const
Definition
TransformComponent.hpp:83
Sleak::TransformComponent::TransformMatrix::Translation
Math::Matrix4 Translation
Definition
TransformComponent.hpp:87
include
public
ECS
Components
TransformComponent.hpp
Generated on
for SleakEngine by
1.18.0