This class is built from native b2World class and it manages basic world properties and functions. Constructor has four parameters: gravity on x-axis, gravity on y-axis, enable sleeping and collisions (last two are optional). Gravity can be later changed by setGravity() and getGravity().
In this class, body creating is really simple because developers can create basic bodies via functions:
- addPolygonBody()
- addCircleBody()
Also joints creating is simple through functions:
- createDistanceJoint()
- createFrictionJoint()
- createRevoluteJoint()
- createMouseJoint()
- createMouseJoint()
- createPrismaticJoint()
- createLineJoint()
- createPulleyJoint()
- createWeldJoint()
Other functions that draw physics word (listed below) are directly mapped to native b2World class functions.
- doDraw()
- doDebugDraw()
- step()
include "code://ui/application.ms"
include "code://skin/skin.metro.ms"
// include box2d libraries
include "code://box2d/physicsWorld.ms" var app = new Application("b2 sample", new MetroSkin()); app.onStart = function(sender)
{
// create physics world
this._world = new PhysicsWorld(0, -9.8);
// set reaction for contact
this._world.onBeginContact = function(sender, contact)
{
console<<”collision\n”;
};
// loads bitmap image for circle body
var img = Bitmap.fromFile("app://ball.png");
// create body
this._ball = app._world.addCircleBody(img, #dynamic, 0.0, 0.0, 0.3, img.width / 2);
// place body into the world
this._ball.setPosition(System.width/2, System.height/2); // create bottom wall
var (width, height) = (System.width, 1);
// create static body
var bottomWall = this._world.addPolygonBody(null, #static, 0.0, 1.0, 1.0, width, height);
// disable rotation
bottomWall.fixedRotation = true;
// place body to the world
bottomWall.setPosition(System.width/2, System.height - (bottomWall.height/2));
} app.onProcess = function(sender)
{
this._world.step(1.0 / 40.0);
app.invalidate();
} app.onDraw = function(sender, canvas)
{
// fill screen with black color
canvas.clear(0xff000000);
// draw physical world
this._world.doDraw(canvas);
} // init and run application
app.init().run();