FMath::SphereDistToLine
#include "Math/UnrealMathUtility.h"
Access: public
Specifiers: static
Description
Finds the closest point on a sphere's surface to an infinite line. When the line intersects the sphere, the closest point on the sphere to LineOrigin is returned instead.
Caveats & Gotchas
- • LineDir must be normalized. Passing a non-normalized direction produces incorrect results without any error — normalize it with GetSafeNormal() before calling.
- • The returned point is on the sphere's surface, not the closest point along the line. If you want the closest point on the line to the sphere's centre, project SphereOrigin onto the line separately.
- • The function does not return whether the line actually intersects the sphere — compute FVector::Dist(SphereOrigin, ClosestPointOnLine) < SphereRadius yourself if you need that flag.
Signature
static CORE_API void SphereDistToLine(FVector SphereOrigin, float SphereRadius, FVector LineOrigin, FVector LineDir, FVector& OutClosestPoint) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| SphereOrigin | FVector | Centre of the sphere. | — |
| SphereRadius | float | Radius of the sphere. | — |
| LineOrigin | FVector | A point on the infinite line. | — |
| LineDir | FVector | Direction of the line. Must be normalized. | — |
| OutClosestPoint | FVector& | Closest point on the sphere's surface to the line (or the projected point when the line intersects). | — |
Return Type
void Example
Compute where a laser beam grazes a sphere obstacle C++
FVector GrazePoint;
FMath::SphereDistToLine(
ObstacleSphere.Center, ObstacleSphere.Radius,
LaserOrigin, LaserDirection.GetSafeNormal(),
GrazePoint
);
DrawDebugSphere(GetWorld(), GrazePoint, 5.f, 8, FColor::Orange); See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?