Sorry I'm not sure if here is the correct place to ask this.
I'm actually having a problem with a calculation for a Game I make in Unity.
Anyway I'm posting this question here instead of StackOverflow because actually it is more the maths that are my problem here.
What I'm trying to archive
Given is this grafic:
In the application an Object can be dragged around from the position B using the mouse.
But for the movement there are two restrictions:
- It can't be tragged further away from
Bas the radiusmaxDistB. - It can't be tragged closer to
Athan the radiusminDistA.
So in short it can be placed anywhere in the green space.
In my program I know
- position A
- position B
- maxDistB
- minDistA
My actual (pseudo) code looks like this:
if(mouseHolding) {
newPosition = mousePosition;
// Check the distance to A
if(Distance(newPosition, A) > minDistA)
{
//Check the distance to B
if(Distance(newPosition, B) <= maxDistB)
{
// in this case just accept the newPosition
draggedObject.position = newPosition;
}
else
{
// otherwise dragg it in the correct direction but
// place the object on the circle arround B
// Get the direction of the dragg as normalized vector
direction = (newPosition - B).Normalize;
// Set the position of the Object to a limit range
draggedObject.position = B + direction * maxDistB;
}
} else {
// THIS IS WHERE I'M STUCK
}
}
My Problem
As you can see I stuck in the case where the object is dragged to close to A.
I know that for a movement on a streight line between A and B I could do e.g.
// Get the direction of the dragg as normalized vector
direction = (newPosition - B).Normalize;
// Get the distance between A and B
circleDist = |A - B|;
draggedObject.position = B + direction * (circleDist - minDistA);
But I have no clue how to calculate it, when dragging in another direction like shown in this image
The blue line would be the mouse dragging starting at B. I would like the object to end up on the red spot.
How do I have to calculate this?



First, the formula to place the object on tangent point of circle A is incorrect, since you don't want to use the constant minimum distance between B and a tangent point of A closest to B in the computation. The distance can change depending on the location.
One way to solve this is to use trigonometry and vector calculus.
Draw a triangle with verteces at A, B, and the tangent point on A (red circle). The length of two edges can be easily found from radius of A and B. All you have to find is the length of the third edge between B and the red circle given the angle formed by the edges between B and red circle and between A and B (you can use dot product of two vectors to compute the angle: $\bf{x}\cdot \bf{y} = |x||y|\cos\theta$). Use general trigonometric formula for finding length of third edge, and multiply that to the direction vector. I'm sure there are some simplification steps that don't require inverse cosine computation.
EDIT: I'm adding an answer based on you newest edit. You would re-arrange the triangle so that we now have verteces at A, red circle, and dark circle. We know the edge length from A to red, and from black to A, and the angle between A-black and red-black using the dot product. Find the length between red and black using trigonometry.