Formula for jumping and going down

Started by
18 comments, last by arnsa 10 years, 9 months ago

Yeah, you need to work out how your game loop is going to work, but asa others have said, this is the general principle.

Gravity accelerates objects at a constant rate. So you just stick that constant somewhere in your code, and assuming you're using Euler integration*, you just decrease the Y velocity by this amount per tick. To jump, you just set, on a particular tick, the Y velocity to some constant value, which is how hard you can jump.

* If you don't know, then you are probably using Euler integration. Look it up.

Advertisement

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.

Now my sprite teleports to the top, if I click jump again, it dissapears and I need to click jump like 4 times so it appears again.

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.

Now my sprite teleports to the top, if I click jump again, it dissapears and I need to click jump like 4 times so it appears again.

We can't debug your code for you. Step through it with a debugger and see if variables have the values you expect them to have, or dump debugging data on a log file and examine it, to understand what's going on.

To make things simple, use a constant value for delta_time for the time being.

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.

Now my sprite teleports to the top, if I click jump again, it dissapears and I need to click jump like 4 times so it appears again.

We can't debug your code for you. Step through it with a debugger and see if variables have the values you expect them to have, or dump debugging data on a log file and examine it, to understand what's going on.

To make things simple, use a constant value for delta_time for the time being.

From what I see here, deltaTime really depends on how long I wait before I click jump. I must be doing something wrong...



void handle_event(SDL_Event event, SDL_Rect *rcSrc, SDL_Rect *rcSprite, int *velocity) {
    int now = SDL_GetTicks(), delay;
    switch(event.type) {
        case SDL_QUIT:
            gameover = 1;
            break;

        case SDL_KEYDOWN:
            switch(event.key.keysym.sym) {
                case SDLK_ESCAPE: case SDLK_q:
                    gameover = 1;
                    break;
        // some stuff
        case SDLK_SPACE:
            printf("now: %d, last: %d, delay: %d\n", now, last, now-last);
            if (now > last) {
                delay = now - last;
                last = now;
            }
            *velocity += (-10) * delay;
            rcSprite->y += *velocity * delay;
            break;
            // some stuff

That doesn't look like you are using a game loop at all.

That doesn't look like you are using a game loop at all.

This function is being called in a game loop.


    while(!gameover) {
        if (SDL_PollEvent(&event))
            handle_event(event, &rcSrc, &rcSprite, &velocity);

        check_sprite_edges(&rcSprite, SPRITE_LION_SIZE);
        draw_background(&screen, &sprite_grass, &rcGrass);

        SDL_BlitSurface(sprite_lion, &rcSrc, screen, &rcSprite);
        SDL_UpdateRect(screen, 0, 0, 0, 0);
    }

The updating of velocity and position should be part of the game loop (or, better yet, called from the game loop), because it should happen whether there are events to process or not.

The updating of velocity and position should be part of the game loop (or, better yet, called from the game loop), because it should happen whether there are events to process or not.

Putting it in the main loop didn't change anything.

I guess you don't know how to debug your code. This is a good opportunity for you to learn.

I guess you don't know how to debug your code. This is a good opportunity for you to learn.

Okay, I've found out where the bug is and now my character's jump depends on gravity and velocity (at last). But here's another problem: it doesn't jump "realistically", it just teleports. If I keep clicking jump, every time it teleports farther. Is this is how it works?



   while(!gameover) {
        now = SDL_GetTicks();
        if (now > last) {
            delay = now - last;
            last = now;
        }
        if(space) {
            velocity += (-3) * delay;
            rcSprite.y += velocity * delay;
            space = 0;
        }
        if (SDL_PollEvent(&event))
            handle_event(event, &rcSrc, &rcSprite, &velocity);
        // etc.

This topic is closed to new replies.

Advertisement