Slow physics start up when there are many objects

So I have a scene with around 250 Dynamic boxes sitting on static terrain. When I start the physics engine I get low FPS as all the objects settle and come to rest on the terrain then finally go to sleep. Once they are asleep everything is fine. I guess what Im trying to do is eliminate that initial slow down. What strategies have other used?

Is there a way to force everything to sleep? Only a few boxes will be moving at any one time.
Is there a way to poll the physics state so I can detect when settling has completed? As I could fade the scene in once that is complete.
If neither of those are possible I’ll need to start looking at tricks to reduce the number of dynamic objects at any one time.

Thanks

You can encourage rigid bodies to sleep by raising their sleeping thresholds.

To test for settling, I would invoke isActive() on each rigid body in the physics space.

Ill give those ideas a shot. Thank you.

1 Like

Btw, if you’re using Native Bullet (as opposed to JBullet) watch out for issue 911:
PhysicsRigidBody sleeping threshold setters have unexpected side effects · Issue #911 · jMonkeyEngine/jmonkeyengine · GitHub

If using NativeBullet with JME 3.2.1 or earlier, an easy workaround would be to set both sleeping thresholds together. Rather than:

body.setLinearSleepingThreshold(lin);
body.setAngularSleepingThreshold(ang);

use

body.setSleepingThresholds(lin, ang);

are you running your physics engine in its own thread? because you should be…

1 Like

yeah, setting the threading mode to parallel should help but it’s a high load until they’ve settled anyway. you can also tune the accuracy and the step size to adjust performance but probably you’re looking for a way to detect when everything has settled

1 Like

I wasn’t using the parallel thread mode so thanks for that suggestion. I was playing around with leaving everything static until some action is taken then switching them to dynamic. That should work also right?

1 Like

I haven’t had initial slowdown with physics, but most of my projects take a good 5-20 seconds to start running stably, lots of geometry loading and generating, light probe rendering… There is no easy way around this, optimizing loading routines takes way to long, so I just dont ‘start’ the game until its stable, and hide it behind a loading screen.

Staggered loading can help in physics, loading in boxes in small groups over several frames, to average out slow down

It may be possible to take a snap shot of the position states for all boxes once they have settled, and use that for their inital positions for future loads, this may reduce the time it takes for them to “settle”.

I dont know about polling the physics space, you could check say 10 random boxes every frame, mark if they are asleep, continue untill all are marked asleep ?

Keep in mind this slow down might be on your machine only, it may be a pile of crap, so judging slowdown by using number of asleep objects may not be accurate, it may be better to check the average frame rate, wait until that hits your target fps and is stable, BAM run your game and profit.

1 Like