Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
Quaternion.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2024 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34#pragma once
35
37#include "Vector3.h"
38namespace EU {
45 class Quaternion {
46 public:
47 float w;
48 float x;
49 float y;
50 float z;
57 Quaternion() : w(1), x(0), y(0), z(0) {}
58
69 Quaternion(float w, float x, float y, float z) : w(w), x(x), y(y), z(z) {}
70
77 Quaternion operator+(const Quaternion& other) const {
78 return Quaternion(w + other.w, x + other.x, y + other.y, z + other.z);
79 }
80
87 Quaternion operator-(const Quaternion& other) const {
88 return Quaternion(w - other.w, x - other.x, y - other.y, z - other.z);
89 }
90
97 Quaternion operator*(float scalar) const {
98 return Quaternion(w * scalar, x * scalar, y * scalar, z * scalar);
99 }
100
107 Quaternion operator*(const Quaternion& other) const {
108 return Quaternion(
109 w * other.w - x * other.x - y * other.y - z * other.z,
110 w * other.x + x * other.w + y * other.z - z * other.y,
111 w * other.y - x * other.z + y * other.w + z * other.x,
112 w * other.z + x * other.y - y * other.x + z * other.w
113 );
114 }
115
122 bool operator==(const Quaternion& other) const {
123 return (w == other.w && x == other.x && y == other.y && z == other.z);
124 }
125
132 bool operator!=(const Quaternion& other) const {
133 return !(*this == other);
134 }
135
141 float magnitude() const {
142 return EU::sqrt(w * w + x * x + y * y + z * z);
143 }
144
151 float mag = magnitude();
152 if (mag == 0) {
153 return Quaternion(1, 0, 0, 0);
154 }
155 return Quaternion(w / mag, x / mag, y / mag, z / mag);
156 }
157
164 return Quaternion(w, -x, -y, -z);
165 }
166
173 float magSquared = w * w + x * x + y * y + z * z;
174 if (magSquared == 0) {
175 // Handling division by zero
176 return Quaternion(1, 0, 0, 0);
177 }
178 return conjugate() * (1.0f / magSquared);
179 }
180
187 Vector3 rotate(const Vector3& v) const {
188 Quaternion qv(0, v.x, v.y, v.z);
189 Quaternion result = (*this) * qv * this->inverse();
190 return Vector3(result.x, result.y, result.z);
191 }
192
200 static Quaternion fromAxisAngle(const Vector3& axis, float angle) {
201 float halfAngle = angle * 0.5f;
202 float sinHalfAngle = EU::sin(halfAngle);
203 return Quaternion(
204 EU::cos(halfAngle),
205 axis.x * sinHalfAngle,
206 axis.y * sinHalfAngle,
207 axis.z * sinHalfAngle
208 );
209 }
210
216 const float* data() const {
217 return &w;
218 }
219
225 /*Matrix4x4 toMatrix() const {
226 Matrix4x4 result;
227 result[0][0] = 1 - 2 * (y * y + z * z);
228 result[0][1] = 2 * (x * y - z * w);
229 result[0][2] = 2 * (x * z + y * w);
230 result[1][0] = 2 * (x * y + z * w);
231 result[1][1] = 1 - 2 * (x * x + z * z);
232 result[1][2] = 2 * (y * z - x * w);
233 result[2][0] = 2 * (x * z - y * w);
234 result[2][1] = 2 * (y * z + x * w);
235 result[2][2] = 1 - 2 * (x * x + y * y);
236 result[3][3] = 1;
237 return result;
238 }*/
239 };
240}
241
242
Declara la API de EngineMath dentro del subsistema Utilities.
Declara la API de Vector3 dentro del subsistema Math.
A quaternion class.
Definition Quaternion.h:45
bool operator!=(const Quaternion &other) const
Inequality operator.
Definition Quaternion.h:132
Quaternion operator*(float scalar) const
Multiplies this quaternion by a scalar.
Definition Quaternion.h:97
Quaternion normalize() const
Normalizes the quaternion.
Definition Quaternion.h:150
Quaternion operator-(const Quaternion &other) const
Subtracts another quaternion from this quaternion.
Definition Quaternion.h:87
float x
The i component of the quaternion.
Definition Quaternion.h:48
Quaternion conjugate() const
Returns the conjugate of the quaternion.
Definition Quaternion.h:163
bool operator==(const Quaternion &other) const
Equality operator.
Definition Quaternion.h:122
Quaternion()
Default constructor.
Definition Quaternion.h:57
float y
The j component of the quaternion.
Definition Quaternion.h:49
float z
The k component of the quaternion.
Definition Quaternion.h:50
Quaternion(float w, float x, float y, float z)
Parameterized constructor.
Definition Quaternion.h:69
float w
The real part of the quaternion.
Definition Quaternion.h:47
const float * data() const
Returns a pointer to the quaternion's data.
Definition Quaternion.h:216
Quaternion inverse() const
Returns the inverse of the quaternion.
Definition Quaternion.h:172
float magnitude() const
Calculates the magnitude (length) of the quaternion.
Definition Quaternion.h:141
Quaternion operator*(const Quaternion &other) const
Multiplies this quaternion by another quaternion.
Definition Quaternion.h:107
Vector3 rotate(const Vector3 &v) const
Rotates a vector by this quaternion.
Definition Quaternion.h:187
static Quaternion fromAxisAngle(const Vector3 &axis, float angle)
Constructs a quaternion from an axis and an angle (in radians).
Definition Quaternion.h:200
Quaternion operator+(const Quaternion &other) const
Adds another quaternion to this quaternion.
Definition Quaternion.h:77
A 3D vector class.
Definition Vector3.h:45
float x
The x-coordinate of the vector.
Definition Vector3.h:47
float z
The z-coordinate of the vector.
Definition Vector3.h:49
float y
The y-coordinate of the vector.
Definition Vector3.h:48
Definition Matrix2x2.h:35
float sqrt(float value)
Computes the square root using the Newton-Raphson method.
Definition EngineMath.h:47
float sin(float angle)
Calcula el seno de un ángulo en radianes.
Definition EngineMath.h:179
float cos(float angle)
Calcula el coseno de un ángulo en radianes.
Definition EngineMath.h:197