From 08e0f7019b66255dfa4965bc3091f41747c0d837 Mon Sep 17 00:00:00 2001 From: Emily Hunt Date: Wed, 22 Jan 2025 22:00:40 +0100 Subject: [PATCH] Try improving framerate calculation --- src/sk8plotlib/__main__.py | 2 +- src/sk8plotlib/maps/__init__.py | 1 + src/sk8plotlib/sk8plotlib.py | 13 ++++++++----- src/sk8plotlib/variables.py | 6 +++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/sk8plotlib/__main__.py b/src/sk8plotlib/__main__.py index 4694421..b2ed12f 100644 --- a/src/sk8plotlib/__main__.py +++ b/src/sk8plotlib/__main__.py @@ -1,5 +1,5 @@ from sk8plotlib import sk8plot -from .maps import sinepark +from .maps import sinepark, thebigsine if __name__ == "__main__": sk8plot(sinepark()) diff --git a/src/sk8plotlib/maps/__init__.py b/src/sk8plotlib/maps/__init__.py index 0c4da65..d1e00a4 100644 --- a/src/sk8plotlib/maps/__init__.py +++ b/src/sk8plotlib/maps/__init__.py @@ -1 +1,2 @@ from .sinepark import sinepark +from .thebigsine import thebigsine diff --git a/src/sk8plotlib/sk8plotlib.py b/src/sk8plotlib/sk8plotlib.py index 7712afb..66c6935 100644 --- a/src/sk8plotlib/sk8plotlib.py +++ b/src/sk8plotlib/sk8plotlib.py @@ -1,3 +1,4 @@ +import numpy as np import matplotlib import matplotlib.pyplot as plt from matplotlib.figure import Figure @@ -15,7 +16,7 @@ matplotlib.use("QtAgg") def sk8plot(fig: Figure): animator = PlotAnimator(fig) animation = FuncAnimation( # noqa: F841 - fig, animator.update_animation, frames=range(100), interval=MIN_TIMESTEP + fig, animator.update_animation, frames=range(100), interval=0. ) plt.show() @@ -44,8 +45,8 @@ class PlotAnimator: self.last_frame_time = this_frame - MIN_TIMESTEP timestep = this_frame - self.last_frame_time - # self.skater.update(np.clip(self.average_timestep, MIN_TIMESTEP, 2*MIN_TIMESTEP)) - self.skater.update(MIN_TIMESTEP) + self.skater.update(np.clip(self.average_timestep, MIN_TIMESTEP, 2*MIN_TIMESTEP)) + # self.skater.update(self.average_timestep) self.camera.move_camera() # Framerate cap - calculated from a 1 second moving average @@ -53,8 +54,10 @@ class PlotAnimator: self.average_timestep * (MAX_FRAMERATE - 1) / MAX_FRAMERATE + timestep * 1 / MAX_FRAMERATE ) - # if self.average_timestep < MIN_TIMESTEP: - # time.sleep(MIN_TIMESTEP - self.average_timestep) + if self.average_timestep < MIN_TIMESTEP: + print("av: ", self.average_timestep) + print("sleep: ", MIN_TIMESTEP - self.average_timestep) + time.sleep(MIN_TIMESTEP - self.average_timestep) print(f"FPS (av): {1 / self.average_timestep:.2f} | FPS: {1 / timestep:.2f}") self.last_frame_time = this_frame diff --git a/src/sk8plotlib/variables.py b/src/sk8plotlib/variables.py index c68117c..0f870f6 100644 --- a/src/sk8plotlib/variables.py +++ b/src/sk8plotlib/variables.py @@ -1,5 +1,5 @@ # Game settings -MAX_FRAMERATE = 45 +MAX_FRAMERATE = 60 MIN_TIMESTEP = 1 / MAX_FRAMERATE GRAVITY = 10 TERMINAL_VELOCITY = 150 # m/s @@ -7,5 +7,5 @@ TERMINAL_VELOCITY = 150 # m/s # User speed settings # Rotation speed per frame USER_ROTATION_RATE = 1.5 * 3.14 # rad/s -USER_ACCELERATION = 10. # m/s/s -USER_MAX_SPEED = 20. # m/s \ No newline at end of file +USER_ACCELERATION = 15. # m/s/s +USER_MAX_SPEED = 25. # m/s \ No newline at end of file -- 2.51.2