I tried making a Quadtree for collission detection but it runs really slow.

Started by
7 comments, last by infectedbrain 11 years, 8 months ago
So after a few months of research and experimentation I made my first tree like data structure (its so cute). But when I put it into action in my game and rendered about 30 2d sprites it lags too much (down to 3 fps) but when I just check for collision against everything on the screen at once it never lags. I need some assistance to find out why.

I put the code on pastebin here.

If you know anything that would help that would be greatly appreciated.

Thanks in advance,
Ben

Edit: Fixed the link. I don't know why that happened.
Advertisement
Your link seems broken. I get unknown paste id.

But in the end, it just comes down to basic debugging and profiling practices. You need to figure out which portion of the code is taking the most cpu cycles. The problem could be anywhere, from the tree structuring to the traversals. I would also render each quad and make sure that it looks like it should. Move objects around and make sure that splits and merges are functioning correctly. Having a visual representation is very important. If that is okay, make sure your traversals are working as they should. Perhaps step through the code with a debugger and see if you are doing redundant things. A quadtree is a decently complex system. You need to break it down in smaller parts and make sure the behavior is as intended.

The problem could also lie in your game. Maybe it's using your quadtree in a bad way (such as traversing it a lot of times per frame).
Hi Infected.

Pastebin gives me "Unknown Paste ID!"
I fixed the link. sorry about that

The problem could also lie in your game. Maybe it's using your quadtree in a bad way (such as traversing it a lot of times per frame).


The only way I can get it to work is by traversing it everytime something moves.. whitch is about every frame. How else can i do that.
Now when I make a few hundred projectiles it lags. But it never stops lagging. Its strange because I made it so that when a projectile goes off the screen it is removed from memory. I tested that and it seems to work. I am making an asteroids clone if that helps.
I fixed that problem. Sorry for that
Looking through your implementation, a lot of it seems very weird. How are you using the code you pasted? How is the code like that creates your tree? How about the code that invokes the functions to dynamically update the tree? and how do you initiate the pairwise collision check state?

In particular the line 92 seems odd. What's going on there? Am I reading it correctly that you're testing all objects in your game level against the current node, then recursively split that node to four quadrants, and again test all objects against each node of the tree? Does line 42 iterate one index too little? Shouldn't it iterate until i < 4? Can you avoid having to use the bool cleanUp variable?

In particular the line 92 seems odd. What's going on there? Am I reading it correctly that you're testing all objects in your game level against the current node, then recursively split that node to four quadrants, and again test all objects against each node of the tree? Does line 42 iterate one index too little? Shouldn't it iterate until i < 4? Can you avoid having to use the bool cleanUp variable?


I made a change to that loop in line 92. I made it check for children then if there is no children check the actual quad. All of the iterations go through 0, 1, 2, 3 so they are all okay.

This topic is closed to new replies.

Advertisement