Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trouble with collision detection with many objects  (Read 1317 times)

0 Members and 1 Guest are viewing this topic.

Student555

  • Guest
Trouble with collision detection with many objects
« on: July 03, 2015, 06:22:20 am »
I am having trouble detecting whether or not collision has occurred with my objects. Only one column of my objects can detect whether or not a collision has occurred. The rest of them are unresponsive.

Both methods of collision detection I have used have been unsuccessful (Regular Bounding Box, Separating Axis Thereon)

I used a multidimensional (2D) vector to store my objects (50 objects). Here is the sample of the code:

Main.cpp
Code: [Select]
Bullet * bullet = nullptr;
vector<vector<Alien>> Army_of_Aliens;
vector< Alien > Aliens;

Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);

Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);
Aliens.push_back(alien);
        .
        .
        . (More)


while(running)
{
            while(Window.pollEvent(Event))
{

switch(Event.type)
{
case sf::Event::Closed:
gRunning = false;
break;

case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
gRunning = false;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
bullet = new Bullet(ship.getPosition());
shoot = true;
}
break;
}

}


      if(shoot == true)
{
bullet->Update(gDeltaTime);
bullet->Render(Window);
Bullet_Position.left = bullet->getPosition().x;
Bullet_Position.top = bullet->getPosition().y;

Bullet_Position.width = 1.0f;
Bullet_Position.height = 8.0f;
Collision = BoundingBox(bullet, Army_of_Aliens, row, col);
}

}


Code: [Select]
bool BoundingBox(Bullet * bullet, vector<vector<Alien>> & Aliens,  const int row, const int col)
{
OrientedBoundingBox OBB1(bullet);
OrientedBoundingBox OBB2(Aliens, row, col);


sf::Vector2f Axis[4] =
{
sf::Vector2f(OBB1.Points[1].x - OBB1.Points[0].x, OBB1.Points[1].y - OBB1.Points[0].y),
sf::Vector2f(OBB1.Points[1].x - OBB1.Points[2].x, OBB1.Points[1].y - OBB1.Points[2].y),
sf::Vector2f(OBB2.Points[0].x - OBB2.Points[3].x, OBB2.Points[0].y - OBB2.Points[3].y),
sf::Vector2f(OBB2.Points[0].x - OBB2.Points[1].x, OBB2.Points[0].y - OBB2.Points[1].y)

};

for(int i = 0; i < 4; i++)
{
float MinOBB1, MaxOBB1, MinOBB2, MaxOBB2;

OBB1.ProjectOntoAxis(Axis[i], MinOBB1, MaxOBB1);
OBB2.ProjectOntoAxis(Axis[i], MinOBB2, MaxOBB2);

if(!((MinOBB2 <= MaxOBB1) && (MaxOBB2 >= MinOBB1)))
return false;

}

return true;
}


Code: [Select]
OrientedBoundingBox(Bullet * bullet)
{
sf::Transform trans =  bullet->getTransform();

Points[0] = trans.transformPoint(0.0f, 0.0f);
Points[1] = trans.transformPoint(bullet->getSize().x, 0.0f);
Points[2] = trans.transformPoint(bullet->getSize().x, bullet->getSize().y);
Points[3] = trans.transformPoint(0.0f, bullet->getSize().y);

}


OrientedBoundingBox(vector<vector<Alien>> & Aliens, int row, int col)
{

for(vector<Alien>::iterator itr = Aliens[0].begin(); itr != Aliens[0].end(); itr++)
{
sf::Transform trans = itr->getTransform();

Points[0] = trans.transformPoint(0.0f, 0.0f);
Points[1] = trans.transformPoint(itr->getSize().x, 0.0f);
Points[2] = trans.transformPoint(itr->getSize().x, itr->getSize().y);
Points[3] = trans.transformPoint(0.0f, itr->getSize().y);
}


}

.
.
.

void ProjectOntoAxis(const sf::Vector2f & Axis, float & Min, float & Max)
{
Min = (Points[0].x * Axis.x + Points[0].y * Axis.y);
Max = Min;

for(int j = 1; j < 4; j++)
{
float Projection = (Points[j].x * Axis.x + Points[j].y * Axis.y);

if(Projection < Min)
Min = Projection;
if(Projection > Max)
Max = Projection;
}

}



At first I thought it was a speed issue, because I was using a nested for loop. So, I switched over to iterators, but nothing changed. Only a few, are able to be detected. I don't know what the problem might be.

Can somebody give me some insight on what the problem could be?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Trouble with collision detection with many objects
« Reply #1 on: July 03, 2015, 06:37:20 am »
We're not really here to debug your code. Set break points, write information to the console, step through the code and find out what's going on. If you don't know how to debug your own code, you better start now, because it's one of the most important things a programmer needs to know. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: Trouble with collision detection with many objects
« Reply #2 on: July 03, 2015, 07:17:26 am »
Don't mean to come off as rude, because I appreciate the advice, but I have been debugging. If my goal was to make you do the debugging i would have posted way more code then I did. My goal was not to make you do the debugging, that is my job. I posted here with the hope that you might give me some in sight on whether or not you think am on the right track. Or maybe some pitfalls of using nested for loops or something of value I should keep in mind. I've been debugging for hours, and I've reached a dead end. I need some ideas to get back on the right direction. 
« Last Edit: July 03, 2015, 07:46:02 am by Student555 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Trouble with collision detection with many objects
« Reply #3 on: July 03, 2015, 07:33:11 am »
The thing is, if you who wrote the code and logic behind it can't figure out what the issue is, chances are really high nobody will figure it out by looking at your code excerpts.

The general approach to solving and issue is: confirm the issue, change logic states with debugger/outputs, isolate the problem by either scrapping ALL code that has nothing to do with it or start a new project and add in code step by step until you get the problem again and then work with that minimal and complete example. If you didn't figure out what goes wrong while isolating the problem, you can post the code and we should be able to easily understand the logic and code, since it's a minimal and complete one. ;)

Nested loops are not something complicated or worrisome, so I'm not sure what you're worried about. Where is that nested for loop anyways?

Since you seem to work with Axis Aligned Bounding Boxes, why don't you construct sf::Rect and call the intersect() function?

I see you use row and col variables, where do they get set?
« Last Edit: July 03, 2015, 07:40:28 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: Trouble with collision detection with many objects
« Reply #4 on: July 03, 2015, 07:45:38 am »
Alright, thanks for the tip. I'll keep tinkering away.

In case you are still wondering I initialized the row and col variables before the while loop:

Code: [Select]
const int row = Army_of_Aliens.size();
const int col = Army_of_Aliens[0].size();


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Trouble with collision detection with many objects
« Reply #5 on: July 03, 2015, 07:55:59 am »
Are all rows initialized with the correct position etc?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Student555

  • Guest
Re: Trouble with collision detection with many objects
« Reply #6 on: July 03, 2015, 08:16:11 am »
Yes, the rows are initialized with the correct position. If i remember correctly the row variable is set to one.

Here is the small snippet of the initialization code:
Code: [Select]
float k = 1;
for(int i = 0; i < row; i++)
{

for(int j = 0; j < col; j++)
{


if(j <= 9 )
{
Army_of_Aliens[i][j].setPosition(k * 50.0f, 50.0f);
Alien_Position.push_back(sf::FloatRect(Army_of_Aliens[i][j].getPosition().x, Army_of_Aliens[i][j].getPosition().y, 30.0f, 30.0f));
}
else if(j > 9 && j <= 19)
{
Army_of_Aliens[i][j].setPosition(k * 50.0f, 100.0f);
Alien_Position.push_back(sf::FloatRect(Army_of_Aliens[i][j].getPosition().x, Army_of_Aliens[i][j].getPosition().y, 30.0f, 30.0f));
}
else if(j > 19 && j <= 29)
{
Army_of_Aliens[i][j].setPosition(k * 50.0f, 150.0f);
Alien_Position.push_back(sf::FloatRect(Army_of_Aliens[i][j].getPosition().x, Army_of_Aliens[i][j].getPosition().y, 30.0f, 30.0f));
}
.
.
.
.

Every object is set to the correct position.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Trouble with collision detection with many objects
« Reply #7 on: July 03, 2015, 10:36:44 am »
This piece of code makes me think that things are not set correctly, then again I've no idea how you position the objects. Usually one would probably write something like this to set the position of a 2D grid.

for(std::size_t y = 0; y < max_y; ++y)
    for(std::size_t x = 0; x < max_x; ++x)
        objects[y][x].setPosition(x * 50.f, y * 50.f);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/