Shared by eszpee
Ez elég jól néz ki.
Robert Kieffer recently told us about Box2DJS, an automated port of the popular Box2D physics library to JavaScript using <canvas> for the rendering.
Using Box2DJS, you can create a flatland-esque 2D world that obeys Newtonian physics:
-
-
var worldAABB = new b2AABB();
-
worldAABB.minVertex.Set(-1000, -1000);
-
worldAABB.maxVertex.Set(1000, 1000);
-
var gravity = new b2Vec2(0, 300);
-
var doSleep = true;
-
var world = new b2World(worldAABB, gravity, doSleep);
-
and then you can add in your own objects and make them do interesting things. Here's a simple example of adding a circular into the world created above:
-
-
var circleSd = new b2CircleDef();
-
circleSd.density = 1.0;
-
circleSd.radius = 20;
-
circleSd.restitution = 1.0;
-
circleSd.friction = 0;
-
var circleBd = new b2BodyDef();
-
circleBd.AddShape(circleSd);
-
circleBd.position.Set(x,y);
-
var circleBody = world.CreateBody(circleBd);
-
There's a bunch of examples of Robert's site, and of course you can refer back to Box2D for more. Interesting use of canvas!
Utolsó kommentek