How to do Tiles 2D collision in C ++

0

Hello.

I would like to know how I can do to get Tiles crash in C ++.

I am using an engine made by my friend, however, I would like to know how to do it.

I'm using SDL 2!

Thank you in advance

Thiago

    
asked by anonymous 23.05.2015 / 18:29

1 answer

4

In general the simplest collision system across rectangles that can be easily adapted to use in tiles works as follows:

class Rectangle
{
public:
    int X, Y; // Posição
    int W, H; // Largura e Altura
};

bool Colisao(Rectangle A, Rectangle B)
{
    if (A.X + A.W > B.X && A.Y + A.H > B.Y)
    {
        if (A.X < B.X + B.W && A.Y < B.Y + B.H)
        {
            // Houve Colisão!
            return true;
        }
    }
    // Não Houve Colisão
    return false;
}

Make sure your friend's engine has some class that stores rectangle or tiles, if you just have to adapt the "Collision" function with your friend's engine classes, but make sure it has not done any collision detection function ja that most engines have physics management functions.

    
24.05.2015 / 00:40