0 votes

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
        SetDestination(get_global_mouse_position())

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

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

Here's the output of print(moveandslide(offset.normalized() * moveSpd).length())

Any ideas how to fix it?

in Engine by (12 points)

Hi,
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?

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.