Game Engine Architecture

(Ben Green) #1

174 4. 3D Math for Games


// check the diagonal
if (trace > 0.0f)
{
float s = sqrt(trace + 1.0f);
q[3] = s * 0.5f;

float t = 0.5f / s;
q[0] = (R[2][1] - R[1][2]) * t;
q[1] = (R[0][2] - R[2][0]) * t;
q[2] = (R[1][0] - R[0][1]) * t;
}
else
{
// diagonal is negative
int i = 0;
if (R[1][1] > R[0][0]) i = 1;
if (R[2][2] > R[i][i]) i = 2;
static const int NEXT[3] = {1, 2, 0};
j = NEXT[i];
k = NEXT[j];

float s = sqrt ((R[i][i]


  • (R[j][j] + R[k][k]))



  • 1.0f);


q[i] = s * 0.5f;

float t;
if (s != 0.0) t = 0.5f / s;
else t = s;
q[3] = (R[k][j] - R[j][k]) * t;
q[j] = (R[j][i] + R[i][j]) * t;
q[k] = (R[k][i] + R[i][k]) * t;
}
}

4.4.5. Rotational Linear Interpolation
Rotational interpolation has many applications in the animation, dynamics
and camera systems of a game engine. With the help of quaternions, rotations
can be easily interpolated just as vectors and points can.
The easiest and least computationally intensive approach is to perform
a four-dimensional vector LERP on the quaternions you wish to interpolate.
Given two quaternions qA and qB representing rotations A and B, we can
fi nd an intermediate rotation qLERP that is β percent of the way from A to B as
follows:
Free download pdf