Adjusting Physics FPS to match monitor refresh rate

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

I have recently begun implementing a system into a project where the physics fps will be adjusted to match the monitor refresh rate. To do this vsync is enabled and in the _process() function of an autoloaded script the fps is measured and then the physics framerate is set to it as such:

func _process(_delta: float) -> void:
    var fps = Engine.get_frames_per_second()
    if fps > 60:
	    Engine.iterations_per_second = fps

However this is causing an issue where objects that use the delta value of physics process are moving slower at high refresh rates such as 360Hz. Here is the movement code:

func _physics_process(delta: float) -> void: 
    direction.x = int(Input.is_action_pressed("MoveRight")) * Input.get_action_strength("MoveRight") - int(Input.is_action_pressed("MoveLeft")) * Input.get_action_strength("MoveLeft")
    direction.y = int(Input.is_action_pressed("MoveDown")) * Input.get_action_strength("MoveDown") - int(Input.is_action_pressed("MoveUp")) * Input.get_action_strength("MoveUp")
    if direction != Vector2.ZERO:
	    velocity = MAXSPEED * direction
	    velocity = Vector2.ZERO
    move_and_slide(velocity * delta)

Surely the decreased delta from the higher physics fps should be balanced out by having more physics frames? How do I fix this?

:bust_in_silhouette: Reply From: AlexTheRegent
linear_velocity is the velocity vector (typically meters per second). Unlike in move_and_collide(), you should __not__ multiply it by delta — the physics engine handles applying the velocity

Right now you have delta^2 factor of speed from FPS instead of linear delta. So remove your multiplication by delta in

move_and_slide(velocity * delta)

Thank you for the help, I’d missed that part of the docs

InitialCon | 2021-01-20 15:56