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