Tutorials » 3D Math » How to use Quaternions

The Untold Engine represents a quaternion as a U4DEngine::U4DQuaternion.

Declaration

There are two ways to declare a Quaternion. You can use the default constructor. It creates a quaternion with a scalar equal to 1.0 and a zero vector.

//declare a quaternion
U4DEngine::U4DQuaternion q;

You can also declare a quaterion by providing a scalar and a vector component.

//declare a vector
U4DEngine::U4DVector3n vec(1.0,0.0,0.0);

//declare a quaterion
U4DEngine::U4DQuaternion p(1.0,vec);

Addition

Two quaternions can be added in two different ways.

You can add two quaternions using U4DEngine::U4DQuaternion::operator+()

//declare quaternions
U4DEngine::U4DQuaternion q;
U4DEngine::U4DQuaternion p;

//1st method
U4DEngine::U4DQuaternion r=p+q;

You can also add quaternions using U4DEngine::U4DQuaternion::operator+=()

//declare quaternions
U4DEngine::U4DQuaternion q;
U4DEngine::U4DQuaternion p;

//2nd method
p+=q;

Dot

To compute the Dot product between two quaternions you use the following method U4DEngine::U4DQuaternion::dot()

//declare quaternions
U4DEngine::U4DQuaternion q;
U4DEngine::U4DQuaternion p;

//Dot product
float m=q.dot(p);

Norm

To compute the norm of a quaternion you use U4DEngine::U4DQuaternion::norm()

//declare a quaternion
U4DEngine::U4DQuaternion q;

//Norm product
float m=q.norm();

Conjugate

The conjugate of a quaternion is computed with U4DEngine::U4DQuaternion::conjugate().

//declare a quaternion
U4DEngine::U4DQuaternion q;

//Quaternion conjugate
U4DEngine::U4DQuaternion m=q.conjugate();

Inverse

The inverse of a quaternion is computed with U4DEngine::U4DQuaternion::inverse()

//declare a quaternion
U4DEngine::U4DQuaternion q;

//quaternion inverse
q.inverse();