Limit vector length of a physics body

Godot Version

4.1.1

Question

I try to limit the vector length of a physic body. Like seen in the picture the grey circle is a physics body, the white circle around is the area it should only be able to move in. (Its like a touch button for movement)

I can calculate the distance between the midle of white area and the physics body with direction_to() and turn off velocity when the distance reached its maximum but i want to still be able to move the physics body on the edge of the radius when the length is maxed.

Any ideas?

Why is a physics body being used for this? Wouldn’t a Node2D with a Sprite2D suffice?

its just an example for better undestanding, the purpose is different and embeded in something else

Just clamp the position.

how does it work with physics body if you have to add velocity and impulse to move the object

Godot docs says this because it’s not best practice but you can still do it.
Also you can use Area2Ds instead of RigidBody2Ds to escape that.

You could also try making it accelerate towards your target, but have a StaticBody2D in the way with a collision shape like the outline of a circle.

i have to use physics for that, since the node also have other tasks to do with it.

Thats the workaround currently, i hoped to make it clean by code

Well, if that works for you then mark it as solution.

Like i said its not what i was looking for, but thanks for your effort

I’m not sure what you want to do but you could try using the RigidBody2D._integrate_forces() callback to modify the physics state of the rigid body.