Author Topic: What's more efficient in terms of brick searching?  (Read 836 times)

I want to see if a brick is close to another but this brick doesn't know the position of the other bricks. I have two options. Container search and looking through every brick in the brickgroup and testing the distance manually.

Which would be more efficient for a rather large amount of bricks? Also, I'm trying to do the boxsearch but it just detects the brick that's calling it (i.e. using the brick who's position is the same as the container's search center)

Code: [Select]
%rad = getBiggestSize(%brick)*2; //forget this for now, return 8
initContainerBoxSearch(%brick.getPosition(),"\"" @ %rad SPC %rad SPC %rad @ "\"",$TypeMasks::FxBrickObjectType);
while(%searchObject = containerSearchNext()) {
echo(%searchObject);

and %brick.getID() == %searchObject.getID().

So basically, you want to check whether there are other bricks in a small radius around a brick?

That sounds like a job for a container radius search, just skip the brick itself and use containerSearchCurrDist() (or similar) to check the distance.

A container or box search is definitely more efficient than cycling through thousands of bricks.
I think the problem you're having is due to the funky way you're defining the measurements of the search. Here's what I think is happening. You're making it into "%rad %rad %rad" and manually inserting quotations into it, making it a string. Strings in TorqueScript are evaluated as 0.
Change it to just %rad SPC %rad SPC %rad

You should also change while(%searchObject = containerSearchNext()) to while(isObject(%searchObject = containerSearchNext())). It's better practice.

Zeblote posted while I was typing this.

Yes, that fixed it. Although I switched from box to radius because it felt more suited for the job.

Since there's an initContainerRadiusSearch, is there a delete function?

initContainerRadiusSearch will actually run a full search and setup an internal buffer with all results, then qsort it by distance from center, then set the current object to the start of that buffer.

containerSearchNext returns the current object (or 0 if it's a nullptr) and then sets it to the next one in the buffer.


There's nothing you can delete from the ts side. It just overwrites the same buffer the next time something else runs a container search.
« Last Edit: March 24, 2016, 11:30:48 PM by Zeblote »