Unity Physics Simulation Method Environment Hodgepodge
Comparison Overview
It was only when I was writing the title that I realized how miscellaneous my work was… Taking advantage of the need to select a paper for a school symposium, I chose PBD and XPBD, and also did a comparison between those and tradition PBA. I remembered that I had always wanted to compare the performance of C# and C++ during my internship but never did, so I wrote two comparisons of the semi-implicit method in Unity and C++ respectively (how can I miss parallelization). Here is a general comparison table first. The specific values don’t make much sense, but it’s still okay to look at the trend. (35x35 cloth grid, 2000 stiffness coefficient, 32 iterations, 64 threads, GTX1650 laptop. Parallelism is all lock-free multi-threading)
Unity | Unity Job + Burst | C++ | C++ Thread | |
---|---|---|---|---|
Time (ms) | 36.9 | 1.6 | 12.2 | 38.8 |
The performance of C++ Thread is a bit unexpected. I think it might be because Thread too costly? However, the performance of C++ is much better than Unity. I think I can try C++ when I want to optimize the code that is not suitable for Job + Burst parallelization the next time.
Semi-Implicit Euler | Implicit Euler | PBD | XPBD | |
---|---|---|---|---|
Main Thread (ms) | 36.9 | N/A | 70.9 | 52.2 |
Job + Burst (ms) | 1.6 | N/A | 2.1 | 2.2 |
Implicit crashes and not be taken into account. XPBD is better than PBD in the main thread may because I used Jacobi instead of Gauss-Seidel when writing PBD, but just look at the parallelization. What disappoint me is that Unity’s Cloth component only needs 0.2ms to simulate these, while my XPBD can only reach 0.4ms while maintaining stability. I will study how to optimize it later when I have time
Semi-Implicit Euler
I wanted to write an explicit one at the begining, but I haven’t written Euler for a long time, so after I finished I find it is a semi-implicit one. P is the grid points array, which is read in as local space. In order to be able to drag and observe the effect in Unity, it is converted to world space and then converted back after calculation.
1 | void Semi_Implict() |
Implicit Euler
There is nothing much to say, it is just gradient iteration. I tried Chebyshev acceleration but it doesn’t seem to make much difference. It is worth mentioning that in the previous version I wrote, I used a normalize and a magnitute to calculate the gradient. After changing to this version, the performance has improved a lot. Daily doubt the implementation of Unity functions.
1 | void Implict() |
PBD
I won’t go into the formulas and algorithm flow, but the general idea is to first move freely, then impose constraints. For a detailed paper interpretation, you can read this jeffzzz: Position Based Dynamics Notes. But PBD actually has some problems. The biggest problem is that it is not so physical. The physical properties it displays are only linked to the time step and the number of iterations. But the time step is related to performance, and the number of iterations is related to performance. It is a landmine to give different time steps and iterations to different objects in order to change the physical properties…
1 | void PBD() |
XPBD
XPBD redefines PBD constraints by introducing elastic potential energy, solving the above PBD problem with less than 5% increase in time (the paper seems to say 2-3%, but it’s roughly the same). The recommended paper interpretation here is zilch: XPBD Paper Interpretation, or you can also take a look at WebXPBDCloth written by the founder Müller. It will be faster to understand it in combination with actual engineering.
1 | void XPBD() |