Optimise physics

How can I optimise physics so that it might be a little more blunt but more performant?

I’m creating a voxel-engine and exploding parts are small physics blocks. But on my macbook pro (2x3.2GHz, 16GB RAM) I can only create like 800 blocks before the frame-rate drops heavily.

This is how I do it…

I’m creating a pool of blocks like this:

node = context->GetSubsystem<Game>()->scene_->CreateChild("Box");
node->SetPosition(Vector3((float)(rand()%1000+1000), (float)(rand()%1000)+1000, (float)(rand()%1000+1000)));
model = node->CreateComponent<StaticModel>();

shape = node->CreateComponent<CollisionShape>();

body = node->CreateComponent<RigidBody>();
free = true;

Then I initiate a new block from the pool like this:

    SharedPtr<Material> m = model->GetMaterial()->Clone();
    m->SetShaderParameter("MatDiffColor",Vector4(color.x_/255.0f, color.y_/255.0f, color.z_/255.0f, 0.2f));
    free = false;

Any suggestions how to increase performance so that I might add more than just 800 blocks (in this case).

Thanks in advance!

If you want this blocks all fly around simultaneously, collide with static geometry and each other. 800 seems like a reasonable ceiling.

Now things you can try:

  1. Using collision layers you can set up it so they will collide with static geometry and not eachother.

  2. Use sphere collison shape. It could be faster.

  3. Make it so only small ammount of all dynamic objects in the scene are active, and the rest are sleeping. I belive you can tweak sleeping parameters in bullet physics, making them more greedy.

1 Like

Thanks for your suggestions, I will try them out :slight_smile:

Physics performance highly depends on number of simultaneoutly collided objects.
So, you keep thousands of objects somewhere, but if you make even ~1k objects activated at the moment, you will lag. Sample 12 called “PhysicsStress” for a reason.

@Eugene yepp, I get that, but I just wanted to make sure that I didn’t do something wrong that made my phys object limit low. And also to get some tips&trix to optimise :slight_smile: I think layered approach would be good in my case.