Simple implementation of the Boids algorithm in Objective-C: counting time through audio


My first attempt to implement the Boids algorithm for iOS using the Sprite kit was a bit disappointing, since everything was moving really slow when the number of agents increased above around 20 or 30 – both on simulator and device.

Doing some tests for building a very accurate metronome (see previous posts), it became clear to me that audio-level sample-based metronomes are both very accurate and CPU-friendly:

– accurate: since everything was being counted on a 1/44100 of a second “frame-rate” and

– CPU friendly: since there the timing mechanism was somehow assigned on the sound card.

– Potentially even more CPU friendly, by making two different methods for (i) defining the acceleration (ones every, say, 0.3 seconds) and (ii) updating the position according to acceleration (every, say, 0.1 seconds).

Therefore, the thought was simple and straightforward: instead of making metronome clicks on specific time intervals, an app’s view elements could be updated. Take a look at the ABetterMetronome thread and things will become clear 😉

Here’s the code! To run it you need to download The Amazing Audio Engine (TAAE), import it properly and place the TAAE main folder one level above the project’s folder.