If you have a big map you can scale it to 0-256 (just arbitrary values, but it helps if they are in power of 2. Like 256 is representated in binary as
11111111
8 bits
http://www.vbforums.com/attachment.p...postid=1663675
Now the red thing on the drawing is an object. If we look at the X possition of the object the start of the object are in about:
X1 = 155,4
and the right side of it is about
X2 = 166.1
and lets say that the top is:
Y1 = 140.1
and bottom is:
Y2 = 150.2
Now since the map is 255 wide, we can use some nice XORing to find out in what level it is. If you look at the drawing you can see that the big black square is a representation of the whole map. We devide it in 4 new sqares that becomes level one in our quad tree
Old picture:
http://www.vbforums.com/
Then those sqares are again devided into 4 sqares that becomes level 2. And so on. We have 9 levels like that. I have only drawn drawn 4 levels, included the root level that is the whole square. You can see that it gets hit on the midle of level 4 in the drawing. So it belongs to level 3. That is included the root level. So we find it in code like this:
We XOR the integer values of the X coordinates of the object we get:
X1: 155=10011011
X2: 166=10100110
------------------------
XOR Res: 00111101
The first "1" bit is in posistion 6. So if we have 9 levels we can just take 9-6 and we get 3.
So we know it is in level three so we wil insert it there...
Look here....it's the green dot.
http://vbforums.com/attachment.php?s=&postid=1663660
But I have just inserted it in a node...but I now only know what level it is supposed to go in...Again, the power-of two dimensionality of our tree makes this a simple task. If all sectors of a given tree level are stored in a 2 dimensional array, the proper section can be found by taking the coordinates of the object being tested and converting them to the scale of the node grid at the tree level. Tree level 2 at the drawing contains 2X2 grid of nodes. Therefor, the coordinates of our object have to be scaled to into the range [0,1] on each axis to find the column and row.
A simple shift operation does the trick. Coordinates of our level are in the range [0,63] (Remeber the whole thing was [0,255])., 6 bits. To get them to range [0,1], we need to convert them to 1-bit values. Shifting the coordinate values to the right by 5 places will perform the conversion (8-bit value to 1-bit value is a right shift of 7). For our object, we take the coordinates (155,140) that is actually (27,12) on this level and shift them down 5 places....
what is the answer of this again.....ehhhh....[0,0]. And that is the up left sqare in the picture in level 3 (included the root)
Hope I didn't screw up the last part, I am not 100% sure of this...but it looks like it worked....tell me if you did fall off somewhere so I can explain that bit a bit better....
And remember that you should XOR the Y coordinates too, to see if the object maybe have to ho up an other level or two....
