wren
Vulkan-based game engine
Loading...
Searching...
No Matches
quaternion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "matrix.hpp"
4#include "vector.hpp"
5
6namespace wren::math {
7
8template <typename T>
9struct Quaternion {
10 static auto identity() { return Quaternion<T>({0, 0, 0, 1}); }
11
12 Quaternion() : data({0, 0, 0, 1}) {}
13 Quaternion(T x, T y, T z, T w) : data({x, y, z, w}) {}
14
15 Quaternion(Vec<T, 3> euler_angles) {
16 const auto roll = euler_angles.data.at(0);
17 const auto pitch = euler_angles.data.at(1);
18 const auto yaw = euler_angles.data.at(2);
19
20 const auto cr = std::cos(roll * 0.5f);
21 const auto sr = std::sin(roll * 0.5f);
22 const auto cp = std::cos(pitch * 0.5f);
23 const auto sp = std::sin(pitch * 0.5f);
24 const auto cy = std::cos(yaw * 0.5f);
25 const auto sy = std::sin(yaw * 0.5f);
26
27 data.at(0) = sr * cp * cy - cr * sp * sy;
28 data.at(1) = cr * sp * cy + sr * cp * sy;
29 data.at(2) = cr * cp * sy - sr * sp * cy;
30 data.at(3) = cr * cp * cy + sr * sp * sy;
31 }
32
33 [[nodiscard]] auto x() const { return data.at(0); }
34 auto x(T x) { data.at(0) = x; }
35 [[nodiscard]] auto y() const { return data.at(1); }
36 [[nodiscard]] auto z() const { return data.at(2); }
37 [[nodiscard]] auto w() const { return data.at(3); }
38
39 auto normalize() { data = data.normalized(); }
40 auto normalized() const { return Quaternion(data.normalized()); }
41
42 auto to_mat() {
43 const auto vec = data.normalized();
44 const auto w = vec.at(3);
45 const auto x = vec.at(0);
46 const auto y = vec.at(1);
47 const auto z = vec.at(2);
48 // const auto ww = w * w;
49 const auto xx = x * x;
50 const auto yy = y * y;
51 const auto zz = z * z;
52
53 auto result = Mat3f::identity();
54 result.at(0, 0) = 1 - 2 * (yy + zz);
55 result.at(1, 0) = 2 * (x * y + w * z);
56 result.at(2, 0) = 2 * (x * z - w * y);
57
58 result.at(0, 1) = 2 * (x * y - w * z);
59 result.at(1, 1) = 1 - 2 * (xx + zz);
60 result.at(2, 1) = 2 * (y * z + w * x);
61
62 result.at(0, 2) = 2 * (x * z + w * y);
63 result.at(1, 2) = 2 * (y * z - w * x);
64 result.at(2, 2) = 1 - 2 * (xx + yy);
65
66 return result;
67 }
68
70};
71
74
75} // namespace wren::math
static constexpr auto identity()
Definition matrix.hpp:18
Definition geometry.hpp:8
Definition quaternion.hpp:9
Quaternion(Vec< T, 3 > euler_angles)
Definition quaternion.hpp:15
static auto identity()
Definition quaternion.hpp:10
Quaternion()
Definition quaternion.hpp:12
auto normalized() const
Definition quaternion.hpp:40
auto w() const
Definition quaternion.hpp:37
auto normalize()
Definition quaternion.hpp:39
auto to_mat()
Definition quaternion.hpp:42
Quaternion(T x, T y, T z, T w)
Definition quaternion.hpp:13
auto x(T x)
Definition quaternion.hpp:34
auto y() const
Definition quaternion.hpp:35
auto z() const
Definition quaternion.hpp:36
Vec< T, 4 > data
Definition quaternion.hpp:69
auto x() const
Definition quaternion.hpp:33
Definition vector.hpp:9
std::array< T, N > data
Definition vector.hpp:112
auto normalized() const
Definition vector.hpp:110
auto at(std::size_t i) -> T &
Definition vector.hpp:28