Download

Store Normalized FVector in variable?

How can I return a normalized vector to the variable ‘HitNormal’ in the code below? Line 54 appears to be done incorrectly. Also, I’m assuming the syntax for performing Line Traces is correct?



#include "Hovercraft.h"
#include "HC_Pawn.h"


AHC_Pawn::AHC_Pawn(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	Mesh = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("Mesh"));
	Mesh->AlwaysLoadOnClient = true;
	Mesh->AlwaysLoadOnServer = true;
	Mesh->bOwnerNoSee = false;
	Mesh->MeshComponentUpdateFlag = EMeshComponentUpdateFlag::AlwaysTickPoseAndRefreshBones;
	Mesh->bCastDynamicShadow = true;
	Mesh->bAffectDynamicIndirectLighting = true;
	Mesh->PrimaryComponentTick.TickGroup = TG_DuringPhysics;
	Mesh->SetCollisionResponseToAllChannels = ECR_Block;
	Mesh->bGenerateOverlapEvents = true;
	Mesh->SetSimulatePhysics = true;

	SetAltitude = 128.0f;
}

void AHC_Pawn::Tick(float DeltaSeconds)
{
	Super::Tick;
	
	FVector Accel = (0.f, 0.f, GetWorld()->GetGravityZ);
	FVector HitNormal;
	float Ground;

	const FName TraceTag("DebugTraceTag");
	GetWorld()->DebugDrawTraceTag = TraceTag;
	
	FCollisionQueryParams GT_TraceParams = FCollisionQueryParams(FName(TEXT("GT_TraceParams")), true, this);
	GT_TraceParams.bTraceComplex = false;
	GT_TraceParams.bTraceAsyncScene = false;
	GT_TraceParams.bReturnPhysicalMaterial = false;
	GT_TraceParams.TraceTag = TraceTag;
		
	/* End Points Of The Two Traces That Determine Altitude */
	const FVector End = FVector(GetActorLocation().X, GetActorLocation().Y, GetActorLocation().Z - 500.0f);
	const FVector VEnd = FVector(GetActorLocation() + (0.5f + (Mesh->Bounds.SphereRadius) / GetVelocity().Size) * GetVelocity());

	/* Do two line-traces, one shooting directly down towards the ground, the other following the direction of the crafts movement. */
	FHitResult GroundTrace(ForceInit);
	FHitResult VelocityTrace(ForceInit);
	GetWorld()->LineTraceSingle(GroundTrace, (GetActorLocation()), End, ECC_Visibility, GT_TraceParams);
	GetWorld()->LineTraceSingle(VelocityTrace, (GetActorLocation()), VEnd, ECC_Visibility, GT_TraceParams);

	if (GetVelocity().Size > 0.0) //&& VTBlocker && GBlocker)
	{
		HitNormal =	FVector::Normalize(GroundTrace.ImpactNormal + VelocityTrace.ImpactNormal);
		Ground = FMath::Min((GetActorLocation().Z - GroundTrace.ImpactPoint.Z), (GetActorLocation().Z - VelocityTrace.ImpactPoint.Z));
	}
	else
	{
		HitNormal = GroundTrace.ImpactNormal;
		Ground = (GetActorLocation().Z - GroundTrace.ImpactPoint.Z);
	}

	Mesh->SetPhysicsLinearVelocity((GetVelocity() + Accel * DeltaSeconds), false, NAME_None);
}


Can I simply do the following to get the result I want?



HitNormal =	FVector(GroundTrace.ImpactNormal + VelocityTrace.ImpactNormal);
HitNormal.Normalize();


If you look in Vector.h you can see the function signature for FVector::Normalize.


bool Normalize(float Tolerance=SMALL_NUMBER);

Your code should set the vector and then call Normalize on the vector like in your second example. If you want to make sure that it worked you could temporarily call IsNormalized() afterwards.