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

Very poor performance with a lot of collisions #52

Closed ghost closed 8 years ago

ghost commented 8 years ago

capture

For example, just having that many collision bodies destroys performance and it drops to around 20 fps

This is obviously because of p2 physics (game.physics.p2.convertTiledmap(mymap, 'Collision');)

Maybe there is another way to handle a lot of these collisions?

Also, how would we would utilize this function?

Tile.prototype.setCollisionCallback = function (callback, context) {

    this.collisionCallback = callback;
    this.collisionCallbackContext = context;

};

Loop through the bodies and then apply the setCollisionCallback method? For example, if you have a water tile and want the character to swim. I just think the documentation is lacking but I don't really know the code well enough to help, I'm still learning but I definitely think this library's documentation is not giving the library justice, there are so many features here and not a lot of examples to illustrate them :( I also am receiving a 70%+ performance increase from the stock Phaser renderer, this library is amazing but I think it's potential to be utilized 100% is slightly hindered by lack of examples

JimVliet commented 8 years ago

Agreed, and once I figure out how to use typescript with this plugin. I'd be willing to write an example for typescript.

englercj commented 8 years ago

If they are static bodies try drawing larger shapes and using convertTiledCollisionObjects instead. That will reduce the number of bodies drastically. That is, have an object layer and draw a couple shapes that represent all those collisions and convert that layer.

The reason that there is little to no docs is because the API is identical to the stock tilemap implementation in Phaser (or tries to be). Only differences are loading maps, and initializing physics bodies.

ghost commented 8 years ago

Good idea @englercj,

I just am getting worried as if you look at a game called Hero Siege, at the 58 minute mark their maps are consisting of a lot of collision blocks (players, monsters, tiles, projectiles, etc) and I am worried that it might hinder our CPU i/o and create lag. Personal question, do you think Phaser is a sufficient engine for that type of game?

I see about the documentation, so basically most of the methods and functions from the regular phaser tiled are used, that's great to know I will take a look at them :).

englercj commented 8 years ago

Yeah I think phaser is fine for this type of game, and p2 is a great engine for it. Just the screenshot you shared above (if each red square is a body) is a lot more bodies than in Hero Siege (which I've played, fun game). It's all about minimizing the bodies, simplifying the shapes, and configuring them correctly (making them static, configuring groups and collision responses, etc).

You can also play around with running p2 in a web-worker. Not sure how easy it would be to get it working with Phaser, but I know p2 can run in a web-worker.