• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# physics in a 2d game..

This topic is 6713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im making a 2d side scroller using directdraw. i''ve done quite a lot on it, but am trying to implement physics (mainly jumping ) anyone out there got any algorithms for jumping, falling, and stuff like that or just general info on it? thx

##### Share on other sites
You can implement realistic gravity by just increasing the velocity of the character downwards each frame.

So, like this (upwards is positive, downwards negative number):

  velocity += gravity;position += velocity;

gravity could be set to eg. -1.0, it's just a matter of taste and how realistic you want it to be.

-Jussi

Edited by - Selkrank on September 1, 2000 2:34:47 AM

##### Share on other sites
damn, that last post was useless huh? sorry, I cant be of any help either, I just thought that post was funny enough to point out. =) I knew I shouldnt have skipped high school physics....

##### Share on other sites
? Did you want the physics equations? Implementation would be just a matter of coding the equations properly. There are some physics references under the References->Programming->Math and Physics links.

##### Share on other sites
well i just need some algorithms for stuff like proper jumping, etc..

thx for the replies.. ill check out the maths + physics articles @ gamedev

##### Share on other sites
Try something like this:

  void main(){ const double START_JUMP_VELOCITY=-1; const double GRAVITY =0.01; const int GROUND_LEVEL=100; double xpos,ypos; // coordinates of character double xvel=0,yvel=0; // velocity of character xvel=0; yvel=0; while (!endofprogram) { if ((jumpkeypressed)&&(yvel!=0)) yvel=START_JUMP_VELOCITY; xpos+=xvel; // add x-velocity to x-position ypos+=yvel; // add y-velocity to y-position yvel+=gravity; // add gravitational acceleration // to the y-velocity if (ypos>=GROUND_LEVEL) // if character has hit the ground yvel=0; drawcharacter(xpos,ypos) }}

Of course if you don''t want the character to stop at the same
level each time you have to change GROUND_LEVEL to be a function of the characters position on screen.

Hope I''ve helped,
FReY

##### Share on other sites
quote:
Original post by FReY

xpos+=xvel; // add x-velocity to x-position
ypos+=yvel; // add y-velocity to y-position

// to the y-velocity

To get exact physics model, you should do it like this:

  yvel+=gravity/2; // add half of the gravitational acceleration // to the y-velocity xpos+=xvel; // add x-velocity to x-position ypos+=yvel; // add y-velocity to y-position yvel+=gravity/2; // add half of the gravitational acceleration // to the y-velocity

To know why, check out my homepages -> my tips -> doing gravity right.

-Hans

Edited by - Hans on September 2, 2000 4:26:06 PM

##### Share on other sites
Hm, very, very interesting Hans. I''ve never thought of it like that before, but of course one realises that mine is just an approximation

Go to Hans'' web page, it''s quite interesting.

FReY

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 27
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634100
• Total Posts
3015527
×