RealDocs

FMath::SegmentDistToSegment

function Core Since 4.0
#include "Math/UnrealMathUtility.h"
Access: public Specifiers: static

Description

Finds the closest pair of points between two 3D line segments. Returns the points on each segment that are nearest to each other, not a distance value.

Caveats & Gotchas

  • If either segment has zero length (start == end), the function has undefined behaviour — use SegmentDistToSegmentSafe instead when segment length cannot be guaranteed.
  • The distance between OutP1 and OutP2 equals the minimum distance between the two segments; compute it yourself with FVector::Dist(OutP1, OutP2) if you need the scalar value.
  • Segments are passed by value (not reference), so large vectors are copied on each call — negligible for FVector but worth noting in tight loops.

Signature

static CORE_API void SegmentDistToSegment(FVector A1, FVector B1, FVector A2, FVector B2, FVector& OutP1, FVector& OutP2)

Parameters

Name Type Description Default
A1 FVector Start of the first segment.
B1 FVector End of the first segment.
A2 FVector Start of the second segment.
B2 FVector End of the second segment.
OutP1 FVector& Closest point on segment 1 to segment 2.
OutP2 FVector& Closest point on segment 2 to segment 1.

Return Type

void

Example

Find the gap between two ropes and apply a force C++
FVector P1, P2;
FMath::SegmentDistToSegment(RopeA_Start, RopeA_End, RopeB_Start, RopeB_End, P1, P2);
float Gap = FVector::Dist(P1, P2);
if (Gap < AttractRadius)
{
    // P1 and P2 are the closest contact points
    ApplyAttractForce(P1, P2, Gap);
}

Tags

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.