gd50 icon indicating copy to clipboard operation
gd50 copied to clipboard

self.player.dy and self.y depend on FPS (dt)

Open LZNEWT00Z opened this issue 3 years ago • 0 comments

Example in Bird.lua 23:32

function Bird:update(dt)

    self.dy = self.dy + GRAVITY * dt
    if love.keyboard.wasPressed('space') then
        self.dy = -5
        sounds['jump']:play()
    end

    self.y = self.y + self.dy
end

Each dt self.y increases. Particularly if FPS is 60 then after 2 seconds (120 frames) self.y == (VIRTUAL_HEIGHT / 2 - 8) + 2420. In case of having 144 FPS after 2 seconds (288 frames) self.y == (VIRTUAL_HEIGHT / 2 - 8) + 5780.

I opened a pull request #5 fixing the issue in Bird.lua. Will add another one (if there's none) for the mario project once I finish the module.


FYI, here's the formula of the original self.y: (VIRTUAL_HEIGHT / 2 - 8) + (GRAVITY / FRAME_RATE) * (FRAMES * (FRAMES - 1) / 2). I made it like this: (VIRTUAL_HEIGHT / 2 - 8) + (GRAVITY * 60/ FRAME_RATE^2) * (FRAMES * (FRAMES - 1) / 2). This way self.y should change the same over the same period of time with different frame rates. I also multiplied the "anti-gravity" burst by 60 (the original frame rate). I tested it with different refresh rates (30, 60, 144, 155) and it seems to work the same now.

LZNEWT00Z avatar May 05 '21 18:05 LZNEWT00Z