box2d tests
by luckylooke
HTML
<script src="http://box2dweb.googlecode.com/svn/trunk/Box2d.min.js"></script>
<canvas id="c0" width="640" height="480" style="border: 1px solid black"></canvas>
CSS
body {
-webkit-transform: translateZ(0);
}
JavaScript
// forked from https://github.com/sethladd/box2d-javascript-fun/tree/master/static/08
// 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);
};
})();
// paulirish end
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;
function bTest(intervalRate, adaptive, width, height, scale) {
this.intervalRate = parseInt(intervalRate);
this.adaptive = adaptive;
this.width = width;
this.height = height;
this.scale = scale;
this.world = new b2World(
new b2Vec2(0, 9) //gravity
,
true //allow sleep
);
this.fixDef = new b2FixtureDef;
this.fixDef.density = 1.0;
this.fixDef.friction = 0.5;
this.fixDef.restitution = 0.2;
this.bodyDef = new b2BodyDef;
this.buildGround();
}
bTest.prototype.buildGround = function () {
//create ground
this.bodyDef.type = b2Body.b2_staticBody;
// positions the center of the object (not upper left!)
this.bodyDef.position.x = this.width / 2 / this.scale;
this.bodyDef.position.y = this.height / this.scale;
this.fixDef.shape = new b2PolygonShape;
// half width, half height. eg actual height here is 1 unit
this.fixDef.shape.SetAsBox((this.width - (this.width * 0.1) / this.scale) / 2, (10 / this.scale) /...