JSFiddle - React, Tailwind, and code Playground
by abernier
HTML
<script src="http://box2d-javascript-fun.appspot.com/Box2D.js"></script>
<canvas id="c0" width="640" height="480" style="border: 1px solid black"></canvas>
JavaScript
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 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, 10) //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;
//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) / 2);
this.world.CreateBody(this.bodyDef).CreateFixture(this.fixDef);
}
bTest.prototype.update = function() {
var start = Date.now();
...