I am trying to calculate the y velocity necessary for my player character to jump up and reach a y position within 1 second.
Given that my game engine is at 60fps, I am doing:
velY = (targetY - playerY) / 60
.. However, I need to account for gravity which is being applied to the character on every update, so I am doing:
velY = ((targetY - playerY) / 60) - (gravity * 60)
This results in the player character always jumping significantly higher than expect.
UPDATE
Attempting to try the Equations of Motion approach, I am setting a timestamp when the player character should begin moving to the target position... So then in my update loop, I am calling a function with currentTime - timestamp.
update: function(paceFactor) {
// paceFactor in this game engine is how many nominal 60FPS game “ticks” have elapsed. So if the game is running at 30FPS, then paceFactor will be 2.0, if it's 60FPS, then 1.0 will be passed in.
this.timer += paceFactor * (1 / 60);
if (this.setVelocityAt) {
this.setVelocityFor(this.timer - this.setVelocityAt, this.targetPoint);
}
this.position.x += this.vel.x * this.speed * paceFactor;
this.position.y += this.vel.y * this.speed * paceFactor;
this.position.y += (gravity * (paceFactor * paceFactor) * 0.5);
this.vel.y += gravity * paceFactor;
},
setVelocityFor: function(time, target) {
if (time > 1) {
time = 1;
this.setVelocityAt = undefined;
}
this.character.vel.x = (target.x - this.character.position.x) / time;
this.character.vel.y = (target.y + (0.5 * GRAVITY * time * time) - this.character.position.y) / time;
},
Then later the position and gravity is applied to the character as described in the previous update.
However, the velocity being set is a huge number which is... wrong.