1
\$\begingroup\$

In Ursina, how can I programmatically obtain the current FPS count as number (like extracting it from the fps counter in the upper-right corner)? I need this for phyiscs calculations in Ursina. I have thought about something like this:

from ursina import *

app = Ursina()

e = Entity(model="some_big_model_to_cause_lags.obj",texture="some_texture.png")

def update():
    print(app.get_fps()) # should print 60, 75, etc.

app.run()

# should print something like:
60
61
63
59
62
59
...

I would also accept app.step() in a loop and somehow get the time between each call.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ For physics calculations, you'd usually want the reciprocal of framerate, the time elapsed between consecutive frames, often called "delta time" or dt, which seems to be exposed in Ursina as time.dt. Is that what you're looking for? (1.0/time.dt would then give you an instantaneous framerate — 0.33333 s/frame ≈ 30 frames/s). Generally though you don't want to use this number directly in a physics calculation, since variations in framerate can then cause inconsistency in your game logic/simulation. We'll usually accumulate this and run game logic on a fixed timestep. \$\endgroup\$
    – DMGregory
    Commented Nov 4, 2023 at 19:01
  • \$\begingroup\$ Yes, that's exactly what I need. The caculations should be at the same speed no matter how many fps the game runs at as the calculations are based on t (time). \$\endgroup\$ Commented Nov 5, 2023 at 9:45
  • \$\begingroup\$ See point 4 here for some info about why scaling by delta time does not always deliver consistent behaviour at varying frame rates. You may want to consider using a fixed timestep if consistency/fairness is important in your game. If you've solved your problem to your satisfaction, please post your solution as an Answer below that can help others dealing with similar issues in the future. \$\endgroup\$
    – DMGregory
    Commented Nov 5, 2023 at 11:28
  • \$\begingroup\$ I found the best thing: time.dt instead of 1/time.dt. Now it runs perfectly at every fps. \$\endgroup\$ Commented Nov 5, 2023 at 11:33

0

You must log in to answer this question.

Browse other questions tagged .