Jump to content

weird shifting applying custom polygonal shape to p2 body via "body.addPolygon"


Recommended Posts

i'll go straight to the point since it's my very first post on the forum and it's almost eve.. 

I'm using phaser 2.6.2, and p2 engine.
I have a simple rectangular sprite obtained via a graphics object, using drawRect and then generating the texture for the sprite.. nothing fancy..
On this object i need a p2 body.. 
1) the classical way ( works as a charm)
enabling the p2 body on the sprite, generating a "body.data.shapes" of "Box" (2 triangles), the result is a pixel perfect shape overlapping the original one

BUT i was thinking.. <<since i will have a lot of these small simple rectangles on the scene, to spare some computation to the p2 engine i could cut the "Box" (2 triangles) default mode, on a single triangle one.. 

2) so i've used "body.clearShapes" method first, and then applied the newly desired triangle coordinates with the "body.addPolygon" method..
and there's my problem...
with the body debug enabled i can clearly see the new triangle, tracing the object in the console i can also notice that "body.data.shapes" now it's "Convex" and there's just a triangle in the array.. so it would be perfect for my needs.. but the new body now is shifted of some pixels toward the positive x axis and that's useless this way.. 

double checked the code, there are no errors in that.. 

i have to admit i don't have already checked the source code of the "body.addPolygon" to see why giving odd sided shapes to the method will generate the shifting... anyway.. 

i wanted to hear from you guys what you think the problem is first, i'll post the code i used, and an image showing a default body applied on the rectangular shape (on top of the image) and my triangle one via "body.addPolygon" (on the bottom of the image) where u could appreciate the x shifting .. 

mySprite.body.clearShapes(); // to clean the box default 
mySprite.body.addPolygon( { skipSimpleCheck : true }, // options 
                                            [[0, 0], [0, mySprite.height], [mySprite.width, mySprite.height / 2]]);

the image (default Box model on top, custom "triangle" body via addPolygon, on bottom) .. you can clearly see the x shifting ... 

Thx for the time spent reading the poem :D and i will wait your considerations on the matter... 

Link to comment
Share on other sites

Hi, read my older answer here:

 It is complex solution not only to your problem. If I remember well, addPolygon takes into account center of mass and places polybon vertices around it.

 My answer is also solving issues when you need collider of correct size for initialy scaled object (collider will not scale later) or flipped. Also solves anchors. And as a neat bonus, it allows you to simply build compound colliders.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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