Expensive physics code

This is a similar thread to the other one, but im not really getting the answers I hoped for.

I guess what Im looking for a list of physics operations that are expensive (trigger expensive recalculations etc)

Like changing the rigidbody center of mass.

every operation is expensive but the worst one is trying to move around mesh colliders especially non convex ones

Is there some definitive list that shows the most expensive to least? Even if its only an approximation.

I had multiple mesh colliders, but they were all convex. I replaced them all with a single box collider, but that was one of the first ‘optimizations’ I tried, so im not sure how much it played into the improvements. I will have to go back and use them again to see how significant the change was.

The problem is there is a lot going on in physics simulate, but I dont know if its because of

  1. Too many colliders
  2. Calling operations that require heavier computations
  3. Un-nessesary Collision events (next optimization I have in mind)
  4. Collision look ups (ie collides, so I start getting components from collision transform)

No.
But most of the regular functions are equally ‘cheap’ in cost.

Costly are really the higher level stuff like:

  1. Long raycasts especially when not with proper layer filtering
  2. Mesh colliders (even more when moved around)
  3. Cloth
  4. badly setup step counts and step time lengths - don’t overdo it with the physics framerate and the number of simulation runs per frame

or the ‘overdoing it’ stuff (at least for the limited performance of the target platform)

  1. stuff that builds on physical dependencies ie joint constructs and alike where a whole chain of equations has to be solved to update the state.
  2. too many basic colliders especially when not properly set to sleep and when not properly layered and layer excluded

whats not costly is applying a force, changing center of mass etc, they are free. They will only alter the next frames outcome and in case of force potentially yield new collisions or kicking an object out of sleep

Good to know.

Cheers dreamora

You can also take note that if you’re going to move an object that has a collider it must have a rigidbody component, if you don’t want to use it with physics just set “Kinematic” to true. Thats because PhysX looks at a simple collider as a static object that will never move, like a terrain, if you move it around there will be an update to the “static stuff matrix” which is expensive and wrong to change.

You can test this through the Profiller, there will be “Expensive static collider” warning if you do it.

heh paulo, while I appreciate your comment, we are way past that level of information :wink:

No problem, just to let others know, this kind of detail is missed by most developers, not everyone use physics extensively.

I’ve favorited this thread, such a nice source of info, maybe others add even more.