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.


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.

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.