Hello my issue is that when my player jumps. There is a tiny but noticeable shake because of how Jumping and gravity is worked.
There are the variables concerning jumpping and gravity,
private float gravity = 0.5f;
private final double MAX_SPEED = 10;
And here a method called in the tick/update method for position. Standard 60 ticks/s.
public void getNextPosition() {
x += velX;
y += velY; //Exhibit A
tileCollision(gameState.getObject());
if (falling || jumping) {
velY += gravity;
if (velY > MAX_SPEED) {
velY = MAX_SPEED;
}
}
if (right && left) {
if (timeRight > timeLeft) {
velX = 5;
} else if (timeLeft > timeRight) {
velX = -5;
}
} else if (right && !left) {
velX = 5;
} else if (left && !right) {
velX = -5;
} else if (!left && !right) {
velX = 0;
}
}
And here is how my jump works
if (key == KeyEvent.VK_SPACE) {
if(player.isJumping() == false){
player.setVelY(-10); //Exhibit C
player.setJumping(true);
}
}
As you can see from code in the update method y += velY; is causing the problem because the jump sets velY to -10. And at the same time applying the gravity as well velY += gravity;
This is my first attempt at a game and I don’t know how to approach gravity another way.
Here is a github link the my eclipse project file.
https://github.com/andrevicencio21/onepiecegame
It’s still at the early stage so hopefully it’s easier to trace.
Menu Controls: Up and Down and Enter;
Game Controls:
A,D for left and right movement
Space for Jump