Circle doesn't touch the rectangle and I get a game over

Why do I get a “Game Over” when the circle doesn’t touch the rectangle? I approach the rectangle and as soon as I put the circle in the same path the rectangle is going, I get a Game Over without touching the rectangle.

#include "raylib.h"
int main()
{
    // window dimensions
    int width{800};
    int height{450};
    InitWindow(width, height, "My Window");

    //Circle cordinates
    int circle_x{200};
    int circle_y{200};
    int circle_radius{25};
    
    // circle edges
    int l_circle_x{circle_x - circle_radius};
    int r_circle_x{circle_x + circle_radius};
    int u_circle_y{circle_y - circle_radius};
    int b_circle_y{circle_y + circle_radius};


    // axe cordinates
    int axe_x{400};
    int axe_y{0};
    int axe_length{50};

    // Axe edges
    int l_axe_x{axe_x};
    int r_axe_x{axe_x + axe_length};
    int u_axe_y{axe_y};
    int b_axe_y{axe_y + axe_length};

    int direction{10};

    bool collision_with_axe = 
                            (b_axe_y >= u_circle_y) && 
                            (u_axe_y <= b_circle_y) && 
                            (l_axe_x <= r_circle_x) && 
                            (r_axe_x >= l_circle_x);

    SetTargetFPS(60);
    while (WindowShouldClose() != true)
    {
        BeginDrawing();

        ClearBackground(WHITE);

        if (collision_with_axe)
        {
            DrawText("Game Over!!!", 400, 200, 20, RED);
        }

        else
        {
            // Game logic drawing begins

            // update the edges
            l_circle_x = circle_x - circle_radius;
            r_circle_x = circle_x + circle_radius;
            u_circle_y = circle_y - circle_radius;
            b_circle_y = circle_y + circle_radius;
            l_axe_x = l_axe_x;
            r_axe_x = r_axe_x + axe_length;
            u_axe_y = u_axe_y;
            b_axe_y = b_axe_y + axe_length;
            //update collision_with_axe
            collision_with_axe = 
                            (b_axe_y >= u_circle_y) && 
                            (u_axe_y <= b_circle_y) && 
                            (l_axe_x <= r_circle_x) && 
                            (r_axe_x >= l_circle_x);



        DrawCircle(circle_x, circle_y, circle_radius, BLUE);
        DrawRectangle(axe_x, axe_y, axe_length, axe_length, RED);

        //move the axe
        axe_y += direction;
        if(axe_y > height || axe_y < 0)
        {
            direction = -direction;
        }

        
        if(IsKeyDown(KEY_D) && circle_x < width)
        {
            circle_x += 10;
        }
        if(IsKeyDown(KEY_A) && circle_x > 0)
        {
            circle_x -= 10;
        }

        // Game logic ends
        }

        
        EndDrawing();
    }


}

Hi HScorching,

There’s two things to look at when troubleshooting collision issues. First, is the bounds-check correct? Second, is the bounding box for both objects correct?

Thankfully, the answer to our first test is yes, as the bounds check looks correct.

The bounding “box” for your circle looks correct, however let’s take a closer look at the bounding box for your axe.

l_axe_x = l_axe_x;
r_axe_x = r_axe_x + axe_length;
u_axe_y = u_axe_y;
b_axe_y = b_axe_y + axe_length;

You’re assigning values to themselves here when you shouldn’t be. For example, l_axe_x = l_axe_x should be l_axe_x = axe_x, this holds true for right, up, and bottom portions of the axe.

This means the bounds of your axe are not properly updating based on the actual position of the axe.

1 Like

Thank you. That was the issue. I just corrected the code and it works as it should now.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms