Weird collision, Game Over when not touching the axe edges

I was trying the code and I noticed that sometimes the check will make the game run into the game over state even if the circle does not touch the axe sometimes.

I noticed this when I added a small code snippet to restart the game after the game runs into the game over state.

    if(collision_with_axe == true)
    {
            if(IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_A) || IsKeyDown(KEY_D))
            {
                collision_with_axe = false;
                circle_x = 0;
            }
    }

So sometimes it will reset my position without getting the Game Over message.

I know for a fact that this is not happening because of keeping the keys pressed down, since the game will stop executing and show the Game Over message and make you press down a key again to return to a new game.

Am I the only one who’s getting this issue?

This is my whole axe game cpp

#include "raylib.h"

int main()
{
    int width = 400;
    int height = 300;
    InitWindow(width, height, "MyWindow");
    
    //circle
    int circle_x = 0;
    int circle_y = height/2;
    float 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
    int axe_x = width/2;
    int axe_y = 0;
    int axe_lenght = 50;
    //axe edges
    int l_axe_x = axe_x;
    int r_axe_x = axe_x + axe_lenght;
    int u_axe_y = axe_y;
    int b_axe_y = axe_y + axe_lenght;

    //physics
    int direction = 10;
    
    //collision
    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() == false)
    {
        BeginDrawing();
        ClearBackground(WHITE);

        if(collision_with_axe)
        {
            DrawText("GAME OVER\nTry Again?", width/2, height/2, 20 , RED);
        } else 
        {
            //Game logic
            //update edges
            l_circle_x = circle_x - circle_radius;
            r_circle_x = circle_x + circle_radius;
            u_circle_y = circle_x - circle_radius;
            b_circle_y = circle_x + circle_radius;

            l_axe_x = axe_x;
            r_axe_x = axe_x + axe_lenght;
            u_axe_y = axe_y;
            b_axe_y = axe_y + axe_lenght;

            //update collision
            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);

            //draw stuff
            DrawCircle(circle_x, circle_y, circle_radius, BLUE);
            DrawRectangle(axe_x, axe_y, axe_lenght, axe_lenght, RED);
            axe_y += direction;
            if (axe_y > height || axe_y < 0)
            {
            direction = -direction;
            }

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

        }

        if(collision_with_axe == true){
            if(IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_A) || IsKeyDown(KEY_D)){
                collision_with_axe = false;
                circle_x = 0;
            }
        }
        
        EndDrawing();
    }
}

Try moving your restart code inside of the if-statement for collision_with_axe.

The way your code works now, if there’s a collision it’ll check for the keys pressed immediately after the collision since the display of the game over message is in the next frame.

And if you’re pressing the correct keys in the same frame as the collision, it’ll reset. Since you have A and D as both movement and reset keys then it would reset the same frame before given the chance to display the game over message on the next frame.

I removed the code altogether but the issue still persists.

unknown_2022.04.29-20.12

I know this is a gif but it is the best way I have to show you what I mean.

in this part of your coding:

your updating u_circle_y and b_circle_y with circle_x while it should be with circle_y.

so you have:

u_circle_y = circle_x - circle_radius; 
 //which should be
u_circle_y = circle_y - circle_radius;

and

b_circle_y = circle_x + circle_radius;
//which should be
b_circle_y = circle_y + circle_radius;

I had a similar problem later in the coding, check if that help you out!

(I’m quite new to this myself, so I might be mistaken though)

*edited to make it easier to read

1 Like

Nice catch!

1 Like

completely missed that! I’m so sorry!

No need to apologize! Sometimes it takes another set of eyes (or even multiple) to identify the problem.

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

Privacy & Terms