Author Archive for Jon Howard

08
Feb
12

HTML5 Game Dev. DOM Manipulation. It’s costly, so minimise its use.

Typically when manipulating display objects in Flash, applying positional data is a trivial task so you wouldn’t think twice about applying the data on every game loop. It doesn’t matter that the object hasn’t moved on that frame. The overhead would be minimal, you can just reapply without penalty – in fact to engage with any conditional to test for the need to apply could be more costly in the long run.

With DOM manipulation, any update seems to be fairly processor expensive. Very non-trivial. Here it is essential that updates should be applied ONLY when absolutely necessary.

I ran a test script at JSPerf.com which demonstrated the issue. I set up a Div with some simple text content. Then manipulate it on every iteration. I move the x-position by 0.2, then round off the position before applying. To optimise I only apply the dom manipulation when the actual object has changed position. The results are stark. A massive performance boost.

Optimise your DOM manipulations. It pays.

The code used is simple, and as follows. Create a div, populate then manipulate:

var targetDIV = document.createElement(“div”);
targetDIV.innerHTML = “test data”;
document.doby.appendChild(targetDIV);
var posX =0, prevPosX=0;

function unoptimisedManipulation()
{
posX+=0.2;
var roundX = Math.round(posX);

if(posX>500) {posX=0;}
targetDIV.style.left=roundX+”px”;

prevPosX = roundX;
}

function optimisedManipulation()
{
posX+=0.2;
var roundX = Math.round(posX);
if(prevPosX!=roundX)
{
if(posX>500) {posX=0;}
targetDIV.style.left=roundX+”px”;

prevPosX = roundX;
}
}

The results aren’t indicating that DOM manipulation should be avoided – Android devices really struggle with Canvas so DOM is preferable – it just takes a bit of effort to make the DOM production ready.

Try running the tests yourself at JSPerf.com : http://jsperf.com/optimising-dom-access

07
Feb
12

HTML5 Flipping a DIV – Horizontally Inverting content with Javascript and CSS

As I prepare for my talk at FITC Amsterdam (Feb 2012) I’m building a web game in both Flash & HTML5.

I’m aiming my HTML5 build at the mobile web (but will try to reach as many desktop browsers as possible) – so it will need to run optimally across Android & iOS.

I’ve discovered a few issues so far that for the purpose of record and reference I’ll post here in game lab.

First up. I have a player character that I’m animating via a spritesheet. I need the character to be able to face left and right.

I made the assumption that I could drop the sheet into a div (as background image) and then apply a scaleX transition to the div when I need to face left, and remove the transition when I need to turn right.

I’m applying the horizontal flip by adding a CSS classname to the div.

The CSS:

.flip-horizontal
{
-moz-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
-o-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH; /*for IE*/
-ms-filter: “FlipH”;
}

Then when I need my character to face left I add the class name to the character div:

function faceLeft() {characterDIV.className = “flip-horizontal”;}
function faceRight() {characterDIV.className = “”;}

So, is this good? Works fantastically on the desktop browsers and really well on Android.

BUT, on iOS (iPad in my tests) there is a BIG lag when the webkit transform is applied.

I mean big – in my game there can be up to a second long hang. Totally unacceptable for any game usage (unless as a one shot on initialisation).

The answer? Unfortunately I couldn’t come up with a technical solution. I had to resort to putting the reverse animation on the spritesheet. This gives me the performance I want – but at the expense of more weight to the game.

If my game has many characters then I am essentially doubling my asset weight for those characters. Boo.

02
Nov
11

Creative Javascript – 3D Tree with Leaf-fall

A HTML5 prototype…

Finally getting around to have a play with HTML5 Canvas – enabled by attending Seb Lee Delisle’s excellent CreativeJS course.

I’m impressed with performance from Canvas nowadays – my first sight of it many moons ago left me pretty disappointed – but now it actually feels useful and some pretty impressive visual effects can be achieved without resorting to WebGL.

The prototype I threw together here demonstrates a growing tree, in 3D with numerous branches, leaves, leaf fall, faux-shading and colour phasing. The demo will re-grow a tree on mouse click, and will allow for left and right rotation by means of mouse position (left/right).

creativeJS

3D Tree with Leaf Fall

To talk through the code a little: The tree is constructed using a recursive buildBranch function. Every branch spawns two new branches, or in the last generation it has a 90% chance of spawning a leaf. The branches are coloured from brown to green related by the recorded generation of the branch.

No 3D libraries were used in this demo – all of the 3D is via a simple scaling calculation for 3D points [ fov / (fov + point.z) ]. This allows for any 3D point to be translated (scaled) onto the 2D canvas.

The Painter’s Algorithm (priority fill) is applied via the sorting of all branches and leaves on their mid-point. This means that those furthest away are drawn to screen first.

Shading is applied by mapping colour to the z value of a branch’s position. This is very simply applied. There is a related alpha to add a fog effect to the furthest away elements, this gives some subtle but nasty artefacts and could be improved.

When a branch is fully grown the leaves will then appear and grow to their full size. When they fall a sine value is applied to rotation to make it rock as it falls. Leaves will collect on the ground.

Performance is pretty good here, but I did have to restrict the number of branch generations to 9. Of course some of the out-of-the-box features that Flash has such as Glow and Blur filters don’t exist – which means work-arounds or approximations have to be used – sound implementation isn’t very consistent across browsers – it is still early days, more and more JS will be used across the web, as it is the browser manufacturers will hopefully drive their products into more useful areas and consistent implementations.

The demo is a fast turn-around ‘prototype’, so not optimal or well formed. Feel free to have a play with the code, offer improvement suggestions or adapt in anyway you wish.

No attempt has been made to cover HTML5 cross-browser issues – so don’t expect this to work in IE!

Direct Link: 3D Tree with Leaf Fall

12
Sep
11

References from Playability: Making Games for Kids

Here are the references from my talk at Flash on the Beach 2011:


Games:

  • Horrible Histories: Moo Spew
  • Horrible Histories: Gong Farmer
  • Shaun the Sheep: Alien Athletics
  • Deadly 60: Deadly Dash
  • Dick and Dom: Let Rip
  • Swingpant's FOTB Flinger



    Your keyboard doesn’t work:

    The functioning key test app. Many keyboards have a mechanical issue that means if two keys are pressed, certain other ones cannot be registered as being used. On the keyboard I am currently typing on if I press [Down], [LEFT] and then [Space], the [Space] fails to work. If I press [A] and [W], then [Q] fails to work.

    Test your keyboard here: http://bit.ly/keytest
    (Sorry users of keyboards that aren’t exactly like the one I have here – I made the app from a pic of this one)


    Accessibility

  • A simple introduction to web accessibility – Ian Hamilton

  • Recommended Reading on Game Design:

    Game Design Books:

  • The Art of Game Design – Jesse Schell
  • A Theory of Fun for Game Design – Raph Koster
  • Challenges for Game Designers – Brenda Brathwaite & Ian Schreiber

  • Academic Papers:

  • MDA: A Formal Approach to Game Design and Game Research (PDF) – by Robin Hunicke, Marc LeBlanc, Robert Zubek
  • Playability: How to Identify the Player Experience in a Video Game – by José Luis González Sánchez, Natalia Padilla Zea, Francisco L. Gutiérrez.
  • From usability to Playability: Introduction to player centred video game development – by José Luis González Sánchez, Natalia Padilla Zea, Francisco L. Gutiérrez.
  • Dissecting Play: Investigating the Cognitive and emotional motivations and affects of computer gameplay – Lindley, Nacke, Sennerston
  • Flash on the Beach Titles by GMunk

    10
    Sep
    11

    Flash on the Beach 2011: My Schedule

    The amazing Flash on the Beach conference is upon us again. A must every year for me to get my fix of the latest tech, design and web game ideas.

    This year I’m speaking on the monday morning – great for me as I don’t have to sweat it for the following few days.

    Some major clashes this year – I may have to miss Seb Lee Delisle, Stray, Joel Gethin Lewis, Keith Peters, Conrad Winchester and Stefan Richter. Sorry guys – my arm is up for being twisted though.

    Mon 12th Sept 2011
    Keynote.
    Jon Howard – Playability: Making games for kids
    Rob Bateman – Flash 11: Get Ready for Gametime
    Eugene Zatepyakin – Natural Features Tracking and Image Pattern Detection / Recognition
    Han Hoogerbrugge – Prostress of the graphic universe of Han Hoogerbrugge
    Jon Burgerman – A short talk about working and not working and how to waste your time proficiently

    Tue 13th
    Tomek Augustyn – Riding the Hislope
    Joa Ebert – The Tale of a travelling salesman and his four colours
    Mike Chambers – Deconstructing theexpressiveweb.com
    David Lenaerts – Keeping It Real
    Remy Sharp – HTML5: Where flash isn’t needed anymore
    Cyriak Harris – Destroying my laptop with After Effects
    Jame Victore – Who died and made me boss

    Wed 14th
    The Elevator Pitch.
    Thomas Vian – All aboard the game engine
    Lee Brimelow – Render for Speed
    Frank Reitberger – Real(hard)time
    Joshua Davis – The Unknown Voyage

    08
    Sep
    11

    Playability: Making games for kids

    I shall be speaking at this year’s Flash on the Beach conference in Brighton, UK (11th to 14th September 2011).

    The subject is playability in relation to making web games for kids. As a lead developer, tech lead and team leader I have had huge amounts of experience working on game design and development with major kid’s TV brands. It is exciting, fun and always a privilege.

    In the session I will be looking at how a gamedev team is made up, the product lifecycle from idea to release. Important points I’ll look at are the specific needs of the game’s audience. How to design the best control method, the most appropriate aesthetics and which mechanics work best.

    I’ll dig into the elements of a successful game – which measures are used to find out if a game is ‘good’ and successful.

    With constantly shifting sands in the world of the web,I’ll touch on what I see as the future of games for a major broadcaster – what technology will be used and on what platforms.

    During the session I’ll be showing examples of games, video examples and I’ve arranged for a Skype call to the TV offices…

    Playability at FOTB2011

    05
    Mar
    11

    Introduction to Mercator Projections, Latitude and Longitude

    Or how to place a map coordinate into 3D space. This post is a brief walk through of the basics from my 2010 presentation “Where in the world? Intercontinental Ballistic Flash”.

    Belgian, Flemish geographer and cartographer Gerardus Mercator, in 1569 presented a cylindrical map projection. The Mercator Projection quickly became the standard map used for nautical purposes. It’s advantages were that the scale is linear in any direction around a point, meaning all angles were preserved (conformal).

    Geographer and cartographer Gerardus Mercator

    Gerardus Mercator

    Around the equator feature shapes are accurately portrayed but as the poles are approached there is a size and shape distortion. The scale increases as the pole is neared, eventually becoming infinite when the pole is reached.

    If you imagine the world as a beach ball. The Mercator Projection is the equivalent of cutting open the ball at each pole and stretching the skin to form a cylinder. Now slice down the cylinder and you have the map.

    The first Mercator projected map. 1569.

    All map projections distort the shape or size of the Earth’s (or for that matter any other planet’s) surface. The Mercator Projection exaggerates areas far from the equator. Greenland seems as large as Africa, but is in fact 1/14 of the size, Antartica appears to be the largest continent but is in fact only 5th in terms of area.

    Despite the obvious distortions, the Mercator Projection makes a great interactive map. Thanks to the conformality it can be zoomed nearly seamlessly.

    So here we have a system that converts a 3D scene, the real world, onto a 2D plane, the map. This system can just as easily convert the 2D back to 3D. It is a coordinate system that requires two positional parameters and one constant: the planet radius.

    The positional parameters are latitude and longitude. As developers we are used to standard coordinates x and y. Across then up. Horizontal then vertical. With latitude and longitude we have to think differently. Latitude is vertical and longitude is across. Up then across. Latitudes run from pole to pole, while longitudes run around the planet.

    There are 180 degrees of latitude from the South Pole to the North pole, and 360 degrees around the planet. Imagine yourself sat at the centre of the Earth with a laser pointer. Point at the Equator then you are at 0 degrees latitude, at the South Pole -90 degrees and at the North Pole 90 degrees. Now point at the Equator again you can turn 180 degrees around the equator to the right (-180 degrees longitude) and to the left (180 degrees longitude).

    With these any point on the surface of the planet can be recorded or indeed any position on a map.

    So we have a system for calculating a position on the surface of a sphere, a 3D coordinate, but how do we convert latitude, longitude and the planet’s radius into a 3D x,y,z coordinate?

    public static function convertLatLonTo3DPos(lat:Number, lon:Number, radius:Number):Vector3D
    {
    var v3d:Vector3D = new Vector3D();

    //Convert Lat Lon Degrees to Radians
    var latRadians:Number = lat * TO_RADIANS;
    var lonRadians:Number = lon * TO_RADIANS;

    v3d.x = radius * Math.cos(lonRadians) * Math.sin(latRadians);
    v3d.z = radius * Math.sin(lonRadians) * Math.sin(latRadians);
    v3d.y = -radius * Math.cos(latRadians);

    return v3d
    }

    Here the latitude and longitude are converted into radians ( TO_RADIANS:Number = Math.PI / 180 ). Then the trigonomic functions are applied to the planet radius to return a vector3D x,y and z.

    16
    Nov
    10

    Adobe User Group XL

    On Nov 17th, I’ll be speaking at the Adobe User Group XL event in Amsterdam. This conference is festooned with glittering array of industry big wigs, not just Flash, but all across the creative spectrum. Belinda van Valkenburg from Pixar (her credits include Monsters Inc., Toy Story 3, Finding Nemo, The Incredibles <- wow!), Roger Stighäll and Daniel Ilic from the amazing North Kingdom, Seb Lee-Delisle from Plug-in Media and Frank Reitberger from Prinzipiell. As well as Northern Europe’s finest and Adobe evangelists Serge Jespers, Mike Jones, et al.

    AUGXL2010

    Adobe User Group XL 2010 Amsterdam


    The event has a theme of ‘Celebrate Creativity’ and feature talks on interactive design and development, cross-media, imaging, gaming, augmented reality and PCB design.

    I shall be doing my Where in the World? Inter-Continental Ballistic Flash talk with a few additions.

    Really looking forward to this conference.




    Categories

    FITC Amsterdam 2012

    Flash on the Beach 2011

    Flash on the Beach 2010

    Flash on the Beach 2009

    Swingpants at FOTB2009

    Twitter Updates


    Follow

    Get every new post delivered to your Inbox.