Lock cursor movement to a single circle

Godot Version

Godot 4.2.1

Question

To preface, I am a beginner to Godot and I am currently making a 2D game. The way I want the mouse movement to work is for the mouse to be stuck inside of a single circle that goes all the way around the character. My question is: How can I program the cursor to only stay on the border of this single circle?
I have asked on reddit but the only answer that I got was someone telling me that I need to use “Viewport.warp_mouse” as well as “InputEventMouseMotion.relative” without giving me any proper explanation of how or where I am supposed to use those specific things.
All I have so far is the basic movement that default to the CharacterBody2D. Is there any way somebody could help me figure this out?

use distance_to to determine the distance between two vector2. in this example, global positions are vector2.

extends Node2D
func  _process(a_delta):
	var v_mouse_position : Vector2 = get_global_mouse_position()
	var v_distance : float = global_position.distance_to(v_mouse_position)
	if(v_distance > 100):
		print (v_distance)

This gives me the distance between the mouse and the position of the character and prints if it is greater than one hundred. Is there a specific function I could use to hold the mouse at a certain distance from the character constantly?

here is code that keeps a point within a circle. you can adapt the code to work with the mouse if needed

extends Sprite2D
var m_center : Vector2 = Vector2(300,300)
var m_diameter : float = 150

func  _draw():
	var v_mouse_position : Vector2 = get_global_mouse_position()
	
	
	#draw large circle
	draw_circle(m_center, m_diameter, Color(1,1,1,.5))
	
	
	#distance of mouse from center of circle
	var v_distance : float = m_center.distance_to(v_mouse_position)
	
	
	if(v_distance < m_diameter):
		#
		# mouse is in circle, just draw the point
		#
		draw_circle(v_mouse_position, 10, Color.YELLOW)
	else:
		#
		# mouse if too far!
		# make point orbit center of circle
		#
		# angle from centire to mnouse position in radians
		var v_angle : float = m_center.angle_to_point(v_mouse_position)
		# convert to Vector2
		var v_position : Vector2 = Vector2.from_angle(v_angle)
		# multiply by diameter to get distance from center of circle
		v_position = v_position * m_diameter;
		# draw point. it is either red or yellow, depending on distance to center
		draw_circle(m_center + v_position, 10, Color.RED)

func _process(delta):
	queue_redraw()

1 Like