How to set clamp to circle not square

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


I am working on a drag and drop style game and am trying to get the movement working correctly, I want to have the model only move up to its speed away from its starting point each turn and I can use clamp to do this, if I set clamp to the x and y it works by locking it to a square. but I want it to be locked to a circle.

any ideas on how to achieve that?

this is my code so far:

extends KinematicBody2D

export (int) var speed = 200

var mouse
var model
var selected
var anchor
var distance
var velocity = Vector2()
var screensize
var destination
var vector
var difference

func _ready():
	anchor = position
	screensize = get_viewport_rect().size

func _physics_process(delta):
	mouse = (get_global_mouse_position())

func select_model():
	model = position
	if mouse.distance_to(model) < size:
		if Input.is_action_just_pressed("mouse_left"):
			selected = true
	if Input.is_action_just_released("mouse_left"):
		selected = false

func get_input():
	distance = mouse.distance_to(anchor)
	difference = anchor - mouse
	if selected == true:
	position = Vector2((anchor.x - difference.x), (anchor.y - difference.y))
	position.x = clamp(position.x, anchor.x - speed, anchor.x + speed)
	position.y = clamp(position.y, anchor.y - speed, anchor.y + speed)
:bust_in_silhouette: Reply From: David Wong

Maybe you apply the code from the answer to the following question: