Jump to content
  • Advertisement

Archived

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

Quantum

physics in a 2d game..

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!