Legacy/Mesh Collision, Obtain Closest Point on Mesh Surface To Other Point

From UE4: Community Wiki
Jump to: navigation, search

Overview

Dear Community,

Use this function to determine what point on a static mesh component is closest to a given point, in this case I use the location of an Actor.

This exact function tells you the closest point on an SMA to an Actor, returning the distance between the two.


The power of this function is that it is returning the point on the actual Mesh Collision Surface!


This is very precise and can lead to some great game effects!


Using With Base Static Mesh Actor Class

You could test a Static Mesh Actor base class by using

TheSMA->StaticMeshComponent->GetDistanceToCollision(PointToTest, OutFVector); //see below for more info

Using With Custom SMA Class

float AYourSMAClass::DistanceOfActorToThisMeshSurface(AActor* TestActor, FVector& ClosestSurfacePoint) const
{
	if (!TestActor) return;
        if (!TestActor->IsValidLowLevel()) return;
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	//Dist of Actor to Surface, retrieve closest Surface Point to Actor
	return StaticMeshComponent->GetDistanceToCollision(
               TestActor->GetActorLocation(), ClosestSurfacePoint
        );
}

Two Function Return Values

The float returned is the distance from your chosen test point to the closest point on the Mesh Collision Surface.

The FVector returned by reference is the actual point on the Mesh Collision Surface!

Simple Collision

If you are having issues getting this function to work properly, make sure the static mesh for the SMA you are testing with has actual simple collision primitives! (not using complex as simple)

Summary

Enjoy!

Rama (talk)