Skip to main content
2 of 5
deleted 423 characters in body
Theraot
  • 28.3k
  • 4
  • 55
  • 83

If I understand correctly, you need to solve this:

triangle diagram

That is a triangle with one side L being the segment from the center of one sphere to the other, another side r is the sum of the radius of the spheres.

We know L, and r. We know that the triangle has a right angle (because one side is a tangent and another side is a radius). And we want to find θ.


We are working on a plane, that would be defined by the two centers of the sphere and whatever velocity vector you are using as reference. And we actually want two angles, one is the angle between the reference velocity vector and vector that goes from the first sphere to the second plus θ, and the other is the same angle but minus θ.


We can find θ like this:

r/sin(θ) = L/sin(τ/4) where τ = one turn
=>
r/sin(θ) = L/1
=>
r/sin(θ) = L
=>
r = L*sin(θ)
=>
r/L = sin(θ)      
=>
asin(r/L) = θ     

Then your angles are

var referenceAngle = angle(referenceVelocity, L);
var angle1 = referenceAngle - θ;
var angle2 = referenceAngle + θ;

Complete pseudo code:

(float angle1, float angle2) getAngles(sphere1, sphere2, referenceVelocity)
{
    var L = sphere2.position - sphere1.position;
    var r = sphere1.radius + sphere2.radius;
    var θ = asin(r/L);
    var referenceAngle = angle(referenceVelocity, L);
    return (referenceAngle - θ, referenceAngle + θ);
}

If you only want to check if the referenceVelocity is out of the range, then:

bool isOutOfRange(sphere1, sphere2, referenceVelocity)
{
    var L = sphere2.position - sphere1.position;
    var r = sphere1.radius + sphere2.radius;
    var θ = asin(r/L);
    return angle(referenceVelocity, L) > θ;
}

Sure, you can use table lookup for the trigonometry if that is a problem.

Theraot
  • 28.3k
  • 4
  • 55
  • 83