lawnjelly / smoothing-addon

Fixed timestep interpolation gdscript addon for Godot Engine (version 3.2 or later)
MIT License
480 stars 28 forks source link

Still getting jitter from the camera movement #9

Open phantomdesvin opened 3 years ago

phantomdesvin commented 3 years ago

Thank you so much for this addon, it works very well with the kinematic2D player, but my game is still very jittery because the camera movement. Is it possible to use the addon to smooth the camera?

lawnjelly commented 3 years ago

Yes you should be able to just attach a camera2d to a smoothing node, I just tested it and it seemed to work for me.

But there may be an order of processing issue in your project, have you got a minimum reproduction project as a zip file, or a screencap of your scene tree? Maybe that will help show what is going on. :+1:

phantomdesvin commented 3 years ago

Then I must be doing something wrong. I am testing it with the latest 2D Platformer Demo (KinematicBody): https://godotengine.org/asset-library/asset/120 and the attached proyect is my attempt to make it work. The character only jitters because the camera jitters, but is perfectly smooth at the top right of the level, because there the camera doesn't move. In order to make the camera and the player work, I had to activate the parameter "global out" of both smooth2D nodes, and change their code references on the level and player scripts. Also turned the physics jitter fix to 0 as recomended. platformer.zip

jwunderlin commented 3 years ago

I'm also having this issue. If you enable the camera smoothing in the platformer project provided by @phantomdesvin you can see the background sprites jitter more easily once the player stops moving. But this jitter happens whenever the camera moves, not just during smoothing.

lawnjelly commented 3 years ago

Ok I think I have fixed it, here's updated version of the project.

What I did:

This may not have been necessary but I always recommend this. In 3d it often is necessary so is now enforced through the addon, it will complain if you try and add a smoothing node with a target that is parent / grandparent.

(you can change it back to 60 as you will see the enemies and platforms are not smoothed, or you could add smoothing nodes to them too).

platformer_smooth.zip

I also saw some warning about the gdscript in smoothing_plugin.gd, I'll see if I can work out what is causing this. It doesn't seem to stop it from working.

lawnjelly commented 3 years ago

There are also some I believe physics quirks going on in that demo. When I first jump I always see a glitch, and this occurs on the original demo too. There are a few other quirks. I haven't looked in detail into this.

I suspect it is due to using full physics 2d when really this kind of game could be done with a much simplified physics model.

phantomdesvin commented 3 years ago

Yes, the camera smothing is working now. Some things I noticed:

The partial floor jitter: not caused by the smoothing addon: on some places (at the start of the level, and also when the player goes to the top right part of the level) only some purple parts of the floor jitter and only while you move the player in that zone. That is strange because it happens even if the camera is not moving (like on the right part of the level). This partial floor jitter is present on both projects, but it is difficult to notice in the original proyect if you have a non-60Hz monitor because everything jitters, so I had to change the physics tick rate to match my monitor Hz to see it.

I also saw the warning about smoothing_plugin.gd while opening your platformer_smooth.zip, but only happens with that proyect so may be something there. It only happened the first time I opened it.

Short duration general jitters (maybe stuttering), not caused by the smoothing addon: it happens in your platformer_smooth proyect and are more frecuent and more clearly seen when the physics tick rate is set high, but difficult to see at 30 ticks. Those short duration jitters also happen in the original platformer demo without the addon (but are harder to notice), so they aren't caused by it. Probably another source of jitter/stutter that is caused by the proyect or by the engine. It happens on the latest release candidate too, and also with exported game, and Vsync via compositor or full screen doesn't solve it.

The best results are obtained by:

  1. The platformer_smooth proyect with 60 physics tick rate, or even 30. (Of course the elements not affected by the smoothing addon won't be smooth, but can be smoothed with more smoothing nodes). With higher physics tick rate, the short duration general jitters become more noticeable. At 30 they are difficult to see.
  2. The original platformer demo but with the physics rate matching your monitor Hz, but of course that solution is not ideal. For some reason, the short duration general jitters are not as visible as with the platformer_smooth proyect with the same high physics tick rate, but are still present.
lawnjelly commented 3 years ago

I also saw the warning about smoothing_plugin.gd while opening your platformer_smooth.zip, but only happens with that proyect so may be something there. It only happened the first time I opened it.

It may be a pre-existing bug in godot core, I posted on this issue which seems to be similar. I'm not super worried about it at the moment because it only seems to occur when the addon is first scanned: https://github.com/godotengine/godot/issues/39921

  • The platformer_smooth proyect with 60 physics tick rate, or even 30. (Of course the elements not affected by the smoothing addon won't be smooth, but can be smoothed with more smoothing nodes). With higher physics tick rate, the short duration general jitters become more noticeable. At 30 they are difficult to see.
  • The original platformer demo but with the physics rate matching your monitor Hz, but of course that solution is not ideal. For some reason, the short duration general jitters are not as visible as with the platformer_smooth proyect with the same high physics tick rate, but are still present.

Some of it may be due to physics, some due to duff timing information from the OS, which delta smoothing can be used to help fix. I have delta smoothing working but it has to be integrated in core, it can't be done as an addon. I was going to do a PR for delta smoothing after semi fixed timestep, but there seemed to be little hope (politically) of getting improvements to timing merged when I was looking at it (a year or so ago). Perhaps we'll be able to improve some of this for 4.x.

The default fixed timestep largely ignores OS timing information, which is why it might not exhibit the problem so much if you happen to match the refresh rate.

phantomdesvin commented 3 years ago

I was going to do a PR for delta smoothing after semi fixed timestep, but there seemed to be little hope (politically) of getting improvements to timing merged when I was looking at it (a year or so ago). Perhaps we'll be able to improve some of this for 4.x.

I would love to see improvements in that area. I see that there is some disscusion about this topic and honestly I don't have the knowledge to know wich one is the best solution, but it is clear to me that we need a solution as soon as possible.

I tried a few more things, and one of them worked very well to my surprise! It's using the same 30 or 60 physics rate + smoothing addon but this time setting the physics jitter fix to 0.5 like it was by default. Surprisingly, the short duration general jitters almost dissapear. You need to examine it very very well to be able to see it but is incredibly subtle and difficult to notice, so the cause is not solved, just masked somehow. But the result is pretty good, for me at least is totally aceptable. I don't know if the result will be the same in all refresh rates though.

lawnjelly commented 3 years ago

Yes it may be that jitter_fix has some effect, but jitter_fix is not designed for that purpose. Delta smoothing is really the more appropriate solution (and / or accurate figures from some newer timing APIs).

https://github.com/godotengine/godot/issues/30791

If I get time I'll try and update and make the files available for delta smoothing etc so that interested users can compile the engine themselves and try it.

Calinou commented 2 years ago

@phantomdesvin Can you still reproduce this in 3.4 beta6? https://github.com/godotengine/godot/pull/48390 was merged in the 3.x branch.

You can also try enabling https://github.com/godotengine/godot/pull/48555 in 3.4 beta6.

phantomdesvin commented 2 years ago

@phantomdesvin Can you still reproduce this in 3.4 beta6? godotengine/godot#48390 was merged in the 3.x branch.

You can also try enabling godotengine/godot#48555 in 3.4 beta6.

With the addon enabled?

Without the addon it still jitters, with and without sync frame delta after draw. I think that will keep happening when the monitor's Hz don't match the physics ticks per second until we have physics interpolation.

With the addon enabled (I'm using the file from lawnjelly , the latest beta gives me this error, that is also given by Godot 3.3.4:

Unable to load addon script from path: 'res://addons/smoothing/smoothing_plugin.gd'. This might be due to a code error in that script. Disabling the addon at 'res://addons/smoothing/plugin.cfg' to prevent further errors.

And the console says:

ERROR: Condition "!f" is true. Returned: ERR_CANT_OPEN
   at: _load_data (scene/resources/texture.cpp:466)
ERROR: Failed loading resource: res://.import/smoothing.png-6b454a779e636eaa20b6c6ac618bf82a.stex. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:270)
ERROR: Failed loading resource: res://addons/smoothing/smoothing.png. Make sure resources have been imported by opening the project in the editor at least once.
   at: (core/io/resource_loader.cpp:270)
SCRIPT ERROR: Parse Error: Can't preload resource at path: res://addons/smoothing/smoothing.png
          at: GDScript::reload (res://addons/smoothing/smoothing_plugin.gd:7)
ERROR: Method failed. Returning: ERR_PARSE_ERROR
   at: reload (modules/gdscript/gdscript.cpp:566)
ERROR: Cannot open file 'res://.import/music.ogg-e27216112531ea490cdad574860a53a5.oggstr'.
   at: (core/io/resource_format_binary.cpp:984)
ERROR: Cannot open file 'res://.import/coin_pickup.wav-69d455f3063e30fd994846647c281aea.sample'.
   at: (core/io/resource_format_binary.cpp:984)
ERROR: Cannot open file 'res://.import/explode.wav-302a3de6d016432bf47477d337a4d553.sample'.
   at: (core/io/resource_format_binary.cpp:984)
ERROR: Cannot open file 'res://.import/hit.wav-27e178036f6cee6545e9f025a3865a36.sample'.
   at: (core/io/resource_format_binary.cpp:984)
ERROR: Cannot open file 'res://.import/jump.wav-127418ac4fc0b61a7fed0684053202c7.sample'.
   at: (core/io/resource_format_binary.cpp:984)
ERROR: Cannot open file 'res://.import/shoot.wav-f3ce9b4ae60220ecd3cf78df7278a10f.sample'.
   at: (core/io/resource_format_binary.cpp:984)

But the game runs well and the addon seems to be working as before: it removes most of the jitter on the player and camera movement.

I noticed that the problem with the floor jitter that I mentioned in my previous comment is solved, I can't reproduce it now, not even on 3.3.4, or with this addon disabled, so I guess that was solved by 3.3.4 or a previous version:

The partial floor jitter: not caused by the smoothing addon: on some places (at the start of the level, and also when the player goes to the top right part of the level) only some purple parts of the floor jitter and only while you move the player in that zone. That is strange because it happens even if the camera is not moving (like on the right part of the level). This partial floor jitter is present on both projects, but it is difficult to notice in the original proyect if you have a non-60Hz monitor because everything jitters, so I had to change the physics tick rate to match my monitor Hz to see it.

The occasional hiccups keep being there in the beta, with and without the addon and with and without sync frame delta after draw: ocassional small hiccups every 3-10 seconds. They improve setting the physics tick rate to 75 to match my monitor's Hz, or to 60 (lawnjelly's proyect is at 10 tick rate) but they are not completely removed, they just happen noticeably less frecuently. Update: I see that it's also improved by setting the window mode to full screen. And at that point the hiccups are so rare that it's really hard for me to know if other settings may have having an effect on the frecuency of the hiccups, so I don't know if smoothing, the jitter fix at 0.5, rhe sync frame delta after draw or any other thing is also improving the hiccups or not. Update 2: yeah I'm pretty sure that the jitter fix at 0.5 is smoothing the hiccups: the frecuency is the same, but the hiccups are stronger if jitter fix is at 0.

There is a new kind of jitter that I'm seeing now for the first time but it's also present in 3.3.4 and may have been present before, because it is hard to notice: the floor (or the camera) jitters when the player reaches its maximum jump height right before falling. That doesn't happen if I set the physics to 60 or 75 so I think it is caused by the interpolation of the addon trying to interpolate 10 TPS physics (as I said that's how lawnjelly's proyect was set) and not knowing if the player is going up or down. Without the addon it's imposible to know if this happens at 10 TPS and it doesn't happen at 75 TPS. Sync frame delta after draw doesn't affect this problem.