englercj / phaser-tiled

A tilemap implementation for phaser focusing on large complex maps built with the Tiled Editor.
MIT License
290 stars 31 forks source link

this.game.physics.p2.convertTiledmap #23

Closed Weedshaker closed 9 years ago

Weedshaker commented 9 years ago

Hi,

again, need to say how awesome the fps performance is with this. I am very happy! ;-)

1) But I just can't get it to work that tiles collide. I set collides = true to the tiles I am using in Tiled:

"tileproperties":
            {
             "14":
                {
                 "collides":"true"
                },

But the Tilelayer outputs this:

bodies[...].collidesWith: Array[0]

can't it be that we need to activate something like "map.setCollisionBetween(1, 12);" ?

2) There is also a problem with the for loop at L:1927. My tiles objects do not start with [0]... but actually with [2], obviously the loop fails there since it tries to access an object key of which the object does exist for. I suggest to check: "if(layer.tiles[y]){"

Thank you for your help.

englercj commented 9 years ago

1) It doesn't set any collision groups for you, if you want to set a collision group you can but we can't know what you want to set it to. I've been doing this:

var bodies = tiledmap.getObjectlayer('collisions').bodies;

for (var i = 0; i < bodies.length; ++i) {
        bodies[i].setCollisionGroup(game.collisionGroups.ground);
        bodies[i].collides(game.collisionGroups.all);
}

Where I have my collisionGroups stored somewhere on my game, and my collision bodies are all in the 'collisions' layer.

2) Good catch, I'll fix that up.

Weedshaker commented 9 years ago

I am using materials and onBeginContact events for triggering the collision logic but for the sake of trying out all options, I did add collisionGroups to my bodies, as you did describe. Thanks for that... But unfortunately, it didn't work... I am gonna try it with a fresh tilemap and code somewhen, for the moment its alright to use object layers for collisions.

I don't think this helps or you really got time but below is the code where I try to convert the tiles:

setCollision: function(layer, tilemap){
        var tilemap = typeof tilemap !== 'undefined' ? tilemap : false;
        var materialName = layer.name;
        // add physics
        if(tilemap){
            // !!!Didn't work last time I checked 20150130!!!
            this.game.physics.p2.convertTiledmap(this.map, layer.name);
            //console.log(layer);
        }else{
            this.game.physics.p2.convertTiledCollisionObjects(this.map, layer.name);
            //console.log(layer);
        }
        // add material and mass
        layer.bodies.forEach(function(item){
            for(var i = 0; i < this.material.length; i++){
                if(materialName.toLowerCase().search(this.material[i].name.replace(/material/ig, '').toLowerCase()) !== -1){
                    item.setMaterial(this.material[i]);
                    item.mass = 1000;
                    break;
                }
            }
        }, this);
    }

anyhow, thanks.

englercj commented 9 years ago

This has been fixed in master and will be released in v2. Unfortunately, it will require Phaser v2.3.0, I will release v2 of this plugin about the same time as Phaser v2.3.0 is released.