0
\$\begingroup\$

How can I do AABB collision engine? What do I need? I need it to do a platformer game (don't want to use box2d etc.).

I tried to make it like this

void GameplayScreen::Update()
{
    ePlayer->Update();
    collision();
}

void GameplayScreen::Draw(SDL_Renderer *renderer)
{
    DrawMap(renderer);
    ePlayer->Draw(renderer);;
}

void GameplayScreen::DrawMap(SDL_Renderer *renderer)
{
    for (int y = 0; y < map.size(); ++y)
        for (int x = 0; x < map[y].size(); ++x)
            if (map[y][x] != 0)
            {               
                SDL_Rect destRect;
                SDL_Rect srcRect;

                srcRect.w = 32;
                srcRect.h = 32;
                srcRect.x = blockID * srcRect.w;
                srcRect.y = 0;

                destRect.x = (x * 32);      
                destRect.y = (y * 32 );  /
                destRect.w = 32;                                        
                destRect.h = 32;                                        

                vBlock[Earth]->Draw(renderer, srcRect, destRect);
            }
}

void GameplayScreen::Collision()
{
    for(int y = 0; y < map.size(); ++y)
        for(int x = 0; x < map[y].size(); ++X)
            if(map[y][x] != 0)
            {
                int mapX = x * 32;
                int mapY = y * 32;
                int mapW = x * 32 + 32; // I tried (x + 32) * 32 also
                int mapH = y * 32 + 32; // I tried (y + 32) * 32 also

                int playerX = ePlayer->getPosX();
                int playerY = ePlayer->getPosY();
                int playerW = ePlayer->getPosX() + ePlayer->getHitBoxX();
                int playerH = ePlayer->getPosY() + ePlayer->getHitBoxY();

                AABB aabb(mapX, mapY, mapW, mapH);
                if(aabb.chechCollision(playerX, playerY, playerW, playerH))
                {
                    //I don't know what do write here
                    //I wrote what I thought would be good

                    if(playerX <= mapW && playerX >= mapX)
                    {
                        std::cout<<"Player collision Left"<<std::endl;
                    }
                    else if(playerW >= mapX && playerW <= mapW)
                    {
                        std::cout<<"Player collision  right"<<std::endl;
                    }
                    else if(playerY <= mapH && playerH >= mapH)
                    {
                        std::cout<<"Player collision top" <<std::endl;
                    }
                    else if(playerH >= mapY && playerH <= mapH)
                    {
                        std::cout<<"player collision bot"<<std::endl;
                    }
                }
                else std::cout<<"NO COLLISION"<<std::endl;
            }
}




bool AABB::checkCollision(float x1, float y1, float w1, float h1)
{
    if ((x <= w1 && x >= x1) 
            && (w >= x1 && w <= w1)
                && ((y <= h1 && h >= h1)
                    || h >= y1 && h <= h1)) // I tried here with &&
    {
        return true;
    }
    return false;
}

The problem with this is that, even if map[y][x] == 0 => no collision.

The game says that there is a collision.

\$\endgroup\$
  • 1
    \$\begingroup\$ It's close to impossible to tell the side of the intersection woth AABBs if that's what you want to achieve \$\endgroup\$ – Bálint Nov 22 '17 at 22:48
  • \$\begingroup\$ +1 to @Bálint's comment. It becomes a lot easier if you can take velocity into account since if you are moving right you know that you couldn't have collided on the left etc. (except in very special cases). \$\endgroup\$ – Charanor Nov 23 '17 at 8:05
  • \$\begingroup\$ So, what do you suggest? how should I make it works? \$\endgroup\$ – Scouteeer Nov 23 '17 at 12:22
  • \$\begingroup\$ I'm not super clear on what you are looking for but if what you are trying to do is find the side that collision occurred this may help. It does not seem like you are using 2D vectors in your sample code, in order to find direction, velocity, etc. you will probably want to implement this so that you can move towards a solution. \$\endgroup\$ – CharmingSalamander Mar 23 '18 at 4:11
-1
\$\begingroup\$

I think you should use the (x0,y0) (x1,y1) co-ordinates of the rectangle instead of the width and height, which it seems like you're doing now. Rectangle collision is answered here https://stackoverflow.com/questions/2752349/fast-rectangle-to-rectangle-intersection.

Code Complete has a pretty good example of how to make a simple platformer http://codeincomplete.com/posts/tiny-platformer/.

\$\endgroup\$
  • \$\begingroup\$ I don't know what you're about with (x0,y0) (x1,y1). In these links you've sent I see no answer. Those codes show me what I already know. \$\endgroup\$ – Scouteeer Nov 23 '17 at 7:12
  • \$\begingroup\$ I mean, you are using the width and height of the rectangle instead of the coordinates of the corner. The links show how to do a rectangle collision engine and use it in a platform game. \$\endgroup\$ – Jay Nov 24 '17 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.