IceShard
1
A personal game engine project, with development focused on 2D/2.5D games.
Toggle main menu visibility
Loading...
Searching...
No Matches
math
public
ice
math
rotate.hxx
Go to the documentation of this file.
1
3
4
#pragma once
5
#include <
ice/math/common.hxx
>
6
#include <
ice/math/matrix/matrix_operations.hxx
>
7
#include <
ice/math/vector/vector_operations.hxx
>
8
9
namespace
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
{
23
return
rotate2d
(
mat2x2_identity
,
rad
);
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
36
mat<2, 2, f32>
rm =
mat2x2_identity
;
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
57
mat<4, 4, f32>
rm =
mat4x4_identity
;
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
common.hxx
matrix_operations.hxx
ice::math
Definition
algorithm.hxx:8
ice::math::mat2x2_identity
static constexpr auto mat2x2_identity
Definition
matrix.hxx:77
ice::math::rad
rad32 rad
Definition
angles.hxx:170
ice::math::mul
constexpr auto mul(U first, Args... args) noexcept
Definition
algorithm.hxx:38
ice::math::rotate2d
auto rotate2d(rad rad) noexcept -> mat< 2, 2, f32 >
Definition
rotate.hxx:21
ice::math::vec
mat< Size, 1, T > vec
Definition
vector.hxx:172
ice::math::atan2
auto atan2(f32 x, f32 y) noexcept -> f32
Definition
common.hxx:230
ice::math::normalize
auto normalize(vec< Size, T > value) noexcept -> vec< Size, T >
Definition
vector_operations.hxx:137
ice::math::sin
constexpr auto sin(rad radians) noexcept -> f32
Definition
common.hxx:156
ice::math::mat4x4_identity
static constexpr auto mat4x4_identity
Definition
matrix.hxx:79
ice::math::cos
constexpr auto cos(rad radians) noexcept -> f32
Definition
common.hxx:186
ice::math::rotate
auto rotate(rad rad, vec< 3, f32 > v) noexcept -> mat< 4, 4, f32 >
Definition
rotate.hxx:26
ice::math::sqrt
constexpr auto sqrt(f32 val) noexcept -> f32
Definition
common.hxx:93
ice::math::rotation
auto rotation(mat< 4, 4, f32 > const &matrix) noexcept -> vec< 3, rad >
Definition
rotate.hxx:72
ice::f32
float f32
Definition
types.hxx:16
ice::math::mat
Definition
matrix.hxx:12
ice::math::mat::v
T v[count_columns][count_rows]
Definition
matrix.hxx:17
vector_operations.hxx
Generated by
1.18.0