Thread: Position problem.

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008

    Position problem.


    Sorry for this thread but i already solved the problem.
    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:

    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:
    if(BallCurrentLocation->x < (Player2CurrentLocation->x - this->ObjBall->ReturnBallWidth()))
    				SizeBat = Player2CurrentLocation->y + Player2->ReturnBatHeigth();
    				if(BallCurrentLocation->y == Player2CurrentLocation->y || BallCurrentLocation->y <= SizeBat)
    					this->Balldirection= true;
    					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,



    I think this statements stinks.
    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)
    Last edited by Jelte; 07-02-2010 at 06:52 AM.
    The Programming Dutchman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 05:02 AM
  2. Infinite loop output problem
    By Jonyb222 in forum C Programming
    Replies: 5
    Last Post: 10-08-2009, 11:18 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
Website Security Test