Page 1 of 1

many rigidBodies performance issues

Posted: Thu Jun 15, 2017 9:46 am
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);
}

Re: many rigidBodies performance issues

Posted: Thu Jun 15, 2017 11:14 am
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();
...
...

Re: many rigidBodies performance issues

Posted: Thu Jun 15, 2017 11:43 am
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..

Re: many rigidBodies performance issues

Posted: Sat Jun 17, 2017 1:23 am
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.

Re: many rigidBodies performance issues

Posted: Sat Jun 17, 2017 11:43 am
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!

Re: many rigidBodies performance issues

Posted: Sat Jun 17, 2017 11:55 am
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