movement smoothness problem

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

Probably dumb question here. Below code is attached to a KinematicBody2D which after mouse click should rotate, wait and start moving to selected point. Everything works fine except the movement because the distance moved between frames is not constant.

extends KinematicBody2D

export var moveSpd = 200
export var moveDelay = 1
export var rotationSpd = 4

var destination
var moveDelayTimer = 0

func _ready():
	destination = position

func _process(delta):
	if Input.is_action_just_pressed("mb_left"):
		moveDelayTimer = 0

func _physics_process(delta):
	var offset = destination - position
	if offset != Vector2.ZERO:
		if rotation == offset.angle():
			if moveDelayTimer >= moveDelay:
				if offset.length() < moveSpd * delta:
					position = destination;
					moveDelayTimer = 0
					print(move_and_slide(offset.normalized() * moveSpd).length())
					#not constant - print((offset.normalized() * moveSpd).length())
					#constant - print(moveSpd)
					#constant - print(offset.normalized().length())
				moveDelayTimer += delta
			var rotOffset = rotationSpd * delta
			if abs(rotation - offset.angle()) >= rotOffset:
				rotate(rotOffset * sign(offset.angle() - rotation))
				rotation = offset.angle()

func SetDestination(dst):
	if ((dst - position).abs() < get_node("Sprite").texture.get_size()):
	destination = dst

Here’s the output of print(move_and_slide(offset.normalized() * moveSpd).length())

Any ideas how to fix it?

seems to be pretty constant. There is only a difference of about 0.0001. This seems to be floatpoint imprecissions or slight “physics-framerate” divergence.

What is the problem? You expect every value to be exact 200?

klaas | 2020-08-18 07:22