2d | move_and_slide() in _physics_process(delta) is very jittery at decent-higher speeds, is this my fault?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Seph

Here is my code:

extends KinematicBody2D

func _ready():

# Default (0, 600)
const GRAVITY = Vector2(0, 600)

# Default (0, -1)
const FLOOR_NORMAL = Vector2(0, -1)

# Default 60

# Max movement speed, Default 300
const MOVEMENT_SPEED = 300

# Default 0.2
const ACCELERATION = 0.2

# Velocity
var velocity = Vector2()

# Used for horizontal movement
var movement = 0

func _physics_process(delta):
# Beginning of all movement
	velocity = move_and_slide(velocity, FLOOR_NORMAL, SLOPE_FRICTION)
	# Resets movement
	movement = 0

		# Left & Right Movement

	# Adjust the movement in order to see how bad the jitter is
	# Movement is A and D, Left or Right arrow keys, or Gamepad left & right

			# Movement - Left
	if Input.is_action_pressed("left"):
		movement -= 25

			# Movement - Right
	if Input.is_action_pressed("right"):
		movement += 25
	movement *= MOVEMENT_SPEED
	# Linear Interpolation, used for acceleration towards a point
	velocity.x = lerp(velocity.x, movement, ACCELERATION)

The only objects in my scene are the Kinematic body, the sprite attached to it, and a camera attached to it set as current with smoothing enabled at a value of ‘20’. That’s all.

This problem pretty much breaks my platformer, so I can’t really do anything more from here. I’m completely stumped.

This code works perfectly fine in Godot v2.2, so I am unsure what the problem is. Any help would be appreciated. If more information is needed, I can provide the project and/or pictures.


Here is a video demonstrating my problem:

Nothing catches my attention as source of problem. So if you can provide something to reproduce problem (scene/project) . Some picture would also be helpful.

Bartosz | 2018-03-29 22:31

is your camera moving? with smooth enabled? project settings with vsync enabled? and pixel snap enabled?

eons | 2018-03-30 00:52

My camera is moving, yes. Smooth is enabled with a value of ‘20’. Vsync is disabled. I am unsure where pixel snap is, so whatever the default setting is.

I am also unsure of what the best way to share my project is, sorry. Could I get a recommendation to make this easier for you guys?

Seph | 2018-03-30 01:25

If you turn off camera smoothing does the jittering persist? Any way you can embed a GIF of the jittering?

Diet Estus | 2018-03-30 04:40

Here is a link to a video showing my problem:

Turning off the camera smoothing makes the jittering go away a bit, but not all the way. I think the problem lies in move_and_slide(), but I’m not exactly sure.

Seph | 2018-03-30 06:54

I struggle with the same issue in my project. I get the same jittering as you when my sprite is free falling and reaches the bottom of the viewport. I, however, don’t notice the jittering when smoothing is disabled. I am going to experiment with writing some custom camera code, as mentioned by girng here and eons below. If I find something that works I will update my answer. This problem really needs a definitive write-up since I feel like a lot of users struggle with it.

Diet Estus | 2018-03-30 20:06

Okay, so I have done a bit of testing with camera smoothing off and the problem still persists. A lot. I made a video showing it:

This is a pretty terrible bug, I hope it gets fixed soon.

Seph | 2018-03-31 18:02

:bust_in_silhouette: Reply From: Diet Estus

This may not be the same issue you are experiencing, but there is a known issue regarding jittering that is caused by, or at least exacerbated by, camera smoothing.

According to this long and old GitHub thread, as of December 2017, the problem is tagged for resolution in Godot 3.1, but only if the devs get around to it (they have a lot on their plate).

You may be able to sift through that forum and other discussions of the problem to find a solution that works for you. (For example, enabling or disabling vsync or pixel snap, enabling filter on imported images, changing GPU settings, or writing custom camera code.)

If you do find a solution, please consider writing up an answer, since it would help others struggling with the problem.

There is also a related question here which seems to be exactly what you’re describing.

Hmm, I turned off camera smoothing and the problem still persists. I am unsure if the problem is with the camera smoothing or with the move_and_slide() itself. Here’s the video I made that shows the problem: https://www.youtube.com/watch?v=UnUPCiF3K98

The jitteryness also seems to be based on how fast my character is moving. The faster it moves, the worse it gets…

Seph | 2018-03-31 18:08

:bust_in_silhouette: Reply From: eons

That jitter seems to be related to camera smooth bug

A workaround for this can be making your own smooth script on the camera to control the speed and avoid reaching the target.

When you turn it off you may be seeing some subpixel related glitch, 2D pixel snap may help a bit, use the search function on project settings panel to find it (check if stretch mode on 2D or viewport aliviates a bit more too).

The remaining jitter may be related to physics process, it needs a fixed step interpolation that should be added in 3.1.
And the last possible jittering cause are the video drivers, some people solve it by turning off vsync, high rate monitors also can benefit from this, it seems.