Game Development Reference
In-Depth Information
When using matrices for rotations, we need some other formats to represent
rotations in our game. We prefer to use a quaternion to avoid a gimbal lock.
A quaternion represents two things. It has the x , y , and z components, which
represents the axis about which a rotation will occur. It also has a w component,
which represents the amount of rotation that will occur about its axis. In short, a
quaternion is a vector and a float.
A quaternion is technically four numbers, three of which have an imaginary
component ( i is defined as sqrt (-1) ). Well, with quaternions, i equals j equals k equals
sqrt (-1) . The quaternion itself is defined as q = w + xi + yj + zk where w , x , y , and z are
all real numbers. We will store a quaternion in a class with four member variables:
float w , x , y , and z .
Well, we are saved from all the math involved as our glMatrix library gives us all
the functions required for our computations. Let's look at a few functions:
quat.rotateX(out, a, rad) // Rotates a quaternion by the given
angle about the X axis
quat.rotateY(out, a, rad) // Rotates a quaternion by the given
angle about the Y axis
quat.rotateZ(out, a, rad) // Rotates a quaternion by the given
angle about the Z axis
quat.setAxisAngle(out, axis, rad) // Sets a quat from the given
angle and rotation axis, then returns it.
quat.setAxisAngle(out, [1,0,0], rad) // Sets a quat from the given
angle and X axis, then returns it.
vec3.transformQuat(out, a, q) // Transforms the vec3 with a quat
mat3.fromQuat(out, q) // Calculates a 3 x 3 matrix from the given
quaternion
quat.fromMat3(out, m) // Creates a quaternion from the given 3 x 3
rotation matrix.
So basically, if you look at the preceding functions, we can get a matrix from a
quaternion and create a quaternion from a rotation matrix. We can rotate our vector
using a quaternion. We can create a quaternion for rotation around an axis using the
setAxisAngle function of our quat class. We will soon be putting these functions
to action.
Understanding perspective
transformations
Although we touched upon perspective transformations in Chapter 1 , Getting
Started with WebGL Game Development , and have been using our perspective matrix
throughout our code, we would like to discuss it in depth here.
 
Search WWH ::




Custom Search