Gravity in game

This is a discussion on Gravity in game within the Game Programming forums, part of the General Programming Boards category; Hi, I need help trying to understand how to implement gravity in game. I think i understand the concept of ...

  1. #1
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629

    Gravity in game

    Hi,
    I need help trying to understand how to implement gravity in game.
    I think i understand the concept of this derivatives
    1. Position Vector. position of an object
    2. Velocity Vector . change in position over time.
    3. acceleration is change in velocity over time.

    My problem is how do implement gravity and force into the game?
    I know the position of the player.
    I know the direction it is facing by using sin and cos thetha..
    can i not use the the direction vector as my velocity? That is one place in which i am confused. How do i use the velocity vector to move in the direction in which i am facing?

    How do i get the acceleration of the player:
    force = mass * acceleration;
    acceleration = force/mass;
    do i just set mass and force to be a random value?

    Can someone please clarify this in a simple manner.
    And if velocity is the change in position over time? What is the use of speed?

    Thanks in advance.
    Eman

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    1. Position Vector. position of an object
    2. Velocity Vector . change in position over time.
    3. acceleration is change in velocity over time.
    good.

    I know the direction it is facing by using sin and cos thetha..
    can i not use the the direction vector as my velocity?
    You can, but why would you? Position is calculated from velocity, not the other way around.

    How do i use the velocity vector to move in the direction in which i am facing?
    The things is that the direction you're facing can be different from the direction you're traveling. Think cars on an icy road. Facing is useful, however, if you want to work with car-like physics - to accelerate, you'd apply a force in the direction the car is facing.


    How do i get the acceleration of the player:
    force = mass * acceleration;
    acceleration = force/mass;
    do i just set mass and force to be a random value?
    You can use any units you want, but it might be easiest to keep track of real-world values: force in newtons and mass in kilograms.

    Force is a vector, by the way. So, to implement some simple gravity in the z-direction, in pseudo-code:
    Code:
    acceleration = <0,0,m*g> / m;  //a = f/m, gravity force = m*g, positive z is down
    velocity += acceleration * dt;     //dt = elapsed time since last update
    position += velocity * dt;
    Note that if you have multiple forces acting on an object, you'd just add them together and stick the sum into your a=f/m equation.

    I'd recommend these articles as an intro to game physics.
    Consider this post signed

  3. #3
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Thanks for really fast reply.

    Quote Originally Posted by bernt View Post

    The thing is that the direction you're facing can be different from the direction you're traveling.
    Think cars on an icy road. Facing is useful, however, if you want to work with car-like physics - to accelerate, you'd apply a force in the direction the car is facing.

    How is the direction in which i am facing different from the direction i am travelling in?
    It appears to be the same to me! :'(
    Oh, and I am working with 2d vectors.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Apply a net acceleration due to gravity per frame. The accleration you choose can be anything but gravity on earth is 9.8m/s^2. This may or may not have any meaning within the context of your game units.

  5. #5
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Eman View Post
    How is the direction in which i am facing different from the direction i am travelling in?
    It appears to be the same to me! :'(
    Think of side-stepping (strafing) in a first person shooter. You can face one direction but be walking sideways. In a simple 2d game this probably isn't an issue.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Perspective View Post
    Think of side-stepping (strafing) in a first person shooter. You can face one direction but be walking sideways. In a simple 2d game this probably isn't an issue.
    oh right, i see. Only if explanations were as simple and straightforward as that
    so how do i apply this to my game.

    I know my initial position. and i need to get to a final position, well wherever the player decides to stop.
    I know the direction vector.
    But i don't know which correct figure to use for the velocity, force, mass. This is what i hate about games tutorials and maths. All this random numbers, and no explanation why they're chosen. You can guess, I am crap at maths.

    if i had an initial position of (320,100) say on the graph.
    and i added (0,1) to the position. It would mean i was down the y axis of the graph.
    If that is the case, is (0,1) my velocity? I know <b>what </b> it tells me, I am looking down.
    (0,1) has to be velocity because it changes the position of the player,right?
    so if that is correct how do i use (or how can i arrive at the correct figure for the) acceleration and speed?
    thanks.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Ok, take a step back and breath. Then go and read this article on vectors: Euclidean vector - Wikipedia, the free encyclopedia

    Now after you have read that article, say you have a position (10, 10) and you are looking along the vector (1, 0) (will refer to this as the look vector). What this tells you is that you are facing straight down the x-axis at the point (10, 10) only. This has no relation whatsoever to do with velocity or movement direction.

    Now say you want to move straight along the y-axis at speed x, this would give you a movement vector or velocity vector. In this case the velocity vector could be x*(0, 1) (x being a scalar). Think about what this means...what part of that formula tells you the direction of the movement and what part tells you about the speed of the movement? Now what would the speed of the velocity vector (2, 2) be?

    The look vector is not to be confused with the velocity vector. The look vector is only used as an orientation vector, it describes how you are oriented in the game world (what way you are facing). The velocity vector describes how are moving in the game world (what direction you are moving and what speed you are moving at). This does not mean they can not point in the same direction (if you move straight forward they would) but there is no restriction whatsoever that a velocity vector is bound to the look vector.

    Now to understand gravity you need to understand what a force is and how it relates to speed (and how it changes speed) and how a set of forces acts on an object and changes the speed. Force - Wikipedia, the free encyclopedia gives you a read on that, but you should really try to understand the above before going into forces.

    Basicly you need to understand Mechanics: Mechanics - Wikipedia, the free encyclopedia
    And some basic linear algebra (vectors).
    Last edited by Shakti; 12-02-2010 at 06:23 PM.

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Shakti View Post
    Ok, take a step back and breath. Then go and read this article on vectors: Euclidean vector - Wikipedia, the free encyclopedia

    Now after you have read that article, say you have a position (10, 10) and you are looking along the vector (1, 0) (will refer to this as the look vector). What this tells you is that you are facing straight down the x-axis at the point (10, 10) only. This has no relation whatsoever to do with velocity or movement direction.

    Facing down, does it not mean i am looking right? to look left i would have to do (-1,0)


    Now say you want to move straight along the y-axis at speed x, this would give you a movement vector or velocity vector. In this case the velocity vector could be x*(0, 1) (x being a scalar). Think about what this means...what part of that formula tells you the direction of the movement and what part tells you about the speed of the movement? Now what would the speed of the velocity vector (2, 2) be?
    so (0,1) is the look vector, and i can move down the y axis....but what does the scalar x represent? speed?
    Now what would the speed of the velocity vector (2, 2) be?
    I honestly don't know the answer to that. Can't it have any amount of speed?

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Read the links i provided.

    Edit: Didnt see your first question in the quote-box.

    I meant facing down in the same context as in facing down a road, bad wording on my part.

    No, the velocity vector part of my post has nothing to do with the look vector part. I gave you the name of the two parts that x*(0,1) contains, so while dealing with velocity vectors forget look vectors for now.
    Last edited by Shakti; 12-02-2010 at 06:47 PM.

  10. #10
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Shakti View Post
    Read the links i provided.

    Edit: Didnt see your first question in the quote-box.

    I meant facing down in the same context as in facing down a road, bad wording on my part.

    No, the velocity vector part of my post has nothing to do with the look vector part. I gave you the name of the two parts that x*(0,1) contains, so while dealing with velocity vectors forget look vectors for now.
    Hey, yeah I read them, it got confusing with all those maths symbols.
    But a speed is a scalar and doesn't tell me which direction i am facing.
    The velocity tells me the speed and direction i am facing.
    So i assume that x is speed and (0,1) is he velocity.

    but if (2,2) is the velocity, which i think it is, i have no idea what the speed is unless i get the length of the vector sqrt(8) and divide by a time. I just don't know lol

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The speed of a vector 2,2 is the length or magnitude of the vector.

    To start out you can do very simple Euler integration without friction / drag.

    accel = force / mass;
    vel += accel * dt;
    pos += vel * dt;

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by Bubba View Post
    The speed of a vector 2,2 is the length or magnitude of the vector.

    To start out you can do very simple Euler integration without friction / drag.

    accel = force / mass;
    vel += accel * dt;
    pos += vel * dt;
    ah, you see this is what confuses me. How is "length" or "distance" equivalent to speed?
    Speed <b>is</b> not distance.
    I mean we have the equation s = d/t ;
    if you noticed in my previous post. i got the magnitude of the velocity vector and divided by time. sqrt(8)/time..
    so how is length of a vector, speed?

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    Basicly the length of a vector is not an actual length in the way you think it is. In the case of a velocity vector the vector holds the following information: the direction and the speed.

    Think of a vector as an information container. It holds 2 kinds of information, the direction of the vector and the length of the vector. How this information is used depends on in what context the vector is used.

    I seriously suggest you read linear algebra if you can take a course at a local college or so, or just search the web. I can not provide you better answers than these really.

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Alright, i get it thanks.
    And i got it work.
    Thanks for help

  15. #15
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Getting it to work may not be your only goal here. The important lesson here is that you understand vectors backwards and forwards. If you do not then when you come up against this next time you will be just as unprepared as you were this time. Vectors are one of the fundamental building blocks of computer graphics and linear algebra.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Replies: 15
    Last Post: 10-20-2009, 10:39 AM
  3. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  4. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

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