+ bh_node *inode = &bh->nodes[ it->stack[it->depth].id ];
+
+ /* Only process overlapping nodes */
+ if( !box_overlap( inode->bbx, box ) )
+ {
+ it->depth --;
+ continue;
+ }
+
+ if( inode->count )
+ {
+ if( it->i < inode->count )
+ {
+ *em = inode->start+it->i;
+ it->i ++;
+ return 1;
+ }
+ 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 )