This class extends Game2d.Scene class and includes native b2World class, in property Physics.world. It manages Scene and basic world properties and functions.
There is a factory method Physics.create and it has four parameters: gravity on x-axis, gravity on y-axis, enable sleeping and collisions (last two are optional). Gravity can be changed later by setGravity() and getGravity().
In this class, body creating is really simple because developers can create basic bodies via functions:
- addPolygonBody()
- addCircleBody()
Joints creating is simple as well, 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.
- draw()
- doDebugDraw()
- step()
include "lib://core/log.ms";
include "lib://game2d/game.ms";
include "lib://box2d/physicsScene.ms"; var app = new Game();
app.onStart = function(sender)
{
// create physics world
sender._scene = PhysicsScene.create(0, -9.8); // set reaction for contact
sender._scene.onBeginContact = function(sender, contact)
{
logI("onBeginContact");
}; // set reaction for contact
sender._scene.onEndContact = function(sender, contact)
{
logI("onEndContact");
}; // loads bitmap image for circle body
var img = Bitmap.fromFile("app://ball.png");
// create body
this._ball = sender._scene.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 = sender._scene.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)
{
sender._scene.step(1.0 / 40.0);
} app.onDraw = function(sender, canvas)
{
// fill screen with black color
canvas.clear(0xff000000);
// draw physical world
sender._scene.draw(canvas);
} // init and run game
app.run();