Level H Engine
Quaternion.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Vec3.h"
4 #include "Mat4.h"
5 
9 struct Quaternion
10 {
12  float w;
13  float x;
14  float y;
15  float z;
16 
20  Quaternion() : w(1.0f), x(0.0f), y(0.0f), z(0.0f) {}
21 
29  Quaternion(float w, float x, float y, float z) : w(w), x(x), y(y), z(z) {}
30 
38  Quaternion(int w, int x, int y, int z) : w((float)w), x((float)x), y((float)y), z((float)z) {}
39 
44  float getLength()
45  {
46  return (float)sqrt((w*w) + (x*x) + (y*y) + (z*z));
47  }
48 
56  {
57  float dp = Qa.x*Qb.x + Qa.y*Qb.y + Qa.z*Qb.z + Qa.w * Qb.w;
58  return dp;
59  }
60 
66  {
67  Quaternion normalised;
68  float length = getLength();
69  normalised.w = w / length;
70  normalised.x = x / length;
71  normalised.y = y / length;
72  normalised.z = z / length;
73  return normalised;
74  }
75 
83  void rotate(Quaternion &quat, Vec3 axis, float angle);
84 
89  Mat4 getMatrix();
90 };
91 
100 {
101  Quaternion q3;
102  q3.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z);
103  q3.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y);
104  q3.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x);
105  q3.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w);
106  return q3;
107 }
float y
Definition: Quaternion.h:14
float x
Definition: Quaternion.h:13
Quaternion operator*(Quaternion q1, Quaternion q2)
Overloads the * operator allowing a Quaternion to be multiplied by another Quaternion. IMPORTANT: (Quaternion1 * Quaternion2) != (Quaternion2 * Quaternion1)
Definition: Quaternion.h:99
float w
the variables for the Quaternions
Definition: Quaternion.h:12
float getDotProduct(Quaternion Qa, Quaternion Qb)
Returns the dot product of two Quaternions.
Definition: Quaternion.h:55
void rotate(Quaternion &quat, Vec3 axis, float angle)
Rotates a Quaternion.
Definition: Quaternion.cpp:5
Quaternion(int w, int x, int y, int z)
Constructs the Quaternion setting the values to the input coordinates.
Definition: Quaternion.h:38
Quaternion(float w, float x, float y, float z)
Constructs the Quaternion setting the values to the input coordinates.
Definition: Quaternion.h:29
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:8
float z
Definition: Quaternion.h:15
Mat4 getMatrix()
Gets a Mat4 from the Quaternion.
Definition: Quaternion.cpp:13
Quaternion()
Constructs the Quaternion setting the values to 1,0,0,0.
Definition: Quaternion.h:20
Contains the Quaternion structure with functions and overloaded operators.
Definition: Quaternion.h:9
Quaternion getNormalised()
Returns the normalised version of the Quaternion.
Definition: Quaternion.h:65
Contains the Mat4 structure with functions and overloaded operators. This is row major.
Definition: Mat4.h:9
float getLength()
Returns the length of the Quaternion.
Definition: Quaternion.h:44