timohausmann / quadtree-js

A lightweight quadtree implementation for javascript

Home Page:https://timohausmann.github.io/quadtree-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

It just doesn't work?

bobbyware opened this issue · comments

I add random tiles of width 64,64 to my entire window and after adding 1 and doing a collision check at the other side of the window, I still get the first object returned.

Yes... somehow all available JS implementations of quad trees are broken... unfortunately the authors don't even answer, when you ask them about it. In the meantime I wrote my own quad tree implementation that fits my requirements. Depending on your needs you might use it as well, but it needs some testing and it only handles power-of-2 dimensions and a single item per sub division (I just don't need more features).

Hey, sorry for the late response, I didn't see any notification.
This implementation is not broken, it's rather likely that our understanding of what a quadtree should do differs. If you could provide any example code I'm glad to help.

Like you can see in the demo, when your cursor is on the very left side, the retrieve function still returns objects all over the canvas in white (the ones crossing certain borders). This is intended behavior since we can't exactly tell the objects boundaries, but just narrow down the objects of possible collisions. Based on a simple fantastic algorithm ;-)

Hi,
sorry but I don't have any code left since then. The QT implementation works for point-dimensional items and with point-dimensional items it works according to my understanding. When I begin to add non-point-dimensional items (quads) the items returned by the retrieve function seems to be totally random. I tested it with a scenario something like that:
A grid of 4000 x 4000 Tiles managed by the QT. Then I insert 16 Tiles with dimension (w x h) 1000 x 1000, so you have 4 tiles per row and 4 rows. The first row looks like this: Tile 1 starts at 0/0 and ends hat 1000 / 1000, Tile 2 starts at 1000 / 0 and ends at 2000 / 1000 etc. When I query for all tiles inside a rectangle of 500/500 (x, y) and 1000 x 1000 (w, h) I expect to get the 4 tiles in the upper left corner. But the retrieve function returned 13 of 16 tiles from everywhere in the grid.

I spent some time with debugging and found out that only the first tile was correctly placed in a leaf node. All other tiles were placed in the data-container of the root node as far as I can remember, so I gave up to find out, why the retrieve function returned that weird set of tiles. The exact behavior of the QT is described in the other Issue I posted some time ago.

I also did some testing with points (just placed points at the center of the tiles described above) and with points the implementation worked as I expected it to work.