Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Relationship between Box2D body coordinates and game world position


Topogram
 Share

Recommended Posts

Hi,

 

Completely new to Phaser but love it. I've purchased and am using the Box2D plugin and have a simple question about the coordinate system.

 

If I create a new game with dimensions width 800 and height 600 like so:

var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', {preload: preload, create: create, update: update, render: render});

 

With the following pixels to meter setting:

game.physics.box2d.ptmRatio = 100;

and create a new Box2D body with a 40 x 20 pixel rectangle fixture (centred on the body) like so:

 

var rect = new Phaser.Physics.Box2D.Body(this.game, null, 400, 0);
rect.static = true;
rect.addRectangle(40, 20, 20, 10);

See the image below: Why is the rectangle not positioned 400 pixels from the left? It looks like it's positioned 200 pixels from the left instead. Is there some scale factor I'm missing here? This is happening on my Retina MacBook Pro and on an external 1920 screen (not Retina).

59c5309ac7588_Box2Dposition.png.892e2773de967f2355d69c945e206575.png

Many thanks in advance,

Link to comment
Share on other sites

After a lot of digging I think the error comes from me changing game.physics.box2d.ptmRatio to 100. If I leave it alone then things are drawn by DebugDraw correctly. If I want to set the ratio to 100 then this fixes things:

game.physics.box2d.ptmRatio = 100;
game.physics.box2d.debugDraw = new Phaser.Physics.Box2D.DefaultDebugDraw(100);
game.physics.box2d.world.SetDebugDraw(game.physics.box2d.debugDraw);

Not sure how much value I'll get from changing PTM to 100 so I'll leave it at the default for the time being...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...