Fixing the axe collision with bottom edge of screen

So one thing that was kind of left on the table was fixing the axe passing through the bottom of the screen (because the axe’s y position is it’s top left corner).
I tried changing the code for moving the axe to:

//move the axe

        axe_pos_y += axe_direction;

        if (bottom_axe_y > canvas_height || axe_pos_y < 0)
        {
            axe_direction = -axe_direction;
        }

where the bottom_axe_y variable is axe_pos_y + axe_length (defined at the beginning of the program)
but then the axe seems to get stuck at the bottom.

But if I do it like this it does work:

//move the axe

        axe_pos_y += axe_direction;

        if ((axe_pos_y+axe_length) > canvas_height || axe_pos_y < 0)
        {
            axe_direction = -axe_direction;
        }

…and I don’t understand why the first one doesn’t work.

Where in the code do you update bottom_axe_y? That can play a factor in how the rest of your code behaves.

Basically in the part right after “game logic begins” but before drawing and moving the shapes.

//game logic begins

        //update circle edges

        left_circle_x = circle_x - circle_radius;

        right_circle_x = circle_x + circle_radius;

        up_circle_y = circle_y - circle_radius;

        bottom_circle_y = circle_y + circle_radius;

        //update axe edges

        left_axe_x = axe_pos_x;

        right_axe_x = axe_pos_x + axe_length;

        up_axe_y = axe_pos_y;

        bottom_axe_y = axe_pos_y + axe_length;

        //update collision with axe

        collisionWithAxe =

                (bottom_axe_y >= up_circle_y) &&

                (up_axe_y <= bottom_circle_y) &&

                (left_axe_x <= right_circle_x) &&

                (right_axe_x >= left_circle_x);

        //draws a circle

        DrawCircle(circle_x, circle_y, circle_radius, PINK);

       

        //draws a rectangle (axe)

        DrawRectangle(axe_pos_x, axe_pos_y, axe_length, axe_length, RED);

        //move the axe

        axe_pos_y += axe_direction;

Tested this out with the master project for the course. And the issue is an order-of-operations issue.

Since bottom_axe_y is being calculated before the axe is moved, it’s always one frame behind. So once it gets to the bottom of the screen it’s going to get it’s direction flipped a little too late and not be able to get from under the bottom of the screen before it flips direction again.

There’s viable two solutions to this:

  1. Reverse the order of operations so that axe_pos_y is calculated before bottom_axe_y, this has the side benefit of collisions being more frame-accurate

  2. Add a line in inside of the if-statement so that it not only flips axe_direction but also sets axe_pos_y to the bottom edge of the screen. But only if it’s reached the bottom.

Personally, option #1 feels like the more elegant solution here. Since it also resolves a second bug that isn’t easily noticeable at first glance.

Do you mean operations within the while loop? Because axe_pos_y is first calculated (or set) before the loop starts where we are defining the variables.

Should I move the code that updates the axe edges (in the while loop) after the code to move the axe?
I tried that and it doesn’t seem to make a difference.

Which lines would you move in this code of mine?

while ( WindowShouldClose()==false )

{

    BeginDrawing();

    ClearBackground(WHITE);

           

    // check collision with axe

    if (collisionWithAxe)

    {

        DrawText("GAME OVER", 350, 200, 20, RED);

    }

    else

    {

        //game logic begins

        //update circle edges

        left_circle_x = circle_x - circle_radius;

        right_circle_x = circle_x + circle_radius;

        up_circle_y = circle_y - circle_radius;

        bottom_circle_y = circle_y + circle_radius;

        //update axe edges

        left_axe_x = axe_pos_x;

        right_axe_x = axe_pos_x + axe_length;

        up_axe_y = axe_pos_y;

        bottom_axe_y = axe_pos_y + axe_length;

        //update collision with axe

        collisionWithAxe =

                (bottom_axe_y >= up_circle_y) &&

                (up_axe_y <= bottom_circle_y) &&

                (left_axe_x <= right_circle_x) &&

                (right_axe_x >= left_circle_x);

        //draws a circle

        DrawCircle(circle_x, circle_y, circle_radius, PINK);

       

        //draws a rectangle (axe)

        DrawRectangle(axe_pos_x, axe_pos_y, axe_length, axe_length, RED);

        //move the axe

        axe_pos_y += axe_direction;

   

        if (bottom_axe_y > canvas_height || axe_pos_y < 0)

        {

            axe_direction = -axe_direction;

        }

 

        //implement circle left right movement

        if (IsKeyDown(KEY_D) && right_circle_x < canvas_width)

        {

            circle_x += circleMoveSpeed;

        }

        if (IsKeyDown(KEY_A) && left_circle_x > 0)

        {

            circle_x -= circleMoveSpeed;

        }

                   

        //game logic ends

    }

    EndDrawing();

}

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

Privacy & Terms