# Ball bounces off the movable bats (extended PONG version)

I am creating an advanced version of the game Pong, where the bats can also move in the x-direction. I am trying to write a code to make the ball bounce off the bats on all four sides of the two bats. I was just able to find codes where the bats are only moveable in y-direction.

It is just an excerpt of the code.
x and y demonstrates the position of the ball, d its diameter.

I do not know why the ball is not reacting to the borders of the bats.
I would be happy if someone could help me! (I am at the beginning of my programming career, so please don’t be too harsh with me)

void reactToBorder () {

//bounce off the left side of bat2
if (x+d/2 == bat2.x && x+d/2 >= bat2.y && x+d/2 <= bat2.y+100) {
dx=dx*-1;
}

//bounce off the right side of bat2
if (x-d/2 == bat2.x+20 && x-d/2 >= bat2.y && x-d/2 <= bat2.y+100) {
dx=dx*-1;
}

//bounce off the bottom of bat2
if (y-d/2 == bat2.y+100 && y-d/2 >= bat2.x && y-d/2 <= bat2.x+20) {
dy=dy*-1;
}

bounce off the top of bat2
if (y+d/2 == bat2.y && y+d/2 >= bat2.x && y+d/2 <= bat2.x+20) { //
dy=dy*-1;
}

//bounce off the right side of bat1
if (x-d/2 == bat1.x && x-d/2 >= bat1.y && x-d/2 <= bat1.y+100) { //bounce off the right side of bat1
dx=dx*-1;
}
if (x+d/2 == bat2.x-20 && x+d/2 >= bat2.y && x+d/2 <= bat2.y+100) {
dx=dx*-1;
}

//bounce off the bottom of bat1
if (y-d/2 == bat1.y+100 && y-d/2 >= bat1.x && y-d/2 <= bat1.x+20) {
dy=dy*-1;
}

bounce off the top of bat1
if (y+d/2 == bat1.y && y+d/2 >= bat1.x && y+d/2 <= bat1.x+20) { //
dy=dy*-1;
}
}

1 Like

Hi,

Welcome to the forum! Can you please format your code by using the `</>` button in the message editor or put bacticks ``` around your code?

By bats, do you mean the animal? I’ll refer to a paddle in my comment The error might be that when you check a collision with the paddles, it depends on the speed of the ball. Let see this simple figure : So at time `0`, the ball is outside the paddle but his velocity is directed toward the paddle.
→ You check the collision, all the conditions are false

Then at time `1`, the paddle moves and it’s now inside the paddle
→ Problem : none of the conditions are true either because in all the conditions you are first checking equality!!

In fact as you probably know, floating point numbers can’t be exactly represented by a computer because the decimal part of a rational number can be an infinite suite of decimals.

For example the decimal representation of one third is `1 / 3 = 0.33333333333333....` (repeats infinitely)

But a computer can’t store that because it’s memory is finite. That’s why floating point numbers have precision.

This can be an issue because a limited precision can introduce rounding errors and other problems.

That’s why checking equality can be wrong sometimes, check this example :

``````float a = 1.00000001;
float b = 1.0;

println(a == b); // true!!
``````

which prints `true`, how ugly is that Also see the https://0.30000000000000004.com/ (you can test in Java : `0.1 + 0.2 == 3``false`)

The above part is not directly related to your issue thought but it’s worth mentioning.

The real problem is that when doing a physics simulation, you need a step which is a time interval between things are calculated (the ball is not moving continuously but position to position).

Again it’s because computers can’t represent things continuously but are forced to quantize the movement here for example.

It means that if your ball moves too fast, then at the moment after, it’s going to go through the wall but checking if it’s coordinate is equal to the one of the paddle is not going to work !

So the best way to check float equality is to have a threshold that is small enough so the numbers are considered equal :

``````float xPaddle = 0f;
float xBall = 0.001f;

// Small enough so the ball can go inside and still be considered touching
float threshold = 0.01f;

println(abs(xPaddle - xBall) < threshold); // -> true
``````