SFML community forums

Help => General => Topic started by: StDH on May 10, 2014, 12:15:40 pm

Title: QuadTree is adding objects to only one children
Post by: StDH on May 10, 2014, 12:15:40 pm
Initialize it with max_level 1 to have only 4 childrens.
its adding objects to only one children : SouthEast.
I have no idea why.
Title: Re: QuadTree is adding objects to only one children
Post by: F2CPP on May 10, 2014, 05:08:13 pm
as a first step, have you looked at outcome of the position comparisons inside void game::QuadTree::insert(sf::Sprite *other)?

if the position x and y of 'other' is larger than x and y (in that order) of the existing values (from constructor or init()) it would simply end up at SE -> insert(other); i.e. SouthEast as you say
Title: Re: QuadTree is adding objects to only one children
Post by: StDH on May 10, 2014, 05:23:19 pm
can you show me repaired code ?
Title: Re: QuadTree is adding objects to only one children
Post by: Nexus on May 10, 2014, 05:34:10 pm
Please show some self-initiative. We like to help by giving advice, but it's not our job to debug and fix your code. Especially since this is not a SFML-related problem.

Have you already used a debugger?
Title: Re: QuadTree is adding objects to only one children
Post by: StDH on May 10, 2014, 05:38:25 pm
i tried editing code but doesnt work.
Title: Re: QuadTree is adding objects to only one children
Post by: Nexus on May 10, 2014, 05:41:39 pm
Then try further... ::)

Or at least adhere to the forum rules (http://en.sfml-dev.org/forums/index.php?topic=5559.0).
Title: Re: QuadTree is adding objects to only one children
Post by: StDH on May 10, 2014, 05:50:54 pm
well, i have been trying to solve this whole day so ......
Title: Re: QuadTree is adding objects to only one children
Post by: Nexus on May 10, 2014, 06:32:56 pm
well, i have been trying to solve this whole day so ......
Then you definitely need to work on your bug fixing skills. I assume you haven't used a systematic approach. I suggest you take a debugger, step through the program, compare variable's actual values with your expectations and find out where and why there are differences. If necessary, split the code into smaller parts that you can test on their own and exclude functionality that does not contribute to the error. This way, you can quickly localize where the problem is.

I'm saying this because I think such advice is of much greater value for you than simply saying "in this line there is the error", and next time you have a similar problem, you waste another day.

Also, some general tips that make your code shorter, easier to understand and thus less error-prone: Use std::unique_ptr instead of raw new/delete. You have code duplication (3 methods to insert, get object and count have very similar structure), you should extract a function that returns the correct object depending on the position:
QuadTree& getChildAtPosition(sf::Vector2f position);
Title: Re: QuadTree is adding objects to only one children
Post by: StDH on May 10, 2014, 08:50:21 pm
i solved problem without debugging  :) :D ;D