How to get collision primitive name involved in collision?

Ok went for a hybrid approach. You can’t access the transform in the newer versions of the engine so you have to test for structs manually :frowning:

Based on the same procedure as @tootzoe1 checked via distance.


FString UCoreBlueprintFunctionLibrary::GetCollisionName(UPrimitiveComponent* OtherComp, FHitResult HitResult)
{	
	FString foundName = "";
	if (OtherComp != nullptr)
	{
		if (UStaticMeshComponent* mesh = Cast<UStaticMeshComponent>(OtherComp))
		{			
				FKShapeElem currentSmallest;
				FVector Start = HitResult.Location;
				FVector End;

				float nearestDist = UE_FLOAT_HUGE_DISTANCE;
				
				for (const FKBoxElem& Elem : mesh->GetStaticMesh()->GetBodySetup()->AggGeom.BoxElems)
				{							
					float tmpDist = FVector::Distance(Elem.GetTransform().GetLocation() + mesh->GetComponentLocation(), Start);
					if (tmpDist < nearestDist)
					{
						End = Elem.GetTransform().GetLocation();
						currentSmallest = Elem;
						nearestDist = tmpDist;
					}
				};
				for (const FKTaperedCapsuleElem& Elem : mesh->GetStaticMesh()->GetBodySetup()->AggGeom.TaperedCapsuleElems)
				{					
					float tmpDist = FVector::Distance(Elem.GetTransform().GetLocation() + mesh->GetComponentLocation(), Start);
					if (tmpDist < nearestDist)
					{		
						End = Elem.GetTransform().GetLocation();
						currentSmallest = Elem;
						nearestDist = tmpDist;
					}
				};

				for (const FKSphereElem& Elem : mesh->GetStaticMesh()->GetBodySetup()->AggGeom.SphereElems)
				{
					
					float tmpDist = FVector::Distance(Elem.GetTransform().GetLocation() + mesh->GetComponentLocation(), Start);
					if (tmpDist < nearestDist)
					{		
						End = Elem.GetTransform().GetLocation();
						currentSmallest = Elem;
						nearestDist = tmpDist;
					}
				};

				for (const FKSphylElem& Elem : mesh->GetStaticMesh()->GetBodySetup()->AggGeom.SphylElems)
				{
					
					float tmpDist = FVector::Distance(Elem.GetTransform().GetLocation() + mesh->GetComponentLocation(), Start);
					if (tmpDist < nearestDist)
					{		
						End = Elem.GetTransform().GetLocation();
						currentSmallest = Elem;
						nearestDist = tmpDist;
					}
				};

				for (const FKConvexElem& Elem : mesh->GetStaticMesh()->GetBodySetup()->AggGeom.ConvexElems)
				{					
					float tmpDist = FVector::Distance(Elem.GetTransform().GetLocation() + mesh->GetComponentLocation(), Start);
					if (tmpDist < nearestDist)
					{						
						End = Elem.GetTransform().GetLocation();
						currentSmallest = Elem;
						nearestDist = tmpDist;
					}
				};
				
				DrawDebugLine(OtherComp->GetWorld(), Start, End+ mesh->GetComponentLocation(), FColor::Cyan, true, 55555);
				foundName = currentSmallest.GetName().ToString();				
				UE_LOG(LogTemp, Warning, TEXT("Hit %s (%s)"), *foundName, *FString::SanitizeFloat(nearestDist));
		}		
	}
	return foundName;
}

1 Like