Jump to content

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


jak6jak
 Share

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 () {
        this.game.physics.startSystem(Phaser.Physics.P2JS);
        
        var level1 = this.game.add.tilemap('level1');
        level1.addTilesetImage('spritesheet_ground', 'mapTiles');

        var wallsLayer = level1.createLayer("layer1");
        wallsLayer.resizeWorld();
        level1.setCollisionByExclusion([],true,'layer1');
        this.game.physics.p2.convertTilemap(level1,wallsLayer);
        this.game.physics.p2.convertCollisionObjects(level1,"objectLayer1");

        player = this.game.add.sprite(110,80, 'player');
        var walk = player.animations.add('walk', [0]);
        player.animations.play('walk', 10, true);
        this.game.physics.p2.enable(player);
        this.game.camera.follow(player);
        player.body.setZeroDamping();
        player.body.fixedRotation = true;
        this.game.physics.p2.setBoundsToWorld(true,true,true,true,false);

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

Any help is appreciated!

Link to comment
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 () {
        this.game.physics.startSystem(Phaser.Physics.P2JS);

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

        //add platforms layer
        wallsLayer = level1.createLayer("layer1");
        wallsLayer.resizeWorld();
        
        //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++) {
            tileMapBodiesArray[i].setCollisionGroup(groundCollisionGroup);
            tileMapBodiesArray[i].collides(playerCollisionGroup);
            tileMapBodiesArray[i].setMaterial(groundMaterial);
        }

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

        this.game.physics.p2.enable(player);
        player.body.clearShapes();
        player.body.addCapsule(40, 40, 0, 0, 1.5708);
        player.body.setCollisionGroup(playerCollisionGroup);
        player.body.collides(groundCollisionGroup);
        player.body.setMaterial(playerMaterial);
        player.body.data.gravityScale = 20;

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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