## Monday, 2 January 2012

### Complex numbers

Complex numbers are a much under-appreciated topic in mathematics, or at least that's how it seems to me looking back on them. Very often they are introduced almost as a mathematical exercise, as a way of solving mathematical problems such as quadratics which are otherwise insoluble. But once this is done all it gives is an impossible solution.

For example in my ballistics application complex number solutions to the quadratic formula in it (given by a negative discriminant) arise when the target is unreachable. And many applications of complex numbers seem like this. Except the more you study mathematics and physics the more useful they become, arising in diverse areas such as dynamics, electro-magnetism, and quantum mechanics.

I will not try and cover even a fraction of the theory of complex numbers. For that see the Wikipedia page. But I will point out some of the key features that are useful for game developers, which I may expand on later with actual applications.

First complex numbers add, subtract and scale like two-dimensional vectors, so can be used for exactly that. I could have used complex numbers for my vector class, although that used integers so would not have been exactly the same.

In addition complex numbers can be multiplied and divided, to generate rotations. Unlike angles or matrices the same complex numbers do the rotations: there is no need to learn and use different algebra or units for rotations.

Where angles or matrices are needed (for example for all Flash APIs) they are easily converted to and from complex numbers. The formula to generate complex numbers from angles is the very simple Euler's formula, while 2D rotation matrices can simply be written down from a complex number (and vice versa).

Their other benefit is they can be generalised into higher dimensions, in particular into three dimensions, using quaternions. I don't know of anyone using quaternions in Flash but they can only become more important with the launch of Stage 3D.