How to add slopes to tilemap that uses p2 for collision?

Recommended Posts

Hello, I am quite new to phaser and Tiled. I was wondering if anyone could give me some help with understanding on how to create tilemap collisions that are more than just squares with the p2 physics system. Right now every tile has a square collision box, I would like the collision shape to match the tile shape so that I can have slopes and what not. I am a quite a bit confused on the Tiled to phaser workflow especially on what p2.convertTilemap does and how Tiled's collision editor ties into phaser. So far my code looks like this:

preload: function () {

		this.game.load.image('mapTiles', "assets/platformer-pack-redux-360-assets/Spritesheets/spritesheet_ground.png");
		this.game.load.tilemap('level1', "assets/testmap2.json", null, Phaser.Tilemap.TILED_JSON);

		this.game.load.spritesheet('player', "assets/kenney_platformercharacters/PNG/Player/player_tilesheet.png", 80,110, 24);

create: function () {
        var level1 = this.game.add.tilemap('level1');
        level1.addTilesetImage('spritesheet_ground', 'mapTiles');

        var wallsLayer = level1.createLayer("layer1");

        player = this.game.add.sprite(110,80, 'player');
        var walk = player.animations.add('walk', [0]);
        player.animations.play('walk', 10, true);
        player.body.fixedRotation = true;

        cursors = this.game.input.keyboard.createCursorKeys();
        wallsLayer.debug = true;
        player.body.debug = true;

Any help is appreciated!

Share this post

Link to post
Share on other sites

So after a long hard look at the source code and the documentation provided I was able to come up with an answer to my own question. :)

First off: convertTilemap only creates rectangle collision boxes and in no way shape or form checks the collisions you have set up in Tiled using the editor. With that in mind let's take a look at what convertCollisionObjects does, covertCollisionObjects takes the shapes you placed in the object layer of your project and creates the physics bodies for them and then returns it in an array. In order to make the ground collide with the player, you need to loop over the array of bodies and set it to collide with the player (or any other object). Here is the code:

create: function () {

        //Tile map
        var level1 = this.game.add.tilemap('level1');
        level1.addTilesetImage('spritesheet_ground', 'mapTiles');

        //add platforms layer
        wallsLayer = level1.createLayer("layer1");
        //creates the player and ground collisionGroup
        var groundCollisionGroup = this.game.physics.p2.createCollisionGroup();
        var groundMaterial = this.game.physics.p2.createMaterial('groundMaterial');
        var playerCollisionGroup = this.game.physics.p2.createCollisionGroup();
        var playerMaterial = this.game.physics.p2.createMaterial('playerMaterial');
        //returns an array of bodies which we then set the collision group and make it collide with player
        var tileMapBodiesArray = this.game.physics.p2.convertCollisionObjects(level1, "objectLayer1");
        for (var i = 0; i < tileMapBodiesArray.length; i++) {

        player = this.game.add.sprite(200, 200, 'player');

        player.body.addCapsule(40, 40, 0, 0, 1.5708);
        player.body.data.gravityScale = 20;


Share this post

Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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


  • Recently Browsing   0 members

    No registered users viewing this page.