IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
rotate.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/math/common.hxx>
8
9namespace ice::math
10{
11
12 inline auto rotate2d(rad rad) noexcept -> mat<2, 2, f32>;
13 inline auto rotate(rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>;
14
15 inline auto rotate2d(mat<2, 2, f32> left, rad rad) noexcept -> mat<2, 2, f32>;
16 inline auto rotate(mat<4, 4, f32> left, rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>;
17
18 inline auto rotation(mat<4, 4, f32> const& matrix) noexcept -> vec<3, rad>;
19
20
21 inline auto rotate2d(rad rad) noexcept -> mat<2, 2, f32>
22 {
24 }
25
26 inline auto rotate(rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>
27 {
28 return rotate(mat4x4_identity, rad, v);
29 }
30
31 inline auto rotate2d(mat<2, 2, f32> left, rad rad) noexcept -> mat<2, 2, f32>
32 {
33 f32 const cosv = cos(rad);
34 f32 const sinv = sin(rad);
35
37 rm.v[0][0] = cosv;
38 rm.v[0][1] = sinv;
39 rm.v[1][0] = -sinv;
40 rm.v[1][1] = cosv;
41 return mul(left, rm);
42 }
43
44 inline auto rotate(mat<4, 4, f32> left, rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>
45 {
46 f32 const cosv = cos(rad);
47 f32 const cosv_1 = 1.f - cosv;
48 f32 const sinv = sin(rad);
49
50 vec<3, f32> const axis = normalize(v);
51 vec<3, f32> const cos1_vec = {
52 cosv_1 * axis.v[0][0],
53 cosv_1 * axis.v[0][1],
54 cosv_1 * axis.v[0][2]
55 };
56
58 rm.v[0][0] = cosv + cos1_vec.v[0][0] * axis.v[0][0];
59 rm.v[0][1] = cos1_vec.v[0][0] * axis.v[0][1] + sinv * axis.v[0][2];
60 rm.v[0][2] = cos1_vec.v[0][0] * axis.v[0][2] - sinv * axis.v[0][1];
61
62 rm.v[1][0] = cos1_vec.v[0][0] * axis.v[0][1] - sinv * axis.v[0][2];
63 rm.v[1][1] = cosv + cos1_vec.v[0][1] * axis.v[0][1];
64 rm.v[1][2] = cos1_vec.v[0][1] * axis.v[0][2] + sinv * axis.v[0][0];
65
66 rm.v[2][0] = cos1_vec.v[0][0] * axis.v[0][2] + sinv * axis.v[0][1];
67 rm.v[2][1] = cos1_vec.v[0][1] * axis.v[0][2] - sinv * axis.v[0][0];
68 rm.v[2][2] = cosv + cos1_vec.v[0][2] * axis.v[0][2];
69 return mul(left, rm);
70 }
71
72 inline auto rotation(mat<4, 4, f32> const& matrix) noexcept -> vec<3, rad>
73 {
74 rad const x = ice::math::atan2({
75 matrix.v[1][2],
76 matrix.v[2][2]
77 });
78 rad const y = ice::math::atan2({
79 -matrix.v[0][2],
80 ice::math::sqrt(matrix.v[1][2] * matrix.v[1][2] + matrix.v[2][2] * matrix.v[2][2])
81 });
82 rad const z = ice::math::atan2({
83 matrix.v[0][1],
84 matrix.v[0][0]
85 });
86 return { x, y, z };
87 }
88
89} // namespace ice::math
Definition algorithm.hxx:8
static constexpr auto mat2x2_identity
Definition matrix.hxx:77
rad32 rad
Definition angles.hxx:170
constexpr auto mul(U first, Args... args) noexcept
Definition algorithm.hxx:38
auto rotate2d(rad rad) noexcept -> mat< 2, 2, f32 >
Definition rotate.hxx:21
mat< Size, 1, T > vec
Definition vector.hxx:172
auto atan2(f32 x, f32 y) noexcept -> f32
Definition common.hxx:230
auto normalize(vec< Size, T > value) noexcept -> vec< Size, T >
Definition vector_operations.hxx:137
constexpr auto sin(rad radians) noexcept -> f32
Definition common.hxx:156
static constexpr auto mat4x4_identity
Definition matrix.hxx:79
constexpr auto cos(rad radians) noexcept -> f32
Definition common.hxx:186
auto rotate(rad rad, vec< 3, f32 > v) noexcept -> mat< 4, 4, f32 >
Definition rotate.hxx:26
constexpr auto sqrt(f32 val) noexcept -> f32
Definition common.hxx:93
auto rotation(mat< 4, 4, f32 > const &matrix) noexcept -> vec< 3, rad >
Definition rotate.hxx:72
float f32
Definition types.hxx:16
Definition matrix.hxx:12
T v[count_columns][count_rows]
Definition matrix.hxx:17