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();
}
}