Moving / Rotating Around a Sphere

Godot Version



Here is some code I’m using to push a player along a spherical surface. It rotates the player using the player node’s quaternion and the distance vector between the player’s global_transform subtracted from the the sphere’s global_transform. I’m having problems on the underside of the sphere, the player starts to drift when you move instead of moving straight in your input indirection. At certain specific points on the sphere the player will get stuck spinning no matter the input direction. This is my first time using quaternions so I don’t really know what I’m doing, but I think the drifting could also be apart of how the movement is being calculated? In the project settings I have gravity set to 0, and this script sets the up_direction. Any help with the math of this or input on which part is causing the drift or spins greatly appreciated!

@export var sphere: Node3D
var player_velocity = Vector3(0,0,0)
func _physics_process(_delta):
	var gravity_direction = (sphere.global_position - player_origin.global_position).normalized()
	up_direction = -gravity_direction

	var input_dir = Input.get_vector("left", "right", "up", "down")
	var movement_dir = (global_transform.basis * Vector3(input_dir.x,0,input_dir.y)).normalized()
	if input_dir:
		player_velocity = movement_dir * speed
		player_velocity = Vector3(0,0,0)

	velocity = player_velocity
	var rotation_quat = Quaternion(Vector3.DOWN, gravity_direction)
	quaternion = rotation_quat