2009/09/03

Physics in my iPhone game (+video)

Hello kids,

As I promised in one of my previous posts I wanna write something about the physics system in the game. I am not really into physics and I don’t want to go much into a details, so don’t expect anything technical...

The physics is the key for my game and because I didn’t feel good about programming all this physics stuff by myself, I have decided to use some existing physical engine. You can find many existing engines all over the internet, but what I really needed was not something like huge massive physical engine, but just few simple functions. I didn’t need those soft body physics or physics for liquids or something like that. I just wanted simple, but fast enough collision system, with gravity involved and the collisions should make some impact on the bricks – you know, to feel like rubber bricks.

So I started searching the web. Again! I completely passed all those well known physics engine like PhysX or Havok. Not only there is a problem with licensing and stuff like that, but they are really huge and don’t fit my needs. Then I find ODE (Open Dynamics Engine), but again it was not the right thing for me. Quite promising was Box2D, it’s a C++ based engine and the implementation to my project didn’t seem that hard. But before I started working on that I found Chipmunk engine and this video. I was impressed with the speed and all the comments how simple it is to implement it.

I found some examples how to include Chipmunk into XCode project - http://johnehartzog.com/2009/04/integrating-chipmunk-into-objective-c/ but for me was very useful the Moon Buggy tutorial which is not for iPhone application, but each line is very good commented. Overall it took me about a day of work and Chipmunk physics was in my game and worked! I was so excited about it, that I said to my grandsons to take a camera to my place and shoot short video from the game. So here it is.



I know, it’s nothing special right now, but you can see the basic functionality is working. With one finger you move with the last imported block, with two fingers you move with all bricks on the screen, double tap import another block. There is gyroscopic feature which influence direction of gravity – to be always down to the ground. And there is some bouncing. Still not like gel or gum, but I am working on it.

On the video you can see also one big problem. It’s not optimized yet. When there are more bricks the framerate falls down rapidly and the game becomes snatchy. You can see two numbers in the left corner of the screen. The first one is an amount of blocks on the screen and the second one is time in milliseconds of one frame. As you can see, most of the time the second number shows (around) 30 milliseconds, but at the end it jumps up to 60 or even more milliseconds which means 16 FPS or less.

That video was shot by my grandsons over the last weekend, so it’s not actual version. Right now I have implemented some optimization and corrected some of my mistakes (Yes, everyone can make a mistake.) and the program can handle much more bricks with stable FPS now. But about that I will write sometime later. Kids, I have to tell you it is great when the summer is over - I don't have to spend so much time in my garden and I can fully concentrate my time on my iPhone game!

Žádné komentáře:

Okomentovat