Topogram

Relationship between Box2D body coordinates and game world position

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,

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.