20 ms·
The article uses a game movement function as a possible motivation for needing the cosine. If object.rotation is constant as the spiral animation seems to sugge
by todd8 5y ago
The article uses a game movement function as a possible motivation for needing the cosine. If object.rotation is constant as the spiral animation seems to suggest, there is an important optimization for rapidly computing the sequence: cos(theta), cos(theta + delta), cos(theta + 2delta), cos(theta + 3delta), ...
This comes up in movement under constant rate of rotation as well as Fourier transforms. See [1] for details, but the basic idea uses the simple trig identities:
cos(theta + delta) = cos(theta) - (a*cos(theta) + b*sin(theta))
sin(theta + delta) = sin(theta) - (a*sin(theta) - b*cos(theta))
The values for a and b must be calculated, but only once if delta is constant:
a = 2 sin^2(delta/2)
b = sin(delta)
By using these relationships, it is only necessary to calculate cos(theta) and sin(theta) once for the first term in the series in order to calculate the entire series (until accumulated errors become a problem).
[1] Press, William H. et. al., Numerical Recipes, Third Edition, Cambridge University Press, p. 219