Changing a body from dynamic to static

In order to assist Googler's, I have separated my question into two topics. I hope you guys don't mind.

Is it possible to change a body from dynamic to static without needing to recreate the new body?


I'm running into a MASSIVE performance hit in my world. I have an open box which slowly fills up with circles, and after about 30 circles it takes a sudden massive nosedive in performance. I'm not sure if it's something I did wrong, or if it's the circles on the bottom (which barely move, but still have a massive amount of pressure on them from the circles on top and therefore may be running lots of calculations). So I'm trying to get the circles on the bottom to turn static to see if it helps performance.
Are you using the bullet flag on anything? This can slow things down.
Erin Catto wrote:
Are you using the bullet flag on anything? This can slow things down.

Yes, at first I was experiencing several frames where some circles would overlap a bit for a frame (which was clearly visible and doesn't look too good for the purpose I'm using them for) until I made all circles into bullets.

Is there any way to remove the overlaps each frame without making the circles into bullets?
Odd. I tried turning the bullet flag off and it's a bit better performance. (though, I still get that "overlap" of circles)

But after a while, it still completely freezes and goes into the 15 second timeout after the stage has filled up with only 25 spheres ranging from 0.83 meters to 1.78 meters in radius. The odd thing is that right before the freeze, the calculation times are at about 170ms, before it jumps to more than 15 seconds.


All of my "personal" code is stripped out, so the only thing affecting any of the bodies are box2d. Perhaps some of my variables are set a little wonky? My code is in AS3. Would it help if I provide the code or the SWF? Would this question be better asked in the Box2DFlash section?


But if possible, I would like to attempt to make the circles static to see if the problem fixes itself. I will gladly write the code for this manually, but I'm just checking if it already exists.
I'm not seeing this sort of slowdown in C++. Not even close.
You could probably get the same effect as a static body by setting the mass and inertia to zero, not sure if there would be any other side effects though.
I'm not sure about AS3, but in C++ you can set the body type to static.
There are still a few bugs in the AS3 port, particularly related to continuous physics and the way it interacts with the solver, which cause trouble even when bodies are not bullets (probably caused by the fact that CCD is always active for non-bullets against static objects). It may be that its going into an infinite loop at some point.

If you turn continuous physics off (the world object has a method), do you still have the same problem?

Either way, posting your code couldn't hurt - if it is related to the CCD problem, I'd love to see it, I'm trying to track that one down.
ewjordan wrote:
If you turn continuous physics off (the world object has a method), do you still have the same problem?

Ha! Wonderful! That fixed it.

And with this new performance increase, once I cranked it up to 30FPS (1/30 step) the "overlapping" pretty much went away (I originally had it at 10FPS with 1/10 step). The little that remains isn't even noticeable. I can now shrink down all the circles to between 0.5 and 1.0 meters for a very nice effect (http://bit.ly/bO2IW0).

ewjordan wrote:
Either way, posting your code couldn't hurt - if it is related to the CCD problem, I'd love to see it, I'm trying to track that one down.

I have attached the code that was causing the original problems. I apologize that the code is a bit messy and thrown together. It was actually a test project which I just hadn't planned out ahead of time, but just added on items and kept messing around with it for different effects. It wasn't really made for public release either. 😛

All the box2d code is in the "Box2DCircles3" class. The major settings were summed up as constants in lines 59 to 68, but let me know if you want me to elaborate on any other part of the code. Or even if you have any opinions on how to optimize it a bit, I would love to hear those as well.

It seems like it likes to freeze up when the circles are busy "cramming" together so much that they overlap, and can't find enough room to ever move out of their stuck positions. I received the "worst" results with a framerate of 10, the max radius of the circles at 50 pixels, a "sideBorderHeight" and "relativeFillArea" of 2.0 (which are both multipliers, not values), and useContinuousPhysics set to false. If it doesn't hit the timeout immediately, refresh it and it will eventually freeze.


Let me know if you want me to strip out the rest of my personal code, though setting the right "flags" should disable those parts anyway.
That (http://bit.ly/bO2IW0) is an awesome effect, fantastic idea!

I'll take a look through your code at some point soon; it's nice to have independent confirmation of the bug, the codebase I discovered it in was complex enough that I couldn't really be sure it wasn't something else going on. I'm still not sure where it's coming from in the engine, but I'm not completely convinced that it's only in the CCD system, I think there might actually be a little something wrong with the stacking behavior even when that's turned off.

Will let you know what I figure out.

Original thread: https://box2d.org/forum/viewtopic.php?t=5719