Thread: bouncing a ball

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    20

    Smile bouncing a ball

    hello friends..... i m a student ....n doing BS in computer science.....
    well friends i have class project of bouncing a ball.... we r using OPENGL...
    and i m not familiar with OPENGL....
    my problem is that I dont know how to make gravity effects on the ball....
    so i want help from u guys.....
    Last edited by ammad; 08-27-2009 at 03:39 PM.

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    i m waiting .....thanks in advance

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    lol - 2 minutes. That's got to be a new impatient record.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    i don't understand??

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, can you draw a ball at a given position? If so, then you're halfway there. You just need to change the position of the ball each frame (or time period or whatever). A simple way to do it goes like this:
    Code:
    #define Y_ACCELERATION -2
    
    int y_position = 100;
    int y_velocity = 0;
    
    while(/* ... */) {
        /* ... */
        y_velocity += Y_ACCELERATION;
        y_position += y_velocity;
    
        /* If the ball reaches the ground, reverse its velocity */
        if(y_position <= 0) {
            y_position = 0;
            y_velocity *= -1;
        }
    }
    Using doubles for positions would probably be a good idea; you'll likely find that an acceleration of 2 doesn't work too well, and you need 2.5, or something.

    What I've done there is treat the Y axis as vertically aligned, with positive values farther away from the ground. Then there's a position, which is acted upon by the velocity, which is acted upon in turn by the acceleration due to gravity.

    Note that this is a really simple way of doing things. If you want a more accurate simulation you might have to use some integrals. I haven't really thought about it, because I really don't think that's what you need here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    thanks....if i will be having any problem then i will ask again.....
    Last edited by ammad; 08-27-2009 at 04:07 PM.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    well i have a question ......does OPENGL provide any built in function to rotate the bouncing ball after the collision.......?????

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What don't you understand? Be more specific.
    1. To make the ball move, you simply draw it at a different position.
    2. Gravity is a form of acceleration here, so you need to simulate acceleration of the ball.
    3. Acceleration is just change in velocity, which is just change in position.
    4. Just add acceleration to velocity to simulate it, and add velocity to position to simulate that.
    5. Bouncing of the ball can be achieved by simply inverting the velocity when the ball hits the ground.


    [edit]
    well i have a question ......does OPENGL provide any built in function to rotate the bouncing ball after the collision.......?????
    I don't know very much about OpenGL, so I can't answer that question. You shouldn't need to rotate the ball, though (unless I'm missing something?), just negate the velocity. For example, if the ball was traveling at a velocity of -30 (negative is downwards), then just make it travel at +30 (upwards), and you have your bounce.
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    i m saying that ball spin after the collision .... suppose u throw a ball downstairs then after collision with the stairs the ball spins?.......and it also increase its acceleration...right
    i want to bounce a ball with the spin also........

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    well ok ....
    one more thing..... i dont want to move my bouncing ball comtinuously.......it should be decreasing its speed......so what should i do ?

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by ammad View Post
    i m saying that ball spin after the collision .... suppose u throw a ball downstairs then after collision with the stairs the ball spins?.......and it also increase its acceleration...right
    i want to bounce a ball with the spin also........
    Well, yes, if a ball is spinning and it bounces, the spin is reversed and the spin affects the trajectory. I imagine if a ball with a large horizontal velocity bounces, it might start spinning as well. You can try to simulate these effects if you want to . . . .

    It sounds like you want more than just a simple ball bouncing up and down; do you want horizontal (or even 3D) movement? If so, then just keep track of the velocity on each axis, and apply acceleration to the vertical axis to simulate gravity. It's not too hard. The simulation won't be too interesting, though, unless you start it with an interesting velocity, or unless other forces (wind?) act on the ball.

    Also, if you have surfaces at different angles, you won't be able to just invert an axis to simulate a ball bouncing off the edge; you'll need to use some trigonometry to figure out the resulting velocities after a collision.

    well ok ....
    one more thing..... i dont want to move my bouncing ball comtinuously.......it should be decreasing its speed......so what should i do ?
    The obvious solution is to make bounces not 100% perfect. You could use, for example,
    Code:
    y_velocity *= -0.9;
    to make a bouncing ball recover 90% of its energy.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    thank u DWK.....
    i will ask questions if i 'm having any trouble.....
    thnks for ur help.....

  13. #13
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    If you don't need to simulate gravity, a simple euler integration would do -

    Code:
    velocity += acceleration * dt;
    position += velocity * dt;
    If you want to simulate gravity you'll have to do a bit more, basically youll have to use y = 1/2gt^2 + v0yt + C;

    which is something like -

    Code:
      PositionY = .5f * gravity * time* time + VelocityY * time + StartPositionY;
    Spidey out!

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    20
    well spidey thanks for ur help .... but its a bounce ....... i mean i dont understand what u saying???
    can u plz explain????

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    My explanation was based on advancing the simulation one "step" per frame. Spidey's using formulae that are based on time elapsed instead, which are a better idea (no dependency on frame rate). They're basic physics equations; Wikipedia has some information on them and I'm sure you can find other resources, too. Classical mechanics - Wikipedia, the free encyclopedia
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a bouncing ball?
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2008, 03:41 AM
  2. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 AM
  3. Bouncing Ball Algorithm
    By Stan100 in forum Game Programming
    Replies: 12
    Last Post: 04-06-2003, 07:36 PM
  4. Bouncing ball - help ??
    By Gugge in forum C Programming
    Replies: 7
    Last Post: 04-13-2002, 02:34 PM
  5. Bouncing ball
    By motocross95 in forum Game Programming
    Replies: 10
    Last Post: 01-07-2002, 09:25 AM