ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!
Optimizing Your Trigonometric Functions
Most games worth making will require you to calculate the angles and distances between various objects, and
most programmers would undoubtedly turn to a little 9th grade trigonometry to solve their problems
(ok, maybe the Sine and Cosine laws are a little more advanced than 9th grade...). The Sin
and Cos (and don't forget Tan and Atn) functions within VB would seem to be the way to go,
but I tell you NO!
These functions certainly do the job, but can be a little slow, especially if you call them a few hundred times
per frame of animation. Space-ship games (Could Lucky be talking about Galaxy?) for example, have a great
need for trig calculations. Every star, every bullet, and every ship must have its X and Y coordinates updated
during each frame of animation, it starts to add up when the action gets heated!
"Bones! What do we do?"
"Dammit Jim, I'm a doctor not a computer scientist!"
Lucky's got your answer: Trig tables! When your program initializes, call a function that pre-loads an array
with the trigonometric data you'll need. For example, you could make an array Sin(359) and fill it with the
values of Sine from 0 to 359 degrees. Later, when you need to calculate the Sine of an angle,
you simply round the value to a whole number and look up the result in your Sin array.
Const Pi = 3.14159
Dim i As Integer
Dim SineArray(359) As Double
For i = 0 To 359
SineArray(i) = Sin(i * Pi / 180)
Next
The code above shows you how to initialize the array. Keep in mind that the VB trig functions only deal in radians
so you have to convert if you intend to use degrees (ie. By multiplying by Pi/180).
Function Sine(ByVal Angle As Single, Optional Degrees As Boolean) As Single
If Degrees = False Then Angle = CInt(Angle * 180 / 3.14159)
Do While Angle < 0 Or Angle > 359
If Angle > 359 Then Angle = Angle - 360
If Angle < 0 Then Angle = Angle + 360
Loop
Sine = SineArray(Angle)
End Function
This code shows you how to write a function that will return the Sine of a given angle, whether it is in
radians or degrees (it converts the angle to degrees if necessary). Also, you'll notice, it first ensures that the
value is within range (ie. 0-359 degrees) which is important since angles greater than 360 and less than 0 are often
returned by your own functions (well, mine anyway) and are still valid and therefore must be dealt with (ie. normalized
back into the 0-359 range).
Do you see how this is to your advantage? By performing all of the calculations only once (during initialization), you
then only have to deal with looking up values in an array (much faster). You do, however, lose a little accuracy, so if
your program requires precise measurements, less than one degree, you'll have to modify this procedure yourself, or
forego it.
I threw together a little Sample Program that might be useful to you. It includes a pretty good
Trig module, feel free to reuse it in your own projects.