- Home /
Handling Large number of Active Rigidbody Evaluations
Background As a side project a friend and I have been developing a solar system simulation for the vive. We are using the Steam VR plugin. We are targetting 64bit PCs and the Vive. We have created our own scripts to create a point based gravity system that enables us to define a gravitational force centered on any game object of our choosing. The simulation starts with a single gravity well existing in the scene and the user can add items to the scene of various masses and velocities and watch them interact with the gravity well.
Problem We can get up to about 500+ active rigidbodies (all using primitive sphere colliders) before things begin to slow down. Every fixed update we gather the gravitational force that needs to be applied to these rigidbodies and then use addforce to adjust their trajectories accordingly. To be honest this works just fine for what we want but for our next step we begin to run into serious problems. I.E. Ideally instead of a single gravitational source (the "star" in the center of the scene) we want ALL rigidbodies in the scene to also have their own gravity, based off of their mass. Our current scripts allow for this, but turning this functionality on causes a huge drop in the number of items we can have in our scene before slowdown. We drop down to about 100-150 and even then it can be problematic. It is pretty obvious what's happening, both in theory and from the profiler- the evaluations we are doing balloon to a huge number because now we are evaluating the amount of force to add, multiplied by the number of items in the scene, for each individual rigidbody.
Question Can anyone point me in the right direction on how to possibly optimize a scene like this? I would like to get back to about 500 active rigidbodies if possible, while allowing all these items to have their own gravity as well. The ideal scenario would be to observe these items coalescing into "chunks" in the same way planetary bodies accrue from masses of smaller particles circling a gravity well. I am doing what research I can, but in actuality I am an animator and artist who dabbles in programming so any assistance would be a great help (currently trying research and learn if it would be possible to somehow offload all these calculations to the gpu).
Apologies for the wall of text, I can provide messy samples of code if you like. Thanks for reading.
Answer by doublemax · Sep 08, 2016 at 09:09 AM
http://gamedev.stackexchange.com/a/19394
It seems that you're doing the gravity calculations yourself. Have you tried letting Unity do it for you?
Apart from the trick in the above post, if you wanted a "real" simulation, you'd usually use the GPU to do the calculations with OpenCL, CUDA etc. I don't know what Unity does internally, but as it uses PhysX, which uses the GPU, maybe the force calculations take advantage of that. Might be worth a try.
That grid idea you linked is a great idea I will research it more. thank you!
As for letting Unity do the gravity calculations, I'm not sure how that would be possible. From everything I've looked up about it, Unity is only capable of doing gravity along a single vector, like in the project settings where it lets you set the Vector3 for the gravity direction.
While this is useful for the majority of use cases I need my gravity to be point based. So the vector along which the force is applied is dependent on both the object's location and the location of the gravity source. If there is a way to get Unity to do gravity from a discrete point in space rather than along a single vector that could be very useful.
As for using the GPU for calculations, the problem I am running into is that I need to be able to add and remove items from the simulation at will and everything I have seen so far uses a given set of items. But I will continue to look, also GPU simulations seem like a completely foreign language and are not well documented in a way that I can easily understand. I'll keep at it. Thank you for your suggestions.
As for letting Unity do the gravity calculations, I'm not sure how that would be possible. From everything I've looked up about it, Unity is only capable of doing gravity along a single vector, like in the project settings where it lets you set the Vector3 for the gravity direction.
Sorry. I'm still a relatively new Unity user myself and didn't realize that. I guess the grid idea is really your best option then.
If you want to research any further, Googe for "Unity Compute Shaders Gravity" . There are some impressive videos out there:
https://www.youtube.com/watch?v=_UBHG2cDD7g
Just to correct one point - Unity's PhysX does not use the GPU - it runs on the CPU. https://blogs.unity3d.com/2014/07/08/high-performance-physics-in-unity-5/
Answer by jmonasterio · Sep 08, 2016 at 05:47 PM
Keep in mind that the effect gravity is inversely proportional to the SQUARE of the distance.
This means that when deciding which objects affect each other, you can probably safely ignore small objects that are far away.
For example, the sun is affected by pluto's gravity, but it doesn't really matter unless you need an extremely accurate simulation.
So you could just focus on nearby objects and big objects, when calculating the effect of gravity on a particular object.
This could greatly reduce the number of calculations, which will allow you support more objects.