Sorry if my english is bad. I want to share about my work about translating an object inside a circle.
You must know that circle has radius to define how wide the circle.
Let's begin with the simple explanation.
Circle
distance = redCircle position - centerBlueCircle position
We just want to get the distance between 2 points. So it will same if you count the opposite.
distance = centerBlueCircle position - redCircle position
After you got the distance, you will know that distance is a how far object to center position of circle. Which means we know that if the distance is less than radius, the object is inside in our circle, but other than the object is out from our circle. So we can restrict the movement of object if the object moves out from the circle.
Explanation
When the object distance to center is larger than radius, then we make restrict move to the object.
posObj is position of object according to center position. We want to get the position according to center position, so the result is not same when we do it the opposite calculation. Because we want to know is the object position in the left side or rigth side or maybe in top side of circle or bottom side of circle. The variable objPos absolutelly has same value with distance but objPos is the real value without absolut the value.
objPos = objectPosition - center
is not same with
objPos = center - objectPosition
objectPosition is the red circle according to the circle picture and center is the center position of blue circle according to circle picture.
After we count the objPos, then we will calculate the position of object when the object is going moves to outside circle.
objPos = objPos * radius / distance
the formula above will always results a position value that has a distance with a center less than equal to radius.
This is the result :