How this formula return intersection between circle and line?

41 Views Asked by At

circle line intersection

as you can see in the picture, the writer said the horizontal = radius * sin(theta) the vertical = 1 - radius * cos(theta) i know x = rcos(theta), y = rsin(theta) but I can't understand where this formula came from.

and the method is

private bool SimulateSphereCast(Vector3 groundNormal, out RaycastHit hit)
        {
            float groundAngle = Vector3.Angle(groundNormal, controller.up) * Mathf.Deg2Rad;

            Vector3 secondaryOrigin = controller.transform.position + controller.up * Tolerance;

            if (!Mathf.Approximately(groundAngle, 0))
            {
                float horizontal = Mathf.Sin(groundAngle) * controller.radius;
                float vertical = (1.0f - Mathf.Cos(groundAngle)) * controller.radius;

                // Retrieve a vector pointing up the slope
                Vector3 r2 = Vector3.Cross(groundNormal, controller.down);
                Vector3 v2 = -Vector3.Cross(r2, groundNormal);

                secondaryOrigin += Math3d.ProjectVectorOnPlane(controller.up, v2).normalized * horizontal + controller.up * vertical;
            }
            
            if (Physics.Raycast(secondaryOrigin, controller.down, out hit, Mathf.Infinity, walkable, triggerInteraction))
            {
                // Remove the tolerance from the distance travelled
                hit.distance -= Tolerance + TinyTolerance;

                return true;
            }
            else
            {
                return false;
            }
        }
    }

And whole articles is here

1

There are 1 best solutions below

1
On BEST ANSWER

The formula in the figure is wrong. It should say $(1 - \cos (\theta)) \times \mathop{radius}.$

It is $1 - \cos(\theta)$ because the distance is measured from the bottom of the circle, not from the center. The vertical distance of that point from the center of the circle is indeed $\cos(\theta)\times \mathop{radius}.$ The vertical distance from the center to the bottom of the circle is $\mathop{radius}.$ The vertical distance shown in the figure is the remaining vertical distance to the bottom after you have already come down a distance $\cos(\theta)\times \mathop{radius}$ from the center of the circle. This remaining distance is

$$ \mathop{radius} - \cos (\theta) \times \mathop{radius} =(1 - \cos (\theta)) \times \mathop{radius}.$$