QuadTree and Polymorphism [SOLVED]

The forum to ask all your programming/game development questions!

QuadTree and Polymorphism [SOLVED]

Postby WSP_SNIPER on Mon Jul 26, 2010 1:25 pm

hi, i was implementing quad trees into my project for faster collision detection. it checks all the entities in the game for collision. the problem is that the entitiies are sometimes bullets or players or somthing and when they are the Update function in the QuadTree class crashes.

here is the quad tree code. this is off the top of my head and it may not be outstanding and i would love you guys to give me some tips on making it better or fixing it all together lol

Code: Select all
#ifndef _QUAD_TREE_H_
#define _QUAD_TREE_H_

#include <SFML/System.hpp>
#include "EntityManager.h"
#include <vector>

///  hope to increase collision decetion speed with this one!!

// need to figure out how im going to do this...
struct NodeRect
{
    int x,  y,  w,  h;

    NodeRect()
    {
        x = y = w = h = 0;
    }

    NodeRect(int setX, int setY, int setW, int setH)
    :
    x(setX),
    y(setY),
    w(setW),
    h(setH)
    {
    }
};
struct Node
{
    Node* child[4];
    NodeRect area;
    std::vector<cell::Entity*> entity;

    ~Node()
    {
        if(child[0]) delete child[0];
        if(child[1]) delete child[1];
        if(child[2]) delete child[2];
        if(child[3]) delete child[3]; // becomes a recursive deallocation
    }

    Node()
    {
        child[0] = child[1] = child[2] = child[3] = NULL;
    }

    Node(int x, int y, int w, int h):
    area(x, y, w, h)
    {
        child[0] = child[1] = child[2] = child[3] = NULL;
    }

    Node(const NodeRect &setArea):
    area(area)
    {
         child[0] = child[1] = child[2] = child[3] = NULL;
    }

    void AddEntity(cell::Entity* _entity)
    {
        entity.push_back(_entity);
    }

};


// in the entity class create an OnCollision method
class QuadTree
{
    public:
        QuadTree(int levels);
        ~QuadTree();

        void Setup(Node* node, int levels = 2); // zero = 1x1 one = 4x4 two = 16x16 exc...

        void Update(EntityManager*);
        void CheckCollisions();

        void FindCell(Node* node, cell::Entity* entity);

        Node* GetNode(){ return m_node; }

    private:
        int m_levels;
        Node* m_node;

    public:

/// just for now
        std::vector<Node*> leafNodes; // for collision checking and stuff
        std::vector<Node*> collisionNodes;

};

#endif




Code: Select all
#include "QuadTree.h"
#include <fstream>
#include <iostream>
#include "Player.h"

QuadTree::QuadTree(int levels)
:
m_levels(levels)
{
    m_node = new Node(0, 0, 640, 480);
    Setup(m_node, levels);
}

QuadTree::~QuadTree()
{
    if(m_node != NULL)
    {
        delete m_node;
    }
}

void QuadTree::Setup(Node* node, int levels)
{
    levels -= 1;
    if(levels >= 0)
    {
        node->child[0] = new Node(node->area.x, node->area.y, node->area.w/2, node->area.h/2);
        node->child[1] = new Node(node->area.x + node->area.w/2, node->area.y, node->area.w/2, node->area.h/2);
        node->child[2] = new Node(node->area.x, node->area.y + node->area.h/2, node->area.w/2, node->area.h/2);
        node->child[3] = new Node(node->area.x + node->area.w/2, node->area.y + node->area.h/2, node->area.w/2, node->area.h/2);

        for(int i = 0; i < 4; i++)
        {
            Setup(node->child[i], levels);  // recursive method lol
        }
    }
    else
    {
        this->leafNodes.push_back(node); // its a leaf node !!
    }
}

void QuadTree::CheckCollisions()
{
    for(unsigned int i = 0; i < collisionNodes.size(); i++)
    {
        for(unsigned int n = 0; n < collisionNodes[i]->entity.size(); n++)
        {
            cell::Entity* entity = collisionNodes[i]->entity[n];
            for(unsigned int z = 0; z < collisionNodes[i]->entity.size(); z++)
            {
                if(z != n)
                {
                    if(entity->Collision(collisionNodes[i]->entity[z]))
                    {
                        collisionNodes[i]->entity[n]->OnCollision();
                    }
                }
            }
        }
    }
}

void QuadTree::Update(EntityManager* mgr)
{

    for(unsigned int i = 0; i < leafNodes.size(); i++)
    {
        leafNodes[i]->entity.clear();
    }
    collisionNodes.clear();
    for(unsigned int i = 0; i< mgr->GetVector().size(); i++)
    {
        FindCell(m_node, mgr->GetEntity(i));
    }

}

void QuadTree::FindCell(Node* node, cell::Entity* entity)
{
    if(node->child[0])
    {
        if(entity->GetX() < node->child[1]->area.x)
        {
            if(entity->GetY() < node->child[2]->area.y)
            {
                FindCell(node->child[0], entity);
            }
            else
            {
                FindCell(node->child[2], entity);
            }
        }
        else
        {
            if(entity->GetY() < node->child[2]->area.y)
            {
                FindCell(node->child[1], entity);
            }
            else
            {
                FindCell(node->child[3], entity);
            }
        }
    }
    else
    {
        node->AddEntity(entity);
        collisionNodes.push_back(node); // if it has an entity check it for collision
    }
}
Image
User avatar
WSP_SNIPER
 
Posts: 156
Joined: Sun May 30, 2010 8:02 pm

Re: QuadTree and Polymorphism [SOLVED]

Postby WSP_SNIPER on Mon Jul 26, 2010 10:14 pm

i solved this by looking at how sfml handled their polymorphism and they used references... i guess thats what caused it to crash me using pointers but idk why. it works now and thats all that matters :mrgreen:
Image
User avatar
WSP_SNIPER
 
Posts: 156
Joined: Sun May 30, 2010 8:02 pm

Re: QuadTree and Polymorphism [SOLVED]

Postby eatcomics on Wed Jul 28, 2010 3:52 am

Could be because pointers were NULL? References can't be declared as NULL where as pointers can... so that can cause problems. I mean that may not be it, but felt it worth pointing out
mv2112 wrote:O.o
User avatar
eatcomics
 
Posts: 289
Joined: Wed Mar 31, 2010 10:06 pm

Re: QuadTree and Polymorphism [SOLVED]

Postby WSP_SNIPER on Wed Jul 28, 2010 8:57 am

eatcomics wrote:Could be because pointers were NULL? References can't be declared as NULL where as pointers can... so that can cause problems. I mean that may not be it, but felt it worth pointing out


ya thats what i was thinking but i was searching for that and i couldent find it. the error was some segmant falt which was i read was writing to memory that was already taken up
Image
User avatar
WSP_SNIPER
 
Posts: 156
Joined: Sun May 30, 2010 8:02 pm

Re: QuadTree and Polymorphism [SOLVED]

Postby eatcomics on Wed Jul 28, 2010 4:28 pm

segfaults are usually trying to read memory that's not there :D
but you're right it can also be reading memory that you're not supposed to access
mv2112 wrote:O.o
User avatar
eatcomics
 
Posts: 289
Joined: Wed Mar 31, 2010 10:06 pm

Re: QuadTree and Polymorphism [SOLVED]

Postby WSP_SNIPER on Thu Jul 29, 2010 1:01 am

eatcomics wrote:segfaults are usually trying to read memory that's not there :D
but you're right it can also be reading memory that you're not supposed to access


ya the debugger brought me to a random file where the error was but i know there were no null pointers lol. actually this is how i put them into the vector myEntities.push_back(&player) so it couldent be null lol :mrgreen: but thanks for the input.

im working on some memory management classes so i dont have to worry about things like null pointers and memory leaks :D :D :D
Image
User avatar
WSP_SNIPER
 
Posts: 156
Joined: Sun May 30, 2010 8:02 pm


Return to Questions & getting help

Who is online

Users browsing this forum: No registered users and 1 guest