Collision detection not updating

If circle is initialized in axe then fail state is achieved, however this cannot be done in the intended way. I’ve compared this to the examples but can’t seem to figure what I’ve gotten wrong.

#include “raylib.h”

int main()

{

#include "raylib.h"

int main()
{
    // Window Dimensions, and title declared
    int width{800};
    int height{450};
    InitWindow(width,height,"Ultra-Sim"); 

    //Circle Coordinates
    int circle_x{200};
    int circle_y{200};

    int circle_r{25};
    //Circle Edges
    int l_circle_x{circle_x - circle_r};

    int r_circle_x{circle_x + circle_r};

    int u_circle_y{circle_y - circle_r};

    int b_circle_y{circle_y + circle_r};

    //Axe Coordinates
    int axe_x{400};
    int axe_y{0};

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

    int direction{10};

         // Fail State
     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);

        // Game Logic Begin
        
    while ( WindowShouldClose() != true ) 
    {
        BeginDrawing();
        ClearBackground(WHITE);


 if(collision_with_axe)
{
          DrawText("FAILSTATE:SUBJECT TERMINATED",400,300,20,RED);
    }

        else

    {
        // Update collision
        l_circle_x = circle_x - circle_r;
        r_circle_x = circle_x + circle_r;
        u_circle_y = circle_y - circle_r;
        b_circle_y = circle_y + circle_r;

        l_axe_x = axe_x;
        r_axe_x = axe_x + axe_l;
        u_axe_y = axe_y;
        u_axe_y = axe_y + axe_l;
    

        // 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_r,BLUE);
        DrawRectangle(axe_x,axe_y,axe_l,axe_l,RED);
    

        //Axe Movement
        axe_y += direction;

        if(axe_y > height || axe_y < 0 )
        
        {
            direction = -direction;
        }


        //Movement
        if( IsKeyDown(KEY_D) && circle_x < width)
        {
            circle_x += 10;
        }

         if( IsKeyDown(KEY_A) && circle_x > 25)
        {
            circle_x -= 10;
        }


        //End Game Logic
        
    }
    
        EndDrawing();
    }
}

}

Thanks for taking a look at this. I feel like it’s something silly, haha

After some back tracking, I was able to get the program to function properly, and after comparing what I posted and the working code. The only difference I noticed, was the order in which I placed

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

and

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

However I don’t see how that being out of order would have caused the issue. So perhaps that isn’t it.

Tested it out myself to make sure, but you’re correct that it must have been something else. Since that boolean expression needs everything part of it to be true in order for the result to be true, so order would not matter in this case.

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

Privacy & Terms