sandbox
Loading...
Searching...
No Matches
transform.hpp
1#ifndef LIBSBX_SCENES_COMPONENTS_TRANSFORM_HPP_
2#define LIBSBX_SCENES_COMPONENTS_TRANSFORM_HPP_
3
4#include <numbers>
5
6#include <libsbx/math/vector3.hpp>
7#include <libsbx/math/matrix4x4.hpp>
8#include <libsbx/math/angle.hpp>
9#include <libsbx/math/quaternion.hpp>
10
11namespace sbx::math {
12
13class transform final {
14
15public:
16
17 transform(const vector3& position = vector3::zero, const quaternion& rotation = quaternion::identity, const vector3& scale = vector3::one)
18 : _position{position},
19 _rotation{rotation},
20 _scale{scale},
21 _rotation_matrix{_rotation.to_matrix()},
22 _is_dirty{true} { }
23
24 ~transform() = default;
25
26 auto operator=(const matrix4x4& matrix) -> transform& {
27 return *this;
28 }
29
30 auto position() const noexcept -> const vector3& {
31 return _position;
32 }
33
34 auto position() noexcept -> vector3& {
35 _is_dirty = true;
36 return _position;
37 }
38
39 auto set_position(const vector3& position) noexcept -> void {
40 _position = position;
41 _is_dirty = true;
42 }
43
44 auto move_by(const vector3& offset) noexcept -> void {
45 _position += offset;
46 _is_dirty = true;
47 }
48
49 auto rotation() const noexcept -> const quaternion& {
50 return _rotation;
51 }
52
53 auto set_rotation(const quaternion& rotation) noexcept -> void {
54 _rotation = rotation;
55 _rotation_matrix = _rotation.to_matrix();
56 _is_dirty = true;
57 }
58
59 auto set_rotation(const vector3& axis, const angle& angle) noexcept -> void {
60 _rotation = quaternion{axis, angle};
61 _rotation_matrix = _rotation.to_matrix();
62 _is_dirty = true;
63 }
64
65 auto scale() noexcept -> vector3& {
66 _is_dirty = true;
67 return _scale;
68 }
69
70 auto scale() const noexcept -> const vector3& {
71 return _scale;
72 }
73
74
75 auto set_scale(const vector3& scale) noexcept -> void {
76 _scale = scale;
77 _is_dirty = true;
78 }
79
80 auto forward() const noexcept -> vector3 {
81 return -vector3{_rotation_matrix[2]};
82 }
83
84 auto right() const noexcept -> vector3 {
85 return vector3{_rotation_matrix[0]};
86 }
87
88 auto up() const noexcept -> vector3 {
89 return vector3{_rotation_matrix[1]};
90 }
91
92 auto look_at(const vector3& target) noexcept -> void {
93 // [TODO] : Figure out how to directly construct the rotation_matrix
94 auto result = matrix4x4::look_at(_position, target, vector3::up);
95 _rotation = quaternion{math::matrix4x4::inverted(result)};
96 _rotation_matrix = _rotation.to_matrix();
97 _is_dirty = true;
98 }
99
100 auto as_matrix() const -> matrix4x4 {
101 const auto translation = matrix4x4::translated(matrix4x4::identity, _position);
102 const auto scale = matrix4x4::scaled(matrix4x4::identity, _scale);
103
104 return translation * _rotation_matrix * scale;
105 }
106
107 auto is_dirty() const noexcept -> bool {
108 return _is_dirty;
109 }
110
111 auto clear_is_dirty() noexcept -> void {
112 _is_dirty = false;
113 }
114
115private:
116
117 vector3 _position;
118 quaternion _rotation;
119 vector3 _scale;
120
121 matrix4x4 _rotation_matrix;
122
123 bool _is_dirty;
124
125}; // class transform
126
127} // namespace sbx::math
128
129#endif // LIBSBX_SCENES_COMPONENTS_TRANSFORM_HPP_
Definition: angle.hpp:271
Definition: matrix4x4.hpp:25
Definition: quaternion.hpp:24
Definition: vector3.hpp:22
Definition: transform.hpp:13