-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ball speed is tied to refresh rate #94
Comments
I am having the same problem - I have a high refresh rate monitor and the ball zooms around the screen ridiculously fast compared to a lower refresh rate monitor |
https://github.com/tonybaloney/vscode-pets/blob/master/src/panel/main.ts#L222-L225 According to MDN, regular refresh rates will be 60fps (so the pre-programming velocity calculation is correct), but for higher rates this increases. There isn't anything in the code to do this right now, https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame |
Hi, I experienced this issue as well and I unfortunately don't have the time to contribute with a pull request right now, but I can help with an insight. If you want to normalize the velocity of an object between different monitor refresh rates, the trick is to tie the velocity to the difference in real time between frames: let previousFrameTime;
let id;
function animate(currentFrameTime) {
id = requestAnimationFrame(animate);
// Math.min is used here to prevent the time difference being too large in case the user
// switches to another window for some time and then returns back to it
const differenceInTime = Math.min(1000, currentFrameTime - previousFrameTime);
// convert to seconds (it just makes calculations more convenient)
const deltaTime = differenceInTime / 1000;
// move the ball by { x, y } velocity per second
ball.position.x += ball.velocity.x * deltaTime;
ball.position.y += ball.velocity.y * deltaTime;
// draw the ball here
previousFrameTime = currentFrameTime;
}
function startAnimating() {
previousFrameTime = performance.now();
animate(previousFrameTime);
}
function stopAnimating() {
cancelAnimationFrame(id);
} The let timeStep = 1 / 60; // update at 60 frames per second
let accumulatedLag = 0;
let previousFrameTime;
let id;
function animate(currentFrameTime) {
id = requestAnimationFrame(animate);
const differenceInTime = Math.min(1000, currentFrameTime - previousFrameTime);
const deltaTime = differenceInTime / 1000;
// keep track of how much time has elapsed so that any lagged time carries over
accumulatedLag += deltaTime;
// this results in 0->n updates per frame, depending on how
// much the simulation has lagged behind
while (accumulatedLag >= timeStep) {
accumulatedLag -= timeStep;
ball.position.x += ball.velocity.x * timeStep;
ball.position.y += ball.velocity.y * timeStep;
}
// draw the ball here
previousFrameTime = currentFrameTime;
}
function startAnimating() {
previousFrameTime = performance.now();
animate(previousFrameTime);
}
function stopAnimating() {
cancelAnimationFrame(id);
} In the game dev world, this is known as a "Fixed Timestep". It's really only useful for physics and collisions and it does sometimes cause jitter, which means the logic has to keep track of I think vscode-pets is simple enough to get away with just the |
Was fixed in #167 |
is there a way to make the ball slower on higher refresh rate? ball jumps like crazy
pets speed are working fine though
The text was updated successfully, but these errors were encountered: