naturally I need a way to tell player from ground. This game will have a level editor based around placing blocks and chunks, so naturally I need to also know ground from ground.
because of this I can't just make an octree that checks if there is ground I need to also get the type of ground it is and in some cases the individual identity of that block.
But this is what has always puzzled me about octrees. How do I keep track of what is inside octree elements dynamically?
EDIT: Here is the octree system I have at the moment, the creation and destruction will be a bit harsh, but its only meant to be created and destroyed once per game so I think it should do. Please comment on it, I've not worked with octree's that much.
public class Octree
{
ushort MaxLevel;
OCtreeElement[] Elements;
BoundingBox Box;
public Octree(ushort MaxLevel, BoundingBox Box)
{
Elements = new OCtreeElement[8];
this.Box = Box;
ushort Level = 1;
Vector3 i = Box.Max - Box.Min;
i /= 2 * Level;
//0,0,0
Elements[0] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
//1,0,0
Elements[1] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
//0,1,0
Elements[2] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
//0,0,1
Elements[3] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
//1,1,0
Elements[4] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
//0,1,1
Elements[5] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
//1,0,1
Elements[6] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
//1,1,1
Elements[7] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
}
}
public class OCtreeElement
{
public ushort Level;
public BoundingBox Box;
public OCtreeElement[] Elements;
public bool isEnd;
public List<ulong> IDs; //ID's of the entity list
public OCtreeElement(ushort Level, ushort MaxLevel, bool isEnd, BoundingBox Box)
{
this.Level = Level;
this.isEnd = isEnd;
this.Box = Box;
if (isEnd)
{
IDs = new List<ulong>();
}
else
{
Elements = new OCtreeElement[8];
if (Level + 1 == MaxLevel)
{
Vector3 i = Box.Max - Box.Min;
i /= 2 * Level;
//same as before except isEnd is set true
Elements[0] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
Elements[1] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
Elements[2] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
Elements[3] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[4] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
Elements[5] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[6] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[7] = new OCtreeElement((ushort)(Level + 1), MaxLevel, true,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
}
else
{
Vector3 i = Box.Max - Box.Min;
i /= 2 * Level;
Elements[0] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
Elements[1] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z)));
Elements[2] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
Elements[3] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[4] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z)));
Elements[5] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[6] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
Elements[7] = new OCtreeElement((ushort)(Level + 1), MaxLevel, false,
new BoundingBox(new Vector3(Box.Min.X + i.X, Box.Min.Y + i.Y, Box.Min.Z + i.Z), new Vector3(Box.Min.X + i.X + i.X, Box.Min.Y + i.Y + i.Y, Box.Min.Z + i.Z + i.Z)));
}
} }
Any and all help is appreciated,
Thanks in advanced,
Bombshell