Disable invisible bodies - m_proxyCount-- and performance++

Sometimes happens it is necessary to disable (disable collision, disable move, disable joint etc.) temporarily any bodies.

b2Body::PutSleep does not approach for this purpose since bodies continue to participate in collision-detection.
One of decisions - time removal such bodies. But a reconstruction of a body is very difficult because of necessity restoration its all parameters.

For the decision of this problem I have added to a body methods b2Body::Freeze() and b2Body::UnFreeze().
b2Body::Freeze(): temporary delete this body and bodies connected to it by joints.
b2Body::Freeze(): restore body and bodies connected to it by joints.

I did platformer-style game and have faced a problem "b2Assert (m_proxyCount <b2_maxProxies) ". In game the big world, but only a small part of the world it is visible during each moment of time.
For the decision of this problem I disconnect objects which are not crossed with the active area (display screen).
For this purpose I have made the new controller - b2PVSController. It call method b2Body::Freeze to the objects which are being behind borders of active area (display screen). For detection overlap it uses new functionality of b2Body - b2Body::GetAABB(). But it is possible to use other ways - center of body or all body shapes aabb.

I hope you find this useful, and that you might consider adding it to a future release.

Attachment:
File comment: patch
FreezeAndPVS.patch [29.16 KiB]
Downloaded 102 times
All changes based on revision 188 of repository.

Sorry for my bad english.
Attachment:
File comment: builded example (testBed)
BuildedExample.rar [250.03 KiB]
Downloaded 81 times
Awesome, just what I need!
This is some brilliant code. I'd never imagined that sort of use of controllers, but it works well. Anyone else think this should be added to the SVN?

A few suggestions that I think would make this even more generally useful, what are your thoughts?
  • Why the distinction between user frozen and boundary frozen bodies?
  • b2World could maintain a separate list of frozen and unfrozen bodies, so frozen ones literally do not impact the Step calculations at all, just eat memory.
  • Using this will be hard for a lot of people, as it interrupts the event cycle (i.e. you might get an Add without a matching Remove). Would it be appropriate to send Remove events?
  • Is it safe to freeze other bodies recursively? This should really become a BFS so we don't blow out the stack on large examples.
  • Your FreezeListener seems like an interface that could be generalized for use with controllers, allowing controllers to have dynamically changing body sets - I shall think on this.
  • b2PVSController should be set up to encourage inheritance, for people who want different sets of bodies to be potentially visible, but otherwise want the same behaviour as that controller.
  • b2DebugDraw could do with a flag to disable drawing frozen objects, or else it's impossible to use b2DebugDraw in a game where you are usually having to only draw the PV bodies.
This sounds really useful.
thank you all for your attention to this topic

BorisTheBrave wrote:
Why the distinction between user frozen and boundary frozen bodies?

If user call a method Unfreeze () for an object outside world, we get assert. Unfreeze() will call by b2PVSController(), if the active region will go beyond the world. Thus, the initial freeze in Box2D - an irreversible operation. Perhaps her name is not very good - probably the best option would be addressed isDead() (instead standart name IsFrozen()). And "e_deadFlag" instead "e_frozenFlag".
BorisTheBrave wrote:
b2World could maintain a separate list of frozen and unfrozen bodies, so frozen ones literally do not impact the Step calculations at all, just eat memory.
b2DebugDraw could do with a flag to disable drawing frozen objects, or else it's impossible to use b2DebugDraw in a game where you are usually having to only draw the PV bodies.

I think it's a good ideas.

BorisTheBrave wrote:
Using this will be hard for a lot of people, as it interrupts the event cycle (i.e. you might get an Add without a matching Remove). Would it be appropriate to send Remove events?

You're talking about contact callback Add / Remove? For example, call Freeze() for sensor, it will always operate Remove, and game logic would be to think that the all objects left the sensor? But really objects still in sensor? I think this behavior will cause an even greater number of errors.
great, ive been trying to find a good way to do this. i want to have vehicles/buildings in a game which when entered by an actor will disable that actor and just draw their head inside with all their normal physics disabled.
Hi

This features is a top one !

I'm currently using Box2d from a BlitzMax Port.

In order to be ported this feature shall be available in the SVN and somewhat "official" (like b2EdgeChain for example).

How far this would be "compatible" with the current SVN version of Box2D.
Does these controllers easily maintainable for the future version of Box2D ?
Is this feature safe ?

Unfortunately I can't answer to these questions myself since C++ isn't my chosen language.
Hope you understand.

Thanks in advance
I need this functionality (just the freeze/unfreeze really) but I cannot apply this patch to the current revision (220). TortoiseSVN isn't very clear, but I think it's because of the change to a Branch/Tag/Trunk repository layout AFTER revision 188 so it can't apply this patch.

If anyone knows how I can apply this patch can you let me know? (Just to save me manually going through the changes and implementing them)
Or maybe an updated .patch file would be just as good...
I have re-created the Freeze/Unfreeze [ByUser] functionality for the latest SVN revision (220)

This includes the restoration of seperate CreateProxy and DestroyProxy functions in b2Fixture which were removed when b2Fixture went in. All the same functionality that is in RefilterProxy so even if it's discouraged (I presume from it's removal) at least it does nothing new.

I haven't found any problems with using it.

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