Why does collisionWithAxe variable need updating each frame?

I am curious as to why we need to add the code for collisionWithAxe a second time? as it doesn’t seem very DRY. I ran the code without it and the collision seems to be detected. Am I missing something? See my code below:

#include “raylib.h”

int main() {

// SET window dimensions and initialise it

int width{800}, height{600};

InitWindow(width, height, "It's not an Axe, it's a rectangle");

//SET inital positions for the circle and "axe"

int circleX{200}, circleY{300}, circleRadius{25};

int axeX{400}, axeY{0}, axeLength{50};

int direction{10};

// circle edges

int l_circleX{circleX - circleRadius}, r_circleX{circleX + circleRadius};

int u_circleY{circleY - circleRadius}, b_circleY{circleY + circleRadius};

// axe edges

int l_axeX{axeX}, r_axeX{axeX + axeLength};

int u_axeY{axeY}, b_axeY{axeY + axeLength};

SetTargetFPS(60);

while ( !WindowShouldClose() ) {

    BeginDrawing();

    ClearBackground(WHITE);

   

    bool collisionWithAxe{( b_axeY >= u_circleY ) &&

                          ( u_axeY <= b_circleY ) &&

                          ( l_axeX <= r_circleX ) &&

                          ( r_axeX >= l_circleX )};

    if ( collisionWithAxe ) {

        DrawText("Game Over!", 250, 250, 50, RED);

    } else {

        // Game Logic begins

        //update edges

        l_circleX = circleX - circleRadius;

        r_circleX = circleX + circleRadius;

        u_circleY = circleY - circleRadius;

        b_circleY = circleY + circleRadius;

        l_axeX = axeX;

        r_axeX = axeX + axeLength;

        u_axeY = axeY;

        b_axeY = axeY + axeLength;

        DrawCircle(circleX, circleY, circleRadius, BLUE);

        DrawRectangle(axeX, axeY, axeLength, axeLength, RED);

        // Move Axe

        axeY += direction;

        if (axeY > height || axeY < 0) {

            direction *= -1;

        }

        if ( IsKeyDown( KEY_RIGHT ) && circleX < width ) {

            circleX += 10;

        } else if ( IsKeyDown( KEY_LEFT )  && circleX > 0 ) {

            circleX -= 10;

        }

        // Game Logic ends

    }

   

    EndDrawing();

}

}

Not having the collision be updated inside of the loop means the collision will stay in one spot no matter where the “axe” actually is.

We do it outside of the loop the one time so we can have everything initialized, then we update it in the loop to match the position of the axe.

The same is true of our circle (ie. the player).

I would say if anything, why does it need to be initialized like that since you aren’t going to collide until the game begins. It seems a bit like overkill. But since we move the render each frame doesn’t it make sense to move the collision too?

It’s good practice to initialize your variables the moment you declare them so we avoid having those variables contain weird data. This isn’t as important for booleans but it’s a good habit to form.

As far as updating the position of the collisions. This is what we achieve by check for collision in the loop, as that’s where we get updated player and axe positions.

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

Privacy & Terms