liabru / matter-js

a 2D rigid body physics engine for the web ▲● ■
MIT License
16.62k stars 1.96k forks source link

The performance is worse than box2d.js #608

Open finscn opened 6 years ago

finscn commented 6 years ago

I found the performance of matter.js is worse than box2d.js (only 40%)

There is a test I created:

http://fatidol.com/phy-benchmark/

There are many different box2d.js projects , I chose https://github.com/flyover/box2d.js , Because it includes Google's LiquidFun , I like this feature.

bchevalier commented 6 years ago

Hey @finscn, could you try out one of my commits? I heavily optimized the engine Performance may be roughly 10x better.

finscn commented 6 years ago

@bchevalier , hi , could you give me a matter.min.js file directly ? I don't know how to build

bchevalier commented 6 years ago

Here you go: minified and unminified

finscn commented 6 years ago

@bchevalier , yeah, it's more fast. But the result is not good enough ( both of liabru version & yours ): image

There are many rect-boxes are overlap . I try ti change positionIterations & velocityIterations, but less effect.

bchevalier commented 6 years ago

I noticed that setting both iterations to ~12 was providing good result. Would you mind updating your online benchmark with the changes? I am curious to see what it gives.

finscn commented 6 years ago

I tried. Even I set iterations to 30 , image

But there are still many rect-boxes are overlap .

BTW , Your version is very fast. I set the count of rect-box to 700 , it's still keeping 60 fps on my macbook,
the box2d.ts is down to 40 fps.

bchevalier commented 6 years ago

Hey @liabru, do you have any idea what may be the reason for that difference in behavior between matter and box2d? I am willing to have a look and try to fix the issue.

liabru commented 6 years ago

Indeed @bchevalier has done some excellent performance work that I've yet to fully integrate (I'll be working on it in May!).

There are likely a few reasons for this, one being the runner does not use a fixed delta by default and the low FPS causing a death spiral.

Try runner.isFixed = true and runner.delta = 5 which should help a bit.

The runner has a lot of room for improvement here which is something I need to revisit.

Other improvements for this would be adaptive iterations (minimum error threshold) which I have a mostly-working implementation (it can cause some wild behaviour).

Also is improving the resolvers initial 'guess' when a pair first collides. In other words, predictive collision warming (warming is already implemented, but it is not primed with any initial guess). The guess would be based on velocity and mass.

@bchevalier the latter is something I can fill you in on some more if you'd like to tackle it!

finscn commented 6 years ago

@liabru , thanks for your suggestion. I've tried runner.isFixed = true and runner.delta = 5 , it's better , but still worse than box2d.

I'll be waiting for the improvement. Thank you

bchevalier commented 6 years ago

@liabru yes please fill me in on that if you don't mind. I cannot promise anything in terms of timeline though. By the way, what are the main differences between matter and box2d? What is the reason why resolving the positions seems more difficult to achieve?

finscn commented 6 years ago

Hi @bchevalier & @liabru , is there any news about new version ?

yungzhu commented 6 years ago

Looking forward to performance improvements Is there any plan or time for this regard?

jcyuan commented 6 years ago

I'm curious what did you modify to make matter faster then box2d? @bchevalier

jcyuan commented 6 years ago

I found that you added separating axis support for Body after comparing between your version and master's. @bchevalier @liabru

finscn commented 6 years ago

Hi @@flyover , I think @jcyuan 's question is a good question. @bchevalier 's forked matter-js is faster than box2d.ts . (original matter-js is not) I wish box2d.ts could as fast as @bchevalier 's forked matter-js .

jcyuan commented 5 years ago

warmly ping @bchevalier @liabru I think the web physics engine still activating is getting more less especially which suitable for mobile client. hope this project can be better and better. thanks for your efforts.

bchevalier commented 5 years ago

@jcyuan, sorry I did not notice your question back in July. To make it faster than box2d, mainly 2 things:

These optimizations do not require any knowledge specific to physics engines.

I would imagine that it's possibly to optimize box2d as well (because the great majority of projects can be optimized).

jcyuan commented 5 years ago

@bchevalier thanks for the info, could you please provide some example? I dont quite understand what kind of optimization can make a such fps difference.

bchevalier commented 5 years ago

you can have a look at the PRs I made: https://github.com/liabru/matter-js/pulls?utf8=%E2%9C%93&q=is%3Apr+author%3Abchevalier

For instance in this one the complexity of an update method was improved: https://github.com/liabru/matter-js/pull/522

In this one I avoided the generation of an id string for every collision at each update by keeping a contact object around: https://github.com/liabru/matter-js/pull/526

In this one I reduced the complexity of the solvePosition method by precalculating some info for each body instead of doing it at each collision (note that there are potentially many more collisions than bodies in the physics world): https://github.com/liabru/matter-js/pull/527

In this one I made sure that the Body class would always be of the same internal JS hidden type by initializing all possible properties in the constructor: https://github.com/liabru/matter-js/pull/528

jcyuan commented 5 years ago

these do make sense! thanks again! @bchevalier

mattbucci commented 5 years ago

@bchevalier and @liabru any idea when these performance updates will be merged? Anything we can do to help?

jcyuan commented 5 years ago

I'm using @flyover 's box2d.ts. here is a benchmark made by @finscn

mapacarta commented 5 years ago

@bchevalier I tested your commit, it seems much faster. Thanks for your efforts.

In my game, I cancel some of the collisions with pair.isActive = false;
It seems you removed this, is there any other way to do this?

megasyl commented 4 years ago

@bchevalier Thank you very much ! Rendering is 10 to 15 times faster with your optimizations ! Very nice work !

Adamsmashem commented 4 years ago

I found that doing Matter.Engine.clear(engine) every frame helps a lot with only a slight accuracy drop

here is an example

press space to not do Matter.Engine.clear(engine)

https://www.openprocessing.org/sketch/932385