I followed this tutorial when creating a QuadTree class and made a few modifications based on the feedback on the tutorial. Everything works as expected until I start clearing and inserting entities, as indicated in the tutorial, each frame. Before clearing and inserting, there are multiple quadrants and only 7 entities are marked for collision detection. However, after clearing and inserting, there is only one quadrant and all 33 entities are marked for collision detection. I would post a video, but it seems redundant as all I said is happening in the video.

Well, I spent quite amount of hours trying some things out and after QuadTree failed to work, I simply decided to post on the forums since I was very frustrated. I have realized the problem was in the constants MAX_OBJECTS and MAX_LEVELS. Since the values were too low, it resulted in only one quadrant and all objects on it. I just changed MAX_OBJECTS to 4 and MAX_LEVELS to 1000 and now it works.

Scratch that. Now some entities aren't even recognized for collision detection. Oh, maaaan!!!! And after some time, it glitches out and marks all entities for collision detection no matter where I go.

Are there any obvious mistakes in the code? If not, I will post more details (including a video) soon.

This will never yield true, unless r.getHeight() returns a negative number ofcourse.

I think this code (which was mainly copied from the tutorial) is designed for a Y-down system. The first clause isn't even needed because all you're really checking for is if the body is above the midpoint.

I did some tweaking on the code and nothing seems to completely fix it. I'm confused about the implementation and don't even know how to properly write getIndex method. I'm so confused. I understand how broken the getIndex method is and I'm so confused by the horizontalMidpoint, verticalMidpoint and all of the checkings performed. If I used contains method in Rectangle class, would it be more expensive?

I forgot to add that I'm using LibGDX. As I looked into the code you linked, I see you're using LibGDX, too. The problem I face is the fact we're both using the same library and that it works for you, but not for me. I literally copied your code and quad tree is not working. At this point, I'm not even sure if I'm just cursed or what. I literally copied your code and it doesn't work. What?! Where is the problem here? How to even find the problem? I'm kind of freaking out right now because I cannot understand how it can work for you, but not for me. I even tried the solution for y-down systems just for the sake of it, but, logically, it didn't work.

I don't know where to look for the error. It cannot be in my BoundsComp class since it extends Rectangle and implements Component. Plus, the bounds of it are rendered so I can clearly see they are correct.

Here is the visual demonstration of what happens when I put your code in:

What actually is the problem? Is it that the entities aren't being registered for detection by other entities? Maybe a different way to test it is to randomly place the entities for testing rather than have it be a grid.

What actually is the problem? Is it that the entities aren't being registered for detection by other entities? Maybe a different way to test it is to randomly place the entities for testing rather than have it be a grid.

Lime rectangle in the screenshot represents player. Gray and blue rectangles represent walls, but blue rectangles are walls that are marked for collision detection by player. So, the problem is the fact entities are not registered for collision detection.

I will try to test it differently, although I doubt it will have different results. Just to add some more background to all of this, I'm creating Bomberman-like game and I actually need it to work with the grid like this. Does a game on that scale actually even requires quad trees? I don't even care anymore at this point, but all I want is to learn how to implement QuadTree so I can feel some sense of accomplishment again.

EDIT:Wow, man. It actually works now. How would I go about creating a grid? Have some special entities that will be checked how? Combine spatial partitioning and quadtree? Use only spatial partitioning? Any suggestions?

Glad to see it works. I made a test for it anyway, which has the same colour coding as yours.

You can see that some entities are still called for checking when they're far away: that's because they don't fit directly in a node.

Since Bomberman is a grid-based game you can have a 2D grid of walls (boolean if you only need wall/not wall). However, if you don't want to really mess with having two separate collision detections (the entities and the walls) you can stick with what you've got. The quadtree definitely helps with collision due to the large number of walls, so keep using it.

Thank you for helping and providing useful information! I will give you cookies (them karma points) after the JGO systems allows me to.

I originally thought about implementing grid based collision detection, but since my next project will be some platformer, I thought I'd want to learn some broad phase algorithm for narrowing the amount of collision checks to perform. I decided to split the entities in two: one will be the part of the map you would get after creating it in Tiled or similar map editor and the second will be entities, particles and that kind of stuff that will be partitioned with quad trees. All of the collision checks will be sprinkled with bitwise collision filtering.

I wish I could abstract collision detection so I don't have to rewrite it with a different genre of game, but hey, I like it this way, too.

Again, thank you so much!

Side note: This is what happens with quad trees when you have a grid of entities:

Funny coincidence: I'm doing a little open-world platformer-ish game right now, so AABB collision was required.

You can check out this package of my AABB stuff. The resolver will take a list of entities (from the quadtree, preferably) and spit out a collision result (you should pool these) of what happened. It doesn't have broad-phase checking as of right now, but I'll take the time right now to implement it.Just added broad-phase checking to the resolver - it makes use of the path bounds rectangle that PhysicsBody can provide.

It works quite well. I use the resolver even with blocks (I pool a bunch of PhysicsBodies for the blocks) and it works perfectly. Other moving bodies work pretty well with it and it's quite fast with the quadtree.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org