Lags related to Grid.update
See original GitHub issueHello.
I have huge world and i experience strange behavior of engine. I need to to create many objects in scene (bullets) and also i have many objects (more than 600).
When i have many objects i experience lags when also add many objects to scene*. And when i have not so many objects (about 100), i experience lags when do not add another objects to a scene, and also when i do so, there is no more lags for some time.
I have very big bucketWidth
and bucketHeight
both equals 700. And also i have positionIterations
12 and velocityIterations
8 but this make no difference to described lags.
* It’s temporary objects (bullets as i said). After some time they removed from world. I experience lags in time that i continuously add new objects, and all ok when i stop do so.
I was do some research and was investigate that it’s something related to Grid.update
method and it’s forceUpdate
mode. First of all Grid.update
are not optimized in google chrome due to large amount of opt/deopt cycles. I can solve this issue by this little snippet:
if (forceUpdate)
return Grid.updateForce(grid, bodies, engine, forceUpdate)
At the top of Grid.update
method. Grid.updateForce
is a simple copy-paste code from Grid.update
. Google chrome can optimize this, and i propose to replace all occurences of Grid.update(grid, bodies, engine, true)
(all force updates of grid) with Grid.updateForce
But this is not solve the lags, just a little speed up.
I can suppose two improvements that can or cannot resolve this issue.
I think that using grid for broadphase structure is inefficient and it must be replaced to something as AABB tree.
And also i can propose to create somethings as World.addFast
method that will add objects without forcing grid/tree rebuild.
Also when i turn on showBroadphase
, i see that many grid regions are empty and because i see them in viewport, they exists in grid.buckets
array. Maybe better remove empty buckets?
Can you comment something or explain the actual root of this issue?
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
The
forceUpdate
option is called when ever the world is modified (bodies added or removed) so if you’re doing that very often (i.e. bullets) with a large amount of total bodies then you may get stuttering because it is quite expensive.The reason it is this way is because it was a tradeoff for keeping the overall design simple (it was one of the first modules I initially wrote). For it to be more efficient, the grid would need to know exactly what has been removed and then update buckets individually accordingly. This is possible but it would require additional modifications to
Matter.Composite
andMatter.Engine
so it’s not straightforward.I’ll label this one as an
improve
as I’d like to implement this eventually.A temporary solution for for anyone with this problem: I think there is a way you can batch this update, at least for body removals.
Rather than remove the bodies from the world do this instead:
garbage
arrayThen when ever the
garbage
array gets to say500
bodies, remove them all from the world in one step and clear thegarbage
array. This should mean the amount of times the grid is rebuilt is1/500
times less.As of the latest release 0.18.0 the grid broadphase has been replaced, so I’ll close this one as it should no longer be an issue. If anybody notices issues with the latest release like this then please do open a new issue, thanks.