PhysicsJS Boilerplate

PhysicsJS by Jasper Palfree <wellcaffeinated.net> http://wellcaffeinated.net/PhysicsJS

HTML

<script src="http://anzol.biz/physicsjs-5310/physicsjs-full.js"></script>
<canvas id="viewport" width="500" height="500"></canvas>

JavaScript

/**
 * PhysicsJS by Jasper Palfree <wellcaffeinated.net>
 * http://wellcaffeinated.net/PhysicsJS
 *
 * Boilerplate
 */

/**
 * PhysicsJS v0.6.0 - 2014-04-22
 * A modular, extendable, and easy-to-use physics engine for javascript
 * http://wellcaffeinated.net/PhysicsJS
 *
 * Copyright (c) 2014 Jasper Palfree <[email protected]>
 * Licensed MIT
 */

Physics(function(world){

    var renderer = Physics.renderer('canvas', {
        el: 'viewport', // id of the canvas element
        width: 500,
        height: 500
    });
    world.add( renderer );
    
    world.add(Physics.integrator('verlet', {
        drag: 0.5
    }));
    
    var square = Physics.body('convex-polygon', {
        x: 250,
        y: 250,
        vx: 0.01,
        vertices: [
            {x: 0, y: 50},
            {x: 50, y: 50},
            {x: 50, y: 0},
            {x: 0, y: 0}
        ]
    });
    
    var bounds = Physics.aabb(0, 0, 500, 500);
    
    world.add(square);
    world.add( Physics.body('convex-polygon', {
        x: 250,
        y: 50,
        vx: 0.05,
        vertices: [
            {x: 0, y: 80},
            {x: 60, y: 40},
            {x: 60, y: -40},
            {x: 0, y: -80}
        ]
    }) );
    
    world.add( Physics.body('convex-polygon', {
        x: 400,
        y: 200,
        vx: -0.02,
        vertices: [
            {x: 0, y: 80},
            {x: 80, y: 0},
            {x: 0, y: -80},
            {x: -30, y: -30},
            {x: -30, y: 30}
        ]
    }) );
    world.add(Physics.behavior('constant-acceleration'));
    world.add(Physics.behavior('edge-collision-detection', {
        aabb: bounds,
        restitution: 0.3
    }));
    world.add(Physics.behavior('body-impulse-response'));
    world.add(Physics.behavior('body-collision-detection'));
    world.add(Physics.behavior('sweep-prune'));
    
    world.on('step', function(){
        world.render();
    });
    
    // Subscribe To Ticker (Advance Simulation)
   ...