Jump to content

p2.body.addpolygon: how coords work?


Recommended Posts

hello! i'm trying to design a simple polygon by coords using this code on a 123x87 sprite

basket.anchor.setTo(0.5,1.0);basket.scale.setTo(0.75,0.75);game.physics.p2.enable(basket, debug);basket.body.clearShapes();basket.body.addPolygon({optimalDecomp: false, skipSimpleCheck: true, removeCollinearPoints: false},   [  [0,0], [1,0], [15,basket.height-10], [basket.width-15,basket.height-10], [basket.width-1,0], [basket.width,0], [basket.width-10, basket.height], [10,basket.height]  ]);

1) the polygon is not centered on the sprite (and i can't find a way to do this with coords.
2) the sprite change is position (x&y) every time i change the polygon (very strange!)
3) i tried to use physic engine with success but the polygon is not scaled to the sprite size. there is a way to scale the polygon too?
Thanks in advance.

Link to comment
Share on other sites

mmmmm... in p2.js

var polygon = new p2.Body({ mass : 1, position:[0,2] });

var path = [[-1, 1],

[-1, 0],

[1, 0],

[1, 1],

[0.5, 0.5]];


fromPolygon will automatically add shapes at proper offsets and adjust the center of mass.

Link to comment
Share on other sites

  • 2 weeks later...

hi ... ehhh so will try to explain it, in basic geometry to find the area of a polygon figure decomposes into triangles why I say this, because this way it can be made a rectangle,and p2 do it that way, a rectangle has two triangle and four vertices. vertices in p2 are given in counter-clockwise, starting with bottom right point.//----var sprite = game.add.sprite(x, y, 'name');----sprite.body.clearShapes();----- var w = game.physics.p2.pxm(sprite.width),----h= game.physics.p2.pxm(sprite.height);-----var shape =[[-w/2,-h/2],[w/2,-h/2], [w/2,h/2],[-w/2,h/2]];---- sprite.body.data.fromPolygon(shape);---\\

I hope you understand and is very possible that I this wrong .:)

Link to comment
Share on other sites

  • 1 month later...
  • 4 weeks later...

I've been having the same issue and it was close to driving me crazy. As JP91 wrote the issue is caused when P2 adjusts the polygon vertices so that center of mass is in the exact middle (I assume).


However this also seems to change the body.x & y in a way that I couldn't get to work properly. In the end I went opened Phaser.js, scrolled down to the polygon function in P2: Body.prototype.fromPolygon

and removed: this.adjustCenterOfMass();

Body.prototype.fromPolygon = function(path,options){    options = options || {};    // Remove all shapes    for(var i=this.shapes.length; i>=0; --i)        this.removeShape(this.shapes[i]);    var p = new decomp.Polygon();    p.vertices = path;    // Make it counter-clockwise    p.makeCCW();    if(typeof(options.removeCollinearPoints)=="number"){        p.removeCollinearPoints(options.removeCollinearPoints);    }    // Check if any line segment intersects the path itself    if(typeof(options.skipSimpleCheck) == "undefined"){        if(!p.isSimple()) return false;    }    // Save this path for later    this.concavePath = p.vertices.slice(0);    for(var i=0; i<this.concavePath.length; i++){        var v = [0,0];        vec2.copy(v,this.concavePath[i]);        this.concavePath[i] = v;    }    // Slow or fast decomp?    var convexes;    if(options.optimalDecomp)   convexes = p.decomp();    else                        convexes = p.quickDecomp();    var cm = vec2.create();    // Add convexes    for(var i=0; i!==convexes.length; i++){        // Create convex        var c = new Convex(convexes[i].vertices);        // Move all vertices so its center of mass is in the local center of the convex        for(var j=0; j!==c.vertices.length; j++){            var v = c.vertices[j];            vec2.sub(v,v,c.centerOfMass);        }        vec2.scale(cm,c.centerOfMass,1);        c.updateTriangles();        c.updateCenterOfMass();        c.updateBoundingRadius();        // Add the shape        this.addShape(c,cm);    }    this.adjustCenterOfMass();    this.aabbNeedsUpdate = true;    return true;};

I think maybe this should be fixed in the official version also, as you can run adjustCenterOfMass() yourself, but you can't undo it. =)

Link to comment
Share on other sites

  • 2 years later...

This is an old question (I ended up here through a Google search), but there's a new answer. Rich added the ability to add polygon data directly through the loadPolygon function in 2015.

Set the first argument to null and the second to the JSON data.

sprite.body.loadPolygon(null, data);

Using this, you won't get any shifting of the sprite the polygon is applied to and the polygon will apply correctly to the sprite.*

*If you are scaling your sprite:

sprite.scale.setTo(scale, scale);

before you call loadPolygon, the polygon will not be applied correctly. You can call loadPolygon first and apply the scale second, but I've noticed unusual physics when it's coded that way. Instead, I've found that scaling the sprite first and then iterating over the polygon data and applying my scale to the polygon data before calling loadPolygon works as I expect.



Link to comment
Share on other sites


  • Recently Browsing   0 members

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