Advertisement

Formula for jumping and going down

Started by August 06, 2013 04:32 PM
18 comments, last by arnsa 11 years, 6 months ago

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);
}

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
Advertisement

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


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

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;
	

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.

Advertisement

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.


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

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

You can call SDL_GetTicks at the beginning of the update, compute that and the value you got in the previous iteration and subtract.

There are actually a few problems with that approach, but I think you should start there.

This topic is closed to new replies.

Advertisement