+ else{
+ it->depth --;
+ it->i = 0;
+ }
+ }
+ else{
+ if( it->depth+1 >= vg_list_size(it->stack) ){
+ vg_error( "Maximum stack reached!\n" );
+ return 0;
+ }
+
+ it->stack[it->depth ].id = inode->il;
+ it->stack[it->depth+1].id = inode->ir;
+ it->depth ++;
+ it->i = 0;
+ }
+ }
+
+ return 0;
+}
+
+VG_STATIC int bh_closest_point( bh_tree *bh, v3f pos,
+ v3f closest, float max_dist )
+{
+ if( bh->node_count < 2 )
+ return -1;
+
+ max_dist = max_dist*max_dist;
+
+ int queue[ 128 ],
+ depth = 0,
+ best_item = -1;
+
+ queue[0] = 0;
+
+ while( depth >= 0 ){
+ bh_node *inode = &bh->nodes[ queue[depth] ];
+
+ v3f p1;
+ closest_point_aabb( pos, inode->bbx, p1 );
+
+ /* branch into node if its closer than current best */
+ float node_dist = v3_dist2( pos, p1 );
+ if( node_dist < max_dist ){
+ if( inode->count ){
+ for( int i=0; i<inode->count; i++ ){
+ v3f p2;
+ bh->system->item_closest( bh->user, inode->start+i, pos, p2 );
+
+ float item_dist = v3_dist2( pos, p2 );
+ if( item_dist < max_dist ){
+ max_dist = item_dist;
+ v3_copy( p2, closest );
+ best_item = inode->start+i;
+ }