1
#ifndef _MATH_LIN_QUAT_H
2
#define _MATH_LIN_QUAT_H
4
#include "math/lin/vec3.h"
14
Quaternion(const float _x, const float _y, const float _z, const float _w) {
15
x=_x; y=_y; z=_z; w=_w;
21
void setXRotation(const float r) { w = cosf(r / 2); x = sinf(r / 2); y = z = 0; }
22
void setYRotation(const float r) { w = cosf(r / 2); y = sinf(r / 2); x = z = 0; }
23
void setZRotation(const float r) { w = cosf(r / 2); z = sinf(r / 2); x = y = 0; }
24
void toMatrix(Matrix4x4 *out) const;
25
static Quaternion fromMatrix(Matrix4x4 &m);
27
Quaternion operator *(Quaternion &q) const
30
(w * q.w) - (x * q.x) - (y * q.y) - (z * q.z),
31
(w * q.x) + (x * q.w) + (y * q.z) - (z * q.y),
32
(w * q.y) + (y * q.w) + (z * q.x) - (x * q.z),
33
(w * q.z) + (z * q.w) + (x * q.y) - (y * q.x)
36
Quaternion operator -()
38
return Quaternion(-x,-y,-z,-w);
40
void setRotation(Vec3 axis, float angle)
42
axis /= axis.length();
44
float sine = sinf(angle);
50
void toAxisAngle(Vec3 &v, float &angle)
53
if (w==1.0f && x==0.0f && y==0.0f && z==0.0f)
60
angle = acosf(cos_a) * 2;
61
float sin_a = sqrtf( 1.0f - cos_a * cos_a );
62
if (fabsf(sin_a) < 0.00005f) sin_a = 1;
63
float inv_sin_a=1.0f/sin_a;
74
Quaternion slerp(const Quaternion &to, const float a) const;
75
Quaternion multiply(const Quaternion &q) const;
76
float &operator [] (int i) {
79
float operator [] (int i) const {
82
//not sure about this, maybe mag is supposed to sqrt
83
float magnitude() const {
84
return x*x + y*y + z*z + w*w;
87
float f = 1.0f/sqrtf(magnitude());
88
x*=f; y*=f; z*=f; w*=f;
92
#endif // _MATH_LIN_QUAT_H