PhysicsGravy: apply physics to any page

PhysicsGravy is a JavaScript library to apply physics to any element on a page with the Box2D engine. I was playing with the JavaScript port of Box2D, and this is what came out. I know it’s nothing new, but you can use this to add any element to the physics simulation easily. It also supports static objects and circles.

It uses CSS transforms, so Firefox 3.5+, Chrome, or Safari 3.1+ is required. I originally tried using relative positioning, but that was quite painful and I wasn’t able to get it to work right. I’ll try again sometime later, so IE could possibly be supported.

You can see a demo (Firefox 3.5+, Chrome, or Safari 3.1+ required):
http://sk89q.therisenrealm.com/playground/physicsgravy/demo.html

See a video of it in action (different from the previous link):
http://www.youtube.com/watch?v=iyQEtC7NXnc

To use it:

if (!PhysicsGravy.isSupported()) {
    alert("Your browser is not supported!");
    return;
}

var pg = new PhysicsGravy();
pg.makeWalls();
pg.simulateAsBoxes($('.dynamic'), 1);
pg.simulateAsBoxes($('.static'), 0);
pg.simulateAsCircles($('.circle'), 1);
var sim = new PhysicsGravySimulator(pg);
sim.start();

Get its source code:
http://github.com/sk89q/physicsgravy/tree/master


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>