# Position problem.

Printable View

• 07-02-2010
Jelte
Position problem.
Solved

Sorry for this thread but i already solved the problem.
Solution:
Code:

`if(BallCurrentLocation->y >= Player1CurrentLocation->y && BallCurrentLocation->y <= SizeBat)`
Hello Everybody,

I write a Pong game. Now I have a little problem with validating the position of the ball. This is the situation:

I made the basic algorithm that moves the ball to the left and the right of the screen when it hits the left/right side of the screen, is works fine. But now i try to include the option: "when the ball hits a players bat change direction". what i made seems to work but i discovered that is doesn't work 100% because sometimes the ball bounces back when the ball misses the players bat.

schema of my screen, see improvised images: http://static.tz.nl/f/g/47234_4053113b.jpeg

so check if the ball hits a players bat i have:

- y,x position on both bats.
- height of both bats.
- width of the ball.

I question is,how can I check with a if statement, if the ball hits a bat or not:

this my code snippet:
Code:

```..... if(BallCurrentLocation->x < (Player2CurrentLocation->x - this->ObjBall->ReturnBallWidth()))                         {                                 BallCurrentLocation->x++;                                }                         else                         {                                 SizeBat = Player2CurrentLocation->y + Player2->ReturnBatHeigth();                                 if(BallCurrentLocation->y == Player2CurrentLocation->y || BallCurrentLocation->y <= SizeBat)                                 {                                         this->Balldirection= true;                                 }                                 else                                 {                                         BallCurrentLocation->x++;                                                                                 if(BallCurrentLocation->x == this->ReturnWindowWidth() - this->ObjBall->ReturnBallWidth())                                         {                                                 Gamestate = false;                                         }                                        } .....```
explanation of code:

first if the ball x position doesn't match the players bat, i moves the ball, but when the ball x position matches the x position of the ball if looks if the position of the ball is between the bat y position + the bats height (because the the bat position is the top of the bat ofcourse). if that is true change direction.

Can you Folks see if this is a correct algorithm, and haves no logical leaks?

Thanks for helping,

Jelte.

EDIT:

I think this statements stinks.
Code:

`if(BallCurrentLocation->y == Player2CurrentLocation->y || BallCurrentLocation->y <= SizeBat)`
I Also discovered that the "bug" (so the ball bounces back but i doesn't hit the players bat) only happens when the bat are "under the ball" so the players bat x position is larger then then ball x position)