GCP Assignment 1
Quaternion.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "math.h"
4 #include "Vec3.h"
5 #include "Mat4.h"
6 
10 namespace Maths
11 {
12 
16  struct Quaternion
17  {
19  float w;
20  float x;
21  float y;
22  float z;
23 
27  Quaternion() : w(1.0f), x(0.0f), y(0.0f), z(0.0f) {}
28 
36  Quaternion(float w, float x, float y, float z) : w(w), x(x), y(y), z(z) {}
37 
45  Quaternion(int w, int x, int y, int z) : w((float)w), x((float)x), y((float)y), z((float)z) {}
46 
51  float getLength()
52  {
53  return (float)sqrt((w*w) + (x*x) + (y*y) + (z*z));
54  }
55 
63  {
64  float dp = Qa.x*Qb.x + Qa.y*Qb.y + Qa.z*Qb.z + Qa.w * Qb.w;
65  return dp;
66  }
67 
73  {
74  Quaternion normalised;
75  float length = getLength();
76  normalised.w = w / length;
77  normalised.x = x / length;
78  normalised.y = y / length;
79  normalised.z = z / length;
80  return normalised;
81  }
82 
90  void rotate(Quaternion &quat, Maths::Vec3 axis, float angle);
91 
96  Mat4 getMatrix();
97  };
98 
107  {
108  Quaternion q3;
109  q3.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z);
110  q3.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y);
111  q3.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x);
112  q3.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w);
113  return q3;
114  }
115 
116 }// End of Maths namespace
float z
Definition: Quaternion.h:22
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:16
float w
the variables for the Quaternions
Definition: Quaternion.h:19
float getLength()
Returns the length of the Quaternion.
Definition: Quaternion.h:51
float x
Definition: Quaternion.h:20
Mat4 operator*(Mat4 mInA, float s)
Overloads the * operator allowing a Mat4 to be multiplied to a scalar.
Definition: Mat4.h:248
Quaternion()
Constructs the Quaternion setting the values to 1,0,0,0.
Definition: Quaternion.h:27
Contains the Mat4 structure with functions and overloaded operators. This is row major.
Definition: Mat4.h:15
Mat4 getMatrix()
Gets a Mat4 from the Quaternion.
Definition: Quaternion.cpp:14
Quaternion(int w, int x, int y, int z)
Constructs the Quaternion setting the values to the input coordinates.
Definition: Quaternion.h:45
float getDotProduct(Quaternion Qa, Quaternion Qb)
Returns the dot product of two Quaternions.
Definition: Quaternion.h:62
void rotate(Quaternion &quat, Maths::Vec3 axis, float angle)
Rotates a Quaternion.
Definition: Quaternion.cpp:6
Quaternion getNormalised()
Returns the normalised version of the Quaternion.
Definition: Quaternion.h:72
float y
Definition: Quaternion.h:21
Quaternion(float w, float x, float y, float z)
Constructs the Quaternion setting the values to the input coordinates.
Definition: Quaternion.h:36
The namespace for all maths code.
Definition: Convert.cpp:5
Contains the Quaternion structure with functions and overloaded operators.
Definition: Quaternion.h:16