julacariote

Tilemap collision with Phaser 2.0

Recommended Posts

Hello,

 

I'm playing with Phaser 2.0, trying to make a very simple platform game.

 

In Tiled Map Editor, it's possible to create an objet layer to draw shapes which could be used as "collision boxes". Is there anyway to use this layer to handle collisions between the player and the ground for example in phaser? The examples only show how to use the setCollisionBetween, setCollision or setCollisionByExclusion methods.

 

Thanks!

post-6203-0-03106600-1395135586_thumb.jp

Share this post


Link to post
Share on other sites

There is experimental support for this in the Tilemap and P2 API. You need to be using P2 physics, it won't work with any other. Then try doing:

game.physics.p2.convertCollisionObjects(map, layer, addToWorld);

Tweak the parameters as required.

Share this post


Link to post
Share on other sites

This seems like quite a powerful feature. How to create the object layer in Phaser?

In Tiled my "Collision" layer is defined as an Object Layer not a Tile Layer, but it doesnt seem to play nice with createLayer function.

map = this.game.add.tilemap('map1');map.addTilesetImage('walls', 'mapTiles');visualLayer = map.createLayer('Tile Layer 1');collisionLayer = map.createLayer('Collision'); //no work :(this.game.physics.p2.convertCollisionObjects(map, collisionLayer);

At line 4, it fails in the console with "Tilemap.createLayer: Invalid layer ID given: null", phaser.min.js:15

I've checked the name in Tiled and the JSON file and it matches.

Share this post


Link to post
Share on other sites

Yes, I ran into the same problem.

 

If you read the code, you will see that map.createLayer('mylayer') search for 'mylayer' in the layers attribute. This attribute is populated by TilemapParser with only layers of type "tilelayer". Therefore, an object layer like the collision layer can't be found here...

 

But I can't find the right way to do that...

Share this post


Link to post
Share on other sites

Yes, I ran into the same problem.

 

If you read the code, you will see that map.createLayer('mylayer') search for 'mylayer' in the layers attribute. This attribute is populated by TilemapParser with only layers of type "tilelayer". Therefore, an object layer like the collision layer can't be found here...

 

But I can't find the right way to do that...

 

I for sure would like this feature!

 

I also have tried to get Object Layer from Tiled MapEditor to work. I getting same error that Rudy and you!

Share this post


Link to post
Share on other sites

I think there is something wrong with the Phaser.Physics.P2.convertCollisionObjects method. See my comments in the code below:

    convertCollisionObjects: function (map, layer, addToWorld) {        if (typeof addToWorld === 'undefined') { addToWorld = true; }                // This method returns the layer index, an integer        layer = map.getLayer(layer);        var output = [];                // But map.collision is an object whose keys are the objects layers names.        // It is defined as an array in Phaser.Tilemap (L117) but Phaser.TilemapParser        // creates it as an object, see Phaser.TilemapParser L344        // So map.collision[layer] is undefined        for (var i = 0, len = map.collision[layer].length; i < len; i++)        {            // name: json.layers[i].objects[v].name,            // x: json.layers[i].objects[v].x,            // y: json.layers[i].objects[v].y,            // width: json.layers[i].objects[v].width,            // height: json.layers[i].objects[v].height,            // visible: json.layers[i].objects[v].visible,            // properties: json.layers[i].objects[v].properties,            // polyline: json.layers[i].objects[v].polyline            var object = map.collision[layer][i];            var body = this.createBody(object.x, object.y, 0, addToWorld, {}, object.polyline);            if (body)            {                output.push(body);            }        }        return output;    },

Share this post


Link to post
Share on other sites

does the collision layer have to be an object layer? I want to be able to build my background/foreground layers in Tiled and then build one hidden collision layer out of tiles that I can then say to the player character "this is the collision layer" - is this possible?
 

Share this post


Link to post
Share on other sites

There is experimental support for this in the Tilemap and P2 API. You need to be using P2 physics, it won't work with any other. Then try doing:

game.physics.p2.convertCollisionObjects(map, layer, addToWorld);

Tweak the parameters as required.

 

Have you look at @julacariote code?

Share this post


Link to post
Share on other sites

It works well if you comment out the line at 1385. Then you just need to pass the object layer name as a string to convertCollisionObjects.

 

Also the only objects parsed are polyline objects. I tried at first with polygons and rectangles but they are ignored by the map parser.

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.

Guest
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.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.