Hi all Hope u can help, I have an x and a y and I want clamped circular movement.

I know how to get a length from the (centre - vector2(x,y)).length() And times it by a radius I.e. 50 But when moving the x and y they can be moved endlessly so it doesn't point in the direction of. Or once say x goes back the other way, I've tried normalizing them but maybe I'm missing something.

Also, though I’m not sure how helpful it will be, you can get the angle the position is from the center using atan2:

var difference_vector = (center - Vector2(x, y))
var angle_from_point = atan2(difference_vector.y, difference_vector.x)

I’m not sure if it will help, but maybe it can be used to calculate which direction the movement needs to be in?

Let me explain my answer in more detail. The reason why I say you should rotate your vector 90°, is because that would give you the tangent along the circular path you are trying to achieve. Use that vector as your velocity vector.

Can you explain what you are trying to accomplish. I'm not sure I understand the question.

hi

yes, trying to do a dual stick control with and aim that is kept in a circle, the left stick is easy, but below is the right stick code to get a smoother facing angle and maintain the direction. the below works quite well but as the lookaxis is moving left it minuses from lookaxis.x and then when i go right it takes a long time to get there due to needing to add until it get right, the same problem with left and right.

i did look at getting the angle from the stick but it's quite jittery, i need it to stay... or maybe I'm over thinking and there is an better solution.

var lookaxis = Vector2()
var deadzone = 0.25

lookaxis.x = float(Input.get_action_strength("look_right")) - float(Input.get_action_strength("look_left"))
lookaxis.y = float(Input.get_action_strength("look_down")) - float(Input.get_action_strength("look_up"))
if abs(lookaxis.x) > deadzone || abs(lookaxis.y) > deadzone:
	dir = lookaxis

var pospos = $Aim.position + lok_axis.normalized() * 20
aimangle = ($Aim.global_position - global_position).angle()
var dist_from_centre = (pospos - position).length()

#keeps the aiming pos in a circle
if dist_from_centre > 300:
	$Aim.position = (pospos.normalized() * 300) * 1 #change 1 to higher for a quicker turn
3 years later