This is my second post, you can read my post before : How To Translate an Object Inside a Circle with Radius
The explanation is same as before, but in here we are using 2 radius values, radiusIn and radiusOut.
This is the 2D doughnut
distance = greenCircle position - centerCircle position
or you can count like this
distance = centerCircle position - greenCircle position
Same as when you restrict movement inside a circle. We use radius to define how far the object can move. So we can restrict the movement of object if the object moves out from the blue circle and holds the object to moves inside a red circle by its radius.
Explanation
When the object distance to center is greater than radiusOut, then we make restrict move to the object so the object keeps inside in blue circle. When the object distance to center is smaller than radiusIn, then we make restrict move to the object so the object keeps outside in red circle. With this 2 conditions we can keeps the object still moving in blue area.
posObj is position of object according to center position of blue/red circle. The variable objPos absolutelly has same value with distance but objPos is the real value without absolut the value.
objPos = greenCirclePosition - centerCirclePosition
is not same with
objPos = centerCirclePosition - greenCirclePosition
After we count the objPos, then we will calculate the position of object when the object is going moves to outside blueCircle or going moves to inside redCircle.
Restrict for blueCircle
objPos = objPos * radiusOut / distance
Restrict for redCircle
objPos = objPos * radiusIn / distance
Formulas above will always results a position value that has a distance with a center less than equal to blue circle radius if the object going moves to outside blue circle and has a distance with a center more than equal to red circle radius if the object going moves to inside red circle.
After that, we must have exception to avoid Divide by zero, when we restrict move to inside red circle. When we restrict the movement inside a red circle, most likely the distance will have a value of zero when greenCirclePosition on the center of circle. So Dividing by zero will hapen when checking to restrict moves to inside red circle.
To avoid this we can generate any values to greenCircle position when its position at the center of circle.
This is the result :