Giter Club home page Giter Club logo

Comments (5)

coltonoscopy avatar coltonoscopy commented on July 29, 2024

from fifty-bird.

snekbaev avatar snekbaev commented on July 29, 2024

@coltonoscopy what would be the recommendation when it comes to 60+ FPS? It's quite unplayable at 144 :)

from fifty-bird.

sharifmarat avatar sharifmarat commented on July 29, 2024

@snekbaev Limit dt with some min_dt: https://github.com/games50/fifty-bird/pull/15/files

from fifty-bird.

snekbaev avatar snekbaev commented on July 29, 2024

@sharifmarat can't recall the details now, but I think the suggested approach wasn't good enough. I remember reading some article about fixing the frame rate issues using different methods and pros and cons of each. One of the better ones was to use the accumulator approach. I think I've implemented some variation of that

from fifty-bird.

yak1r avatar yak1r commented on July 29, 2024

A few years late to the party, but I didn't see this mentioned anywhere, so FWIW I'll explain why you experienced this issue and share a fix for it. Hopefully it will help future students:

**tl;dr: ** Pull Request #21 will fix this issue.

We'll start by discussing basic physics (probably mandatory knowledge for game developers anyway):
Our bird's location is defined by x and y values. The units of these values are pixels.
The speed, dy, is given in units of pixels / second (so, for example, if our bird has speed of dy = 20, it means it will pass 20 pixels every second).
Now we get to the gravity. Gravity is just acceleration towards the positive direction of the y-axis. Acceleration measures the degree in which the speed changes (so, for example, if gravity is 20, it means the bird's speed is increased by 20 pixels / second every second),
so if the speed's units are pixels / second, the acceleration units are pixels / second / second or pixels / second ^ 2.

now, when we calculate the bird's speed this is done correctly, we take the gravity and multiply it by the elapsed time, dt, giving us the speed:

self.dy = self.dy + GRAVITY * dt

And indeed, when looking at the units alone, (pixels / second ^ 2) * seconds = pixels / second and those are indeed the units of speed as expected.

The mess starts when we try and add this speed to the bird's y position:

self.y = self.y + self.dy

Notice how we take the bird's current y position and add the speed to it without multiplying it by the time elapsed, dt.
This makes no sense in physics. You can't simply add two measurements of different units!

To demonstrate how this affects users who run on a frame rate which is not 60 fps, let's consider two users, user A and user B.
User A runs the game on 60fps (dt of every frame is 1 / 60 = 0.01666). User B runs the game on 120fps (dt of every frame is 1 / 120 = 0.008333).
Now, let's check the bird's y position after 0.2 seconds (so 12 frames for user A and 24 frames for user B), by running this simple code (written in python for my convenience :) ):

GRAVITY = 20  # As defined in fifty-bird
def simulate_frames(num_frames: str, dt: float) -> float:
    y = 136  # As defined in fifty-bird
    dy = 0  # Starting y speed is 0
    for i in range(num_frames):
        dy += GRAVITY * dt
        y += dy
    return y

print(simulate_frames(num_frames=6, dt=1 / 60))  # User A
print(simulate_frames(num_frames=12, dt=1 / 120))  # User B

Running the above code prints:

>>> print(simulate_frames(num_frames=12, dt=1 / 60))  # User A
162.0
>>> print(simulate_frames(num_frames=24, dt=1 / 120))  # User B
186.0

so over the same timeframe, user A saw the bird move 26 pixels, and user B saw it moving 50 pixels, almost twice as much!

How can we fix this?
simply comply to the laws of physics, and multiply the speed by dt before adding it to the bird's y position:

function Bird:update(dt)
    self.dy = self.dy + GRAVITY * dt

    if love.keyboard.wasPressed('space') or love.mouse.wasPressed(1) then
        self.dy = -5
        sounds['jump']:play()
    end

    self.y = self.y + self.dy * dt
end

However, this will cause the bird to move much slower, and we will need to set new values.
There is no right answer here, but from playing around with different values I put it as GRAVITY = 750 and dy = -200 for jumping, that seemed similar enough to what's shown in the lecture's video.

from fifty-bird.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.