Jump to content

Creating a P2 collision group from a tilemap


anissen
 Share

Recommended Posts

I have a tilemap and some sprites. Some of the sprites must collide with each other and all most collide with the tilemap.

 

When I set a sprite to collide with a collision group (ie. playerSprite.body.collides(enemyCollisionGroup)), the sprite *only* collides with that group. To make it also collide with the tilemap I guess I need to do a playerSprite.body.collides(tilesCollisionGroup). However, I cannot figure out how to correctly construct tilesCollisionGroup.

 

I tried several different approaches along the lines of

    map.setCollisionBetween(1, 12, true, layer2);    var tileObjects = this.physics.p2.convertTilemap(map, layer2);    var tilesCollisionGroup = this.physics.p2.createCollisionGroup();    for (var i = 0; i < tileObjects.length; i++) {        var tileBody = tileObjects[i];        tileBody.setCollisionGroup(tilesCollisionGroup);        tileBody.collides(playerCollisionGroup);    }

... with no luck.

 

Any ideas for handling collisions between sprites/sprites and sprites/tiles using P2? Thank you for your time :)

Link to comment
Share on other sites

do it like this:

map = this.add.tilemap('level01');map.addTilesetImage('Space', 'tiles'); layer = map.createLayer('Space');layer.resizeWorld();// next set collisionsmap.setCollisionBetween(1, 99);// example//then below the collision set up have thisthis.physics.p2.convertTilemap(map, layer);
Link to comment
Share on other sites

Solved it! As with so many other things, the solution appeared after a good nights sleep :)

 

Thank you for your reply, Heppell08. However, my problem was that I had set the different collision groups to collide with the tilesCollisionsGroup, but not the other way around.

 

So, I had done something like

    var tilesCollisionGroup   = this.physics.p2.createCollisionGroup();    var playerCollisionGroup  = this.physics.p2.createCollisionGroup();        for (var i = 0; i < tileObjects.length; i++) {        var tileBody = tileObjects[i];        tileBody.setCollisionGroup(tilesCollisionGroup);    }    var ship = this.add.sprite(200, 200, 'ship');    this.physics.p2.enable(ship, false);    ship.body.setCollisionGroup(playerCollisionGroup);    ship.body.collides(tilesCollisionGroup);

When I should have done it like this (notice tileBody.collides(playerCollisionGroup):

    var tilesCollisionGroup   = this.physics.p2.createCollisionGroup();    var playerCollisionGroup  = this.physics.p2.createCollisionGroup();        for (var i = 0; i < tileObjects.length; i++) {        var tileBody = tileObjects[i];        tileBody.setCollisionGroup(tilesCollisionGroup);        tileBody.collides(playerCollisionGroup);    }    var ship = this.add.sprite(200, 200, 'ship');    this.physics.p2.enable(ship, false);    ship.body.setCollisionGroup(playerCollisionGroup);    ship.body.collides(tilesCollisionGroup);

:)

Link to comment
Share on other sites

haha.. to late.. gave me quite a headache 2 weeks ago when i did the exact same thing...  but i still wonder if this is the appropriate way.. to cycle over every tile in the array of "tilebodies"

 

isn't there a function like:

pseudo:

 "tileObjects.setAll(setCollisionGroup,'tilesCollisiongroup'); 

????

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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