Archived

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

Jumping in a Platform Game

This topic is 5307 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

Hi I am having some problems working out how to go a good jumping algorithm which is correct depending on the milliseconds that the current frame has.. I tried having a inertia and a gravity setting (ie the gravity would reduce the inertia a little bit at a time) but that really screws out when you work out the speed to move the sprite based on the time since last frame (ie spritey -= milliseconds / speed) unfortuantly the time I divide the milliseconds (say 3 on average) and the small gravity (ie .25), I get a huge value which totally destroys the inertia (the time I reduce it).. this is complicated by the fact that speed (which needed to calculated on milliseconds) as actually faster the smaller it so the inertia needs to be increased by some log value which I don't really know how to do.. any ideas on how to do that (or any snippits of code).. I have a current version which works ok but that basially moves a sprites Y (via a offset table) depending on a frame rate set, but if it can;t keep up if wanders off slightly.. Cheers in advanced.. Chris [edited by - jumpman on June 4, 2003 7:18:59 AM] [edited by - jumpman on June 4, 2003 7:21:50 AM]

Share this post


Link to post
Share on other sites
on a totally different note.. what script command do I need to put in to my profile to make the url in my signature work..

I tried [url]www.myhome.com[/url] but that dosn''t seem to work on these forums..

Cheers



http://members.iinet.net.au/~cleathley/

Share this post


Link to post
Share on other sites
try multiplying by the elasped time since the last frame for any position updates in your app

x += speed * elapsedTime

for your exact problem you may need to post your codes




Share this post


Link to post
Share on other sites
First of all, base your gravity model on real physics, or you''ll always have problems

position of sprite = some number
velocity = change in position per unit of time
acceleration = change in velocity per unit of time

Acceleration due to gravity is -9.8 m/s^2, which means every second the velocity needs to increase by 9.8 m/s. So here''s your code (note that you may need to change numbers around to fit your world''s units)

spriteaccel -= 9.8;
spritevelocity += spriteaccel * timedelta;
spritepos += spritevelocity * timedelta;

where timedelta is some fraction of a second. To find timedelta, just use some timing function (I like to use timeGetTime()) that you can turn into fractions of a second if needed. Always be sure that timedelta = the number of seconds (or fractions of seconds) between this frame and the last frame. This will ensure that you always have a reliable animation regardless of framerate.

Share this post


Link to post
Share on other sites
i will try that.. I always thought that you divide the time delta (time between frames) by the speed. I guess it work''s the same aslong as ''speed'' is a much lower value

unfortuanly physics is not my best but I will keep on plugging away.. I''ve gotten a lot done on jman to let this stop me.. (I''ve just not done this type of animation/maths before..)

thanks guys..


Jumpman - Under Construction

Share this post


Link to post
Share on other sites
quote:
Original post by ApochPiQ
spriteaccel -= 9.8;
spritevelocity += spriteaccel * timedelta;
spritepos += spritevelocity * timedelta;


There are two problems here:

First a typo -- the first line should be
    spriteaccel = -9.8;   
(because acceleration is constant)

Second, an problem with the algorithm -- your code may or may not be a reasonable approximation, but the actual formula is this:
    spriteaccel = -9.8;
spritepos += spritevelocity * timedelta + .5 * spriteaccel * timedelta * timedelta;
spritevelocity += spriteaccel * timedelta;
Notice the order has changed and spritepos is computed differently.

Edit: formatting

[edited by - Jambolo on June 5, 2003 2:33:26 AM]

Share this post


Link to post
Share on other sites
once I changed everything to be timedelta * speed (instead of timedelta / spped (as nehes demos do)) then everything is working a lot better

here was the code I came up with which is close to how I originally wanted to do it..

#define JUMPINERTIA 0.6f // speed of jump
#define JUMPGRAVITY 0.0064f // gravity of jump

// when the user presses jump
m_JumpInertia = JUMPINERTIA;
m_JumpGravity = JUMPGRAVITY;

// and the jump processing loop (minus all the bits to stop the jump when he has done it.)
m_JumpInertia -= milliseconds * (m_JumpGravity / pgp->m_RunSpeed[pgp->m_CurrentPlayer]);
float speed = (m_JumpInertia * pgp->m_SpeedScaleYf) / pgp->m_RunSpeed[pgp->m_CurrentPlayer];
m_SpriteYf -= milliseconds * speed;

SpeedScaleYf is my resolution pre-scaller so everything works no matter what screen res things are in and m_RunSpeed is my speed divisor (jumpman slowed down the movement depending on the speed the user picked).

Its great to have him running around, climbing and jumping now.. still got some little things to sort out but that will be simple enough..

thanks for all the help

Chris

Jumpman - Under Construction

Share this post


Link to post
Share on other sites