anissen Posted March 28, 2014 Share Posted March 28, 2014 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 More sharing options...
Heppell08 Posted March 28, 2014 Share Posted March 28, 2014 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 More sharing options...
anissen Posted March 29, 2014 Author Share Posted March 29, 2014 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); Juls and kctang 2 Link to comment Share on other sites More sharing options...
Heppell08 Posted March 29, 2014 Share Posted March 29, 2014 Glad you got it working. I also realised after posting you had worked all that simple stuff out and wanted something totally different haha. Nice bit of coding there though! Someone will make use of that too I believe! Link to comment Share on other sites More sharing options...
valueerror Posted March 29, 2014 Share Posted March 29, 2014 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 More sharing options...
Recommended Posts