IOS, Box2D and OpenGL.

Looking through the top selling IOS apps (well, the games section), it seems like having realistic physics is the place to be. (Angry Birds of course, but also others like Cover Orange, Cut The Rope, and the new number one app Tiny Wings).

Tiny Wings (excellent by the way) credits Box2D on one of it’s screens – this got me thinking how easy it would be to get something up and running that uses the Box2D library.

There’s a lot of stuff out there regarding Box2D on IOS, but most of it is related to cocos2d, and that’s not really my thing. Fortunately for me, I found an excellent blog post on Cocoanetics which walks you through how to quickly get Box2D working in a simple app.

My code is based largely on their excellent work (and of course the equally impressive Box2D library itself) – the changes I’ve made are purely to get something working using Open GL.

A big pile of blocks...

The code here generates a universal app (ipad or iphone/ipod touch). All the blocks are generated at random (size, colour, linear and angular velocity). Touching the screen restarts the process (or waiting for the blocks to all stop moving…).

Change the MAX_RECTS define in the ViewController.h file to change how many blocks are generated – size and colour info is done in Rectangle.m. Here’s the project:

nibfree

Side note – the project is called nibfree as I was originally trying to get a universal opengl app working with no nib file (done!).

I’m not sure why, but it’s quite mesmerising watching how the blocks fall.

Big thanks to the Cocoanetics guys for their original blog.

As usual, any feedback at all is welcomed.

Back in the saddle again – iSpacies2

Blimey, almost 8 months since my last post.

I’ve finally got back on to doing some iPhone coding – spending some time away from this and coming back, it felt like I’d forgotten a lot of stuff. After checking my old projects (iSpacies, Ant Attack) it appeared that maybe I didn’t know as much as I’d thought…

So, with that in mind, I decided to do iSpacies from scratch, giving myself only a couple of weeks, and to focus on what I didn’t like last time.

Here’s what’s better than before:

  • The graphics (same sprites, but a nicer background with a parallax starfield)
  • The sound (it’s got some)
  • The code (much more object based this time)
  • Collision code on the bases (much improved)
iSpacies2

iSpacies2 in action

When I started this I wasn’t sure that revisiting an old project was going to be beneficial – but now I’ve done it, I’d recommend it to anyone. I’m much happier with the way this one has turned out, and it’s giving me some momentum to carry on with new stuff.

Left box controls left/right movement, right box to fire (anybody got any nice button graphics I can use?)

Here’s the project, would really appreciate some feedback on this (good or bad):

(project removed)

There are ants lol

Following my last update, and fortunately for me (and the project), someone has come forward to help out with the graphics – Colin Wren is his name (see his site here), and he’s a top man.

The graphics Colin supplies are in .dae format, and with a lot of help from this site (Tipu’s iTiger) and an XML parser (TBXML), and much head scratching, we got the models going nicely.

At the moment they’re not animated, but they will be soon…

Other things now in there are bombs and collisions. To aid testing on the simulator I’ve added an auto-jump feature on the player – a collision in the x or y plane makes him/her jump. This also works quite nicely when testing on hardware so I may keep it (maybe as an option). I’ve also updated to the latest version of the 71Squared Sound Manager.

The button top left flicks between the male and female model – other controls as before (bomb button now works of course).

So, still plenty of things to be done, but slowly and surely it’s coming together.

Here’s the latest project folder:
(removed)

Happy New Year – Ant Attack project source

Here’s the latest version of the Ant Attack project – I’ve not had much time to spend on this over the last month or so, so I’ve decided to release it as is (unfinished of course).

The map data used in here is ripped directly from the spectrum original – see this website for an explanation of how the data is stored:

Hacking 3D Ant Attack

Inside the map class I read in this data and parse it into discrete blocks (total of 2650 of them) and then render the ones closest to the current camera position – this system isn’t great by any means, but does provide a simple enough algorithm and reasonable frame rate. (Would appreciate it if someone could test this on a 3GS device and let me know the frame rates achieved).

At the moment, the ‘ants’ in the map just run around at random, colliding with the map and making occasional direction changes.

The large button on the left is used to move, right button to jump – the button next to it currently does nothing (other than stop the jump sound playing). The red buttons allow zooming in and out – if you touch the middle of the screen (where your player block is) you can change the camera angle…

The sound code is basically taken from the 71Squared guys, the glu functions were found on Iphone Development (amongst others).

All opinions / comments / questions are welcomed…

(project removed)

Moving into the 3rd dimension

I’ve spent the last couple of months playing around with things in 3d (OpenGL) on the iPhone. I find it easier to stick with and learn something if there’s a definite end goal, and so after thinking about it for a while, I decided to do a version of the classic ZX Spectrum game 3d Ant Attack by Sandy White (see his page here: Sandy Whites Ant Attack page).

The main difference between the original and this one is that my version will be ‘proper’ 3d, compared to the isometric original (not to disrespect that version, it’s an absolute classic). This also means we can get nice things happening, like zooming and panning the camera, lights, textures and other bits and pieces.

Here’s a screenshot of how it’s coming along – all graphics (quite clearly) are programmer art. Hopefully I can find someone to help me out with something a little bit prettier.

Graphic artist needed!

Graphic artist needed! Click to zoom.

The green squares overlaid are the control buttons (left big box for movement, right smaller box for jump). I’m finding the hardest thing to get right is the control method on the iPhone, and am still not happy with how I’ve got it at the moment. The small brightly coloured cube is your player (I did mention it was programmer art).

So far I’ve got the map in, along with a camera system allowing zooming and switching angles. You can move the player box, jumping and general map collision are working well. I’ve also got 5 ants (well, brightly coloured boxes) running around and colliding with the walls. Current frame rate (on a 1st gen iPod touch) is between 40 and 60fps.

Still to be done then, in no particular order:

A.I. for the ants

People to rescue

Sound

Better graphics (any volunteers?)

Lighting

User interface tweaks

Bombs

Menu screens

Sounds like a lot of stuff, but after getting all the 3d stuff going I’m hoping I’ve got the hard bits done….source code will follow when it’s finished if anyone’s interested.

iSpacies – Beginning iPhone game development.

iSpacies

iSpacies

When I first start to work on a new piece of hardware, almost inevitably my first attempt at getting something up and running is a version of Space Invaders – I consider it  the extended version ‘Hello World’ for games programming, If you stop and think about it for a minute, getting something like Space Invaders running involves not only gaining a reasonable understanding of the underlying hardware (including graphics / input / memory / sound etc), plus a grounding in basic game logic function and flow, but also a chance to get to grips with the less obvious side of development where sometimes people come unstuck – the IDE, compilers, linkers, and associated tools.

Other machines I’ve done this on are the PC (when Direct X first appeared), Playstation One (on the Net Yaroze home development kit), Gameboy Advance, Nintendo DS, and finally here we are on the iPhone.

This time around there were a few more things to consider. First of all, aswell as needing an iPhone or iPod Touch if you want to test on real hardware (no different there to any of the other machines mentioned, luckily I already had a Touch), you need a Mac to develop on (at least without jumping through flaming hoops – Google is your friend if you want more info). You’ll also need to be ready to learn Objective-C, which is the language used to develop with. Basically it’s an object-oriented superset of C (but different to C++). If, like me, you’re coming from a C background, and are prepared to spend some time playing around with the dev kit, and going through various tutorials on the internet, then you’ll eventually get used to it.

Enough babble – on to the project. Even though this is a 2D game, I made an early decision to go the Open GL route (I’ve got a little bit of past experience).

There are a few websites I want to mention as excellent examples of iPhone tutorials:

www.71squared.co.uk – The particle system I’m using here is almost completely theirs (small change to use my texture class), as is the particle.png, and the Singleton class. Absolutely excellent site with amazing tutorials.

Iphone Development – Fantastic info on all things iPhone (and co-author of the best iPhone book I’ve read)

Simon Maurice’s Coding Heaven – A great series of iPhone specific OpenGL tutorials.

There are lots of things missing from this at the moment: In no particular order Sounds, High scores, Difficulty curve, Extra lives.

I’m running the latest version of Xcode (3.1.3) and iPhone SDK (3.0). Let me know if you have any problems with it (I’m far from being an expert but will try my best to help).

Also, this is probably not the finest example of Objective C (or iPhone) code, but I’m offering it up for beginners like me. Any suggestions / criticisms / improvements please let me know. Maybe we can all learn and improve together…

Here’s the project:   (removed)

Game Controls: Press to the left or right of ship to move left or right (has to be near the bottom of the screen). Anywhere in middle of screen to fire. Press anywhere near the top of the screen to Pause / Unpause (controls aren’t perfect either!)