Jump to content
  • Advertisement
Sign in to follow this  
arnsa

Formula for jumping and going down

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

I can't seem to find any formula for my 2D game. I have a simple sprite and I wan't it to jump up and then go back. It has only to jump up and down, no need for "parabola jump" or anything. I hope you know what I mean.

 

P.S. I tried something like that, but that didn't work well. It seems that the sprite just teleports up and then teleports back down.

for(int co = 0; co < 50; co++) {
                rcSprite.y += 1;
                usleep(4000);
}

Share this post


Link to post
Share on other sites
Advertisement

You gave very little information. However, it seems me that you try to animate the sprite besides the game loop. Timing (and animation is just the timed alteration of variables) is usually driven by the game loop, as well as the rendering is. So, if you alter the rcSprite.y without ever rendering intermediately, you'll see "teleporting".

 

The usual structure (although it needs some tweaking) is as follows:

 

In the game loop ...

... iterate until the game is quitted

... ... measure the time that has elapsed since last recent pass

... ... handle user input

... ... update the animated states

... ... perhaps do physics

... ... perhaps do collision handling

... ... render

 

In the user input section ...

... if the user triggers jumping and rcSprite.jumping == false then set rcSprite.jumping = true

 

In the update routine of the rcSprite ...

... if rcSprite.delta > 0 and rcSprite.y >= jumpHeight then set rcSprite.delta = -1

... if rcSprite.delta < 0 and rcSprite.y <= groundHeight then set rcSprite.delta = 0, rcSprite.jumping = false, rcSprite.y = groundHeight

... if rcSprite.jumping == true then set rcSprite.y += rcSprite.delta

 
In the render routine ...
... use current rcSprite.y for rendering
Edited by haegarr

Share this post


Link to post
Share on other sites

"Parabola jump" is nothing fancy: You jump by changing the velocity of your character vertically by some amount. Every frame, while the character is in the air, you do something like this:

velocity.y += -gravity * delta_time;
position.y += velocity.y * delta_time;

`delta_time' is the time elapsed in one frame. You need to detect when you hit the ground so you can stop the jump. You may have to play around with this for a little while to iron out any glitches, but it's very doable.

Edited by Álvaro

Share this post


Link to post
Share on other sites
//set some forces (vectors with length of resulting force and direction of the force)
Acceleration = result_force / mass;      // a = F / m
//dt is the time between frames



vel = vel + (Acceleration * dt);
pos = pos + (vel * dt);
//now to jump add force to resulting force that has (in example negative direction that gravity force has) but with greater value

Share this post


Link to post
Share on other sites

I actually made something like that, but in my opinion, that's not the right way to do... Any suggestions/advices? Btw, there's some kind of a bug that when a sprite jumps, it leave all of his colors behind him and when he goes down, same happens... Maybe I should somehow redraw the background (grass) when jumping?

        if ( space ) {
            for(int co = 0; co < 50; co++) {
                rcSprite.y -= 1;
                usleep(500 * co);
                SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);
                SDL_UpdateRect(screen, 0, 0, 0, 0);
            }
            for(int co = 50; co > 0; co--) {
                rcSprite.y += 1;
                usleep(300 * co);
		        SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);
		        SDL_UpdateRect(screen, 0, 0, 0, 0);
            }
            space = 0;
	
Edited by arnsa

Share this post


Link to post
Share on other sites

Doing the entire jump in blocking for loops like that is very bad. Your entire game will halt, no other game logic will be processed and no keys will respond, until the entire jump has been completed. You need to progressively process your jump over several frames like the previous posted have suggested.

Share this post


Link to post
Share on other sites

Unless you are working on a very underpowered platform (Commodore 64?), you shouldn't have to worry about restoring the background when something moves. You simply redraw the whole screen on every frame.

 

Search the web for "game loop" to get a more detailed description of how to structure your program.

Share this post


Link to post
Share on other sites
//set some forces (vectors with length of resulting force and direction of the force)
Acceleration = result_force / mass;      // a = F / m
//dt is the time between frames



vel = vel + (Acceleration * dt);
pos = pos + (vel * dt);
//now to jump add force to resulting force that has (in example negative direction that gravity force has) but with greater value

Maybe that would work, but I don't really know what should be the ratio between the force and the mass aka what numbers should I take for those?

Share this post


Link to post
Share on other sites

"Parabola jump" is nothing fancy: You jump by changing the velocity of your character vertically by some amount. Every frame, while the character is in the air, you do something like this:

velocity.y += -gravity * delta_time;
position.y += velocity.y * delta_time;

`delta_time' is the time elapsed in one frame. You need to detect when you hit the ground so you can stop the jump. You may have to play around with this for a little while to iron out any glitches, but it's very doable.

 

What do you mean by "time elapsed in one frame"? How do I count it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

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