Skip to content

Instantly share code, notes, and snippets.

@headwinds
Created November 18, 2013 14:03
Show Gist options
  • Save headwinds/96d27083d4ca5c0f2ab1 to your computer and use it in GitHub Desktop.
Save headwinds/96d27083d4ca5c0f2ab1 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title>box2d-html5 :: A 2D Physics Engine for HTML5 Games</title>
<base href=../>
</head>
<body>
<canvas id="c" width="640" height="480" style="border: 1px solid black"></canvas>
<script src=requirements/promise.js></script>
<script src=requirements/require.js></script>
<script src=test/game.js></script>
<script src=test/loader.js></script>
<script>
if (window.console) {
window.alert = function (msg) {
console.log(msg)
document.title = msg
}
}
loader.load('contrib', 'lib', 'utils', function (err, res) {
if (err) {
alert('(ノ・ω・)ノ [[error]] loading paths.json')
return
}
require.config({ baseUrl: '../', paths: res })
require(['lib/box2d'], function (box2d) {
alert('(ノ・ω・)ノ [[box2d]] version ' + box2d.b2_version
+ ' revision ' + box2d.b2_changelist);
// once box2d is loaded, init the game - see game.js
init();
requestAnimFrame(update);
})
})
</script>
<div>
<a href="http://blog.sethladd.com/2011/09/box2d-javascript-example-walkthrough.html">the code for this game demo is taken from Seth Ladd's excellent box2d tutorials</a>
</div>
</body>
</html>
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(/* function */ callback, /* DOMElement */ element){
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.getElementById("c");
var ctx = canvas.getContext("2d");
var world;
//var Box2D = box2d;
function init() {
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2MassData = Box2D.Collision.Shapes.b2MassData
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
;
world = new b2World(
new b2Vec2(0, 10) //gravity
, true //allow sleep
);
var SCALE = 30;
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
//create ground
bodyDef.type = b2Body.b2_staticBody;
// positions the center of the object (not upper left!)
bodyDef.position.x = canvas.width / 2 / SCALE;
bodyDef.position.y = canvas.height / SCALE;
fixDef.shape = new b2PolygonShape;
// half width, half height. eg actual height here is 1 unit
fixDef.shape.SetAsBox((600 / SCALE) / 2, (10/SCALE) / 2);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//create some objects
bodyDef.type = b2Body.b2_dynamicBody;
for(var i = 0; i < 150; ++i) {
if(Math.random() > 0.5) {
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(
Math.random() + 0.1 //half width
, Math.random() + 0.1 //half height
);
} else {
fixDef.shape = new b2CircleShape(
Math.random() + 0.1 //radius
);
}
bodyDef.position.x = Math.random() * 25;
bodyDef.position.y = Math.random() * 10;
world.CreateBody(bodyDef).CreateFixture(fixDef);
}
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("c").getContext("2d"));
debugDraw.SetDrawScale(SCALE);
debugDraw.SetFillAlpha(0.3);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
setTimeout(init, 6000);
}; // init()
function update() {
world.Step(
1 / 60 //frame-rate
, 10 //velocity iterations
, 10 //position iterations
);
world.DrawDebugData();
world.ClearForces();
requestAnimFrame(update);
}; // update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment