What causes this tile/camera glitch?

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

Sometimes when the camera moves it shows this weird visual tile glitch. What could be the cause, and where should I start looking for a solution?

The game is stretched in viewport mode, gpu pixel snap is on. Camera smoothing is turned off, all movement is done by code.

glitch still
glitch gif

Not sure what causes this but there is a GitHub repo for bugs in Godot.

exuin | 2021-06-06 17:38

:bust_in_silhouette: Reply From: code

I think the problem is with your code

the way you move your camera might be a little buggy can you showcase your camera movement code

Thanks for having a look!
By the way, although I’ve changed nothing on the camera, I haven’t spotted this glitch in a while, which is weird.
Anyway, here is the camera movement code, it’s a look ahead camera:

extends Camera2D

export var look_ahead_factor = 0.2

# look ahead shift in direction
const SHIFT_TRANS = Tween.TRANS_SINE
const SHIFT_EASE = Tween.EASE_OUT
export var shift_duration = 0.8

var facing = 0

onready var prev_camera_pos = get_camera_position()
onready var tween = $ShiftTween
#onready var player = get_parent()

func _ready():
	# adjust limits based on tilemaps in scene, function omitted in this paste
	auto_set_limits()

func _physics_process(delta):
	# update camera offset to always look ahead
	check_facing()
	prev_camera_pos = get_camera_position()

func check_facing():
	var new_facing = sign(owner.velocity.x)
	if new_facing != 0 && facing != new_facing:
		facing = new_facing
		var target_offset = get_viewport_rect().size.x * facing * look_ahead_factor
		tween.interpolate_property(self, "position:x", position.x, target_offset, shift_duration, SHIFT_TRANS, SHIFT_EASE)
		tween.start()

Hapiel | 2021-06-09 20:39

i found the problem

you are running a tween inside a process which will cause lagging because as your camera move your tween will need to recalculate the speed at which your camera move to the target and the closer you get the speed will keep changing causing the glitch.

I will advice to use signal as a trigger for your camera movement which will make your tween only run once

code | 2021-06-10 00:20