• 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
Sparkon

Quadtree collision detection

6 posts in this topic

Hello guys! for my game i've been working on a quadtree implementation for collision detection. The quadtree is extremely simple, but i have a lot of issues with collision detection.
the main issues are two.
-> first off , not all objects (entities ) are added in the quadtree and i have no clue why ( the objects that are not added are part of the "boundaries" of each zone.
-> to check for the collision side i've divided it into 2 steps, first i'm checking if the position of the bullet is inside the entity ( wall in this case ) and then i'm tracing a ray from the previous position to the new position and check which side it intersects. I've been debugging it for a while, and the "ray tracing" is not a problem.
The implementation is not entitrely completed, i'll polish it and improve it later.
The Entities in the quadtree are divided into fixed ( will neve be removed ) and not fixed ( will implement them later, don't need them now : i've removed their code )
here's the header

p.s. the code might seem long, but it's really easy to understand. Thank you in advance

Quadtree header
[code]
class QuadTree
{
public :
QuadTree(){};
QuadTree(float,float,float,float,int,int);
~QuadTree();
void Clear();
void AddEntity(Entity*);
void AddFixedEntity(Entity*);
std::vector<Entity*> GetEntitiesAt(float,float);
std::vector<Entity*> GetFixedEntitiesAt(float,float);
private :
float X_;
float Y_;
float Width_;
float Height_;
int Level_;
int MaxLevel_;

QuadTree* Parent_;
QuadTree* RightUp_;
QuadTree* LeftUp_;
QuadTree* RightDown_;
QuadTree* LeftDown_;
bool Contains(QuadTree*,Entity*);
std::vector<Entity*> Entities_;
std::vector<Entity*> FixedEntities_;
};
[/code]

Quadtree source
[code]
/**=============================
QuadTree::QuadTree
=============================**/
QuadTree::QuadTree(float x,float y,float width,float height,int level,int maxLevel):
X_(x),
Y_(y),
Width_(width),
Height_(height),
Level_(level),
MaxLevel_(maxLevel)
{
if (Level_ == MaxLevel_)
return;
LeftUp_ = new QuadTree(X_,Y_,Width_ / 2.0f , Height_ / 2.0f, Level_ +1,MaxLevel_);
RightUp_ = new QuadTree(X_ + Width_ / 2.0f,Y_, Width_ / 2.0f,Height_ /2.0f,Level_ +1,MaxLevel_);
LeftDown_ = new QuadTree(X_, Y_ + Height_ / 2.0f,Width_ / 2.0f,Height_ / 2.0f,Level_ +1,MaxLevel_);
RightDown_ = new QuadTree(X_ + Width_ / 2.0f,Y_ + Height_ / 2.0f,Width_ / 2.0f,Height_ / 2.0f,Level_ +1,MaxLevel_);
}
/**=============================
QuadTree::~QuadTree
=============================**/
QuadTree::~QuadTree()
{
if (Level_ != MaxLevel_)
{
delete LeftUp_;
delete LeftDown_;
delete RightUp_;
delete RightDown_;
}
}
/**=============================
QuadTree::AddFixedEntity
=============================**/
void QuadTree::AddFixedEntity(Entity* entity)
{
if (Level_ == MaxLevel_)
{
FixedEntities_.push_back(entity);
return;
}
if (Contains(LeftUp_,entity))
{
LeftUp_->AddFixedEntity(entity);
return;
}
else if (Contains(RightUp_,entity))
{
RightUp_->AddFixedEntity(entity);
return;
}
else if (Contains(LeftDown_,entity))
{
LeftDown_->AddFixedEntity(entity);
return;
}
else if (Contains(RightDown_,entity))
{
RightDown_->AddFixedEntity(entity);
return;
}
if (Contains(this,entity))
{
FixedEntities_.push_back(entity);
}
}
/**=============================
QuadTree::GetFixedEntitiesAt
=============================**/
std::vector<Entity*> QuadTree::GetFixedEntitiesAt(float x,float y)
{
if (Level_ == MaxLevel_)
{
return FixedEntities_;
}
std::vector<Entity*> returnOne,returnTwo;
if (!Entities_.empty())
{
returnOne = FixedEntities_;
}
if (x >= X_ &amp;&amp; x <= X_ + Width_ / 2.0f)
{
if ( y >= Y_ &amp;&amp; y <= Y_ + Height_ / 2.0f)
{
returnTwo = LeftUp_->GetFixedEntitiesAt(x,y);
returnOne.insert(returnOne.end(),returnTwo.begin(),returnTwo.end());
return returnOne;
}
else if (y >= Y_ + Height_ / 2.0f &amp;&amp; y <= Y_ + Height_)
{
returnTwo = LeftDown_->GetFixedEntitiesAt(x,y);
returnOne.insert(returnOne.end(),returnTwo.begin(),returnTwo.end());
return returnOne;
}
}
else if (x >= X_ + Width_ / 2.0f &amp;&amp; x <= X_ + Width_)
{
if ( y >= Y_ &amp;&amp; y <= Y_ + Height_ / 2.0f)
{
returnTwo = RightUp_->GetFixedEntitiesAt(x,y);
returnOne.insert(returnOne.end(),returnTwo.begin(),returnTwo.end());
return returnOne;
}
else if (y >= Y_ + Height_ / 2.0f &amp;&amp; y <= Y_ + Height_)
{
returnTwo = RightDown_->GetFixedEntitiesAt(x,y);
returnOne.insert(returnOne.end(),returnTwo.begin(),returnTwo.end());
return returnOne;
}
}
return returnOne;
}
/**=============================
QuadTree::Contains
=============================**/
bool QuadTree::Contains(QuadTree* child,Entity* entity)
{
if (entity->Position_.x < child->X_ ||
entity->Position_.y < child->Y_ ||
entity->Position_.x > child->X_ + child->Width_ ||
entity->Position_.y > child->Y_ + child->Height_ ||
entity->Position_.x + entity->Width_ < child->X_ ||
entity->Position_.y + entity->Height_ < child->Y_ ||
entity->Position_.x + entity->Width_ > child->X_ + child->Width_||
entity->Position_.y + entity->Height_ > child->Y_ + child->Height_ )
return false;
return true;
}
/**=============================
QuadTree::Clear
=============================**/
void QuadTree::Clear()
{
if (!Entities_.empty())
Entities_.clear();
if (Level_ != MaxLevel_)
{
LeftDown_->Clear();
LeftUp_->Clear();
RightUp_->Clear();
RightDown_->Clear();
}
else
{
Entities_.clear();
return;
}
}
[/code]

Here's the bullet to wall collision
[code]
/**=============================
CollisionHandler::BulletToWall
=============================**/
int CollisionHandler::BulletToWall(Bullet&amp; bullet,sf::RenderWindow&amp; window)
{
// getting data from quadtree
std::vector<Entity*> walls = QuadTree_->GetFixedEntitiesAt(bullet.GetPosition().x,bullet.GetPosition().y);
// creating ray from old position to new one
sf::Vector2f startVertex = bullet.GetPosition();
sf::Vector2f endVertex = bullet.GetPreviousPosition();
std::size_t s = walls.size();
for (int i = 0; i < s; ++i)
{
// checking if it is inside first
if (IsInside(bullet.GetPosition(),walls[i]))
{
sf::Vector2f topLeft = walls[i]->Position_;
sf::Vector2f topRight = sf::Vector2f(walls[i]->Position_.x + walls[i]->Width_,walls[i]->Position_.y);
sf::Vector2f downRight = sf::Vector2f(walls[i]->Position_.x + walls[i]->Width_,walls[i]->Position_.y + walls[i]->Height_);
sf::Vector2f downLeft = sf::Vector2f(walls[i]->Position_.x,walls[i]->Position_.y + walls[i]->Height_);
// if it inside i'm gonna check the angle by tracing a ray from the previous position to the new position and check on which side it intersects
if (AreIntersecting(endVertex,startVertex,topLeft,topRight))
return COLLISION_TOP;
else if (AreIntersecting(endVertex,startVertex,topRight,downRight))
return COLLISION_RIGHT;
else if (AreIntersecting(endVertex,startVertex,downRight,downLeft))
return COLLISION_DOWN;
else if (AreIntersecting(endVertex,startVertex,downLeft,topLeft))
return COLLISION_LEFT;
else
return COLLISION_TRUE;
}

sf::ConvexShape s(4);
s.setPoint(0,walls[i]->Position_);
s.setPoint(1,sf::Vector2f(walls[i]->Position_.x + walls[i]->Width_,walls[i]->Position_.y));
s.setPoint(2,sf::Vector2f(walls[i]->Position_.x + walls[i]->Width_,walls[i]->Position_.y + walls[i]->Height_));
s.setPoint(3,sf::Vector2f(walls[i]->Position_.x,walls[i]->Position_.y + walls[i]->Height_));
s.setFillColor(sf::Color(0,255,0));
window.draw(s);
}
return COLLISION_FALSE;
}
[/code] Edited by Sparkon
0

Share this post


Link to post
Share on other sites
Your ::Contains method seems to have too many checks. Normallly you only need 4 tests for 2D containment detection.
If you want to only put entity into node if it is fully contained in it, you have to test that:

Entity.minx >= quadtree.minx
Entity.maxx <= quadtree.maxx
Entity.miny <= quadtree.miny
Entity.maxy >= quadtree.maxy
1

Share this post


Link to post
Share on other sites
[quote name='Lauris Kaplinski' timestamp='1352182902' post='4997891']
Your ::Contains method seems to have too many checks. Normallly you only need 4 tests for 2D containment detection.
If you want to only put entity into node if it is fully contained in it, you have to test that:

Entity.minx >= quadtree.minx
Entity.maxx <= quadtree.maxx
Entity.miny <= quadtree.miny
Entity.maxy >= quadtree.maxy
[/quote]

mm maybe that's the problem... i'm going to check it as as soon as i get home ( i'm in class right now :P). Btw is there anything wrong with my check? ( yes, i admit i checked too many things ).
0

Share this post


Link to post
Share on other sites
You're doing a logical OR in all those checks. It shouldn't be a logical AND? Since if any limit is outside bounds, you already know that node can't contain the data.

With a logical OR, if even only one of all those checks is true and the rest fail, it will return true.

So a (Entity.minx >= quadtree.minx && Entity.maxx <= quadtree.maxx && Entity.miny <= quadtree.miny && Entity.maxy >= quadtree.maxy ) check should do. You need all of those conditions to be true for the node to be able to contain the data. Thus the AND (&&).

btw, you should be careful with returns in the middle of the code. Its easy to forget and leak memory by allocating new memory and having a branch in your logic that returns before you get rid of the resources you no longer use. Edited by TheChubu
0

Share this post


Link to post
Share on other sites
[quote name='TheChubu' timestamp='1352190222' post='4997928']
You're doing a logical OR in all those checks. It shouldn't be a logical AND? Since if any limit is outside bounds, you already know that node can't contain the data.

With a logical OR, if even only one of all those checks is true and the rest fail, it will return true.

So a (Entity.minx >= quadtree.minx && Entity.maxx <= quadtree.maxx && Entity.miny <= quadtree.miny && Entity.maxy >= quadtree.maxy ) check should do. You need all of those conditions to be true for the node to be able to contain the data. Thus the AND (&&).

btw, you should be careful with returns in the middle of the code. Its easy to forget and leak memory by allocating new memory and having a branch in your logic that returns before you get rid of the resources you no longer use.
[/quote]


Mmm... I'm sorry but i don't get the point of the OR. I'm actually checking if any of the conditions is true and then immediatly return false otherwise return true. This way should be faster because if the obect is outside the bounds i do from 1 to 8 checks ( or 4 ) otherwise return true.

Maybe i misunderstood your question. If so i apologize
0

Share this post


Link to post
Share on other sites
I think should be:

bool QuadTree::Contains(QuadTree* child,Entity* entity)
{
if ( entity->Position_.x + entity->Width_< child->X_ ||
entity->Position_.x > child->X_ + child->Width_ ||
entity->Position_.Y + entity->Height_< child->Y_ ||
entity->Position_.y > child->Y_ + child->Height_ )
return false;
return true;
}
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