Hello,
Ive researched a lot about this problem but i dont understand their logics about this.
so i have this code
void Ball::update(const sf::RenderWindow& window,Paddle& p1,Paddle& p2,float delta)
{
if ( isBallCollidedToPaddle(p1) )
{
if ( pongball.getPosition().y < p1.getPad().getPosition().y )// if the ball collides to the upper part of player 1 paddle
{
if ( angle > 90 && angle < 180 )
{
angle = 180 - angle;
}
else
{
angle = -angle;
}
}
else if ( pongball.getPosition().y > p1.getPad().getPosition().y )// if the ball collides to the lower part of player 1 paddle
{
if ( angle > 90 && angle < 180 )
{
angle = -angle;
}
else
{
angle = 180 - angle;
}
}
}
else if ( isBallCollidedToPaddle(p2) )
{
if ( pongball.getPosition().y < p2.getPad().getPosition().y ) // if the ball collides to the upper part of player 2 paddle
{
if ( angle < 90 && angle > 0 )
{
angle = 180 - angle;
}
else
{
angle = -angle;
}
}
else if ( pongball.getPosition().y > p2.getPad().getPosition().y ) // if the ball collides to the lower part of player2 paddle
{
if ( angle < 90 && angle > 0 )
{
angle = -angle;
}
else
{
angle = 180 - angle;
}
}
}
pongball.move(speed.x * cos(angle*Ball::pi/180)*delta,speed.y * sin(angle*Ball::pi/180)*delta);
}
so far this code is only able to do the basic reflection,I based the reflection from ponggame.org
, but i want to know how to make it reflect as horizontal, for example if the ball collides at the center of the paddle the ball will reflect horizontally, and also the reflection for when the ball collides on the edge of the paddle
some people reflects the ball depends on how far it collides from the middle of the paddle, i dont want to copy paste their computation without understanding it so can anyone elaborate the logic for this way?
thanks and sorry for my english