# calculate pong angle sometimes right sometimes wrong

I'm workin on a pong-game and i tried to implement the bounce of the paddle. I used the answears in this question In Pong, how do you calculate the ball's direction when it bounces off the paddle? but sometime the angle is right and sometimes the angle is definitly wrong and i dont know why. I think i got something with the radian and the degrees wrong.

This is my canvas class where i draw the ball and the paddles:

public class DrawView extends View {
private Paint paint;
private Point ball;
private float x;

private int strokeWidth;

public DrawView(Context context) {
super(context);
paint = new Paint();
strokeWidth = 8;
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Style.STROKE);
x = 400;
ball = new Point(400,400);
}

public DrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(ball.x,ball.y,5,paint);
canvas.drawLine(x,50,x +150,50, paint);
canvas.drawLine(x,900,x+150,900, paint);
}

public void setBall(Point ball) {
this.ball = ball;
}

public Point getBall() {
return ball;
}

@Override
public void setX(float x) {
this.x = x;
}

@Override
public float getX() {
return x;
}
public int getStrokeWidth() {
return strokeWidth;
}

public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
}
}


and this id the class where i calculate the next step:

public class PongActivity extends AppCompatActivity {

private RelativeLayout layout;
private Point ball;
private DrawView canvas;
private Context context;
private float degree;
private float speed;
private Display display;
private Point size;
int screenWidth;
int screenHeight;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pong);
layout = findViewById(R.id.layout);
context = this;
canvas = new DrawView(context);
speed = 10;
degree = 45;
display  = getWindowManager().getDefaultDisplay();
size  = new Point();
canvas.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
canvas.setX(motionEvent.getX());
canvas.invalidate();
return true;
}
});
}

private boolean calculateDegree() {
display.getSize(size);
screenHeight = size.y;
screenWidth = size.x;

if (ball.x < 0 || ball.x > screenWidth) {
degree = 180 - degree;
fly();
return true;
}
else if (ball.y < 0 ||ball.y > screenHeight - 50) {
degree = 360 - degree;
fly();
return true;
}else if (ball.y < 58 && ball.y > 50 && ball.x > canvas.getX() && ball.x < canvas.getX() + 150 || ball.y > 900 && ball.y < 908 && ball.x > canvas.getX() && ball.x < canvas.getX() + 150) {
float middle =  ((canvas.getX() + 75) - ball.x);
degree = (float) (middle * maxAngle);
degree = (float) Math.toDegrees(degree);
fly();
return true;
}else{
return false;
}

}

public void fly(){
ball = canvas.getBall();
float nextX = (float) Math.cos(degree * Math.PI /180) * speed;
float nextY = (float) -Math.sin(degree * Math.PI /180) * speed;
ball.set((int) (ball.x + nextX), (int) (ball.y + nextY));
canvas.setBall(ball);
canvas.postInvalidate();

}

@Override
public void run() {
do{
try {
sleep(10);
ball = canvas.getBall();
if(calculateDegree()) {
calculateDegree();
}else{
fly();
}
} catch (InterruptedException e) {
e.printStackTrace();
}

}while(ball.y > 0 || ball.y < screenHeight);
}

}
}

• Can you show us a reproducible test case where this method gives the wrong result, and what result you expect instead for that case? – DMGregory Dec 21 '19 at 2:45