# Collision detection with an ECS

## Recommended Posts

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.

Edited by exOfde

##### Share on other sites

It could be i found my own stupid mistake... the collision detection doesn't work without coordinates.

so obvious but takes more than 2 days to find ....

##### Share on other sites

Suggestion: when you run into an error like that, after you've examined your code for obvious coding errors, set breakpoints and step through your code, examining the values of variables to determine if the values are correct. Continue stepping through the code until you find a line or function which has good data being input, but results in bad or incorrect data.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627714
• Total Posts
2978773

• 9
• 21
• 14
• 12
• 42