Skip to content

Instantly share code, notes, and snippets.

@shadaj
Created May 7, 2014 00:05
Show Gist options
  • Save shadaj/c2a00943b3ee53c9594e to your computer and use it in GitHub Desktop.
Save shadaj/c2a00943b3ee53c9594e to your computer and use it in GitHub Desktop.
Physics({ timestep: 4 }, function (world) {
var viewWidth = window.innerWidth
,viewHeight = window.innerHeight
,edgeBounce
,renderer
;
// create a renderer
renderer = Physics.renderer('canvas', {
el: 'viewport'
,width: viewWidth
,height: viewHeight
});
// add the renderer
world.add(renderer);
// render on each step
world.on('step', function () {
world.render();
});
// resize events
window.addEventListener('resize', function () {
viewWidth = window.innerWidth;
viewHeight = window.innerHeight;
renderer.el.width = viewWidth;
renderer.el.height = viewHeight;
}, true);
function random( min, max ){
return (Math.random() * (max-min) + min)|0
}
function dropInBody(){
var body;
body = Physics.body('rectangle', {
width: 50
,height: 50
,x: (viewWidth / 2)
,y: 50
,vx: 0
,vy: 2
,restitution: 0.9
,styles: {
fillStyle: '#d33682'
,angleIndicator: '#751b4b'
}
});
world.add( body );
}
var bottom = Physics.body('convex-polygon', {
vertices: [{x: 0, y: 500},{x: 0, y: 498},{x: viewWidth, y:498},{x:viewWidth, y:500}],
x: viewWidth/2,
y: 499,
treatment: 'static'
})
world.add(bottom)
var int = setInterval(function(){
if ( world._bodies.length > 40 ){
clearInterval( int );
}
dropInBody();
}, 700);
// add things to the world
world.add([
Physics.behavior('body-impulse-response')
,Physics.behavior('body-collision-detection')
,Physics.behavior('sweep-prune')
,edgeBounce
]);
// subscribe to ticker to advance the simulation
Physics.util.ticker.on(function( time ) {
world.step( time );
});
// start the ticker
Physics.util.ticker.start();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment