Dec 05 2010

Turn down that noise!

I now have fully deformable terrain! Srsly, click the read more and try the game! :)

If you didn’t guess from the title, I’ve added sound to the 16k game. This was kind of interesting. Generally, I’ve done sound by embedding mp3 files in my games. So my first attempt was to generate some sounds– shots and explosions with sfxr, then package them as 11khz mp3s in Audacity. Unfortunately, the smallest I could pack the mp3s was about 3kb.

I tried embedding one of the explosion sounds into the previous build, but the release size went up from 8.88kb to 11.9kb. I only wanted 2 sounds, so that was doable, but it would eat up all my remaining space and I’m not quite done adding features to the game.

The other option is to use FP10’s ability to dynamically generate sounds. Dr. Pettersson (the guy who wrote sfxr in the first place) gives a good overview of sound synthesis here. If you need an overview of how to actually do this in AS3, see here. After playing around with noise for a while (varying how long each amplitude was being held), I eventually got an explosion and shot sound I could live with. (EDIT: Forgot to mention, to provide some additional situational awareness to the player, the volume of the explosions and shots is proportional to the distance of the player to the sound. I did consider also adjusting the Left/Right levels based on the relative x/y of the sound and the player, but haven’t implemented that yet if at all. It’d be hard for me to test, since I’m working on a laptop.)

Unfortunately, I thought this led to a game that was a little too ‘sparse’ sounding. I needed to add a sound track. Again, this isn’t too difficult, I just used sine waves as my ‘instruments’ as depicted in the second resource. However, making 8192 (or multiples thereof) calls to Math.sin() every time the buffer needed to be refilled was a performance hit I couldn’t take. So I changed the game to pre-calculate those values for every note when it is loaded (just 3 octaves from A3->A7) and store them in an array (wavetable). This was the first performance optimization I needed to make in this entire project.

I also found I was getting a clicking sound every time the notes changed. So, switched from feeding the raw wave amplitudes to fading the notes in and out. (Apparently my speakers don’t like abrupt changes in the waveform coming to them.) That covers playing notes… to actually play a tune I store the score in a class I creatively called ‘CScore’ which also tracks where in the score the soundtrack is currently playing. A method in that class returns the indexes of the wavetable for the notes to be played.

Every time the sound buffer needs to be refilled, the next note is called. This happens every 1/5th of a second… so my CrummySynth is locked at about 240bpm. I actually designed the melody 4 bars at a time using a website for children checking to make sure they weren’t too discordant. (I don’t have composition software) Then, because CScore understands notes by semitones above A3, I encoded each melody by hand first into the lowest octave and then into the middle. To make everything string together, each 4 bar melody starts on a C (stop laughing Meg).

Honestly, in a production game where embedding or streaming wasn’t an option, I’d rather just use FLOD.

To keep everyone sane, I added a Mute at this time as well. Press M to turn off the sound.

Right, so with a soundtrack of sorts, the game was starting to look and sound good. One thing that had been bugging me is that I had set the center of my collision spheres to be the center of rotation– basically the (0,0,0) coordinate for the enemy tank models. As a result, you could shoot under the center of the tanks to kill them. Since the game did need a little more stuff going on visually, I added a check to the shot movement code, to see if the shots were moving underground. If so, I added an explosion… That looked ok, and while playing that version I realized I could decrease the height of the terrain where the shot impacted. Thus deformable terrain. Wikipedia is remarkably mealy-mouthed on the subject of which game was the first 3D game with deformable terrain. If anybody knows, leave a note.

Click the read more to play the current build. Release build is now 10.8kb. Any comments are welcome… but I’m particularly interested right now in whether the game is running smooth or not on different computers.

W- Move Forward            I- Pitch Turret Up
S – Move Backward          K- Pitch Turret Down
A – Rotate Tank Left        J- Rotate Turret Left
D- Rotate Tank Right      L- Rotate Turret Right

Space- Shoot          C- Align Turret with Tank      M-Mute

3 Comments

  • By Richard Davey, December 5, 2010 @ 5:08 am

    Hey that’s quite nice. I like the sound and fx, very well done indeed. Adds nicely to the game – especially as it’s so subtle. Not sure I like the way the terrain deforms right below you. Makes it feel strange. But loving the enemy tanks.

  • By Ann, December 5, 2010 @ 7:14 pm

    I liked the graphics, and it’s easy to start playing.

  • By Tony, December 6, 2010 @ 7:56 pm

    Thank you both for your comments!

    @Richard- I think I’m going to tone down the terrain lowering some before the final release. Either that or cap it so that it can’t be lowered more than 2 or 3 steps. I’m not really good yet at tuning gameplay.

Other Links to this Post

RSS feed for comments on this post. TrackBack URI

Leave a comment