Thread: How to create a bouncing ball?

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    How to create a bouncing ball?

    Hi,

    I am trying to make a well known game (I forget the name) where you control a rectangle 'racket' at the bottom of the screen, move the racket left and right and deflect a ball up the screen to hit and destroy blocks.

    Well I'm stuck with how to keep the ball bouncing around the screen:

    http://img169.imagevenue.com/img.php...122_1099lo.jpg

    In the image I have linked to above I've wrote some if statements to dectect when the ball has reached the side of the screen, at which point I plan to call a function to move the ball in it's deflected angle, but :

    A matches E, B matches D, C matches G and D matches B.

    The only difference is their preceeding states (the direction of the ball before it hits the edge) so I figure I need to use this in combination with the if statement, but I am unsure how.

    I considered using a switch statement, but as that only has 1 argument, thought this won't work.

    So basically I am looking for some advice on how best to achieve a continusously bouncing ball around the screen.

    ( I am aware I have not yet addressed balls which hit the corners exactly, ie: 0,0)

    If anyone can offer me some advice, I would be most appreciative.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are looking at 2D collision detection, right? That should be possible to find with google.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    No, I'm wanting to achieve a continusously bouncing ball around the screen, I'm not yet looking at that.

    I am at an internet cafe as I have no intenet at home so hoped I could ask here and check back later in the day in the hope for some advice.

    So, basically I'm looking for advice on how to choose the if statement to execute - how to include the balls previous direction/state so as to execute the right option next, so that as a whole there are no matching options (previous state + option).

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Depends on how you want to do it. If you want to do it in an ms-dos console search for how to get the coordinates of the cursor in the screen. If you want to do it on a win32 window there's an example on how to do it in forger's win32 tutorial... http://www.winprog.org/tutorial/ Or more specifically http://www.winprog.org/tutorial/animation.html

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the colision detection is just a comparison like you suggest. There should just be two checks for each dimension (X, Y) - something like
    Code:
    ball.x <= left || ball.x >= right || ball.y >= top || ball.y <= bottom
    .
    Then you need to reflect the incoming angle as the bounce angle. A very quick think about it makes me thing that if dx and dy is the movement per timeunit, I'm pretty sure that you just reverse the direction of the side you hit (so if you hit on the x axis, dx = -dx).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> ball.x <= left || ball.x >= right || ball.y >= top || ball.y <= bottom

    Depends where the .y and .x coordinates are taken from. You might need to include a ball.height and a ball.width in those calculations too.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    Well, a simple bouncing algorithm is actually very easy.

    First, declare two variables for the x and y velocity of the ball, and multiply it by -1 each time the ball hits the edge of the screen.

    int xVel = 1;
    int yVel = 1;

    Your game update function should contain something like this (X and Y are the ball coordinates):

    X += xVel;
    Y += yVel;

    if(X < 0 || X > SCREEN_WIDTH)
    xVel *= -1;

    if(Y < 0 || Y > SCREEN_HEIGHT)
    yVel *= -1;

    Well, that's all.
    Last edited by ThLstN; 09-12-2008 at 03:11 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Isn't that what I said?

    And xVel *= -1 is the same as xVel = -xVel;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't create child windows
    By OnionKnight in forum Windows Programming
    Replies: 4
    Last Post: 04-10-2011, 04:13 PM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 11:32 AM
  3. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  4. Create a file from c++ program
    By Dan17 in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2006, 04:25 PM
  5. How to Create reference to an array in C++
    By shiv_tech_quest in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2002, 10:01 AM