Hello,
Since two weeks i work with/learn to use the artemis framework. But know i found
a problem which i can't solve by myself in my eyes. The problem is:
The function i use to detect a collision found always a collision!
Obliviously i checked the function. But there i did not find a mistake. I look also up from
where i had this function ( lazyfoo.net ) but also here i cannot find a mistake. Here the function
i use:
bool CollisionSystem::check_collision_PP(std::vector<SDL_Rect>& A, std::vector<SDL_Rect>& B)
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boxes
for (int Abox = 0; Abox < A.size(); Abox++)
{
//Calculate the sides of rect A
leftA = A[ Abox ].x;
rightA = A[ Abox ].x + A[ Abox ].w;
topA = A[ Abox ].y;
bottomA = A[ Abox ].y + A[ Abox ].h;
//Go through the B boxes
for (int Bbox = 0; Bbox < B.size(); Bbox++)
{
//Calculate the sides of rect B
leftB = B[ Bbox ].x;
rightB = B[ Bbox ].x + B[ Bbox ].w;
topB = B[ Bbox ].y;
bottomB = B[ Bbox ].y + B[ Bbox ].h;
//If no sides from A are outside of B
if (((bottomA <= topB) || (topA >= bottomB) || (rightA <= leftB) || (leftA >= rightB)) == false)
{
//A collision is detected
return true;
}
}
}
//If neither set of collision boxes touched
return false;
}
My second idea where the error lies was the way I collect all entities to check for collision was may be the error but also here if i debug, the numbers are fine; even if i see on the screen that two entities intersect with each other the result doesn't change( which confuse me the most):
void CollisionSystem::processEntity(artemis::Entity& e)
{
//Collect all entities of one group
artemis::Bag<artemis::Entity*>* collisionbag = world->getGroupManager()->getEntities(world->getGroupManager()->getGroupOf(e));
//just in case create an Bag without gaps
artemis::Bag<artemis::Entity*> bag = *collisionbag->getGapless();
if(bag.isEmpty())
throw;
//looks like here is something go wrong, but what???
for(int i=0; i < bag.getCount();i++)
{
if(this->check_collision_PP(hitboxMapper.get(e)->box,hitboxMapper.get(*bag.get(i))->box ))
std::cerr<<"Collision\n";
else
std::cerr<<"Nothing\n";
}
//this part works absolutly fine :D
if (positionMapper.get(e)->getY() <= MinY ||
positionMapper.get(e)->getY() + hitboxMapper.get(e)->Height >= MaxY)
{
velocityMapper.get(e)->velocityY *= -1;
}
if (positionMapper.get(e)->getX() <= MinX ||
positionMapper.get(e)->getX() + hitboxMapper.get(e)->Width >= MaxX)
{
velocityMapper.get(e)->velocityX *= -1;
}
}
If someone have a clue perhaps what i am doing wrong or what i could check also to find my mistake i would really appreciate it.