Infinite acceleration and sliding on planetoid platformer 2D

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

I’ve got this simple script for moving around, and jumping on a planet in 2D.
Currently though, when standing still or holding any movement key down for an extended duration the player will spin out of control around the planet. I’m quite new to Godot and I’m stumped by this problem.

Gyazo gif of the problem in action

The only script in this scene is on the player (KinematicBody2D):

    extends KinematicBody2D

export var move_speed = 200.0
var velocity := Vector2.ZERO

onready var target = get_parent().get_node("Planet")

onready var jump_velocity := -800
onready var jump_gravity := 1600
onready var fall_gravity := 400

func _physics_process(delta):
	var gravity_dir = (target.global_transform.origin - global_transform.origin).normalized()
	var rotatedVelocity = velocity.rotated(rotation)

	var input_vector = Vector2.ZERO
	input_vector = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

	velocity.y += jump_gravity * delta
	velocity.x = input_vector * move_speed

	rotation = gravity_dir.angle() - PI/2

	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity

	rotatedVelocity = move_and_slide(rotatedVelocity, -gravity_dir)