Jump to content

P2 Physics : body.addPolygon with custom offset ?


Recommended Posts



I'm new to phaser. I'm trying to make a very simple game where you must avoid collisions. I started with Arcade Physics and I managed to make everything I wanted easily.

But then I added complex characters and I needed more than a rectangle body to have accurate collisions.


So I switched to P2 Physics and found out how to set my character's body as a polygon :

player = game.add.sprite(0, 0, 'character')game.physics.p2.enable(player, true)player.body.clearShapes()player.body.addPolygon({}, [[0, -50], [-50, 0], [0, 50], [50, 0]])

Which results in




That's pretty cool, but I can't create a polygon that will perfectly fit my character since it is automatically "centered", and because of the character's animation, he isn't centered in the sprite.


So I looked for a way to set an offset to my player body's polygon. I saw that there were offset arguments on `addRectangle` and `addCircle`, but not on `addPolygon` (automatically computed).

I also saw the `addShape` method which takes a `p2.Shape` as first argument (btw I didn't find any documentation on this class). I tryed to use it with a Phaser.Polygon, but it failed.


Any idea ?

Link to comment
Share on other sites

  • 2 weeks later...

Good news, I managed to somehow solve this myself :)


Since the polygon added with addPolygon is centered on the sprite body's anchor, I just moved the anchor. That's it !

It's not very "clean" since I have to adjust the anchor visually in debug mode, but it works.


I think the cleanest way would be an option in the addPolygon method to set if you want your polygon to be centered relatively to the anchor, or to be absolute, or with an offset. I tryed to look at the addPolygon method but didn't understand much  :rolleyes:

Link to comment
Share on other sites


  • Recently Browsing   0 members

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