FMath::SegmentDistToSegmentSafe
#include "Math/UnrealMathUtility.h"
Access: public
Specifiers: static
Description
Safe variant of SegmentDistToSegment that handles degenerate zero-length segments without undefined behaviour. Use this whenever either segment's endpoints could be identical.
Caveats & Gotchas
- • The additional safety check adds a small amount of overhead vs the non-safe variant. Prefer SegmentDistToSegment when you can statically guarantee non-zero segment length.
- • When one segment degenerates to a point, OutP1 (or OutP2) will equal that point. The result is well-defined but may be surprising if you expected OutP1 != OutP2.
- • The template parameter T is typically float (FVector) or double (FVector3d). Mixing scalar types in the same call is a compile error.
Signature
template<typename T>
static CORE_API void SegmentDistToSegmentSafe(UE::Math::TVector<T> A1, UE::Math::TVector<T> B1, UE::Math::TVector<T> A2, UE::Math::TVector<T> B2, UE::Math::TVector<T>& OutP1, UE::Math::TVector<T>& OutP2) Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| A1 | UE::Math::TVector<T> | Start of the first segment. | — |
| B1 | UE::Math::TVector<T> | End of the first segment. | — |
| A2 | UE::Math::TVector<T> | Start of the second segment. | — |
| B2 | UE::Math::TVector<T> | End of the second segment. | — |
| OutP1 | UE::Math::TVector<T>& | Closest point on segment 1 to segment 2. | — |
| OutP2 | UE::Math::TVector<T>& | Closest point on segment 2 to segment 1. | — |
Return Type
void Example
Robust closest-point query where segments may have zero length C++
FVector P1, P2;
// Either segment might be a zero-length degenerate if both handles overlap
FMath::SegmentDistToSegmentSafe(HandleA_Start, HandleA_End, HandleB_Start, HandleB_End, P1, P2);
DrawDebugLine(GetWorld(), P1, P2, FColor::Yellow, false, -1.f, 0, 1.f); See Also
Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?