I have a procedurally generated terrain system that is constantly adding and removing chunks from Bullet's dynamics world. This happens as the chunks become relevant to the game logic (and are added to the physics world) or become irrelevant (and are removed from the physics world). Adding the terrain chunks back in causes severe lag. Is there a way to add and remove objects from the dynamics world efficiently?
The terrain chunks are stored in btBvhTriangleMeshShape(s) and are added/removed like such:
dynamicsWorld->removeRigidBody(chunk);
dynamicsWorld->addRigidBody(chunk); // severe lag
Any advice is greatly appreciated
Efficiently add/remove objects to/from the dynamics world
-
- Posts: 9
- Joined: Mon Mar 14, 2016 3:46 pm
-
- Posts: 350
- Joined: Sat Jul 04, 2015 10:33 am
- Location: Bern, Switzerland
Re: Efficiently add/remove objects to/from the dynamics worl
Hi,
First of all, have you considered to use btheightfieldterrainshape? It could serve as a single chunk and is probably very suited for terrains Have you had any experiences with it?
The issue you are experiencing is strange indeed. The way you are adding and removing chunks is the way you should do it.
I intend to make a terrain example for bullet physics at some point with automatically loading and unloading terrain chunks (terrain paging, basically what you are doing here). Therefore I would love to help you discuss the issues if you would be interested to share some code with me for the example.
Can you build a simple version running in the example browser that shows your issue so that we can debug into it? Does it boil down to add, remove and readd the trianglemeshshape or does the issue go further?
First of all, have you considered to use btheightfieldterrainshape? It could serve as a single chunk and is probably very suited for terrains Have you had any experiences with it?
The issue you are experiencing is strange indeed. The way you are adding and removing chunks is the way you should do it.
I intend to make a terrain example for bullet physics at some point with automatically loading and unloading terrain chunks (terrain paging, basically what you are doing here). Therefore I would love to help you discuss the issues if you would be interested to share some code with me for the example.
Can you build a simple version running in the example browser that shows your issue so that we can debug into it? Does it boil down to add, remove and readd the trianglemeshshape or does the issue go further?
-
- Posts: 9
- Joined: Mon Mar 14, 2016 3:46 pm
Re: Efficiently add/remove objects to/from the dynamics worl
Hello!benelot wrote:Hi,
Unfortunately a heightfield is not suitable for a voxel terrain system such as this: http://http.developer.nvidia.com/GPUGem ... 1fig01.jpgbenelot wrote:First of all, have you considered to use btheightfieldterrainshape? It could serve as a single chunk and is probably very suited for terrains Have you had any experiences with it?
Indeed. I have tried scaling the size of the chunks (to make fewer but larger chunks) and that does speed things up, but it is nowhere near the speed that it should be and rebuilding large chunks on my end is not efficient in the least.benelot wrote:The issue you are experiencing is strange indeed. The way you are adding and removing chunks is the way you should do it.
I should also mention that what I am doing is a bit more complicated than what I described in the OP. This is a multiplayer game that requires determinacy, and the only way to achieve that with Bullet Physics is -apparently- to reset bullet physics each cycle (you can read about that adventure in this thread).benelot wrote:Does it boil down to add, remove and readd the trianglemeshshape or does the issue go further?
So, the objects are collected out of the old bullet physics state, that state is destroyed by calling the destructors and the memory is reset with memset, and the contructors are called and all the rigid bodies are added back in. That's what's really going on. I wish there were a better way but that's the only way to get around the fact that bullet can produce two radically different simulations despite being given the same exact input parameters and being on the same computer using a fixed timestep.
It handles this fine with large numbers of primitives (i.e. cubes, spheres etc) but with the triangle meshes it slows down to being unusable. It would seem that there is some information not being preserved in the transfer from the old state to the new state, that then has to be recalculated. Is there a way to preserve this information? If not, is there a way to otherwise speed up the process?
I imagine it would be fairly straightforward to provoke by instancing some large meshes and adding/removing them each cycle.benelot wrote:Can you build a simple version running in the example browser that shows your issue so that we can debug into it?
-
- Posts: 96
- Joined: Wed Mar 16, 2016 10:11 am
Re: Efficiently add/remove objects to/from the dynamics worl
I am also facing that same problem as OP, at the exact same lines of code.
In my case, "chunk" is a compound body - compose of many child shapes.
If I want to add/remove a single child shape from the compound body, or change collision mask, I will have to remove and add the whole body back.
It is also performance bottleneck of my program now.
My best solution is to limit size/complexity of the "chunk", but it is not practical.
In my case, "chunk" is a compound body - compose of many child shapes.
If I want to add/remove a single child shape from the compound body, or change collision mask, I will have to remove and add the whole body back.
It is also performance bottleneck of my program now.
My best solution is to limit size/complexity of the "chunk", but it is not practical.
-
- Posts: 350
- Joined: Sat Jul 04, 2015 10:33 am
- Location: Bern, Switzerland
Re: Efficiently add/remove objects to/from the dynamics worl
Could be fairly simple to provoke..so would you be interested in us looking into it by providing an example that works?
You can simple override the BasicExample of the Bullet Example Browser. Then just post the code files that cause the issue. It is usually much easier to discuss things if everyone helping you can just download the source and find the cause for the issue. Especially if it is reproducible every time.
You can simple override the BasicExample of the Bullet Example Browser. Then just post the code files that cause the issue. It is usually much easier to discuss things if everyone helping you can just download the source and find the cause for the issue. Especially if it is reproducible every time.
-
- Posts: 96
- Joined: Wed Mar 16, 2016 10:11 am
Re: Efficiently add/remove objects to/from the dynamics worl
Sorry, I encapsulated Bullet and execute most of Bullet's operation via it.
Thus, it is not trivial to create a test case.
Remove/Re-add is very expensive but not as much as I thought, though.
Thus, it is not trivial to create a test case.
Remove/Re-add is very expensive but not as much as I thought, though.
-
- Posts: 4
- Joined: Thu Aug 25, 2016 3:50 am
Re: Efficiently add/remove objects to/from the dynamics worl
I'm working on a voxel game myself. I am using a triangle mesh rigid body instead of a compound body. Is the compound body more efficient?hyyou wrote:I am also facing that same problem as OP, at the exact same lines of code.
In my case, "chunk" is a compound body - compose of many child shapes.
If I want to add/remove a single child shape from the compound body, or change collision mask, I will have to remove and add the whole body back.
It is also performance bottleneck of my program now.
My best solution is to limit size/complexity of the "chunk", but it is not practical.
-
- Posts: 96
- Joined: Wed Mar 16, 2016 10:11 am
Re: Efficiently add/remove objects to/from the dynamics worl
The official somewhere stated that - try to use primitive if possible ; it is cheaper than triangle mesh.
I don't know if it is still applicable for this case.
I also doubt which one is faster.
I don't know if it is still applicable for this case.
You use mesh, so you have to modify mesh's shape if terrain is modified, right?I am using a triangle mesh rigid body instead of a compound body.
I also doubt which one is faster.