Page 1 of 2

Physics - Bouncing Ball

Posted: Sun Jan 25, 2009 8:13 pm
by dandymcgee
I've looked all over the place and can't seem to find something that I would assume isn't all that difficult.. a way to simulate a ball vertically bouncing. Here's the monster I've created in attempting such a feat:

Code: Select all

void Ball::Init()
{
    //For Demonstrative Purposes I'll "Declare" Variables Here:

    SCREEN_HEIGHT = 480;
    DOT_HEIGHT = 20;
    y = 0;
    yVel = 16;
}

void Ball::Update()
{
    if( ( yVel == 1 ) && ( y + DOT_HEIGHT >= SCREEN_HEIGHT ) )
    {
        y = SCREEN_HEIGHT - DOT_HEIGHT;
        yVel = 0;
    }else if( yVel < 0){
        if( ( y <= ( SCREEN_HEIGHT + ( ( yVel / 16 ) * SCREEN_HEIGHT ) ) ) )
        {
            yVel = yVel * -1;
        }
        y += yVel;
    }else if( yVel > 0){
        if( y + DOT_HEIGHT >= SCREEN_HEIGHT )
        {
            y = SCREEN_HEIGHT - DOT_HEIGHT;
            yVel = yVel / -2;
        }
        y += yVel;
    }
}
The vertical velocity of the ball starts out as +16, and upon hitting the floor it changes to -8, goes 1/2 of the way up the screen and changes to +8, hits floor and changes to -4, goes 1/4 of the up the screen and changes to +4.. etc. When it hits the floor with a vertical velocity of 1 it stops.

Obviously this doesn't look realistic at all, as I believe the velocity should slow down while the ball is moving upward, and speed up while the ball is falling. I have no idea about the physics behind these motions, so any articles, formulas, or example source code would be much appreciated.

Thanks ;)

Re: Physics - Bouncing Ball

Posted: Sun Jan 25, 2009 9:58 pm
by JS Lemming
:shock2: HAHA! I love how you are trying to hardcode the physics like that. Don't fret though, life is simpler than it sounds. Gravity! Have a look see...

Code: Select all

void Ball::Init()
{
    //For Demonstrative Purposes I'll "Declare" Variables Here:

    SCREEN_HEIGHT = 480;
    DOT_HEIGHT = 20;
    y = 0;
    yVel = 16;
}

void Ball::Update()
{
    //apply gravity to ball
    yVel = yVel + 2; //I pulled 2 out my ass, choose whatever you like

    //simulate friction with air or whatever... lessen the velocity so the ball doesn't
    // return to the same spot after it bounces
    yVel = yVel * 0.99; //I have no clue if that's a good value or not, play around with it.

    y = y + yVel;

    //if ball hits ground
    if( y + DOT_HEIGHT >= SCREEN_HEIGHT ) )
    {
        y = SCREEN_HEIGHT - DOT_HEIGHT;
        yVel = -yVel;
    }
}
This is just a quick response so I hope I didn't screw that up. I probably should have it explained it better. Maybe others will take a shot at it.

EDIT: Make sure your variables aren't ints....

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 9:10 am
by XianForce
Just a quick related question since I'm a nub, if they aren't supposed to be ints what are they supposed to be? Floats, right?

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 9:48 am
by M_D_K
yep floats.

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 12:50 pm
by MarauderIIC
Or doubles for more accuracy.

EDIT: Also, if you scale everything to the appropriate sizes, you can use gravity g = 9.8m/s^2 for super realism!
And if you can find the current position,

velocity^2 = (initial velocity)^2 + 2 * (acceleration) * (current position - initial position)
velocity = sqrt( [initial velocity] * [initial velocity] + 2 * [acceleration] * [current position - initial position] )

Or if you'd rather do it by time elapsed,

velocity = (initial velocity) + (acceleration) * (current time) //assuming initial time is zero

And you'll need the formula for an inelastic collision, probably

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 2:05 pm
by dandymcgee
I actually just stole LazyFoo's move the ball application to try my hand at applying physics ( I was too lazy to create a whole new app just for this, but I eventually will ).

@JSLemming - I actually just found an application you and Falco wrote quite some time ago that has an amazing character jump function, I was going to check out the physics in that when I get the time.
EDIT - Wow! That's the greatest physics ever haha. Just changed gravity to 1 :P. I'm gonna be playing with this for a while o.O

@MarauderIIC - I'll make an attempt at applying what you said and let you know if there's something I can't figure out. Thanks for the formulas :)

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 2:15 pm
by MarauderIIC
JS's is the easy way and why do it the hard way when nobody will notice? BECAUSE ITS FUN DANGIT

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 2:32 pm
by dandymcgee
MarauderIIC wrote:JS's is the easy way and why do it the hard way when nobody will notice? BECAUSE ITS FUN DANGIT
Haha kinda like the kid in my comp sci class insisted I use a single label and just change it's X coord to make "scrolling text". Instead I created a dynamic array of labels which read from a text file and drew them to the screen.

g = 9.8m/s^2 - How many pixels are in a meter?

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 2:53 pm
by MarauderIIC
As many as you want, just keep to scale.

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 3:44 pm
by JS Lemming
dandymcgee wrote:@JSLemming - I actually just found an application you and Falco wrote quite some time ago that has an amazing character jump function, I was going to check out the physics in that when I get the time.
Which application is that?

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 7:02 pm
by dandymcgee
MarauderIIC wrote:As many as you want, just keep to scale.
Wow.. That's actually a Really good answer haha ;) thanks.
JS Lemming wrote:Which application is that?
Hmm "SDLDev.exe" is the app and "TileCollisionDestruction.cpp" is the source name. The player is a 1x2 blue rectangle who can jump with x and shoot with c to destroy the green tiles. ReadMe = "ReadMe Foolish Mortal!.txt". Just a test application.

Re: Physics - Bouncing Ball

Posted: Mon Jan 26, 2009 9:59 pm
by eatcomics
dandymcgee wrote:
MarauderIIC wrote:As many as you want, just keep to scale.
Wow.. That's actually a Really good answer haha ;) thanks.
JS Lemming wrote:Which application is that?
Hmm "SDLDev.exe" is the app and "TileCollisionDestruction.cpp" is the source name. The player is a 1x2 blue rectangle who can jump with x and shoot with c to destroy the green tiles. ReadMe = "ReadMe Foolish Mortal!.txt". Just a test application.
Where'd you get it, I wanna try, gimme a turn, c'mon don't be selfish... Just got the phone call schools canceled lol random... it rang as I was typing this, we're supposed to have 5 - 9 inches of snow lol....

Re: Physics - Bouncing Ball

Posted: Tue Jan 27, 2009 1:42 pm
by MarauderIIC
Dan's got most everything, I hear. Also, 5 inches of ice, snow emergency (preparation) declared.

Re: Physics - Bouncing Ball

Posted: Tue Jan 27, 2009 4:23 pm
by eatcomics
Is this true danny??? I would love it if you pmed me with the code so I could try it out *Bats his eyes* Or you can burn in hell for the rest of eternity!!!! :mrgreen: But seriously I would love to try it out... mabe if you won't give it to me gyro or marauder will..... right guys ;)

Re: Physics - Bouncing Ball

Posted: Tue Jan 27, 2009 6:16 pm
by dandymcgee
We've got something like 8" - 10" predicted for tomorrow.. more than likely no school.
eatcomics wrote:Is this true danny??? I would love it if you pmed me with the code so I could try it out *Bats his eyes* Or you can burn in hell for the rest of eternity!!!! :mrgreen: But seriously I would love to try it out... mabe if you won't give it to me gyro or marauder will..... right guys ;)
Well as I don't own any of it I don't feel comfortable distributing it without explicit permission from the two creators (SuperSonic [GyroVorbis] & JSLemming). If they don't mind and don't have a copy for you then I can probably get it to you.

As for where I got it from.. well that's one of my highly guarded secrets ;)