make the quad tree max detail level dynamic

This commit is contained in:
James Seibel
2023-03-25 11:44:47 -05:00
parent a465a8da9d
commit eec8db6223
2 changed files with 10 additions and 1 deletions
@@ -45,4 +45,11 @@ public class MathUtil
public static long pow2(long x) { return x * x; }
/** Equivalent to Log_2(numb) */
public static int log2(int numb)
{
// properties of logs allow us to use the base Log_e() method
return (int)(Math.log(numb) / Math.log(2));
}
}
@@ -8,6 +8,7 @@ import com.seibel.lod.core.pos.Pos2D;
import com.seibel.lod.core.util.BitShiftUtil;
import com.seibel.lod.core.util.DetailDistanceUtil;
import com.seibel.lod.core.util.LodUtil;
import com.seibel.lod.core.util.MathUtil;
import com.seibel.lod.core.util.gridList.MovableGridRingList;
import org.apache.logging.log4j.Logger;
@@ -48,8 +49,9 @@ public class QuadTree<T>
this.centerBlockPos = centerBlockPos;
this.widthInBlocks = widthInBlocks;
this.treeMaxDetailLevel = 10; // TODO in the future we may need to make this dynamic // detail 10 = (2^10) 1024 blocks wide
this.treeMinDetailLevel = treeMinDetailLevel;
// the max detail level must be greater than 0 (to prevent divide by 0 errors) and greater than the minimum detail level
this.treeMaxDetailLevel = (byte) Math.max(Math.max(1, this.treeMinDetailLevel), MathUtil.log2(widthInBlocks));
int halfSizeInRootNodes = Math.floorDiv(this.widthInBlocks, 2) / BitShiftUtil.powerOfTwo(this.treeMaxDetailLevel);
halfSizeInRootNodes = halfSizeInRootNodes + 1; // always add 1 so nodes will always have a parent, even if the tree's center is offset from the root node grid