physics in a 2d game..

Started by
6 comments, last by Quantum 23 years, 8 months ago
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
Advertisement
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
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....
There is no spoon.
? 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.
well i just need some algorithms for stuff like proper jumping, etc..

thx for the replies.. ill check out the maths + physics articles @ gamedev
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
do unto others... and then run like hell.
quote: Original post by FReY

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


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
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

do unto others... and then run like hell.

This topic is closed to new replies.

Advertisement