Physics in Phaser handling large worlds

Recommended Posts

I have a tile-based world around 10,000 tiles big. Each tile has a static physics body that needs to interact with bullets of varying shapes as well as a rectangular player. In theory I could get by with just rectangular and circular hitboxes (so Arcade is out of the question), but Ninja is deprecated so I'm not really interested in using it.


Right now I'm sitting at around 5 ms of P2 processing time, which is impressive but still too heavy for my use case (I'm developing on a pretty good computer). I've tried various techniques of optimization, including destroying off-screen sprites, but that operation is pretty expensive.


I'm not sure if the problems I'm encountering can be resolved by using a library other than P2, or if it's just an impossible problem due to the amount of computation required.


I'd love to hear some input on this from others. :)

Share this post

Link to post
Share on other sites

It's really tough to say much about optimizing, since it depends so heavily on your specific implementation details. If you absolutely must have such a large world and you don't want to be destroying sprites (or p2 bodies), have you considered setting the shapes in bodies that are offscreen into sensors? They won't trigger collisions, so if you didn't attach any contact events to them they effectively become immaterial to P2.


Are you using impact events? If you are, you can try disabling them and instead use contact events to process collisions.


Other than that though, P2 is pretty expensive since it's so general. I wouldn't rule out Ninja entirely to be honest, it's built for speed. AABB vs. AABB collisions will be present in Phaser 2.4, so of the "basic" collision types it would only really be missing AABB vs. Circle collisions. If your circles will mostly impact tiles or you can get by checking for overlap instead of a proper collision (eg. for bullets), maybe Ninja would be right for you.


Or, heck, maybe implementing AABB vs. Circle collisions in Ninja will prove to be faster and better for your application than trying to squeeze more performance out of P2.

Share this post

Link to post
Share on other sites

I don't think P2 has support for sensors, or it's not in the documentation. I'd be fine with destroying bodies expect it seems to be a pretty computationally expensive operation.

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.