Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Jump height varies slightly  (Read 4502 times)

0 Members and 1 Guest are viewing this topic.

Dinipants

  • Newbie
  • *
  • Posts: 16
    • View Profile
Jump height varies slightly
« on: May 13, 2017, 08:18:05 am »
I'm making a platform game and I have jumping and collision working. However, I've noticed that when I jump, the actual height my character reaches varies each time, by almost 10-15 pixels sometimes. It's quite noticeable

Originally I thought it's because I wasn't updating their position using a fixed frame time, but I have verified that I am. This is the relevant section of my main game loop:


        while (window.isOpen()) {
                ...
                if (gameIsClosing == false) {
                        if (accumulatedTime >= frameRate) {
                                game.draw(&window);
                                game.update(&window, accumulatedTime);
                                accumulatedTime -= frameRate;
                        }
                }
        }
 

In the game.update() function I call each character's update method, which in turn calls their move method:

        void move() {
                this->sprite->move(this->xVelocity, this->yVelocity);
        }
 

To try and see if this move function was being called at regular intervals and Y velocity was being updated by a regular amount I passed that interval down from the loop to the character and printed it out in the character's update function. This is the output:

Quote
Y velocity: 0.09600189
Time: 0.01000087

Y velocity: 0.19400188
Time: 0.01000049

Y velocity: 0.29200187
Time: 0.01000015

Y velocity: 0.39000186
Time: 0.01000078

Y velocity: 0.48800185
Time: 0.01000945

Y velocity: 0.58600187
Time: 0.01000008

Y velocity: 0.68400186
Time: 0.01000076

Y velocity: 0.78200185
Time: 0.01005336

It looks like the time interval is pretty constant @0.01 and the Y velocity is getting updated pretty much constantly @0.1

So why is the jump height varying so much each time?

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Jump height varies slightly
« Reply #1 on: May 13, 2017, 08:31:06 am »
I'm guessing this data is from the characters decent?
(As I see it it looks like the velocity is accelerating on the positive y axis. Thus moving the sprite faster and faster downwards.)

That's fine and all and don't really say anything about the issue.

What is your code used
: to activate the jump
: to calculate the yVelocity
: to stop the sprite on ground collision
?

Best regards
string message = "some random dude";
cout << "I'm just " << message;

Dinipants

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Jump height varies slightly
« Reply #2 on: May 13, 2017, 09:10:58 am »
Yeah since it gets called dozens of a time a second I could only give a small snippet of the output. That's from when it was descending back down.

For activating the jump, there is this in the main loop:
        while (window.pollEvent(event)) {
                ...
                // Parse user input
                game.parseEvent(event);
       
        }

 

The parseEvent() function is thus:
void parseEvent(sf::Event event){
        this->inputTracker->handleInput(event);
        if (this->characterManager->getPlayer()->getIsJumping() == false && this->inputTracker->didPlayerJump() == true) {
                this->characterManager->getPlayer()->setYVelocity(this->characterManager->getPlayer()->getJumpHeight());
                this->characterManager->getPlayer()->setIsJumping(true);
        }
};
 

  • handleInput(event) just records which key was pressed and stores it in a boolean in the inputTracker
  • The if statement condition checks for if they're jumping and whether they pressed up.
  • If they did jump and they aren't already jumping, it sets the character's Y velocity to the jump height and records that they're jumping in a boolean

There's no real calculation for Y velocity, it's linear. As you can see in my original post, on every update if they aren't on the ground it just increments it by a variable called 'gravity' (which in this case is a float equal to 0.98f). In the character's update function I call applyGravity():
void applyGravity() {
        if (this->yVelocity < this->terminalVelocity && this->touchingBottom == false) {
                this->yVelocity += this->gravity;
        }
}
 



For collision with the ground, it's a bit hard to put the code as it's quite mostly just a bunch of maths related if statements, but essentially if the two objects are colliding and I calculate that it must be a collision on the bottom of the character, I execute this code:
// Below
touchingBottom = true;
if (allChars[i]->getYVelocity() > 0) {
        allChars[i]->setYVelocity(0);
}
allChars[i]->setPosition(
        /* X */ allChars[i]->getSprite()->getPosition().x,
        /* Y */ allWorldSprites[j]->getPosition().y-allChars[i]->getSprite()->getGlobalBounds().height/2 + 1
);
allChars[i]->setIsJumping(false);

...

allChars[i]->setTouchingBottom(touchingBottom);
 

 that's just the code for bottom collision

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Jump height varies slightly
« Reply #3 on: May 13, 2017, 09:44:54 am »
Okay.
Having no way to test this, and no images , my guess is that the error is not so much how high you jump.
But where you jump from.
Eg, it looks like you don't have a way to align the jumper with the ground after they land. So they may land a few pixels into the ground... Then jumping later, they ofcourse will miss those pixels I jumping hight.

That is my guess at least....
string message = "some random dude";
cout << "I'm just " << message;

Dinipants

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Jump height varies slightly
« Reply #4 on: May 13, 2017, 09:57:43 am »
But I do. If you look in the last code sample (the collision one) I set the position of the character using the world sprite they collide with as a reference. Plus I can see from the character sprite that it's always in the same spot when I land/jump.

Dinipants

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Jump height varies slightly
« Reply #5 on: May 13, 2017, 11:16:45 am »
I can fix it by decreasing the FPS slightly. I had it at 100 FPS originally (0.01). Decreasing it to around 75 fixes it. Guess that from 75+ it starts to get a bit too intense to keep track of (which is weird, since there's hardly anything happening in the game, I have a high end computer and the numbers seemed to be within reason) but oh well. Guess I'll just stick to 60

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: Jump height varies slightly
« Reply #6 on: May 13, 2017, 05:19:15 pm »
Most monitors have a 60 fps refresh rate, and given that you have a linear velocity speed, then the highest point of the jump will be quite sharp and happening in single distinct frame, so at fps more than 60, that frame could be in one of the frames skipped by your monitor, so you won't see it and think the jump was shorter than the other times you see it when it is aligned with the 60 refresh rate.
« Last Edit: May 13, 2017, 05:21:36 pm by K.F »

AFS

  • Full Member
  • ***
  • Posts: 115
    • View Profile
Re: Jump height varies slightly
« Reply #7 on: May 13, 2017, 05:45:27 pm »
I don't know if it's related to the problem or not, but this caught my attention:

        while (window.isOpen()) {
                ...
                if (gameIsClosing == false) {
                        if (accumulatedTime >= frameRate) {
                                game.draw(&window);
                                game.update(&window, accumulatedTime);
                                accumulatedTime -= frameRate;
                        }
                }
        }
 

You are passing "accumulatedTime" to your update function. Shouldn't you pass "frameRate" instead?

Dinipants

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Jump height varies slightly
« Reply #8 on: May 14, 2017, 06:36:26 am »
Most monitors have a 60 fps refresh rate, and given that you have a linear velocity speed, then the highest point of the jump will be quite sharp and happening in single distinct frame, so at fps more than 60, that frame could be in one of the frames skipped by your monitor, so you won't see it and think the jump was shorter than the other times you see it when it is aligned with the 60 refresh rate.

Yeah I think that's it. I'll just stick to 60 for now.

I don't know if it's related to the problem or not, but this caught my attention:

        while (window.isOpen()) {
                ...
                if (gameIsClosing == false) {
                        if (accumulatedTime >= frameRate) {
                                game.draw(&window);
                                game.update(&window, accumulatedTime);
                                accumulatedTime -= frameRate;
                        }
                }
        }
 

You are passing "accumulatedTime" to your update function. Shouldn't you pass "frameRate" instead?

That was just for debugging so I could print out the time each update to make sure it was consistent. I've removed that now.