Sign in to follow this  

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

This topic is 1955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 [url="http://pastebin.com/tjjnhwtU"]here[/url].

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. Edited by infectedbrain

Share this post


Link to post
Share on other sites
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). Edited by french_hustler

Share this post


Link to post
Share on other sites
[quote name='french_hustler' timestamp='1344287209' post='4966812']
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).
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='clb' timestamp='1344296074' post='4966854']
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?
[/quote]

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.

Share this post


Link to post
Share on other sites

This topic is 1955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this