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
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);
}
}
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;
}
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?