I am currently trying to make a 2D side-scroller game, but I so not know where to find a tutorial on gravity, so maybe you guys could help me find one.
Thanks!
I am currently trying to make a 2D side-scroller game, but I so not know where to find a tutorial on gravity, so maybe you guys could help me find one.
Thanks!
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Take a page from any basic physics course - gravity is acceleration, and acceleration is a change in velocity. So add a constant to your y-velocity each frame and voila! Gravity.
Consider this post signed
That's how i'd do it, and then ofcourse i'd set InMidAir to true whenever there is no collision with the bottom of the bounding box of the character and the maptiles.Code:if(InMidAir) { yVelocity += GRAVITATIONAL_ACCELERATION; if(yVelocity > MAX_FALL_SPEED) yVelocity = MAX_FALL_SPEED; }
Edit:
And if you wan't to make the character able to jump, just set yVelocity to like 4 (Or whatever matches your desired jump-height/speed/length) whenever space is pressed, and make sure GRAVITATIONAL_ACCELERATION is like -0.2. Oh and then you need to make the '>' in the above code into an '<' and set MAX_FALL_SPEED to a negative value. This way pressing space will instantly make the character jump up and the y-velocity will slowly decrease so you get a nice smooth effect, rather than something mechanical where the character suddenly stops moving up and starts moving down quickly in an instant. This way a negative y-velocity will match the character moving down the screen, and a positiv y-velocity will make the character move up the screen. It won't match the screen coordinates however if you have the origin in the top left corner, which i believe SDL always has right?
Last edited by Neo1; 06-01-2010 at 01:06 PM.
How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.
One very important part of the equation was left out. If you do not base all your calculations on a frame delta or timeDelta which represents the amount of elapsed time since the last frame any animations and/or movements you do will not be synced with the game loop.yVelocity += GRAVITATIONAL_ACCELERATION * timeDelta
Without timeDelta if the FPS increases you gain nothing and when it decreases you get horrible stuttering. As well your code without the timeDelta will run faster on one machine and slower on another.