## Recommended Posts

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

but:
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?

##### 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]];

polygon.fromPolygon(path);

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

##### Share on other sites

• 2 weeks later...

Care to elaborate further? 'Coz it's a mess..

##### Share on other sites

body.addPolygon() ; -use same coords and it gets fucked up...

##### Share on other sites

Care to elaborate further? 'Coz it's a mess..

What you want to elaborate? Have you found anything else?

##### Share on other sites

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 .

##### Share on other sites

Never mind.

P2 is just weird - makes me miss Farseer Physics a lot!

##### Share on other sites

• 1 month later...

@Teemu-Tor did you figure out how to properly add polygon shapes? I'm still struggling with this

##### Share on other sites

• 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

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. =)

##### 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.

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.