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…

Ant Attack

13 Responses to “Happy New Year – Ant Attack project source”

  1. J says:

    there are no ants lol

  2. Abraham Si says:

    John,

    Just want to let you know that it is a cool game. My kids figured out how to play it even thought it’s not finished.

    This is the first 3d game that I found on the web which is not a port using OpenGl (like Wolfenstein 3d classic from id software).

    Thanks for your work. It just needs some polishing and will do great in the app store.

    Abraham

  3. John says:

    Thanks Abraham – I appreciate the comments.

  4. Gandi says:

    How do you play?

  5. John says:

    There’s not much to it at the moment other than running and jumping around the city. Eventually there’ll be people to find and lead to safety, whilst avoiding (or shooting) the ants. (Unless you mean how do you get it on your iphone/ipod touch, in which case you need the iPhone Dev kit to compile the project…)

  6. D2 says:

    Hi John,
    I know you mention that the map is read from the BIN file and reference the site as far as how it is read, but how did you go about building it / editing it? I think this is a fascinating hard core tutorial into the 3d side of Open GL, and a great piece of code to learn from. Although if you could let me into the mastery of the map dev, I’d be forever grateful :)

    D2

  7. John says:

    Hi D2,

    Thanks for the post.

    I’m not sure what you mean by editing, but I’ll try and explain the map functions.

    The raw map data is a 128×128 byte array (total of 16384 bytes), and this is read into a similar size array in the Map class.

    Each byte (actually the lower 6-bits) represents the vertical data for the map in that location.

    For example, if the byte was 0×00111111 that would be a solid wall 6 blocks high (1 bit per block).
    If the byte was 0×00101111 it would be one block, a one block space, and then a block 4 high (remember we ignore the top 2 bits).

    Let’s stick with a map byte of 0×00101111. To keep things simple we’ve now got 2 choices:

    We can render each block individually, giving us 5 blocks to render, or with a bit of merging we can render 2 blocks, 1 block of height 1, and 1 block of height 4. Rendering 2 blocks is obviously quicker than rendering 5 blocks, and so this is essentially what the optimisemap functions do in the map class – they work out any adjoining bits and combine them into one larger block.

    So for each byte of the map, we write a block (or number of blocks) into our map data structure – we know the vertex coords of each element – the x and y is based on the position in the raw map data, and the z is based on the location and size of the merged block we’ve calculated.

    After we’ve done that for the whole map we end up with a collection of blocks to be rendered (total of 2650 of them in actual fact).

    Rendering all of these every frame is a bit too much for my iPod touch (1st gen) – not sure what it’s like on a 3gs, so once the map is optimised I step through the newly created array (the 2650 blocks) and work out the starting block number of each row in the y-direction.

    Then, when rendering the scene it’s just a question of working out which rows to render, based on where the camera is currently pointing. For example, if the camera is pointing to y-position 50, then we only need to draw rows around 50 (rows 30 to 55 say, although I can’t remember the exact numbers off the top of my head).

    This brings the frame rate up nicely, and is actually quite simple to implement. This process of merging blocks could be enhanced to take into account blocks next to each other, but I haven’t got round to doing that yet (in doing so, it would actually bring the number of blocks to render down further and aid performance).

    Hope this answers your question – let me know if this doesn’t make sense (or I’ve missed what you were asking about).

  8. D2 says:

    Now that was a sensational answer .. it’s exactly what I was looking for :)

  9. Farzad says:

    Hi,
    It is fantastic
    Would you please tell me when the next edition is?

  10. Mike says:

    That is an EXCELLENT project. I’ve often looked at Ant Attack and wanted to have a go at something like that myself. I think what you have so far is such a good start.

    I really want to see this progress as I think there is a real market for updates speccy games :)

    I hope the project is still moving forward. Once I’ve got my book out the way this is exactly the kind of thing I want to start looking at. Your code has already given me some ideas :o )

    Thanks for sharing and keep up the excellent work.

    Thanks

    Mike

  11. John says:

    Wow Mike – a real honour for me that you’ve posted over here.

    The project is still going – thankfully a graphic artist came forward to help out, so now we’ve got it looking a lot better, as well as progressiing a few other things.

    There will be a site update in the next day or two with the latest build (3d models for the player, ants, and bomb being the highlights).

    Good luck with the forthcoming book…

  12. John says:

    Update on main page…

Leave a Reply