Download

Help! Need Intersection of Line and FBox

I would like to see where an instanced static mesh bounding box ends to tile instances. They should be able to be tiled in a direction as well as have a transform. I’m currently using FMath::LineBoxIntersection() but adding rotation results in incorrect hit locations.

My code:


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;
}

This works perfectly without rotation, or with rotation in a straight line (OffsetDirection=(1,0,0):

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

Adding 45 yaw rotation simply increases extents giving me the wrong hit result (OffsetDirection=Normalized(1,1,0):

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

Thanks in advance! This has been causing me a huge amount of frustration and I’m really hoping one of you brilliant people can point me in the right direction!

Your sample code got me more confused than helped to understand what exactly you’re trying to do.
Anyway, there’s a query IsPointInBox() function and FBox has a GetCenter() method:

https://docs.unrealengine.com/en-US/…Box/index.html
https://docs.unrealengine.com/en-US/…ter/index.html

Oh and there’s also this one which considers box transformations:
https://docs.unrealengine.com/en-US/…orm/index.html

Hey thanks so much for the response! I understand now that my previous code segment greatly hindered more than helped. I’ve updated it with my progress and some gifs to really demonstrate what’s going wrong.

The last function you mentioned (IsPointInBoxWithTransform) would be amazing if it was a line and gave a hit result.

Please let me know if you need any more details! I really appreciate you looking at it!