SDL gravity.

This is a discussion on SDL gravity. within the Tech Board forums, part of the Community Boards category; I am currently trying to make a 2D side-scroller game, but I so not know where to find a tutorial ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    SDL gravity.

    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!

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    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

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Code:
    if(InMidAir)
    {
         yVelocity += GRAVITATIONAL_ACCELERATION;
         if(yVelocity > MAX_FALL_SPEED) yVelocity = MAX_FALL_SPEED;
    }
    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.

    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.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    yVelocity += GRAVITATIONAL_ACCELERATION * timeDelta
    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.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little class problem
    By bijan311 in forum C++ Programming
    Replies: 12
    Last Post: 05-23-2010, 11:42 AM
  2. SDL project setup
    By rogster001 in forum C Programming
    Replies: 22
    Last Post: 08-28-2009, 08:05 AM
  3. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21