How to optimize my sprite collisions (using moveWithCollisions)

Hey everyone,

I’m using sprites a lot in my game, they are used for enemies, particles and debris etc pretty much everything.

To make the sprites “collide” with the world, I’m using a invisible box and calling moveWithCollisions on it. I then just update the sprite position to the box position

This works great, but I would like more performance.

moveWithCollision seems like the only function that will work as I have a complex level design. I tried to use intersectMesh but doesn’t work with my design.

My PC seems to hit about 500 of these objects before I get I get frame drops. e.g. < 60 fps

I’m using instancing for the “colliders” which gave me a small boost.

The function that seems to be slowing me down is moveWithCollisions().

Please see a playground below with a basic example of what I am doing:

https://playground.babylonjs.com/#JREX9A

If anyone could think of ways to optimize or other solutions that would be really appreciated.

Thanks

Without move collisions and just simply moving the “collider” position manually - it seems to handle 10,000 fine

frame rate is 71

// box.moveWithCollisions(new BABYLON.Vector3(0.01, 0, 0))
box.position.x += 0.01

Would anybody know if converting this moveWithCollisions() to webassembly would speed things up?

I have come up with a solution to only call moveWithCollions() every x frames and lerp the sprite.
I think it is slightly more performant.

Still would appreciate any feedback on improvements.

https://playground.babylonjs.com/#JREX9A#1

Hi kestrelpi,

Babylon’s collision mechanism that underlies moveWithCollisions() seems to be an O(n^2) problem (O(n) to collide a single mesh with every other mesh), so I don’t think it was really intended for use at this scale. You could try optimizing yourself using spatial partitioning and collision masking, but I think your best bet is to probably use a dedicated physics engine like Ammo.js to handle your movement and collisions. A dedicated physics engine will (hopefully) already contain optimizations to help you run your scenario as quickly as possible, likely simplifying the number of disparate features you have to pull together as well. Hope this helps, and best of luck!

Hi synthetic,

Thanks for the reply.

I did try a physics engine first but found it a lot slower. I think I’m being a bit too ambitious, so I think I’ll just rework each level to have less going on instead.