Thread: Ball bouncing in Breakout-like game

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Exclamation Ball bouncing in Breakout-like game

    Hi,

    I'm currently developing a game like Breakout in C using allegro.
    The player controls a paddle on the X-Axis and has to prevent the ball from hitting the ground.

    My problem is that I cannot come up with how to solve the bouncing problem satisfactory. I want the ball to bounce of the paddle at an angle that depends on the distance from the paddle's center.

    I know there are hundreds of threads you can find on google, and i looked through them all which took me days, but I could not get it to work.

    If you could help me, that would be great !

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So how are you defining the path the ball takes? Are you storing an angle or are you storing a velocity vector?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Arc length and Chord length - Math Central
    What you draw on screen for the paddle is the straight line chord ADB
    The bouncy surface is the arc AB, centred on C.

    Depending on how extreme you want to make the bounce, you make r smaller, or you make r very large if you want the paddle to be essentially flat.

    The rest is just some trigonometry to work out the maths.

    Once you've done it, 'r' basically becomes a difficulty parameter in your game.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    4
    Thanks for your reply.

    @tabstop
    I am storing the angle as xveloity and yvelocity float values.

    @salem
    "What you draw on screen for the paddle is the straight line chord ADB
    The bouncy surface is the arc AB, centred on C."
    so far I understand what is going on. But I don't know what does represent the balls position and what exactly do I have to calculate for my xvel and yvel values ?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't know the ball's position, how do you draw it on the screen?

    If you treat the surface of the paddle as a circle, then the usual bounce formula v_new = v_old-2*proj_normal v_old gives you the new velocity. Again, you'll have to do a bit of work to write down the normal.

    Alternatively, you will (had better!) know at all times where the center of the paddle is, so when you find a collision you can easily tell how far away from the center of the paddle you are. The larger that distance, the more the x-velocity will be.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    4
    "Alternatively, you will (had better!) know at all times where the center of the paddle is, so when you find a collision you can easily tell how far away from the center of the paddle you are. The larger that distance, the more the x-velocity will be."

    Thats actually what I am currently doing.
    I'm calculating the distance of the balls position from the center and then
    b->xvel *= distance_to_pCenter / 30.f;
    I create some factor based on this to multiply the ball's velocity with.

    I was only wondering if there is a more elegant solution, since I saw formulas with cos, sin, atan2 and crazy thinks like that.
    But it seems like this would require a lot of work

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only reason it works out this simple is because your paddle is parallel to the x-axis. If your paddle could rotate (or was round as Salem suggested above) then you would have to use the real formula instead.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    Ok,

    Thanks for your help guys !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bouncing a ball
    By ammad in forum C++ Programming
    Replies: 16
    Last Post: 08-27-2009, 05:02 PM
  2. bouncing ball
    By planet_abhi in forum Game Programming
    Replies: 2
    Last Post: 11-10-2003, 07:18 AM
  3. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 AM
  4. Bouncing ball
    By SKINp in forum Game Programming
    Replies: 4
    Last Post: 01-13-2003, 02:26 PM
  5. Bouncing ball
    By motocross95 in forum Game Programming
    Replies: 10
    Last Post: 01-07-2002, 09:25 AM

Tags for this Thread