many rigidBodies performance issues

Post Reply
Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

many rigidBodies performance issues

Post by Salocin808 »

Hi!

First of all: I am new to this. But it would be amazing if guys could help me out.

I am creating a lot of cubes (>7000). Each frame an update method is called where a cube is created and a rigidBody is added to the cube. After around 200 cubes the bodies, rigidbodies stop being created. Also FPS is decreasing rapidly.

Is there a specific method on how to create multiple instances of rigidBodies?
create cube in physics world:

Code: Select all

btRigidBody* Physics::createCube(float edge_length, float px, float py, float pz, float mass)
{
	//ref:http://stackoverflow.com/questions/9117932/detecting-collisions-with-bullet

	btCollisionShape* colShape = new btBoxShape(btVector3(edge_length, edge_length, edge_length));

	return Physics::addCollisionShape(colShape, px, py, pz, mass);
}
User avatar
Typhontaur
Posts: 135
Joined: Fri Nov 04, 2005 2:22 pm

Re: many rigidBodies performance issues

Post by Typhontaur »

if the cubes are same and use same shape, you can use same shape for all cubes...without create new shape for new cube...

a possible pseudo code

Code: Select all

btCompoundShape *G_BTPhysics::GetBModelShape (int index)
{

	for (uint32 i = 0; i < bmodels.Count(); i++) // check if already exist
		if (bmodels[i].Index == index)
			return bmodels[i].Shape;

	BModel model;
	model.Index = index;
	model.Shape = new btCompoundShape();
...
...
Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

Re: many rigidBodies performance issues

Post by Salocin808 »

Typhontaur wrote:if the cubes are same and use same shape, you can use same shape for all cubes...without create new shape for new cube...

a possible pseudo code

Code: Select all

btCompoundShape *G_BTPhysics::GetBModelShape (int index)
{

	for (uint32 i = 0; i < bmodels.Count(); i++) // check if already exist
		if (bmodels[i].Index == index)
			return bmodels[i].Shape;

	BModel model;
	model.Index = index;
	model.Shape = new btCompoundShape();
...
...
unfortunately that does not change anything :/ I am calling addCollisionShape with the same btBoxShape now but the problems stay the same..
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: many rigidBodies performance issues

Post by drleviathan »

The cost of adding a new object to the world scales approximately by Order(N), but it was my understanding that it wasn't really a problem until you started to get up to around 10k objects. I certainly wouldn't expect the cost to be prohibitive at only 200, however you did not clarify if the problem was the cost of adding each new object or the cost of simulating all 200 (dynamic?). A single pile of 200 boxes should simulate at high FPS on modern hardware but I could imagine scenarios where they might not, such as (a) poor choice in minimum substep (b) old version of Bullet or (c) maybe the rendering is slowing down your frame-rate. You'd have to debug your code to figure out where all of the time is being spent.

It is possible to use the CProfileManager code to get a details measure of where time is spent in the Bullet simulation.
Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

Re: many rigidBodies performance issues

Post by Salocin808 »

drleviathan wrote:The cost of adding a new object to the world scales approximately by Order(N), but it was my understanding that it wasn't really a problem until you started to get up to around 10k objects.
Hi!

No the problems occur way earlier. It takes me around 10 seconds to get from 170 fps to 40 fps while running 1070 gtx graphics (turned off v-sync).
drleviathan wrote: I certainly wouldn't expect the cost to be prohibitive at only 200, however you did not clarify if the problem was the cost of adding each new object or the cost of simulating all 200 (dynamic?). A single pile of 200 boxes should simulate at high FPS on modern hardware but I could imagine scenarios where they might not, such as (a) poor choice in minimum substep (b) old version of Bullet or (c) maybe the rendering is slowing down your frame-rate.
The problem occurs when having many rigid bodies in my physics world. I tried instancing many cubes when starting the program and the FPS are constant. So the problem does not seem to be that I am dynamically adding new bodies to the physics world but the fact that the physics world contains many bodies.

ad (a): maybe you are right. I tried different substeps but don't really understand how to choose the best one tbh.
ad (b): no, newest version of bullet
ad (c): not possible since rendering the cubes without bodies doesn't influence the frame-rate

Thanks for the help!
Salocin808
Posts: 7
Joined: Thu Jun 15, 2017 9:32 am

Re: many rigidBodies performance issues

Post by Salocin808 »

drleviathan wrote:(c) maybe the rendering is slowing down your frame-rate.
Omg... that was actually it. I was using a debugdrawer for the physics world which was slowing down the frame-rate. Damn.. that took me ages to figure it out.

Thanks again.

Cheers
Post Reply