Page 1 of 1
Clear a Space
Posted: Mon Dec 12, 2011 12:00 am
by snoozbuster
So, I was wondering... What's the best way to go about clearing everything out of a Space? I have an extension method that looks like this:
Code: Select all
public static void Clear(this Space s)
{
while(s.Entities.Count > 0)
foreach(Entity e in s.Entities)
s.Remove(e);
}
The problem with this is that it doesn't remove things such as StaticMeshes, because such devices don't have Entities bound to them. How would one go about removing them? Why doesn't Space already have a function to clear everything out of it?
Re: Clear a Space
Posted: Mon Dec 12, 2011 12:26 am
by Norbo
If the Space instance must absolutely not be recreated, then you can maintain a list of every ISpaceObject and remove them from the Space. This isn't recommended though unless you absolutely must maintain the objects in an external pool.
Instead, simply constructing a new Space and disposing of the old one is preferred option. This involves dumping every reference to the space- every entity, collidable, and constraint of the old simulation gets tossed in the trash. This will cause garbage collections- but this type of thing is usually only done during loads where garbage really isn't a problem. This is what the BEPUphysicsDemos project does. Even on the Xbox360 simulation switching is quite speedy; when it's not speedy, it is usually because of something else like graphics-side vertex buffer construction (very noticeable on the 3375 floating cubes demo).
Why doesn't Space already have a function to clear everything out of it?
Because it's a fairly rare requirement as there exist alternatives like re-creation or controlled removals. Extraneous features are avoided when possible.
If it becomes an issue or if I get more requests for it, I can put it on the to-do list.
Re: Clear a Space
Posted: Mon Dec 12, 2011 1:30 am
by snoozbuster
Norbo wrote:
Why doesn't Space already have a function to clear everything out of it?
Because it's a fairly rare requirement as there exist alternatives like re-creation or controlled removals. Extraneous features are avoided when possible.
It seems like that would be used more than rarely. Either way, I just added a couple of extension methods that added things to a list, and then Clear() removed everything in that list from the space. I guess that could be useful if I needed something to be ignored by Clear(), cause I could just add it with the normal space.Add(), and it would be ignored by Clear(). It took, like... Four lines of code. No, six. Exactly six lines of code. One to declare the list, one to get a new list (the word I'm looking for is escaping me), one to add things to the list when they're added to the space, one to remove them from the list when they're removed from the space, and two to iterate and remove things from the list. Took an extra three to convert it to a dictionary, since it's a static list.
It would have taken more to track down all my references to the current Space (not to mention reset all the settings for the new Space). Seems like this is easier...
Re: Clear a Space
Posted: Mon Dec 12, 2011 1:52 am
by Norbo
In certain situations, it can be easier to do it that way. It depends on the setup. It can also be slower. If that doesn't matter, no problem.
As you've seen, it is trivial to do if needed. A slightly more optimized internal Clear function might show up at some point, but it's not a significant issue since it's so easy to handle in a way that fits a specific game's needs. That's probably why this is so rarely asked about, and that's why it's not in the engine already.

Re: Clear a Space
Posted: Mon Dec 12, 2011 5:06 am
by snoozbuster
Norbo wrote:It can also be slower.
How so?
Re: Clear a Space
Posted: Mon Dec 12, 2011 5:15 am
by Norbo
Each object removal must ensure that the subsequent state of the Space is still valid. A true "Clear" would not have to do so. Only the final state must be valid.
The difference in performance would probably be negligible for the vast majority of simulations. For those where it differs noticeably, it still probably wouldn't be enough to matter. In other words, unless tests actually show that it is a problem, I wouldn't worry about it.
Re: Clear a Space
Posted: Mon Dec 12, 2011 5:28 am
by snoozbuster
I see. Alright, thanks.