So I have a code like this one below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
void get_sum( INNER_ID id, vector<INNER_ID>& dont_check )
{
vector<INNER_ID> below = get_below( id );
vector<INNER_ID>::iterator second;
for( auto it = below.begin(); it != below.end(); ++ it ){
second = find( dont_check.begin(), dont_check.end(), *it );
if( second == dont_check.end() ){
dont_check.push_back( *it );
get_sum( *it, dont_check );
}
}
}
int main(){
vector<INNER_ID> return_;
get_sum( 0, return_ );
for( auto it = return_.begin(); it != return_.end(); ++ it ){
printf("%i\n", *it );
}
return 0;
}
|
I use this algorithm for my "crappy" physic engine, so the point of this algorithm is to get the sum of mass below an object.
get_below( id )
function can get the ids of what object is below them.
But before I need ids of the object below them to apply impulse, force, and some other physic stuff.
One object doesn't neccesarrly rest on top of one object, it can rest on 2 object or more.
when I look at it, it resemble a tree, maybe it's not. I just don't really know very much about tree algorithm
I cannot optimize a recursive code so I think, I better turn this into an iterative but I cannot seem to find a way to do that
can anyone help me ?
Thanks in advance for your reply....