Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Quadtree problems - I have a nice quadtree set up... but don't know how to use it.

Author
Message
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th May 2010 08:49
I have a nice little quadtree set up. I can add and remove nodes, and objects, and "clean up" a node (remove all unnecessary subnodes). Basically, I have all the basic functions... except for being able to find the closest object, or n closest objects, etc. which is the whole point of creating a quadtree >.>

(a bit off-topic, but my computer crashed, and I have a half-working quadtree now, but I can rewrite the code)

So... What's a good algorithm for finding the n closest objects to an object using a quadtree data structure? The only thing I can think of is to have like eight different functions for finding the node above, below, to the left of the object, etc. but that would hardly be useful.


TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 12th May 2010 09:48
Isn't searching for a node Just like searching for a file? You will have to scan from the defined node through all combinations until you find the sub-node. If you scan upwards, you only ever have one possibility.

TheComet

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 14th May 2010 01:50
I'm still working on getting my source code back to where my computer was before I crashed (I was in one of those zones, so it was good code that worked first time around), and I'll post something up once I have everything done.

I'm not exactly sure what you're saying TheComet... but I'm using quadtrees with the goal to avoid doing anything involving processing all possible combinations.


Kira Vakaan
17
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 14th May 2010 02:54
Hmm, I'm not sure a tree structure is what you want for finding the n closest objects to an object. I think you'd be much better off just calculating the squared distances from your main object to each other object and using an efficient sorting algorithm to find the n closest. The tree would only be useful if you had a distance beyond which you could ignore all outside objects. You could do both. Use the tree to determine which objects are inside a certain radius or defined shape, calculate the distances, and then use a sort to grab the n closest.

Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 14th May 2010 03:19
Yes I agree with Kira Vakaan, you can sort and search in n log n time which is definitely quick enough. You can probably modify a sorting algorithm to tell you the closest objects. My favourite is a combination of quick sort and selection sort.

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 16th May 2010 12:55
Ah, I see.

Anyways, I'm not *really* looking for something to find the n closest neighbors, I just wanted to do that to test out my quad tree. I'm not even sure how to get a couple adjacent neighbors :\

Also, have the quadtree thing up and running for the most part. Code got all muddled, so i just scrapped it and restarted.
code:


Runs pretty well, but I was stuck for a while on a stupid mistake I made where the width of everything was negative, giving really weird patterns where I added nodes (one was actually a diagonal sierpinski triangle... no clue why/how X_X). The error I have now is that the cleanNode function sometimes deletes objects. I guess that when cleanNode calls itself, there can be a node that has subnodes that have subnodes etc. that has an object (instead of four terminating subnodes), and then that gets deleted. Every time you right click, and a "0" pops up, an object dies
It's 3 am though so I'll get it done tomorrow XD


Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 17th May 2010 04:04
AAahahah cool I did it. the last problem I was having was that the "break" command didn't do what it does in other languages, and so cleanNode only worked as expected when the object was in the bottom right node (last on the list), any other object would be overwritten (not breaking from the loop when there's a nonzero object number). Also, the absolute function didn't do what I thought it should. Anyways, new, awesome code: I get about 60 fps with 4000 objects moving, being updated, being recounted, and their sub nodes being cleaned behind them, etc.
Here's the code:


but my problem still remains: I'm not really sure how to check adjacent nodes up to a certain distance for objects...


Login to post a reply

Server time is: 2026-07-25 23:11:52
Your offset time is: 2026-07-25 23:11:52