# Creating gravity

3 replies to this topic

### #1random234

New Member

• Members
• 1 posts

Posted 10 October 2006 - 12:25 AM

Hi. I have a question, since I haven't really done this before, how would you go about making gravity in C++? I'm trying to make a 2d platform game, and I want the characters to come down after jumping and avoid going through objects. Help would be appreciated.

### #2Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 10 October 2006 - 01:52 AM

You just have to give all objects a constant downward acceleration each frame. You accumulate the acceleration into the velocity and the velocity into the position. Preventing them from going through things is harder and requires collision detection. If it's just a flat floor you can just clamp their vertical coordinate to an the appropriate range.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3GroundKeeper

Valued Member

• Members
• 110 posts

Posted 13 October 2006 - 06:52 AM

Just have to:

1. Take the laptop
2. Open the window
3. Jump out

Voila, and you have brought gravity to the gaming experience...
Sorry if this was a bit off but I coulbn't resist.

### #4Xoat

New Member

• Members
• 3 posts

Posted 13 October 2006 - 09:09 AM

hi random,

There are a couple of ways of making gravity in a game:

if you game is little and easy and there arent that many different highs you could just always make the camera go down to say Y = 20:

if (bJump = True) and (Camera.Y > 50) then

Camera.Y = 50

bJump = False

end if

if you game is bigger and you want your character to jump on different things. then you need collision detection. This means that you have to check if you character is going thru objects.:

if(bJump = True)

'check collision for every object down the camera.

'move the camera down every frame if there isnt anything beneith it

end if

an other approach would be using a fysics engine for downing your gravity work. this is the hardest option i think, but the nicest if your game is designd to handeld this. if you want to use this option you should look at things like: Netwon, ODE and tomahak i think... but there are a million other engines out there that also could do this..

regards,

Xoat

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users