How to add collision properly in raylib C++?
02:03 05 May 2026

So basically, my collision code only works if the object is completely stationary. What I want to do is that I want my rectangle to be running at the ball, and then the moment it collide, it actually touches instead of the rectangle simply going through it. How do I do that?

This is my code right now:

//Collision: 
        float prev_x = ball.x - ball.speed_x;
        float prev_y = ball.y - ball.speed_y;

        if(CheckCollisionCircleRec(Vector2{ball.x, ball.y}, ball.radius, Rectangle{player.x, player.y, player.width, player.height})){
            if(prev_y + ball.radius <= player.y){
                ball.y = player.y - ball.radius;
                ball.speed_y *= -1;
            }
            //Left
            else if(prev_x + ball.radius <= player.x){
                ball.x = player.x - ball.radius;
                ball.speed_x *= -1;   
            } 
            //Right
            else if(prev_x - ball.radius >= player.x + player.width){
                ball.x = player.x + player.width + ball.radius;
                ball.speed_x *= -1;
            }
        }
c++ game-development raylib