So this was an interesting experience, I didn’t follow the tutorial that closely cause I’m a bit more advanced in experience than this section was targeted at but it was kinda fun. I actually went another direction with collision entirely however to account for the radius of the circle over treating it like a square with a bit of maths taking the 2d coordinates of the circle and edges of the rect and checking if their distance is less than the radius of the circle. it’s a bit more intense math wise using sqrt and pow many times each loop but I cut back on calls when further away by checking if the circle and rectangle are anywhere close enough to potentially collide in the first place. This could be simplified massively if you turn the “axe” into a circle also cause you would then only need to check if the range is less than each radius added together
My final product
#include "raylib.h"
#include "raymath.h"
char gameOverText[] = "Game Over\nPress R to Restart";
int width{500};
int height{350};
int circlePosX{100};
int circlePosY{100};
int circleRadius{25};
int rectPosX{width/2};
int rectPosY{0};
int rectSize{50};
int rectDirection{5};
int moveSpeed{3};
int runMultiplier{3};
bool gameOver{};
bool isStrafing() {
return ((IsKeyDown(KEY_W) || IsKeyDown(KEY_S)) && (IsKeyDown(KEY_A) || IsKeyDown(KEY_D)));
}
int getSpeed() {
if(IsKeyDown(KEY_LEFT_SHIFT)) { // Running?
return isStrafing() ? (moveSpeed * runMultiplier) * 0.8 : moveSpeed * runMultiplier;
} else {
return isStrafing() ? moveSpeed * 0.8 : moveSpeed;
}
}
int limitBounds(int input, int max) {
return Clamp(input, 0 + circleRadius, max - circleRadius);
}
void HandleInput() {
if(IsKeyDown(KEY_D)) {
circlePosX = limitBounds(circlePosX + getSpeed(), width);
}
if(IsKeyDown(KEY_A)) {
circlePosX = limitBounds(circlePosX - getSpeed(), width);
}
if(IsKeyDown(KEY_W)) {
circlePosY = limitBounds(circlePosY - getSpeed(), height);
}
if(IsKeyDown(KEY_S)) {
circlePosY = limitBounds(circlePosY + getSpeed(), height);
}
}
int inRange(int ax, int ay, int bx, int by, int range) {
return sqrt(pow(ax - bx, 2) + pow(ay - by, 2) * 1.0) < range;
}
void HandlePhysics() {
int rectX = rectPosX + (rectSize / 2);
int rectY = rectPosY + (rectSize / 2);
if (inRange(rectX, rectY, circlePosX, circlePosY, circleRadius + rectSize)) { // close
if (
inRange(rectPosX, rectPosY, circlePosX, circlePosY, circleRadius) ||
inRange(rectPosX + rectSize, rectPosY, circlePosX, circlePosY, circleRadius) ||
inRange(rectPosX, rectPosY + rectSize, circlePosX, circlePosY, circleRadius) ||
inRange(rectPosX + rectSize, rectPosY + rectSize, circlePosX, circlePosY, circleRadius)
) {
gameOver = true;
}
}
}
void Render() {
DrawCircle(circlePosX, circlePosY, circleRadius, BLUE);
DrawRectangle(rectPosX, rectPosY, rectSize, rectSize, RED);
rectPosY += rectDirection;
if (rectPosY > height - rectSize || rectPosY < 0) {
rectDirection = -rectDirection;
}
}
int main() {
InitWindow(width, height, "My Game");
SetTargetFPS(60);
while(!WindowShouldClose()) {
BeginDrawing();
ClearBackground({20, 20, 20, 255});
if (!gameOver) {
HandleInput();
HandlePhysics();
Render();
} else {
int textWidth = MeasureText(gameOverText, 20);
DrawText(gameOverText, (width*0.5)-(textWidth*0.5), height/2, 20, RED);
if(IsKeyDown(KEY_R)) {
circlePosX = 100;
circlePosY = 100;
gameOver = false;
}
}
EndDrawing();
}
}