Physics - Bouncing Ball

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Physics - Bouncing Ball

Post 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 ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Physics - Bouncing Ball

Post 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....
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Physics - Bouncing Ball

Post 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?
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Physics - Bouncing Ball

Post by M_D_K »

yep floats.
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Physics - Bouncing Ball

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Physics - Bouncing Ball

Post 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 :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Physics - Bouncing Ball

Post by MarauderIIC »

JS's is the easy way and why do it the hard way when nobody will notice? BECAUSE ITS FUN DANGIT
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Physics - Bouncing Ball

Post 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Physics - Bouncing Ball

Post by MarauderIIC »

As many as you want, just keep to scale.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Physics - Bouncing Ball

Post 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?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Physics - Bouncing Ball

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Physics - Bouncing Ball

Post 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....
Image
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Physics - Bouncing Ball

Post by MarauderIIC »

Dan's got most everything, I hear. Also, 5 inches of ice, snow emergency (preparation) declared.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Physics - Bouncing Ball

Post 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 ;)
Image
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Physics - Bouncing Ball

Post 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 ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply