• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PureSnowX

Quadtree problem.

4 posts in this topic

Hello all!

I'v been adventurous enough to try to implement a Quad-tree.
But I keep getting these Unhandled exceptions everywhere around the code, it seems like it doesn't allocate the memory or the memory of the allocated items get corrupted.

Am I doing something wrong inside my code? I know that if I place to many objects near each other the program will most likely crash due to it trying to produce infinite nodes since there are to many nodes inside the new node.

But it's crashing during start up so it's not that.
Hopefully someone can help me and spot the problem somewhere inside my code.

Here is an example of the error message I am getting:
[quote]
Unhandled exception at 0x00488cf6 in SFML Quadtree.exe: 0xC0000005: Access violation reading location 0x00000000.
[/quote]

And down below is the vital and "minimal" ( excluding the Object class since it only contains a rectangle object ) and the headerfile of the Quadtree since well most of the information is already inside the .cpp file.

[CODE]

#include <iostream>
#include <SFML/Graphics.hpp>
#include "Quadtree.hpp"


int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Quadtree - Structure and Split Example");
sf::Event evnt;
Quadtree qTree( 0,0, window.getSize().x, window.getSize().y, window, 0 );
int drawCalls = 0;
int collisionCalls = 0;

sf::Clock frameClock;
const float TIMER = 1.0f / 0.5f;
while(window.isOpen())
{
while(window.pollEvent(evnt))
{
if(evnt.type == sf::Event::Closed)
window.close();

if(evnt.type == sf::Event::MouseButtonReleased)
{
if(evnt.mouseButton.button == sf::Mouse::Button::Left)
{
qTree.add(new Object(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y));
std::cout << "Added object to node" << std::endl;
std::cout << collisionCalls << " collisions performed." << std::endl;
collisionCalls = 0;
}
}

}
if(frameClock.getElapsedTime().asSeconds() > TIMER)
{
frameClock.restart();
std::cout << drawCalls << " drawcalls performed." << std::endl;
}
drawCalls = 0;
window.clear(sf::Color(94, 174, 255));
qTree.display(drawCalls);
window.display();
}

return 0;
}
[/CODE]

And here is the Quadtree.cpp
[CODE]
#include "Quadtree.hpp"
#include <iostream>

const int Quadtree::MAX_DEPTH = 7;
const int Quadtree::MAX_OBJECTS = 4;

Quadtree::Quadtree(float x, float y, float width, float height, sf::RenderWindow& wndw, int depth)
{
quadX = x;
quadY = y;
quadWidth = width;
quadHeight = height;
window = &wndw;
nodeStatus = LEAF;
quadDepth = depth;
objectNumber = 0;

rect.setFillColor(sf::Color::Black);
rect.setOutlineColor(sf::Color::Magenta);
rect.setOutlineThickness(1);
rect.setPosition(x, y);
rect.setSize(sf::Vector2f(width, height));

quad.left = x;
quad.top = y;
quad.width = width;
quad.height = height;

for(int i = 0; i < 4; i++)
nodes[i] = NULL;
}
Quadtree::~Quadtree()
{
for(int n = 0; n < 4; n++)
{
if(nodes[n] != NULL)
delete nodes[n];
}
}


void Quadtree::add(Object* object)
{
objectNumber++;
objects.push_back(new Object(*object));
if(objectNumber >= MAX_OBJECTS)
{
split();
}
}

void Quadtree::split()
{
if(quadDepth >= MAX_DEPTH)
return;

for(int i = 0; i < objects.size(); i++)
{
pass(objects[i]);
delete objects[i];
objects.erase(objects.begin() + i);
--i;
}
}

void Quadtree::pass(Object* object)
{
/* Determite where it should be placed */
if(object->getX() > quadWidth/2) /* If true, place somewhere on the right side */
{
/* SE or NE */
if(object->getY() > quadHeight/2) /* It's the SE Node */
{
if(nodes[1] == NULL)
{
nodes[1] = new Quadtree(quadX + quadWidth/2, quadY + quadHeight/2, quadWidth/2, quadHeight/2, *window, quadDepth +1); // SE
}
nodes[1]->add(object);
return;
}
else
{
if(nodes[0] == NULL)
{
nodes[0] = new Quadtree(quadX + quadWidth/2, quadY, quadWidth/2, quadHeight/2, *window, quadDepth + 1); // NE
}
nodes[0]->add(object);
return;
}
}
else
{
/* Is it NW or SW*/
if(object->getY() > quadHeight/2)
{
/* SW */
if(nodes[2] == NULL)
{
nodes[2] = new Quadtree(quadX, quadY + quadHeight/2, quadWidth/2, quadHeight/2, *window, quadDepth +1);// SW
}
nodes[2]->add(object);
return;
}
else
{
if(nodes[3] == NULL)
{
nodes[3] = new Quadtree(quadX, quadY, quadWidth/2, quadHeight/2, *window, quadDepth +1); // NW
}
nodes[3]->add(object);
return;
}
}
}

void Quadtree::display(int& drawCalls)
{
drawCalls++;
window->draw(rect);
for(int n = 0; n < objects.size(); n++)
window->draw(objects[n]->getRect());


for(int i = 0; i < 4; i++)
if(nodes[i] != NULL)
nodes[i]->display(drawCalls);
}
[/CODE] Edited by Moonkis
0

Share this post


Link to post
Share on other sites
[left]Your display function is gonna crash. You've got NULL in your nodes array for leaf nodes, but still try call display(sprite) on them. This is probably what's causing the crash on 1st run through the loop.[/left]

If you're getting a crash earlier than that, it's likely to be in code you're not showing.
You're also not initializing nodeStatus in the constructor.
0

Share this post


Link to post
Share on other sites
[quote name='MortenB' timestamp='1337466818' post='4941535']
[left]Your display function is gonna crash. You've got NULL in your nodes array for leaf nodes, but still try call display(sprite) on them. This is probably what's causing the crash on 1st run through the loop.[/left]

If you're getting a crash earlier than that, it's likely to be in code you're not showing.
You're also not initializing nodeStatus in the constructor.
[/quote]
I made quite some changes to my code, it works without crashing BUT I'm not sure if I'm handling the memory correct inside the add and pass methods.
The Object constructor takes an "const Object&" parameter which should make sure the memory is still pointed at thus not leaking any memory.



[quote name='Nyxenon' timestamp='1337485657' post='4941573']
Are you not releasing any memory? It looks like you're allocating, but not releasing. That might be the problem.
[/quote]

I'm releasing the memory inside the destructor of a Quadtree and it calls every nodes ( that is not null ) and deletes them as well, causing a chain destruction starting from the bottom.

The new problem is when it's supposed to split up, noted in the "Pass" or "Split" method after the fist split it starts getting weird, it splits up the wrong nodes and adds the object to them. Is something wrong with my math?

EDIT: Here is a picture that explains the problem, notice the split in the right corner.
[img]http://a4.sphotos.ak.fbcdn.net/hphotos-ak-ash2/156222_394253790613792_100000875227598_1056387_1779802622_n.jpg[/img] Edited by Moonkis
0

Share this post


Link to post
Share on other sites
Forgot to add the x and y coordinates to the "pass(Object* object)" check. Fixed it now and it works wonders!
Still though can anyone see just by looking at the code if I have memory leaks?
0

Share this post


Link to post
Share on other sites

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  
Followers 0