Coding

HTML 5 Asteroids on the iPad

I’ve added touch controls to the HTML 5 Asteroids game that will only appear when you’re running it on an iPad.  The game will expand to fill the whole screen for maximum asteroid blasting action.  Orient your iPad in landscape fashion!

Play It!

 

There are a couple caveats (warning some nerdy content ahead):

Tunneling Effect

On the iPad the game runs at around 22 frames per second at its peak, not nearly as good as around 40 on a laptop, though it is enough for gameplay even if it can get choppy at times when there’s a lot of stuff whizzing around.

Unfortunately I’m only testing for collisions when the objects are rendered so it’s quite possible with lower framerates to have a bullet flying out of your ship first get rendered on one side of the asteroid you’re trying to pulverize and in the next frame get rendered on the other side letting it sail undamaged into your ship.  This is called the tunneling effect.  This is not a problem on a laptop or desktop because the framerates are high.  Since we’re sampling less per second (~22 verses ~40) a moving object is drawn less times though it’s traveling the same distance.  In other words if an asteroid travels from the top of the screen to the bottom of the screen in one second, on your computer it will most likely get drawn 40 times during that trip and on an iPad it will get drawn only 22 times leaving larger gaps between the drawings.  These larger gaps leave more of a chance for things to slip through and not collide when your eyes say they should have.

I’m considering augmenting the collision detection to remove the tunneling effect but it will need extra math which means slower framerates, at least initially.  It might be a better use of my time to pump framerates up.

Buggy Touch Events

There’s a bug with the iPad’s implementation of touch events in Javascript: if you have two fingers on the screen and raise one of them, a “touchend” event gets fired containing a reference to both fingers even though the second one hasn’t been raised.  If you then raise the second one, a “touchend” event is not fired.

It drove me nuts trying to figure this out and I haven’t been able to work around it.

What this means for the game is that using two fingers at the same time will get frustrating fast as the control you’re holding down will stop working periodically.  I suggest tapping for the best experience instead of holding.

Oh, this does not happen on the iPhone in case you were wondering, it works as expected! I have sent a bug report to apple (with a sample html page) and hopefully they already have a fix that’s not available yet or they will fix it for a future release.

On a side note, why aren’t Apple’s bug lists searchable by the public?  They must get reams of dupes.

In any case, let me know how it works!

Coding
Games
Javascript
Projects

Comments (0)

Permalink

HTML 5 Asteroids

For some months I’ve been playing around with HTML 5’s Canvas element because I was curious about it, especially after seeing it run Flash.  I hacked at it for a while then realized I had produced an Asteroids game.

Play It!

 

The source code is up on GitHub!

The Canvas element is super cool.  Basically all of the sprites in the game are simple paths defined by an array of points.  These points are translated, rotated and scaled based on their state at any given point in the game using the standard Canvas transformations.  Plus it gives it a nice retro feel.

Let me know what you think!

Here’s some cool links to things that helped me out with the game:

  • Determining Whether A Point Is Inside A Complex Polygon.  Firefox does the wrong thing when it comes to isPointInPath(), the main Canvas method I used for collision detection; It doesn’t apply the current transformations before determining if there’s been a collision (and they won’t fix it for some reason).  I implemented this method to get around the problem but it works fast enough it could be used on all browsers.
  • Metanet Software, the makers of the awesome flash game N, have made a couple of extremely useful tutorials on collision detection.  These describe some of the techniques I used in the game, in particular the grid based broad-phase they describe in their second tutorial.
  • I used typeface.js ’s convert tool to convert the awesome Vector Battle font into some JSON point data that could be easily used in the canvas.  Typeface.js is a neat library for easily embedding arbitrary fonts in the page.  On supported browsers it creates small canvas elements and renders the font inside based on the CSS.  I borrowed bits from the library to get the font rendering.

Coding
Games
Javascript
Projects

Comments (55)

Permalink

Ruby Vim Fu

This last Thursday Alf and I gave a talk at LA Ruby on Vim, in particular how we use Vim to edit Ruby code.  Giles asked us to present on Vim months ago and we finally got around to doing it.

I think it went well considering we probably spent more time figuring out the background color scheme on our slides than preparing what we were going to say.  I wasn’t worried, I’ve been using Vim for 15 years now so it was my favorite kind of speech: one where I felt completely at ease with the subject matter.

In any case here are the slides in a viewable form on Github, as well as the source.  We used the Slideshow gem to set them up.

The slides probably will not be of any particular use to you since they’re rather sparse.  We however added a bunch of links on the end to blogs/screencasts/cool sites we like as well as all of the plugins we mentioned during the talk.

So you don’t have to hunt through the slides here are those links in all their glory:

Links

http://www.vim.org/

MacVimhttp://code.google.com/p/macvim/

http://vim.wikia.com/wiki/Best_Vim_Tips

Cheat Sheet!http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

http://items.sjbach.com/319/configuring-vim-right

Screencasts on Vimhttp://vimcasts.org/

Vim Tip a Day on Twitterhttp://twitter.com/vimtips

http://durdn.com/blog/2008/11/26/vim-mind-share-soaring-roundup-of-10-vim-articles-recent-and-older-gems/

Vim Recipieshttp://vim.runpaint.org/toc/

http://biodegradablegeek.com/2007/12/using-vim-as-a-complete-ruby-on-rails-ide/

Plugins

The Rails Pluginhttp://rails.vim.tpope.net/

NERD Treehttp://www.vim.org/scripts/script.php?script_id=1658

Command-Thttp://www.vim.org/scripts/script.php?script_id=3025

BufExplorerhttp://www.vim.org/scripts/script.php?script_id=42

AutoTaghttp://www.vim.org/scripts/script.php?script_id=1343

Vimballhttp://www.vim.org/scripts/script.php?script_id=1502

NERDCommenterhttp://www.vim.org/scripts/script.php?script_id=1218

NERDSnippetshttp://github.com/scrooloose/nerdsnippets

Alignhttp://www.vim.org/scripts/script.php?script_id=294

RubySingleTesthttp://www.vim.org/scripts/script.php?script_id=2869

Coding
LA Ruby
Ruby
Vim

Comments (0)

Permalink

Ruby Single Test

I, like many developers, do my best to follow the practice of Test Driven Development (TDD). In practical terms this means you write a test to test your code before you write the code itself. This approach forces your mindset into having a clearer sense of how the code will be used since you’re writing code to use the code first.  Got it?  Increased test coverage also means more robust and maintainable code.

My first formal introduction to TDD was a three-day TDD workshop put on by Jim Weirich and Joe O’Brien while I was at the Los Angeles Times. Jim, an expert EMACS user, had his system setup to run tests quickly from within the editor, even showing a red or green notice indicating the pass/fail status of the test.

This may not seem like a big deal to people using any number of IDEs but personally, I’m a Vim user (specifically MacVim, it’s an extremely well done port of Vim using Cocoa). I love it. I’m way faster editing inside of Vim; my brain has mapped around all of the commands because I’ve been editing with it for so long. I’ve been using vi variants in one form or another since my Freshman year of college when my Computer Science 101 professor forced us all to code with it for our programming projects on the school’s Sun server. While doing Java work two or so years ago, I developed mostly on Eclipse and eventually broke down and purchased a license for the Eclipse vi keybinding plugin. Now that I’m employed for Ruby work I’ve switched back to Vim full-time (after a 1/2 hour dabbling with Textmate).

Jim’s EMACS setup made me jealous. During the class I figured out how to set the make program to use ruby (or spec for rspec tests) and Quickfix will run the tests for the file. Here’s the command:

:set makeprg=ruby\ %

The \ escapes the space and the % gets expanded to the current buffer’s filename when Quickfix is activated. Quickfix was originally used for a “quick” way of compiling your program to see if there’s any errors. Vim will even move your cursor to the file and line number of the first error if there are any. This works for ruby tests too! Another Vim setting, errorformat, is how Vim parses errors from running makeprg.

OK, so this is all great, but what if my test file is huge? What if it takes 10+ seconds to run? What if I’m only interested in a single test because I’m being a good developer and practicing TDD?

This is where the Ruby Single Test plugin comes in. The plugin is an extraction of some functions I’ve been playing with in my .vimrc file for the past couple of weeks. All it does is provide a command for running the test block your cursor is hovering over in your test file (<leader>. by default.  <leader> is usually comma or backslash). Most of the time you’re only interested in a single test while you’re editing code so turn-around time is vastly improved. No more jumping back to the console to run a test.

To install just drop the ruby_single_test.vim file in your .vim/plugin directory.

Currently it works for Test::Unit as well as Rspec tests.  I’ve been using this quite a bit recently and I think I’ve worked out most of the bugs. My setup isn’t necessarily the same any everyone’s so please let me know if you have any problems with it! The code is up on Github, so feel free to play with it.

Coding
Plugins
Vim

Comments (3)

Permalink

Javascript Love



I’ve been a busy bee releasing stuff into the wild lately. Most of them have been Ruby on Rails plugins but I’ve also been doing some Javascript work. Here’s two tidbits that I developed at the Los Angeles Times.

Inplace_preview

Inplace_preview is the first. It monkeypatches ScriptaculousInPlaceEditor to give it a preview mode. It also includes an ability to do an ajax call for rendering the preview. It comes in handy when using an InPlaceEditor for editing HTML but with the ajax rendering anything would be possible.

Here’s an example (if you save you’ll get a big-ol error since we don’t actually save here):

Click Here to Edit…

Gallery_scroller

Gallery_scroller is small Javascript library for creating a scrolling gallery that we’re using on more projects then I expected. It requires Prototype and Scriptaculous effects.js libraries.

Here’s an example:

The documentation on both of these goes into detail on how they work, so check out their github sites!

Coding
Los Angeles Times

Comments (0)

Permalink