Jump to content

addPolygon broken - example inside


Recommended Posts


Here's an example of the issue




In my game I have several defined "areas", that are basically dynamically generated polygons.


This is an example of a set of polygon coordinates:


I draw these using a Graphics object and that works great:




Now the weirdness begins.

I try to add a body with the same polygon sensor shape using addPolygon and the same coordinates, like so:

    Phaser.Sprite.call(this, this.game, 0, 0);    this.game.physics.p2.enable(this,true,false);    this.body.clearShapes();    this.body.addPolygon({},this.pointArray);    this.body.data.shapes[0].sensor=true;

Below is what I get. Body shape polygon is OK, but notice that the graphics coordinates (green polygon) were shrunk down by a factor of 20, even though the only thing that was changed was the addition of the body.

This affects every child object of my area sprite, so if a child object has some X value, that value will get translated to X/20!




This does not happen if I use addRectangle:

    Phaser.Sprite.call(this, this.game, 0, 0);    this.game.physics.p2.enable(this,true,false);    this.body.clearShapes();    this.body.addRectangle(500,500,150,150);    this.body.data.shapes[0].sensor=true;

Notice how now both the rectangle body shape as well as the original coordinates behave accordingly.




Now for more weirdness - If I put the addPolygon part AFTER the creation of the Graphics object I get the image below - both the polygons have the proper dimensions, but now every child sprite is offset by some amount.




I'm really at a loss at how to debug this. I'm sure this has something to do with the phaser\p2 MPX conversions, but hours later into debugging and I still can't wrap my head around this.

It's a crucial aspect of my game.



Link to comment
Share on other sites

I just wrote code to add polygons with a point array and they seem to work fine here... Here's my code:

myPolygon = this.add.sprite(x, y);this.physics.p2.enable(myPolygon, false);myPolygon.body.clearShapes();myPolygon.body.addPolygon({}, myPolygonArray);myPolygon.body.static = true;

The polygon starts and ends at (0, 0) – but so does yours. Hm.

Link to comment
Share on other sites

Ahh.. Maybe you need to draw the shape before you add the polygon – in case the array itself is changed during the add


edit: wait that's what you did earlier.. but I seem to remember that adding a p2 body automatically sets the anchor. Maybe if you re-set that again?


edit 2: hmm no that doesn't seem to be the solution either. Could it be that the points in the array are somehow rearranged when adding the polygon? I know they are converted to counterclockwise in the process...

Link to comment
Share on other sites

I never use that method. I have not looked at this in detail but it looks like a bug. Would you mind to create a ticket in github for this ?

To get this fixed, you could try an alternative method. addPhaserPolygon. It's still undocumented. Search for it. I added that method together with the Phaser PhysicsEditor Exporter to manage all my shapes including sensors in a single PhysicsEditor File. The fileformat differs only slightly so you can the files yourself






Link to comment
Share on other sites


  • Recently Browsing   0 members

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