Search code examples
c++graphicsgeometrysdl

Creating an X amount of rectangles which are not intersecting each other on a specified screen size


I managed with some help to know how when two rectangles are intersecting each other, from there it should be easy to make what i just said in the title but ...

So, short story of what i just did below: Created a for loop from 1 to Number_of_Obstacles In that for an random obstacle (rectangle/square) is created and it will be checked if it is overlaped with all other obstacles created from 0 to the loop contor (or in other words every obstacle stored in the vector)

Again, the doOverLap function works. Tested it with a square which i made a controller and other random rectangle created on the screen. It outputs in chat when i'm overlaping it and trust me, i overlaped it from all angles.

Here is a picture with the overlaping issue: https://i.sstatic.net/PJnxV.jpg

bool doOverlap(A a, B b)
{
    if (a.x1 > b.x2 || b.x1 > a.x2)
        return false;
    if (a.y1 > b.y2 || b.y1 > a.y2)
        return false;
    return true;
}

struct Obstacles {
    int X, Y;
void Create_Random_Obstacles(Obstacles Obj[], int Numar_Obstacole)
{
    srand(time(NULL));

    A Rectangle_1;
    B Rectangle_2;

    /* To avoid rendering outside of the screen */
    int X_Axis = X_RESOLUTION - 40;
    int Y_Axis = Y_RESOLUTION - 40;

    int obstacolX = rand() % X_Axis + 1;
    int obstacolY = rand() % Y_Axis + 1;

    Obj[0].X = obstacolX;
    Obj[0].Y = obstacolY;

    for (int i = 1; i < Numar_Obstacole; i++)
    {
        obstacolX = rand() % X_Axis + 1;
        obstacolY = rand() % Y_Axis + 1;

        Rectangle_1.x1 = obstacolX;
        Rectangle_1.x2 = obstacolX + 40;
        Rectangle_1.y1 = obstacolY;
        Rectangle_1.y2 = obstacolY + 40;

        for (int j = 0; j < i; j++) {
            Rectangle_2.x1 = Obj[j].X;
            Rectangle_2.x2 = Obj[j].X + 40;
            Rectangle_2.y1 = Obj[j].Y;
            Rectangle_2.y2 = Obj[j].Y + 40;

            if (doOverlap(Rectangle_1, Rectangle_2))
            {
                std::cout << "Overlap\n";
            }
            else
            {
                Obj[i].X = obstacolX;
                Obj[i].Y = obstacolY;
            }
        }   
    }
}

void Render(SDL_Renderer* renderer, Obstacles Obj[], int Numar_Obstacole) {
    for (int i = 0; i < Numar_Obstacole; i++) 
    {
        SDL_Rect r{ Obj[i].X, Obj[i].Y, 40, 40 };
        SDL_SetRenderDrawColor(renderer, 255, 160, 15, 255);
        SDL_RenderFillRect(renderer, &r);
    }
}

};


Solution

  • Restart selection when collision occurs, something like:

    bool Has_Overlap(const Obstacles& obj, const Obstacles* Objs, int Size)
    {
        B Rectangle_2;
        Rectangle_2.x1 = obs.X;
        Rectangle_2.x2 = obs.X + 40;
        Rectangle_2.y1 = obs.Y;
        Rectangle_2.y2 = obs.Y + 40;
        for (int i = 0; i != Size; ++i) {
            A Rectangle_1;
            Rectangle_1.x1 = Obs[i].X;
            Rectangle_1.x2 = Obs[i].X + 40;
            Rectangle_1.y1 = Obs[i].Y;
            Rectangle_1.y2 = Obs[i].Y + 40;
            if (doOverlap(Rectangle_1, Rectangle_2)) {
                return true;
            }
        }
        return false;
    }
    
    void Create_Random_Obstacles(Obstacles* Objs, int Size)
    {
        /* To avoid rendering outside of the screen */
        const int X_Axis = X_RESOLUTION - 40;
        const int Y_Axis = Y_RESOLUTION - 40;
    
        for (int i = 0; i < Size; i++)
        {
            do {
                Objs[i].X = rand() % X_Axis + 1;
                Objs[i].Y = rand() % Y_Axis + 1;
            } while (Has_Overlap(Objs[i], Objs, i));
        }
    }