HTML5 Game Development – Lesson 8

HTML5 Game Development – Lesson 8

2 41590
HTML5 Game Development - Lesson 8

HTML5 Game Development – Lesson 8

This is our next game development article. We continue a series of articles on game development in HTML5 using canvas. Today I will show how to apply some physics to your project (with using Box2D). Box2D is popular open source engine that simulates 2D physics for applications. 2D Physics Engine is a hot topic in game development. With the help of physics engine, we can easily create a playable game by just defining an environment and a simple rule.

Here you can read our previous lesson: Developing Your First HTML5 Game – Lesson 7.

Here are our demo and downloadable package:

Live Demo
download in package

Ok, download the example files and lets start coding !

Step 0. Pre-phase

First, you have to download Box2D library here.

Step 1. HTML

Today we have to link all necessary library files to our project.


<!DOCTYPE html>
<html lang="en" >
        <meta charset="utf-8" />
        <title>HTML5 Game Development - Lesson 8 | Script Tutorials</title>
        <link href="css/main.css" rel="stylesheet" type="text/css" />
        <script src="js/protoclass.js"></script>
        <script src="js/jquery-1.6.min.js"></script>
        <!-- box2djs -->
        <script src='js/box2d/common/b2Settings.js'></script>
        <script src='js/box2d/common/math/b2Vec2.js'></script>
        <script src='js/box2d/common/math/b2Mat22.js'></script>
        <script src='js/box2d/common/math/b2Math.js'></script>
        <script src='js/box2d/collision/b2AABB.js'></script>
        <script src='js/box2d/collision/b2Bound.js'></script>
        <script src='js/box2d/collision/b2BoundValues.js'></script>
        <script src='js/box2d/collision/b2Pair.js'></script>
        <script src='js/box2d/collision/b2PairCallback.js'></script>
        <script src='js/box2d/collision/b2BufferedPair.js'></script>
        <script src='js/box2d/collision/b2PairManager.js'></script>
        <script src='js/box2d/collision/b2BroadPhase.js'></script>
        <script src='js/box2d/collision/b2Collision.js'></script>
        <script src='js/box2d/collision/Features.js'></script>
        <script src='js/box2d/collision/b2ContactID.js'></script>
        <script src='js/box2d/collision/b2ContactPoint.js'></script>
        <script src='js/box2d/collision/b2Distance.js'></script>
        <script src='js/box2d/collision/b2Manifold.js'></script>
        <script src='js/box2d/collision/b2OBB.js'></script>
        <script src='js/box2d/collision/b2Proxy.js'></script>
        <script src='js/box2d/collision/ClipVertex.js'></script>
        <script src='js/box2d/collision/shapes/b2Shape.js'></script>
        <script src='js/box2d/collision/shapes/b2ShapeDef.js'></script>
        <script src='js/box2d/collision/shapes/b2BoxDef.js'></script>
        <script src='js/box2d/collision/shapes/b2CircleDef.js'></script>
        <script src='js/box2d/collision/shapes/b2CircleShape.js'></script>
        <script src='js/box2d/collision/shapes/b2MassData.js'></script>
        <script src='js/box2d/collision/shapes/b2PolyDef.js'></script>
        <script src='js/box2d/collision/shapes/b2PolyShape.js'></script>
        <script src='js/box2d/dynamics/b2Body.js'></script>
        <script src='js/box2d/dynamics/b2BodyDef.js'></script>
        <script src='js/box2d/dynamics/b2CollisionFilter.js'></script>
        <script src='js/box2d/dynamics/b2Island.js'></script>
        <script src='js/box2d/dynamics/b2TimeStep.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactNode.js'></script>
        <script src='js/box2d/dynamics/contacts/b2Contact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactConstraint.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactConstraintPoint.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactRegister.js'></script>
        <script src='js/box2d/dynamics/contacts/b2ContactSolver.js'></script>
        <script src='js/box2d/dynamics/contacts/b2CircleContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2Conservative.js'></script>
        <script src='js/box2d/dynamics/contacts/b2NullContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2PolyAndCircleContact.js'></script>
        <script src='js/box2d/dynamics/contacts/b2PolyContact.js'></script>
        <script src='js/box2d/dynamics/b2ContactManager.js'></script>
        <script src='js/box2d/dynamics/b2World.js'></script>
        <script src='js/box2d/dynamics/b2WorldListener.js'></script>
        <script src='js/box2d/dynamics/joints/b2JointNode.js'></script>
        <script src='js/box2d/dynamics/joints/b2Joint.js'></script>
        <script src='js/box2d/dynamics/joints/b2JointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2DistanceJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2DistanceJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2Jacobian.js'></script>
        <script src='js/box2d/dynamics/joints/b2GearJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2GearJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2MouseJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2MouseJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2PrismaticJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2PrismaticJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2PulleyJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2PulleyJointDef.js'></script>
        <script src='js/box2d/dynamics/joints/b2RevoluteJoint.js'></script>
        <script src='js/box2d/dynamics/joints/b2RevoluteJointDef.js'></script>
        <script src="js/script.js"></script>
            <h2>HTML5 Game Development - Lesson 8</h2>
            <a href="" class="stuts">Back to original tutorial on <span>Script Tutorials</span></a>
        <div class="container">
            <canvas id="game" width="800" height="600"></canvas>

Step 2. CSS


I won’t publish CSS styles today, there are only a few page layout styles. You can find this file in our package.

Step 3. JS

js/jquery-1.6.min.js and js/protoclass.js

Both libraries available in our package. Next file is most important – it is our main code of our game:


var canvas, ctx;
var canvasWidth;
var canvasHeight;
var world;
var iBorder = 5;
// get rand number between X and Y
function getRand(x, y) {
    return Math.floor(Math.random()*y)+x;
$(function() {
    // create the world
    world = createWorld();
    // get canvas and context
    canvas = document.getElementById('game');
    ctx = canvas.getContext('2d');
    canvasWidth = parseInt(canvas.width);
    canvasHeight = parseInt(canvas.height);
    // create the ground
    createGround(canvasWidth / 2, canvasHeight - iBorder, canvasWidth / 2, iBorder, 0);
    createGround(iBorder, canvasHeight / 2, iBorder, canvasHeight / 2, 0); // left border
    createGround(canvasWidth - iBorder, canvasHeight / 2, iBorder, canvasHeight / 2, 0); // right border
    // add first rand object
    // draw frame
// add random objects
function addObjects() {
    var iVar = getRand(1, 2);
    if (iVar == 1) {
        var x = getRand(100, 600);
        var y = 0;
        var r = getRand(10, 40);
        createCircleAt(x, y, r);
    } else if (iVar == 2) {
        var x = getRand(100, 600);
        var y = 0;
        var w = getRand(5, 40);
        var h = getRand(5, 40);
        createBoxAt(x, y, w, h);
    // set timer
    setTimeout(addObjects, 500);
// draw frame
function frame() {
    world.Step(1.0 / 60, 1);
    ctx.clearRect(0, 0, canvasWidth, canvasHeight);
    // draw world
    drawWorld(world, ctx);
    // set timer
    setTimeout(frame, 10);
// create world
function createWorld() {
    // set the size of the world
    var worldAABB = new b2AABB();
    worldAABB.minVertex.Set(-1000, -1000);
    worldAABB.maxVertex.Set(1000, 1000);
    // Define the gravity
    var gravity = new b2Vec2(0, 200);
    // set to ignore sleeping object
    var doSleep = false;
    // finally create the world with the size, graivty and sleep object parameter.
    return new b2World(worldAABB, gravity, doSleep);
// create ground (box-type object)
function createGround(x, y, width, height, rotation) {
    // box shape definition
    var groundSd = new b2BoxDef();
    groundSd.extents.Set(width, height);
    groundSd.restitution = 0.4;
    var groundBd = new b2BodyDef();
    groundBd.position.Set(x, y);
    groundBd.rotation = rotation * Math.PI / 180;
    return world.CreateBody(groundBd);
// create box
function createBoxAt(x, y, w, h) {
    var boxSd = new b2BoxDef();
    boxSd.density = 1.0;
    boxSd.friction = 1.0;
    boxSd.restitution = .5;
    boxSd.extents.Set(w, h);
    // add to world as shape
    var boxBd = new b2BodyDef();
    return world.CreateBody(boxBd);
// create circle
function createCircleAt(x, y, r) {
    var boxSd = new b2CircleDef();
    boxSd.density = 1.0;
    boxSd.friction = 1.0;
    boxSd.restitution = .5;
    boxSd.radius = r;
    // add to world as shape
    var boxBd = new b2BodyDef();
    return world.CreateBody(boxBd);
// draw world
function drawWorld(world, context) {
    for (var b = world.m_bodyList; b != null; b = b.m_next) {
        for (var s = b.GetShapeList(); s != null; s = s.GetNext()) {
            drawShape(s, context);
// draw shapes
function drawShape(shape, context) {
    context.strokeStyle = '#0000ff';
    context.fillStyle = 'rgba(100, 100, 255, 0.8)';
    switch (shape.m_type) {
        case b2Shape.e_circleShape:
            var circle = shape;
            var pos = circle.m_position;
            var r = circle.m_radius;
            var segments = 16.0;
            var theta = 0.0;
            var dtheta = 2.0 * Math.PI / segments;
            context.moveTo(pos.x + r, pos.y);
            for (var i = 0; i < segments; i++) {
                var d = new b2Vec2(r * Math.cos(theta), r * Math.sin(theta));
                var v = b2Math.AddVV(pos, d);
                context.lineTo(v.x, v.y);
                theta += dtheta;
            context.lineTo(pos.x + r, pos.y);
            context.moveTo(pos.x, pos.y);
            var ax = circle.m_R.col1;
            var pos2 = new b2Vec2(pos.x + r * ax.x, pos.y + r * ax.y);
            context.lineTo(pos2.x, pos2.y);
        case b2Shape.e_polyShape:
            var poly = shape;
            var tV = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[0]));
            context.moveTo(tV.x, tV.y);
            for (var i = 0; i < poly.m_vertexCount; i++) {
                var v = b2Math.AddVV(poly.m_position, b2Math.b2MulMV(poly.m_R, poly.m_vertices[i]));
                context.lineTo(v.x, v.y);
            context.lineTo(tV.x, tV.y);

I have added my comments everywhere, hope that all this code is pretty understandable.

Live Demo
download in package


And that’s it! You have just completed our new lesson with HTML5 and Box2D. Congratulations!



  1. I have been learning the 7 courses, just found eighth courses, box2d I in the production of flash game used, similar to the game as if angry birds, hole games, these are useful to the engine, but on the HTML5 box2d I have not, want to continue to study in your future.

Leave a Reply