# Essential Math WeblogThoughts on math for games

## 5/15/2004

### Correction: Quaternion Creation

Filed under: Erratical,Mathematical — Jim @ 8:37 pm

This is both a correction, and an improvement to the creation of a quaternion from two vectors. Or as it is commonly known, Shortest RotationArc.

The current code doesn’t handle the case where the two vectors are opposing; there’s an if statement to detect it, and a comment, but no recovery code. The problem is that there are an infinite number of orthogonal rotation axes — we just need to pick one. The solution is to check to see which axis the start vector (it could be the finish vector, it doesn’t matter) is generally pointing along. If it’s pointing generally along the $z$-axis, then we take the cross product with the $x$-axis to get our rotation axis. Otherwise we take the cross product with the $z$-axis.
(more…)

## 5/4/2004

### Correction: Quaternions

Filed under: Erratical,Mathematical — Jim @ 9:35 pm

Ah, our first errata. How bittersweet.

Eric Mumau sent me this one. On page 499, equation 10.11 is wrong. It should read:

$(2w^2-1)\mathbf{p}+2(\mathbf{v} \bullet \mathbf{p})\mathbf{v} + 2w(\mathbf{v} \times \mathbf{p})$

Correspondingly, the following equation should read:

$R_\mathbf{q}(\mathbf{p}) = (2 \cos^2 (\theta/2)-1)\mathbf{p} + 2(\hat{\mathbf{r}}\sin(\theta/2)\bullet\mathbf{p})\hat{\mathbf{r}}\sin(\theta/2) + 2\cos(\theta/2)(\hat{\mathbf{r}}\sin(\theta/2)\times\mathbf{p})$

The rest of the page follows as before.

This affects the code which starts at the bottom of page 499. At the top of 500, the first three lines should be rewritten as:
[cpp]
float vMult = 2.0f*(x*vector.x + y*vector.y + z*vector.z);
float crossMult = 2.0f*w;
float pMult = crossMult*w – 1.0f;
[/cpp]
Note that the code on the CD is not the same as that given in the text, and in fact will actually work. However, it is slightly less efficient than what is given here.