Friday, May 1, 2009

Natural Selection: Project Status Update

It is the start of a new month and it feels like an appropriate time to write a project status update for my Natural Selection iPhone game project.

I've been keeping keen records for this after-hours project which may interest number junkies:

  • I started the project (tracking time) 44 days ago on March 18th 2009.
  • I have worked a total of 109 man-hours on the project, which is 13.64 man-days (nearly 3 man weeks).
  • I have averaged 2.48 man-hours a day and peaked at 8 man hours on some weekends
  • I have spent 46 man-hours (5.75 man-days) on the code base and the remaining 63 man-days on documentation and R&D, although this ratio is sure to correct with time
  • I am (under-)charging my time at a graduate salary ($25AUD/hour) totalling my time at $2,725.00.
  • The sum of expenses to-date is a little more than $5,000AUD (2 iPhones sadly, apple dev program, phone case, and chargeable time).
  • Weddings and other random commitments aside, a first cut by July with a launch soon after would be reasonable.
Progress has been reasonable, although could have been better. Efforts on the code base did not begin until a physics engine and game engine were selected. I spent a fair amount of time (nearly a calender month or approximately 42 man-hours) researching, testing, and evaluating physics engines, graphics engines, and game engines. I think my saving grace was my support network.

At the start of the project (20th March) I organized an on-going developer meet-up with the other mayhem guys. The goal was to spend 2 hours, every fortnight to discuss progress with our respective side-projects and offer ideas and support. We had our first meeting on the 25th March, and by our second meeting just before Easter (8th April) I felt self-conscious still chatting about requirements documents and my progress on engine evaluations. It was this second meeting that kicked me into gear. I got pragmatic over the Easter break, started the build, and real-progress has been steady since.

The following two images are screenshots of my app taken from my iPhone. The first (left) is an early version of the prototype taken April 20th and shows basic rendering of a two shape creature in a box world with functioning physics. The second shot (right) was taken today and shows a simple background (thanks Gimp) and a functioning creature and world definition/management and the start of an evolutionary process (goal is to reach the flag).


My medium term plan is to elaborate the evolutionary process some more and then start to flesh out the 'game' features. I am going to add some basic menus for selecting the proto-creatures from which to start an evolutionary process as well as the environments (levels) where the process will take place. Longer term, I want to start to develop a 'look' for the game, and start seriously developing/acquiring the assets (images, sounds, music). I'm thinking creative commons and sites like 99designs might be useful.

Most importantly, I'm having fun. Classical ANSI C and C++ memory management is still as painful as I remember, although objective-C is new to me and interesting as a different take on the OO paradigm.

Sunday, April 19, 2009

Dropping a Dimension: Cocos2D and Box2D on iPhone

Over Easter I decided to drop a dimension in my iPhone project to go from a 3D evolution game simulator to 2D.

I started out playing with the Chipmunk physics engine, just out of curiosity. I watched some videos of the engine in action, read the forums and downloaded the code base and example moon buggy project. The code comes with an Xcode project and the 8 demos make working with the engine look easy.

I started playing around with cocos2d for the iphone because it came with Chipmunk already ported. I hacked on the chipmunk demos (also ported to cocos2d) and found the lack of OO-design frustrating and the speed terrible, even after reading tweaks from the cocos2d forum and blog.

I kept seeing references to the Box2d physics engine (elements of which Chipmunk is based) and eventually dropped what I was working on and grabbed a copy of the code base. The code also had an Xcode project and I was impressed by the number and variety of demos. I dug deeper and came across the box2d port to iphone donated by Simon Oliver who built Rolando using Box2D. I graped the port from the box2D SVN server and started developing my game. I started fleshing out a basic game engine with physics and rendering call backs each frame, tweaked based on some game tutorial screencasts on 71^2.

The speed of the demos was still not good enough, but the object-oriented design sat right and I assumed I could optimize my application into submission (I have so far). While googling fixes for the issues and questions I was having in my basic game engine I kept coming back to the cocos2d forums and codebase. It was obvious that cocos2d had already solved the problem of a simple game engine, and then some.

I was decided. The rapid progress I was able to affect in 2D with both chipmunk and box2d as well as the ready-to-exploit and well documented cocos2d game engine convinced me.

The following is the dead-simple procedure I wrote down for manually integrating the box2D physics engine into the cocos2d game engine for the iPhone:

  1. Check-out cocos2d-iphone from SVN. This is because the downloadable archive from the site does not allow one to decouple Chipmunk as easily.
  2. Check-out box2d physics engine from SVN. This is because the downloadable archive is outdated and does not include the iPhone port (and subsequent changes to the codebase?).
  3. Create a new Xcode project for cocos2d based on Monocle Studios - Introduction to Cocos2d iPhone Whitepaper. I only included the cocos2d directory (no Chipmunk for example), and I tweaked a few things a long the way (OpenGL-based project as a starting point, names of things, etc).
  4. Because Chipmunk is no longer in my codebase I used the new replacement macros whenever the old Chipmunk macros were referred to (such as in forum posts and in the new cocos2d project tutorial in the previous step).
  5. Add the box2d source to the project, specifically the 'include' and 'sources' directories.
  6. The first build had some compile issues (a 'FALSE' rather than a 'false' and missing ';') that required minor fixes.
  7. The first thing I did was create a custom Box2D cocos2d layer that ran the physics engine and generated a basic world with cute 2-box creature (big box with a little box flipper).
  8. I used the rendering code listed in the box2D iPhone port (GLES-Render.mm) for drawing shapes, segments and points, and used the debug-drawing engine in the box2D core (b2World.cpp) as the basis for traversing world elements for drawing each frame. I considered using the cocos2d primitives (ChipmunkDemo/main.m) as is done in the ported Chipmunk examples, but I figured I am going to need custom drawing routines anyway, so the simple custom OpenGL drawing routines were a good base. I also ripped the mouse/touch code from the iPhone port of Box2D which works great.
  9. I had some initial problems importing Box2D.h into my Objective-C++ header files. The solution was simple enough, involving #ifdef __cplusplus/#endif around the cpp header.
  10. Finally, when compiling for iPhone hardware I had some additional errors in Box2D ('finite' was not declared in this scope), that were easily fixed.
I'm sure there are easier/better ways to get up and running with Box2D in cocos2d-iPhone (tell me please!) and millage may vary as the code bases change (I checked out the working copies of each project), although it has worked well enough for me thus far. If there is sufficient demand, I'll create an opensource project with the bare-bones project, or even just open up my current codebase. Let me know.

I'm currently struggling with the fabled "codesign failed with exit code 1" while trying to push my application to my iPhone device, specifically "object file format invalid or unsuitable". I've dropped 6 hours into this problem already and am still nowhere, although I'm reasonably sure it is not project specific. I will get there, but it is so damn frustrating!

Reading has turned up an array of interesting snippets. Cocos2d game engine was originally written in Python and ported to Objective-C for the iPhone. Notes on Cocos2d iPhone Development provide a great intro to cocos2d and specifically the arrangement of scenes, layers and nodes. Box2D has been ported all over the place including to Flash, and there are lots of useful box2d-flash related posts that are directly useful (like this lot that helped me figure out Revolute Joints). I have also seen lots of discussion about tuning box2d for performance (like 1m bodies are the sweet spot) and interesting physics engine comparisons and speed tests.

My current plan is to continue to realize the procedural creature generation and evolutionary engine for my project in 2D and play-test the hell out of it. If something fun drops out I will polish and push it to the app store and/or consider a port to 3D. The raw computation required for any meaningful morphology/controller evolutionary process is still my biggest technical risk. I am still not convinced it is viable on this hardware. I've got some thoughts regarding work-arounds (pre-evaluated strata in the search space, amazon EC2 compute servers, etc), although I am also thinking about a creature app without evolution.

Sunday, April 5, 2009

Oolong Engine for the iPhone: A First Look

I set a side some time this weekend to have a look at the Oolong Engine for the iPhone and one thing is clear - it is the domain of the graphics programmer, hardcore or otherwise (note: not me).

The project started as a side project by Wolfgang Engel exploring graphics programming on the iPhone and iPod Touch, seemingly initiated in late 2007. The project went by the name iGDK (the iPhone Game Development Kit), was located on google code, and got some early attention given that the official iPhone SDK was at that time unreleased. Still in late 2007, Wolfgang renamed the project to the Oolong Engine, seemingly changed the license agreement to the MIT License and created the still current google code project and public facing website. A mailing list followed soon after and is now the primarily means of support containing almost 12 months of user interest discussions.

The main form of documentation for the project is Wolfgang's blog entitled "Diary of a Graphics Programmer", that details (among other things) insight into his progress on the project as a free-time exercise, as well as a series of (at last count) 9 iPhone programming tips released at then end of 2008 (approximately one year after the project was initiated).

For completeness, the following list the Oolong related posts from the blog:

Interpreted as an attempt at induction for programmers new to the platform and his engine, Wolfgang's wrote a series of iPhone and iPod Touch (iP*) programming tips from November 2008 to January 2009. Wolfgang's contribution in the Quake III Arena port to the iPhone shines through in some of these articles.
From the blog posts and news entries on the projects webpage, one may deduce that the Wolfgang's contributions to the project peaked early to mid 2008 (around the launch and resultant integration with the iPhone 2.0 SDK) and have simmered down since, except for an interest in VFP and the writing of programming tips over the 2008 Christmas period. The google project also lists Erwin Coumans from the Bullet Physics project as a second project owner, and there are number of additional members listed as contributing to the effort.

Oolong is the side project and domain of serious graphics and game physics programmers, and as such code is what this project is all about.

The project incorporates a number of sister open source projects and offers a suite of example projects. There is no documentation provided with the project other than some out-dated and vapid readme files for the examples and some reasonable in-line comments littered throughout the examples and the core engine code.

The leveraged third-party open source projects that were listed (or that I could deduce) and integrated into the code base are as follows:
  • Bullet Physics (2.73), for 3D physics simulation
  • Memory Manager, apparently by Fluid Studios although seemingly previously listed on Flip Code
  • IPROF, A Portable Industrial-Strength Interactive Profiler for C++ and C, by Sean Barrett
  • enet, the network subsystem used in cube
  • VFP math library (also by Wolfgang Engel) offering math functions in iPhone-friendly ASM
  • OpenAL sound engine/wrapper, (?)
The /Examples directory offers a number of discrete example Xcode projects logically grouped into sub-directories. Many of the examples worked for me (in the iPhone Simulator), although some failed to demonstrate their intended feature, some black-screened (is that a thing?), and a number crashed (specifically the 10 Ported PowerVR Examples under the Renderer directory). Initially I thought that the 10 'Ported PowerVR Examples' were a direct porting of the 10 Imagination Technologies Khronos OpenGL ES 1.x SDK for POWERVR MBX examples, although this may not be the case given the the names of the examples do not clearly align.

Given my newness to the platform, I am unsure whether the failing example projects were an artifact of the 'project in flux' or my iPhone SDK that was hacked to run on my PPC iBook G4 (yep, non-Intel). Additionally, the PowerVR Examples use code optimized for the iPhone hardware and so may need to be deployed to the device for execution. Given that I tested the examples in the simulator, this is a third possible cause for the examples that crashed out.

I am in no position to assess the efficiency of the engines rendering pipeline, but I bet it is smokingly fast relative to competing engines. What is clear to me is that the engine is bare-bones, requiring the programmer to do a lot of work to realize the (in principle) simplest 3D scenes. I am not a graphics programmer and I've been spoiled recently by frameworks in other domains that have delivered big wins with modest effort. I'm not giving up, but I want to hedge my engine betting. My next objective is to assess SIO2 and see what it can offer in terms of a more scripted (ready to write game code) solution.

Saturday, March 28, 2009

Review of 3D Engines for the iPhone

I need a 3D engine for my iPhone project. I've hacked together my own 2D engines for rendering and physics in the past, but I have done very little OpenGL, and all the time I spent hacking around in the Quake, Quake2, Quake3 engines was focused on game logic and AI. This means that it is hard for me to define suitable requirements upon which to discriminate between available iPhone 3D engine middleware.

Requirements
Generally, I think my needs are modest (although still too rough to prepare a feature matrix comparison):

  • 3D rendering and rigid body 3D physics
  • Demonstrated iPhone integration and deployment (technically possible and apple has permitted deployment)
  • Low polygon counts regarding world entities, robust physics simulation (one creature as a dozen or so cuboids connected by simulated muscles)
  • Facility to execute physics simulations in a/the world without rendering (at the same time as rendering would be really nice but implausible given the hardware)
  • Programmatic management of 3D world entities (definition, instantiation, post-simulation manipulation)
  • No cost (I don't mind open sourcing my project if it is permitted by the conditions of the Apple AppStore)
Candidates
Two clear candidates have emerged that may satisfy my needs, as follows:
  • SIO2 Interactive, SIO2: An opensource (LGPL) game engine for the iPhone. It uses the Bullet physics library and 3D objects and scenes are prepared using the opensource tool Blender. The project offers many tutorials (16) some of which are screencasts. The project also boasts a healthy number (15) of deployed iPhone applications available in the AppStore. The engine has a free version that imposes a flash screen (advertisement for the engine), an indie version is available without this restriction for $50USD. The volume of developer documentation in the form of tutorials is great, although the seeming dependence on Blender to prepare the 3D models and scenes used by the engine may be a deal breaker if there is no programmatic workaround.
  • Oolong Game Engine (on google code): An opensource (MIT License) game engine for the iPhone with some credibility given that it was developed in large part by Wolfgang Engel, Rockstar Games' lead graphics programmer. It uses the Bullet Physics Library for 3D physics and fluid studios for memory management. It has seemingly been used in a small number of titles (3?) currently available in the AppStore and proposes that it was used as the basis for the iTorque Game Builder. The source code comes with a number of demonstrations, although developer support documentation and tutorials are really lacking, and the mailing list is quite empty. This might be an engine for hard core developers.
Missed the cut
I came across four engines that missed the cut only because of an associated cost or maturity. I may reconsider selecting one of these engines if (and only if) it can be demonstrated that their adoption will save me time (or money - yes I'm keeping timesheets) on this project.
  • Unity 3D Engine (Unity Technologies): A cross platform engine that offers iPhone integration features and support. A large number of titles use the engine and presumably some of which are available in the AppStore. The core engine costs $199.00USD (Indie) plus there is a cost of $399USD (Basic) for iPhone integration totalling at least $600USD to get up and running. The cost buys support both in terms of documentation and a company that will return your support emails and calls. The engine looks slick and it seems a lot of people are using it.
  • tTGB - the iTorque Game Builder (Garage Games): The Torque engine has been ported to the iPhone and is offered as two products: iTGB for 2D games which is available now for $500USD (presumably built on the old TGEA platform), and a planned but as yet un-released T3D that presumably will have iPhone support. The software is both a builder software for making the game (WYSIWYG 2D editor) as well as an engine and packing tools for deploying your game. As with Unity 3D, the cost of iTGB is for the documentation and human support, and also like that engine it looks really slick. It was unclear to me how may titles have been built using this product and are available in the AppStore.
  • Ston3D Engine (ShiVa): The Ston3D engine offers iPhone integration support as a free add-on after purchasing the core product called ShiVa for $169.00GBP (about $244USD). ShiVa is a Windows-based game development product (like the Torque product) and offers a crippled free version that prevents publishing created games. A number (at least 8) of iPhone titles have been released based on the engine and are available in the AppStore.
  • CubicVR Engine (on sourceforge): An opensource (LGPL) game engine with no clear stable builds (only SVN access). There are some iPhone demonstration projects provided with the source code. It uses the Bullet physics library and supports other mobile and console platforms. The code base is seemingly the basis for the Pocket Hoops game in the AppStore. The project does not seem very mature and there are no clear tutorials or support documentation for starting a game project. As such, it did not make the cut.
Out of Scope
I came across a number of related projects that partially addressed some of my needs, but ultimately were out of scope. They are listed here for completeness, or in case I miss-interpreted the projects.
Alternatives
Some alternatives popped into my head while 'deep googling' this problem and I thought I'd list them in the event I do decide to re-scope the project.
  • Write my own: One option that I briefly considered and dismissed given the time it would consume is to write a 3D rendering engine myself. I might be able to piece together the things I need from tutorial sites like Nehe and OpenGL ES. For the physics I could use an off-the-shelf solution like Box2D (used for Rolando) or Bullet.
  • Port an engine: There are many great 3D engines out there just ready and waiting to be ported to the iPhone. May of the communities of those engines have manifest demand, are discussing, or have already started the porting process like Irrlicht, OGRE, and Allegro. Frankly, I do not have the current chops or inclination to get into a port.
  • Use a Quake: Both the opensource (GPL) Quake and Quake III Arena game engines have been ported to the iPhone and are available on jailbroken phones via Cydia. Both engines are 3D, have long histories of community involvement (mods and engine ports), and the source code of their ports are available (quake4iphone and quake3-iphone). Also, id Software might be releasing these titles officially soon enough. I've had experience with these engines before, but not regarding the simulation and rendering of arbitrary programmatic generated world entities. As such I may have more trouble than a conventional 3D engine in trying to shoehorn non-quake techniques into the engine (maybe a good fallback position).
The Plan
As a warm up I was thinking of writing some tutorials on how to compile and deploy wolf3d, doom, quake, and quake3 for the iPhone. I suspect they will not be too hard to write given the sources are out there, and I expect it'll provide a good opportunity for getting up-to-speed on the environment.

Regarding my choice of 3D engine, I want to go with Oolong for as long as possible and fallback to SIO2 if progress proves too slow. I have booted the sample programs from both projects in the iPhone simulator, and have started to get a feeling for how one might map the algorithms and data structures from morphology evolution research into a 3D world. This brief research into available engines gives me some alternatives if the scope of the project changes or if I get fed up and decide I require developer documentation and support. I'm looking forward to starting the build early next month, after I finalize some GUI and algorithm designs.

Check out a similar round-up by Matthias Gall from September 2008 entitled "3D Engines on iPhone / iPod".

Please drop me a comment or email if you know of any further 3D engines or related resources.

Wednesday, March 25, 2009

Not Reading Passively: iPhone Links

In the spirit of not reading passively, I've been trying hard to write summaries of all the iPhone related links I've been reading. Here are a recent mix from the last week of high-level iPhone propaganda and low level technical details:

  • iPhone SDK 3.0: A New Beginning: The new 3.0 iPhone OS as the new desktop. iPhone apps are more like simple desktop apps than conventional mobile apps. Android is a long way behind re developer base/app maturity. Constraints on the iPhone are liberating. iPhone is secure, no viruses like windows mobile and blackberry. Lots of fanboy spam in this post as well.
  • Should An iPhone App Developer Charge Or Run Ads? (Galaxy Impact Case Study): TechCrunch post on paid apps versus free app with advertising. Example app tanked after releasing for free then charging a dollar then free again. Lesson is to pick the right model at the start and stick with it.
  • Pinch Media Data Shows The Average Shelf Life Of An iPhone App Is Less Than 30 Days: Only 20% of users return to app after initial download. After 30 days, 5% of users still use app. Return is about 70c per user, equiv with free app is an $8 CPM (most are 0.50 to 2.00).
  • Coder's Half-Million-Dollar Baby Proves iPhone Gold Rush Is Still On: Wired article on the iShoot case study (lucky bastard). >600K in a month, 37K a day as number one in app store.
  • Good iPhone Practices: Discussion of managing buttons in UITableViewCells. Only use a XIB file only for a complicated interface. Discussion of instantiating UIViewController subclasses.
  • Intro to the Objective-C Runtime: How objective c works at the run time level. A class is a struct. An object is a pointer to a struct, the first variable of which is a pointer to its class. Struct contains compatibility info for older operating systems, built on top of C, can go rooting around to see how it works/hangs together.
  • Using Multiple OpenGL Views And UIKit: Discussion on how to mix openGL views with UIKit controls/views. Apple suggest to avoid doing transformations with opengl views, avoid putting transparent buttons on them. Stop doing frame updates when opengl view is not visible. Discussion of opengl view, mixed with uiview, mixed with multiple opengl views.
  • Cocos2D: Open Source 2D Game Engine for iPhone Developers: Engine for easy development of 2d games for the iphone. Open source, built in physics engine (chipmunk).
  • iPhone - Interface Builder Tutorial: Excellent interface builder tutorial that was recommended by someone/where. Step-by-step, very clear.
  • Tutorial: JSON Over HTTP On The iPhone: Tutorial that demonstrates how to consume, parse, and display a JSON feed from the web in an iPhone app.
  • Apple iPhone Makes Up Half Of U.S. Smart Phone Web Traffic: Study that suggests that 50% of smart phone traffic on the web is coming from iPhones. Up from 10% in August 2008. Android is at 5% in the same market.
  • Engineers show off "no jailbreak" iPhone keyboard hack: What I liked about this article was the clever workaround for interfacing with the phone, specifically by using the headphone port (WTF?). It "allows two-way transmission of data using frequency shift keying, the same method used in early modems". Awesome.
  • Apple Putting The Squeeze On iPhone Developers: A rumour site. Describes an amendment to the iPhone developer contract that forces developers to submit to a 90 day refund policy for apps in the app store. Developers would have to cover apples 30% cut as well, leaving them out of pocket (how could that be?). Although the clause may have always been in the contract.
  • PhoneGap: A very interesting project focused on automatically generating native (!) iPhone (and other) apps from javascript. Way Cool! You can access device features. Oh yeah, it's open source. Check the video.

Monday, March 23, 2009

Running a Genetic Algorithm on the iPhone

I saw a post the other morning on the Genetic Argonaut blog entitled "Running Genetic Algorithms On A Cellphone". The post described the work by Christian Perone running a genetic algorithm in python on a mobile phone, specifically a Nokia N73 with Symbian and PyS60.

"Cool" I thought. "I can do something like that!"


I uploaded an old genetic algorithm script I wrote in Ruby for my Intelligent Algorithms project and it ran first time under iPhone Ruby (installed by Cydia), no problems at all. The above image is a screen shot I took in my jailbroken iPhone console running my GA script on a simple two-dimensional ("sphere") function optimization problem.

Not a big deal really. Wait until you can play with evolved 3D creatures on the iPhone, that will turn some heads!

If anyone wants the ruby script, shoot me an email.

3D Virtual Creature Evolution Part 1: Videos

Inspiration is an important concern for Natural Selection, my current iPhone application under development. In communicating the inspiration for the app over recent weeks, I have reverted to accompanying any description I write with a link to a YouTube clip of Karl Sim's early 1990's research. I thought I would take some time to elaborate on this inspiration and dig up a bunch of related motivational resources.

I'm breaking this down into two posts, the first (this one) provides a collection of YouTube links to the results of 3D creature evolution, and the second longer more academic most summarises the state of research with lots of links to people and papers (I'll publish it in about a week).

An important element of broader success of the field of virtual creature evolution (and ALife) is the visual results it provides. Karl Sims is commonly referred to as the godfather of the of the study of evolved virtual creatures not because he released the first papers on the topic (he didn't), but because he (or someone close) ensured that digital videos of the work were released and popularised. Importantly, these videos are still around on the web and are held aloft as the seminal examples of this and the broader field can produce.

The two great video's of Sims' work I often share around are the following (they both have narration):

Sims' creatures are adapted to address very specific and narrow goals in simulated 3D environments including: swimming speed, walking (distance from origin, total distance traveled), jumping, light following, and block possession.

Any searching of this topic on YouTube will very quickly turn up results related to Lee Graham's 3D Virtual Creature Evolution (3DVCE) project. The project offers software to design and run experiments for creature evolution as well as a distributed environment for more elaborate experiments. A very clever marketing strategy adopted by Graham (intentionally or otherwise) was to exploit YouTube to both publicise the fruits of his efforts, and as a database for the favourites and the 'zoo' of user submitted creatures. The effect is a near dominance of video-based searching on the topic.

There are far too many videos to list (and I have watched a large number of those that are out there). I've listed a broad pseudo-popular and interesting collection, as follows:
Graham also links to an impressive number of related projects, providing an excellent summary of the state of web-available projects in the field.

Nicolas Lassabe has some interesting challenges involving walking, stair climbing, block pushing, walking across blocks, and even skateboarding:
Evo Runners (I can't access their blog either) provides some good videos, some with some excellent production quality, including:
The AnimatLab have a very interesting video involving the evolution of different flapping strategies in a simulated bird:
I really love the videos by Tyler Streeter of his evolved humanoid jumping behaviour. It makes me laugh out loud every time!
Finally, here are some additional random clips I came across and thought worthy of a look:
An excellent aggregation of videos and links in this field (inspiring this post) was compiled and published by Alex Champandard of AIGameDev last February entitled: Evolving Virtual Creatures: The Definitive Guide.

Please, if you know of some more/better videos of three dimensional creature evolution leave a comment or get into contact with me.