Saturday, May 18, 2024
HomeGame Developmentc++ - Breakout Game Ball Reflection after hitting paddle

c++ – Breakout Game Ball Reflection after hitting paddle


I am working on a Breakout game. My requirements are when the ball hit the paddle in the center it goes straight up regardless of the direction it was coming from, then for the left and right it reflect at a less steeper angle as we move towards left or right. Also when it hit the edge it goes back in the direction it was coming from.

void GameControl::paddle_ball_Collision(){
    
    if(ball.getX() + ball.getRadius() >= paddle.getX() && 
       ball.getX() - ball.getRadius() < paddle.getX() + paddle.getWidth() && 
       ball.getY() + ball.getRadius() > paddle.getY() + paddle.getHeight() && 
       ball.getY() - ball.getRadius() < paddle.getY() + paddle.getHeight()){
        
        ball.setYVelocity(-ball.getYVelocity());

        int velocityX = 1 - 2*(paddle.getX() - ball.getX()) / (paddle.getWidth() / 2);
        ball.setXVelocity(velocityX);
    }
}

This logic is partially working. But there’s an issue. The right side of paddle is working fine. But the left side, when the ball hit left side it it goes straight up and as we move towards the angle gradually gets less steeper. Its also working fine for the right egde. Ball goes back in the direction it was coming from if it hits the egde.

How can I fix this for proper functionality??

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments