Monday, 16 January 2012

More on angles

In a recent post I asserted that angles are rarely needed for game mathematics. There are a few exceptions but far fewer than many people realise. But it's easy to doubt this assertion. What is there theory behind it? Is there a way to check when angles can be avoided? Not only can both questions be answered but the answer to them is the same.

When studying trigonometry the units used are radians. These are such that a whole turn, half-turn, right angle and many common angles are fractions or multiples of pi (π = 3.1415926). They are so fundamental to mathematics that unlike other units such as degrees they do not have a symbol. They are related to degrees as follows

π radians = 180°,

though in use the word 'radians' can be omitted, treating the measure as a number.

One important property it that π is a transcendental number. This means not only is it not a fraction but it is not the solution to any polynomial equation, or at least not one written with whole numbers and fractions. Because of this it cannot be calculated by performing arithmetic calculations and taking roots (square roots, cube roots etc.) starting from whole numbers. A number that can be calculated this way is an algebraic number.

But many situations do only involve such calculations and produce algebraic numbers. Distances or speeds are calculated with Pythagoras' theorem, a simple polynomial. Matrix or complex number calculations or transformations use just linear calculations. Even complex dynamics problems, such as those involving the coefficient of restitution, require only linear calculations.

(A related result is that constructable lengths, that is lengths that can be constructed using compass and straightedge, must be algebraic numbers. Few problems are solved like  this now, as calculators and computers are far more accurate, but many two dimensional problems can be represented this way.)

As π is transcendental the results of such calculations cannot include angle measures based on π. If angles are used they will need to drop out of the calculation later, requiring  inverse trigonometric functions as well as normal ones, adding to the cost of the calculations and often requiring extra bounds or range checking. Instead many such problems involve nothing more complex than linear (+, −, ×, ÷) calculations and square roots (arising from Pythagoras' theorem, solving quadratics, etc.), so avoiding angles altogether.

The exceptions are mostly those which generate formulae too complex to solve, a few of which can be solved using trigonometry. Anything involving dividing a angle into three or more equal parts generates cubic equations and more complex equations, too complex to solve algebraically but which can be done using trigonometry.

The other main exception is where linear motion is being converted to rotational motion. For example a wheel with diameter 1 metre rotating one full turn will roll forward 2π metres, using the formula for the circumference C = 2πr which involves π directly. And of course some APIs in ActionScript take an angle (though often in degrees not radians, so the calculated angle needs multiplying by 180 / π).

But in general when doing mathematics for a game angles and π can be left out of the final calculation and are not needed in-game, with very few exceptions.

No comments:

Post a Comment