https://github.com/H1Tech/Pong/tree/master/Pong/src/game
The above is the entire source of the current version of the game.
private void ballColide() {
int diameter = Pong.pong.ball.getDiameter();
int ballX = Pong.pong.ball.getX() - diameter;
int ballY = Pong.pong.ball.getY() - diameter;
double relInt = (y+(height/2)-ballY);
double normInt = (relInt/(height/2));
double angle = normInt * Pong.pong.ball.MAX_ANGLE;
long velX = Math.round(Pong.pong.ball.SPEED*Math.cos(angle));
long velY = Math.round(Pong.pong.ball.SPEED*Math.sin(angle));
if(paddleNum == 1){
if(ballX - Pong.pong.ball.velX <= x+width/2 && ballX - Pong.pong.ball.velX >= 0){
if(ballY <= y+height/2 && ballY >= y-height/2){
Pong.pong.ball.setVel(velX, velY);
}
}
}
if(paddleNum == 2){
if(ballX + diameter + Pong.pong.ball.velX >= x-width/2 && ballX + diameter + Pong.pong.ball.velX <= Pong.pong.width){
if(ballY <= y+height/2 && ballY >= y-height/2){
Pong.pong.ball.setVel(velX, velY);
}
}
}
}
The code above is the current collision detection system.
My problem is that sometimes the ball phases through the paddle, and other times the ball gyrates on the paddle.
I would very much appreciate tips on how to fix this.
Please keep in mind that I am a pre-college student (Starting this fall). I just opened what we are supposed to be learning (JFrames without libraries). Most likley this code is unorthodox or against common standards. If you have the time, would you mind telling me what I got wrong/right or otherwise. I appreciate your time.