Jump to content

how to collide with a polygon created in tiled (p2 physics)


valueerror
 Share

Recommended Posts

  • 2 weeks later...

thats exactly what i was trying to do but it just doesn't work...

 

i even copied your demo - all your assets, the code and the json file..  it works..  i also looked at the map in "tiled"   it just works but when i try to load my own json file it doesn't work

 

everything seems to be fine (no error messages) but still no collision :(   did you do anything special in your json file ?

 

edit: i also tried to change your poligong.json file.. i added another polygon..   the outcome - nothing changed.. your polygons collide.. my new one does not!

 

i even browsed the code of the json file.. no visible difference..  the only thing i noticed was that your polygons aren't strictly in a 32x32 manner  ..  how did you do that?  tiled only allows me to create polygons along the 32x32 raster..  

Link to comment
Share on other sites

Just change the name of the object layer in my json file, maybe your polygons not close completely, try to see if they are being loaded map.collision. I modified the size when I add a new tileset "tile width" and "tile height ".

Link to comment
Share on other sites

i don't understand.. why should i change the name of the object layer in your json file..   i tried your json file and all your polygons on the objectlayer "capa objeto" load and collide properly..  when i add another polygon (closed of course) your's still work but the new one i just added does not collide..  :(  i just don't get what i am doing different..  even in the source of the json file i see no difference between your objects and mine...

Link to comment
Share on other sites

ok.. a minute ago i found out how to make a polygon collide with other objects...    i played around with your json file and found out that your objects are NOT closed completely..  they do not act as one closed form like a rectangle for example.. they still are an open polyline and just look closed because the beginning and the end overlap..  

 

so i tried "polyline" instead of "polygon" or "rectangle" aaaaand... it works..  i don't know why but using polylines does the trick...    is this a BUG or are rectangles and other closed forms just not supported yet ?? ?? 

 

 

and still one open question remains..  your polylines are completely offgrid  they are not in any way bound to the 32x32 or any other grid size..   how??  i'm using tiled version 0.9.0  and it doesn't allow this :(  are you using a newer verison that supports polylines offgrid?

Link to comment
Share on other sites

hmmm..but when i open your tilemap it has 32*32 tiles.. thats weird..

just to clear up my confusion..

when you say tileset height/width you mean the w/h of the tiles on the map.. right? 'cause changing the w/h of my tileset images doesnt affect the behaviour of the grid where i paint the polylines..

may i ask how small your tiles were.. the polyline connection points are all on so different heights/widths.. must be 8*8 or something..

Link to comment
Share on other sites

hey JP91  ..    because you said that "when the polyline is drawn, you can create them the size you want."   i opened up tiled again (freshly motivated)  and gave it yet another try.. nothing..  i could create polylines only along the grid.. then i went through the settings and found "magnetic raster" turned on!!  i turned this option of and now i can paint polylines wherever i want...  yay!!   :D   so this thread is definitely solved !

 

 thx again!

Link to comment
Share on other sites

  • 1 month later...

For reference, you can place points anywhere in Tiled even with the raster active, if you hold the CTRL key!

 

 

...and, with a little tinkering, you can use polygons instead of polylines as well. 

 

Here's the code. I load the JSON file as a physics object and not a tilemap (I don't actually know if this makes a difference or not):

this.load.physics('level', 'assets/level.json');

And then I parse the physics data manually in my create function:

this.scenery = new Phaser.Group(this, this.world, null, false, true, Phaser.Physics.P2);  this.levelData = this.cache.getPhysicsData('level'); for (i = 0; i < this.levelData.layers[1].objects.length; i++) {    this.scenery.addAt(this.add.sprite(this.levelData.layers[1].objects[i].x, this.levelData.layers[1].objects[i].y), i);    this.physics.p2.enable(this.scenery.children[i], true);    this.scenery.children[i].body.clearShapes();    thepolygon = this.levelData.layers[1].objects[i].polygon;    polygonarray = [];    for (j = 0; j < thepolygon.length; j++) {        polygonarray[j] = [thepolygon[j].x, thepolygon[j].y];    }    this.scenery.children[i].body.addPolygon({}, polygonarray);}

Careful about which layer you poll though :)

Link to comment
Share on other sites

  • 4 months later...
 Share

  • Recently Browsing   0 members

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