Download

Hit.Normal odd results

Hi,

after 6 hours of reaserch and not been able to solve the issue, I have decided to come here asking for help.

I am just writing a c++ class which spawn mesh on the landscape: when the slope of the landscape is too high the function return a false bvalue so we are not going to spawn everything

To achive such result I am going to line trace and get the ImpactNormal on the ground. The idea is quite simple: the acos of a dot vector (from the hit.impactnormal and a (0,0,1) vector) should return the angle, and, since the slope is exposed in blueprint, if that value is higher, return a false result; true for the opposite

This in theory because the problem I am having is that I can’t properly access to the Hit.normal value: and I think the problem is tied to deferencing a pointer.

Let me show the function:

bool ABuilder::RitornaInclinazione(FVector APosition)

{

// this is the returned variable: false by default
bool InclinazioneAmmissibile = false;

//here we initialise the variables for the line trace
FVector StartingPos = FVector(UnaInclinazione.X, UnaInclinazione.Y, 150000) +FVector(Scene->GetComponentLocation().X, Scene->GetComponentLocation().Y, 0.f);
FVector EndingPos = FVector(UnaInclinazione.X, UnaInclinazione.Y, -150000) +FVector(Scene->GetComponentLocation().X, Scene->GetComponentLocation().Y, 0.f);
FCollisionQueryParams TraceParams;
UWorld* World = GetWorld();
FHitResult Hit;

//here we create a line trace: it works and hit the landcape
World->LineTraceSingleByChannel(Hit, StartingPos, EndingPos, ECollisionChannel::ECC_Visibility, TraceParams, FCollisionResponseParams::DefaultResponseParam);

//…as by the debug line
DrawDebugLine(World, StartingPos, EndingPos, FColor::Blue, false, 15, 0, 2.0f);

//here we are pointing to the Impact Normal address
FVector* GetHitNormal = &Hit.ImpactNormal;

//here we dereference the pointer: we should get the values stored in the address
FVector ImpactNormalGot = *GetHitNormal;

//free memory
//GetHitNormal = nullptr;

//here we get the component of the vector
float XNormal = ImpactNormalGot.X;
float YNormal = ImpactNormalGot.Y;

//let’s check if it’s okay–> no its’nt…
UE_LOG(LogTemp, Warning, TEXT(“xx is %d - yy is %d”), ImpactNormalGot.X, ImpactNormalGot.Y);

//on the other hands this work
UE_LOG(LogTemp, Warning, TEXT(“Normal vector is %s”), *ImpactNormalGot.ToString());

}

As I have said the problem is probably tied to the pointer becuase if I run the function in Ue4, I get those values in the Log:

LogTemp: Warning: xx is -1073741824 - yy is -2147483648
LogTemp: Warning: Normal vector is X=-0.014 Y=0.464 Z=0.886

As you can see, Normal Vector is properly returned… when converted to string, just like here below:

UE_LOG(LogTemp, Warning, TEXT(“Normal vector is %s”), *ImpactNormalGot.ToString());

On the other hand, the X,Y component of the hit of the normalVector aren’t returned: Ue4 return strange values I can’t even understand (xx is -1073741824 - yy is -2147483648)

Does someone know where the problem is and how I can solve it?

Thanks in advance

Ok: after a fast f**k I have solved the issue.

Err, it’s a bit embarassing but I am going to say just to avoid someone else would waste the time I have wasted…

This is the mistake:

//let’s check if it’s okay–> no its’nt…
UE_LOG(LogTemp, Warning, TEXT(“xx is %d - yy is %d”), ImpactNormalGot.X, ImpactNormalGot.Y);

It’s not “%d” for float but “%f” : I thought %d was like a RegExp from Java, where usually %d means digit and so number.