More than a year ago @tiegz, @shinyee_au, and I participated in gamehackday. We started off with this idea for importing photos from a foursquare location into a game, and then battling your friends that were also at the same location.
Eventually our overambitious hack was whittled down into just taking a photo and turning it into a platforming level for ImpactJS, without any user-authoring. I’ve recorded a screencast about it (I highly suggest you watch it to understand what’s going on), but never got a chance to explore some of the code involved.
There were two components to the hack itself, analyzing a photo to create a tile map from it, and injecting that tile map into ImpactJS at runtime (which it definitely wasn’t meant to do!).
“Tile-izing” an Arbitrary Photo
We could have gone with some very advanced edge detection techniques, but instead used a very simple algorithm that we basically just thought up on the spot:
- Average the RGB values for each pixel. If less than or equal to
(255 * 3 / 2), mark as solid.
- Divide the image into arbitrary tiles. For each tile, if the majority of pixels within it are marked as solid, the tile is solid, otherwise the tile is transparent.
To actually accomplish this, the following hacky code was used. This is completely unedited, and does have some bugs (it was a hackday!):
The function can be divided into two components, and uses the raw pixel data from a canvas. The first is determining if a pixel should be considered black or white:
This discards any alpha channel, since we assumed we were using photos. It also groups pixels based on tilesize.
px is the “pixel index”, which assumes that pixels are indexed from the top left to right, one row at a time. The raw pixel data, _idata, is a single array with the R, G, B, and A values one after another. Therefore
p is incremented by
4 each step. One other note is that
~~() is a faster shorthand for
Once the pixels were converted to a form of bicolor, the next step was to make the tilemap in a format that ImpactJS expected.
This loops through the bicolor array of arrays of
false backwards. If the average of all tiles is black/solid, then mark the tile as solid. ImpactJS expects the map to be an array of rows, each holding a single cell. The only other strange part is the
_reverse variable, which we added to allow for images that were predominantly dark.
This code is pretty inefficient, but it worked!
The last component, not shown here, was to take the
_map array, and convert it to JSON via
JSON.stringify. This was then
POSTed to a server-side script which helped with the second stage…
Getting ImpactJS to Load a Dynamically Generated Level
Once the map data was generated clientside and sent back to the server, the server injected it into a simple template that exposed the data as global variables:
We then hardcoded ImpactJS to look for a level called
game.levels.dynamic in our
main.js game initialization file:
As you can see, we used the demo code from ImpactJS as a start. The trickiest part was the map file itself. To avoid doing anything super crazy, we simply referenced the previously defined global variables (
MAP) from within a static map file:
Referencing these global variables allows ImpactJS to still do its thing regarding its internal loading system.
This is a bare minimum ImpactJS level, containing three layers: background, collision, and main. We used the uploaded image (stored temporarily on the server) as the background. The collision layer is invisible, but ImpactJS uses an image to represent the collision layer when using its level editor, Weltmeister. The main layer uses a custom tileset we created that contains only two tiles: a translucent square, and an empty tile. You can see this in the screencast; tiles that are solid are slightly grey compared to those that aren’t.
And that’s basically it! We’d always wanted to release the source, but due to a few reasons (tired of it after the hack, ImpactJS isn’t open source, etc) we just never got around to it. But now, hopefully, this explains how it worked! Definitely let me know if things could be clarified.