Jump to content

P2 collision mesh misaligned


Recommended Posts

I got a problem that the P2 collision mesh is not properly aligned with the bitmap.


Here's the sprite's bitmap:




I measured the pixel coordinates on the bitmap and got this mesh:

    "ship": [
            "density": 2, "friction": 0, "bounce": 0,
            "shape": [
                16, 31,
                0, 29,
                16, 0,
                31, 29

And the CoffeeScript code:

        @ship = game.add.sprite(200, 200, 'ship')
        game.physics.p2.enable(@ship, true)
        @ship.body.loadPolygon('physicsData', 'ship')

But as you can see in the image, the physics debug polygon (purple) is not properly aligned with the bitmap.




My theory is that the bitmap is automatically centered on the physics polygon, because no matter how I tweak the values in the physics mesh, it won't align with the bitmap. For example, if I add 50 to all x coordinates in the polygon, the sprite will appear 50 pixels to the right, but the polygon will still be drawn on top of the sprite, not beside it. Is there a way to disable this?


Link to comment
Share on other sites

I can confirm this. Something is going wrong with the center of mass.

By using an alternative polygon data format it's working.


Try this:

//data format from custom physics editor exporter 'phaser'.//See phaser/resources/PhysicsEditor Exporter    "ship":[      {        "isSensor": false,        "filter": {          "group": 0,          "categoryBits": 1,          "maskBits": 65535        },        "polygons":[           [16, 31,0, 29,16, 0,31, 29]        ]      }    ],//ship.body.loadPolygon('physicsData', 'ship');//use this method for the alternative data format. The format is a little bit more sophisticated.ship.body.addPhaserPolygon('physicsData', 'ship');


The right one is created using the alternative method. It has the same y position as the ship on the left, so it's clearly something with the mass. Both ships are centered (as every p2 body). I will have a look at the default parser.


Link to comment
Share on other sites


  • Recently Browsing   0 members

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