Gravity

This is a discussion on Gravity within the Game Programming forums, part of the General Programming Boards category; Hi.. I am trying to make a basic side scroller game and I am wondering how I should implement gravity. ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    10

    Gravity

    Hi..
    I am trying to make a basic side scroller game and I am wondering how I should implement gravity.
    I wrote something and it works but i am wondering if there is another or better way to do it.

    vel_y, tmp_y, y, x, h, and w belong to the object that's subject to gravity.

    Code:
    if(vel_y < MAX_SPD) vel_y += G;
              tmp_y += vel_y;
              
              for(i = 0; i < 15; i++ ){
                    
                    if((tmp_y+h > blocks[i].y) && (y+h < blocks[i].y + blocks[i].h) && (x > blocks[i].x) && (x < blocks[i].x + blocks[i].w)){
                                tmp_y = y;
                                vel_y = G;
                                
                                y = blocks[i].y - h;
                                
                                break;
                                
                                
                    }
                    y = tmp_y;
              }
    Also i would be grateful if you could recommend some books.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Gravity is not simply a unit change of velocity, so should not just be added to a velocity. It is an acceleration (i.e. a continuous - ongoing - change of velocity over a time period) associated with a force that attracts two bodies to each other.

    Look up "Newton's Law of gravitation" to better understand what gravity is, and how to represent its effects on objects. Look up "kinematic equations" to understand what the results are when an acceleration is applied to a moving object.

    Coding up actual equations to handle such things is trivial.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gravity allegro
    By nukecake in forum Game Programming
    Replies: 4
    Last Post: 09-02-2011, 12:14 PM
  2. SDL gravity.
    By bijan311 in forum Tech Board
    Replies: 4
    Last Post: 06-02-2010, 08:08 PM
  3. antimatter and gravity
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-08-2003, 02:58 AM
  4. Gravity pong!
    By Nutshell in forum Game Programming
    Replies: 10
    Last Post: 04-25-2003, 08:52 AM
  5. Gravity? We don't need no steenking gravity!
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-28-2002, 06:24 PM

Tags for this Thread


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