Sign in to follow this  

Collision detection with an ECS

This topic is 1279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 1279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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