HTML5 Zone is brought to you in partnership with:

Chris has been married to the love of his life, Danielle, for over four years now and they recently had their first child, their daughter Helena. Chris was an expert wrangler of developer content for DZone, then spent some time in the marketing department for DZone and AnswerHub, where he taught himself to code html. Now he handles production duties for DZone, so he's responsible for all those emails you get from DZone! BTW, if you're looking for someone to bore you to death with endless football knowledge, Chris is your go-to-guy! Chris is a DZone employee and has posted 311 posts at DZone. You can read more from them at their website. View Full User Profile

Blast Aliens in Your Browser with Your iOS Trigger Device

03.21.2012
| 14284 views |
  • submit to reddit
The guys over at WebDigi, a London-based web application development company, has released a demo of Space Invaders Spaceship Pilot, a pretty amazing application that uses iOS devices as the controller for a web browser-based game on your desktop or laptop.  No application is required on the iOS device, you simply scan the QR code with the device and the controller code reads the accelerometer values and sends the movements to the node.js server.

WARNING: This demo may cause a significant decrease in productivity around the workspace, as it almost did here at DZone.




Here are the key parts of the iOS device controller code:
//Detect if the browser supports DeviceMotionEvent
if (window.DeviceMotionEvent != undefined) {
//ondevicemotion is fired when iOS device detects motion
  window.ondevicemotion = function(e) {
//ax is the movement on the x axis.
//This motion is used to move the ship in the game
  ax = event.accelerationIncludingGravity.x * 5;
  ay = event.accelerationIncludingGravity.y * 5;
 
//Status 0 is start, 1 is left, 2 is right, 3 is stay
if(status == 0){ //initial condition
  status = 3; //stay
  socket.emit('spaceChange', {'ax': 3});
  statusmsg = 'Waiting for movement';
}
if(ax > 14 && status != 2){ //move right on device
  status = 2;
  socket.emit('spaceChange', {'ax': 2});
  statusmsg = 'Moving ship right';
}
if(ax < -14 && status != 1){ //move left on device
  status = 1;
  socket.emit('spaceChange', {'ax': 1});
  statusmsg = 'Moving ship left';
}
if(ax > -14 && ax < 14 && status != 3){ //device held steady
  status = 3;
  socket.emit('spaceChange', {'ax': 3});
  statusmsg = 'Ship held steady';
}

Only changes in direction on the iOS device are pushed to the node.js server, reducing the amount of data pushed between phone and server.  The node.js server is coupled with Socket.IO, making it easier to deliver data in realtime using most web browsers.  Since Socket.IO code is run on both the server and client sides, pairing it with node.js allows accelerometer values to be pushed instantly from mobile safari to your browser.

After testing (read: playing) the demo for a few minutes, I couldn't help but be impressed with the speed at which the accelerometer data was sent to the browser.  As pointed out on the WebDigi Blog:

Current space invaders game uses only X axis movement on the iOS device.  However, all three dimension values are available on iOS Safari ondevicemotion method.  It is technically possible to do much more with the iOS device.

-- webdigi

So if you have an iOS device, and you miss playing Space Invaders, be sure to give this demo a try.

Also, special thanks to Roshan Abraham (DZone username: rabraham) for sending us a link to this information.  If you know of any cool demos, useful tips, etc. be sure to submit them to DZone using this button:



which is located near the top of each page.