How to Collide 2D from C++ Tiles

Asked

Viewed 675 times

0

Hello.

I wonder how I can get collision of Tiles in C++.

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

I’m using SDL 2!

Grateful from now on

Thiago

  • Hello Thiago. Welcome to SOPT. What exactly is your question? You don’t know how to define the Iles in the "engine made by your friend" (by the way, does she already make collision detection?), don’t know how the collision works in general or did something that isn’t working as expected? Your question is very abstract, and you will receive equally abstract answers. In fact, someone might be able to tell you to read the other questions that deal with collision. There are some already: http://answall.com/questions/tagged/detec%C3%A7%C3%A3o-de-colis%C3%A3o

  • Does the engine developed by your friend have any class that stores rectangles or information about Tile? If possible put the class you select to store them

1 answer

4


Overall the simplest collision system via rectangle that can be easily adapted to use on 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;
}

Check if your friend’s engine has any class that stores rectangle or Tiles, if you have just adapt the function "Collision" with the engine classes of your friend, but make sure it has not done any collision detection function since most Engines brings physics management functions.

  • Thanks. But I talked to my friend and he took care of it. I’m just wondering how to do a 2D gravity correctly, but still, thank you =D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.