Tuesday, April 7, 2009

DevDiary: Artificial Life updates coming soon

I've been working on some changes recently to the Artificial Life iPhone app. Some of them have already been submitted to Apple and should be approved any day now.

The most recent submission includes support for saving the world state when the app exits along with having the protozoa grow in length as they attack others or eat. These two changes are in the pending update now.

The following describes some more upcoming changes that weren't ready in time for this latest app update, but will definitely be in the next (since they are working now):

I changed the graphics slightly - the creatures now are semitransparent and they pulse slightly, giving them a somewhat jelly fish look. It was mainly done as a test to see how practical it is to have all the creatures represented as bitmap images rather than just dots. It appears the graphics engine is pretty capable of keeping up - according to Instruments, I'm only using about 20% of the graphics power.



The main slowdown in the app is actually caused by the creatures' artificial intelligence routines. The slowdown is exponential in relation to the population. Each creature has to search through the list of all the creatures in the world when making decisions. So, 50 creatures means 2500 decisions. 100 creatures and the number jumps to 10,000. 300 creatures and it's 90,000. Obviously that's not an ideal way to process the AI.

For those interested, this issue is called the "nearest neighbor" problem. You can read about it on Wikipedia. It's a well studied computer science problem.

To address this speed issue, the world is now divided into a 9x9 grid and the creatures are put into one of 81 bins depending on their location. (Anything out of range of the 9x9 grid is put into the closest edge bin). For finding their nearest neighbor, a creature now only looks in the 3x3 grid area of its current location. This gives a significant boost in speed in the app. While there is still a noticeable slowdown when you reach the 300 protozoa maximum, the slowdown is much more tolerable.

I've also implemented a small particle system for "special effects" in the app. Now, when a protozoa gets killed by the sun, or is attacked by another protozoa, there will be some minor particle effects generated. You can see a sample here in this short video.






controller="true" loop="false" bgcolor="#000000" pluginspage='http://www.apple.com/quicktime/download/'>



And finally, the last change made was to have the protozoa's initial diameter be based on its age. So, when one is newly born, it is very small and will increase in size as its age increases. It reaches full size at at 40.

Again, I should mention the first two changes - saving the world state and protozoa growing in length - should appear in the iTunes App Store any day now. The remaining changes will most likely be submitted in a week or so.

All content copyright © 2009  Brian Stormont, unless otherwise noted.   All rights reserved.