Game Development Reference
In-Depth Information
APPENDIX C
Quaternion Operations
This appendix implements a class called Quaternion that encapsulates all of the oper‐
ations you need to handle quaternions when writing 3D rigid-body simulations.
Quaternion Class
The Quaternion class is defined with a scalar component, n , and vector component, v ,
where v is the vector, × i + y j + z k . The class has two constructors, one of which initializes
the quaternion to 0, and the other of which initializes the elements to those passed to
the constructor:
class Quaternion {
public:
float n; // number (scalar) part
Vector v; // vector part: v.x, v.y, v.z
Quaternion(void);
Quaternion(float e0, float e1, float e2, float e3);
float Magnitude(void);
Vector GetVector(void);
float GetScalar(void);
Quaternion operator+=(Quaternion q);
Quaternion operator-=(Quaternion q);
Quaternion operator*=(float s);
Quaternion operator/=(float s);
Quaternion operator~(void) const { return Quaternion( n,
-v.x,
-v.y,
-v.z);}
};
// Constructor
inline Quaternion::Quaternion(void)
 
Search Nedrilad ::




Custom Search