Jump to content

p2 physics config issue


Recommended Posts

Before creating an issue on github, decided to put it here (as suggested here https://github.com/photonstorm/phaser/blob/master/CONTRIBUTING.md)
Class: Phaser.Physics.P2
Method: constructor

Issue: physics config's user-defined options (mpx, mpxi, pxm, pxmi) get deleted in Phaser.Physics.P2 constructor.



Why: game class instance passes the physics config to Phaser.Physics instance, then the config gets passed to Phaser.Physics.P2.
Within this chain no other props are created, as a result 'broadphase' prop is never present in the physics config and always created by the p2 class.
So, when a new p2 instace is created, the config with your custom mpx, mpxi, pxm, pxmi props gets replaced with a new config of { gravity, broadphase } only.
As a result p2 uses default mpx, mpxi, pxm, pxmi methods (so they don't get overriden)
How-to-fix:  switch from config replacement to property adding.
Current code:

Phaser.Physics.P2 = function (game, config) {/** * @property {Phaser.Game} game - Local reference to game. */this.game = game;// @note// what if i already defined 'gravity' and 'broadphase' is not defined (btw it's never defined until this point)// my gravity prop will be also overridenif (typeof config === 'undefined' || !config.hasOwnProperty('gravity') || !config.hasOwnProperty('broadphase')){    config = { gravity: [0, 0], broadphase: new p2.SAPBroadphase() };}

The fix:

Phaser.Physics.P2 = function (game, config) {/** * @property {Phaser.Game} game - Local reference to game. */this.game = game;// config exists? if not - create oneif (typeof config === 'undefined'){    config = {};}// we got gravity? if not - define [0,0]if (!config.hasOwnProperty('gravity')){    config.gravity = [0, 0];}// we got broadphase? if not - create a new instanceif (!config.hasOwnProperty('broadphase')){    config.broadphase = new p2.SAPBroadphase();}// ... now mpx, mpxi, pxm, pxmi props will override the default methods properly//  Pixel to meter function overridesif (config.hasOwnProperty('mpx') && config.hasOwnProperty('pxm') && config.hasOwnProperty('mpxi') && config.hasOwnProperty('pxmi')){    this.mpx = config.mpx;    this.mpxi = config.mpxi;    this.pxm = config.pxm;    this.pxmi = config.pxmi;}
Link to comment
Share on other sites


  • Recently Browsing   0 members

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