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:
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!
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!
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,
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!
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!
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!
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!
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....
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!!!! But seriously I would love to try it out... mabe if you won't give it to me gyro or marauder will..... right guys
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!!!! 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!