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
23 ~transform() = default;
24
25 auto position() const noexcept -> const vector3& {
26 return _position;
27 }
28
29 auto position() noexcept -> vector3& {
30 return _position;
31 }
32
33 auto set_position(const vector3& position) noexcept -> void {
34 _position = position;
35 }
36
37 auto move_by(const vector3& offset) noexcept -> void {
38 _position += offset;
39 }
40
41 auto rotation() const noexcept -> const quaternion& {
42 return _rotation;
43 }
44
45 auto set_rotation(const quaternion& rotation) noexcept -> void {
46 _rotation = rotation;
47 _rotation_matrix = _rotation.to_matrix();
48 }
49
50 auto set_rotation(const vector3& axis, const angle& angle) noexcept -> void {
51 _rotation = quaternion{axis, angle};
52 _rotation_matrix = _rotation.to_matrix();
53 }
54
55 auto scale() const noexcept -> const vector3& {
56 return _scale;
57 }
58
59 auto scale() noexcept -> vector3& {
60 return _scale;
61 }
62
63 auto set_scale(const vector3& scale) noexcept -> void {
64 _scale = scale;
65 }
66
67 auto forward() const noexcept -> vector3 {
68 return -vector3{_rotation_matrix[2]};
69 }
70
71 auto right() const noexcept -> vector3 {
72 return vector3{_rotation_matrix[0]};
73 }
74
75 auto up() const noexcept -> vector3 {
76 return vector3{_rotation_matrix[1]};
77 }
78
79 auto look_at(const vector3& target) noexcept -> void {
80 auto result = matrix4x4::look_at(_position, target, vector3::up);
81 _rotation = quaternion{result};
82 _rotation_matrix = _rotation.to_matrix();
83 }
84
85 auto as_matrix() const -> matrix4x4 {
86 const auto translation = matrix4x4::translated(matrix4x4::identity, _position);
87 const auto scale = matrix4x4::scaled(matrix4x4::identity, _scale);
88
89 return translation * _rotation_matrix * scale;
90 }
91
92private:
93
94 vector3 _position;
95 quaternion _rotation;
96 vector3 _scale;
97
98 matrix4x4 _rotation_matrix;
99
100}; // class transform
101
102} // namespace sbx::math
103
104#endif // LIBSBX_SCENES_COMPONENTS_TRANSFORM_HPP_
Definition: angle.hpp:251
Definition: matrix4x4.hpp:24
Definition: quaternion.hpp:22
Definition: vector3.hpp:22
Definition: transform.hpp:13