FBox Line Intersection

I’m testing a line to the origin of a transformed FBox and want to figure out where on the box the line meets using a FMath::LineExtentBoxIntersection(). Everything works perfectly until I add rotation which simply increases or decreases the extent of the box and doesn’t provide an accurate hit result.

FVector GetTransformedMeshExtents(UStaticMesh* ISMMesh, const FTransform& MeshTransform, const FVector& OffsetDirection)
{
// Get mesh bounding box and transform
FBox TransformedBox = ISMMesh->GetBoundingBox().TransformBy(MeshTransform);
FVector TotalBoxExtent = TransformedBox.GetExtent() * 2.f;

// perform a line-box test from direction to box center
FVector HitLocation;
FVector HitNormal;
float HitTime;
FVector BoxSweepExtent = FVector::ZeroVector;
bool LineHit = false;

FVector LineEnd = TransformedBox.GetCenter();
FVector LineStart = OffsetDirection * TotalBoxExtent;


LineHit = FMath::LineExtentBoxIntersection(TransformedBox, LineStart, LineEnd, BoxSweepExtent, HitLocation, HitNormal, HitTime);


if (LineHit)
return HitLocation;
else
return FVector::ZeroVector;
}

I’d really prefer not to place collision in the world and do a trace.

Without rotation, or with rotation in a straight line (OffsetDirection=(1,0,0):

result (perfect): Imgur: The magic of the Internet

With 45 degree yaw rotation (hit result stops early as extent has increased):

result (OffsetDirection=Normalized(1,1,0): Imgur: The magic of the Internet

Thank you so much in advance!!

Edit: added pictures, code, and hopefully clarified question.

FMath::LineExtentBoxIntersection looks like a line-box test with an optional extent to add to the supplied box. It doesn’t look the extent is used in any other way

Thank you so much for the response! You are totally right! The extent just makes the supplied box bigger I should have checked the definition. I passed in a FVector::ZeroVector into extent and it functions exactly like a line-box test.

However, it appears that once I add rotation to the FBox, the intersection just uses the max extent and doesn’t take into account rotation.

Without rotation (perfect):

With 45 degree yaw rotation (hit result stops early as extent has increased):

I’ve edited my question with code and more gifs

FBox will always be axis aligned. So instead of adding rotation to your box (which will then be again axis aligned, messing up the result), try rotating the line instead in the opposite direction and get the intersection with the fixed box. Then apply the transform on the result to bring it back (since we rotated the line)

FTransform BoxTransform;	// TODO: Fill me
FBox Box = FBox(FVector(-100, -100, -100), FVector(100, 100, 100));	// TODO: Fill me

FVector RayOrigin = Box.GetCenter();
FVector RayDir = FVector(1, 0, 0);	// TODO: Fill me


FVector NewRayDir = BoxTransform.Inverse().GetRotation().TransformVector(RayDir);
FVector HitLocation, HitNormal;
float HitTime = 0;
const float TraceDistance = 1000;
if (FMath::LineExtentBoxIntersection(Box, RayOrigin, NewRayDir * TraceDistance, FVector::ZeroVector, HitLocation, HitNormal, HitTime)) {
	// Rotate the result back (since we rotated the ray with the inverse)
	HitLocation = BoxTransform.TransformVector(HitLocation);

	// ...
}