2

I've been trying to create a game using JavaFX, but couldn't help but notice random speedups in the animation speed. I've been using animation timer, and I created a simple script to output the amount of fps the last 2 frames were played at, based on the amount of ms in-between animation timer handle method invocations. Here is the output:

0
8
5
62
62
62
62
62
62
62
62
62
62
62
62
62
31
62
62
62
62
62
62
35
200
66
142
111
62
62
62
62
58
66
62
62
62
62
58
66
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
62
58
62
66
62
62
62
62
62
62
62
62
62
62
62
62
58
66
62
62
62
62
62
62
62
62
62
62
62
62
62
62
58
66
62
62
62
62
62
58
62
66
62
58
62
66
62
58
66
62
62
62
62
62
62
62
62
62
62
62
58
66
62
62
62
62
62
62
58
62
66
62
62
58
66
62
62
62
62
62
62
62
62
62
62
58
62
62
62
62
62
66
52
71
62
62
62
66
62
62
62
62
62
62
62
62
62
62
62
62
62
27
1000
37
166

So obviously, you can see that while it does hover around an ok 62, random speedups and slowdowns are present (notably random 100s, 200s, and an entire 1000 at the end), and I need to find a way to remove them to keep the game's physics running properly. Is there any way to set the frame cap? I've been searching around the internet, and haven't been able to find any solution that works. While most of them more or less seem ok, 1000 was especially troublesome, passing over 16 frames in the span of 1s.

  • 2
    Perhaps a duplicate of: [How to create 100fps animation in JavaFx8](https://stackoverflow.com/questions/28819409/how-to-create-100fps-animation-in-javafx8/28820370#28820370) – jewelsea Dec 13 '19 at 00:14
  • 2
    Also see: [What is the preferred way of getting the frame rate of a JavaFX application?](https://stackoverflow.com/questions/28287398/what-is-the-preferred-way-of-getting-the-frame-rate-of-a-javafx-application), in case a suggested method there is superior to whatever you are doing. – jewelsea Dec 13 '19 at 00:15
  • 3
    You probably need to modify your physics engine implementation if its results vary when the framerate is not constant. It is not a good idea to perform calculations assuming a constant framerate, instead calculations should be made based on the actual time taken since calculations were last made for the prior rendered frame. See this [bouncing balls example](https://gist.github.com/james-d/8327842), which updates the world based upon the elapsedTime for each frame. – jewelsea Dec 13 '19 at 00:22

0 Answers0