Download

Reflecting line trace of a surface.

Hi, i’m currently working on a small game in which you redirect line traces in order to bounce them on certain objects until it reaches the end point.

What i need help with is the actual calculation of what direction the reflected line trace needs to have.

This is my code at the moment.


	
UWorld* TheWorld = this->GetWorld();
FColor debugColor = FColor::Red;

FHitResult HitResult(ForceInit);
FVector StartFVector = this->GetActorLocation();
FVector EndFVector = StartFVector + GetActorForwardVector() * RayLength;
	
FCollisionQueryParams TraceParams(TEXT("MyTrace"), true, this);
TraceParams.bTraceComplex = true;
TraceParams.bTraceAsyncScene = true;
TraceParams.bReturnPhysicalMaterial = false;
	
if (TheWorld->LineTraceSingle(HitResult, StartFVector, EndFVector, ECC_WorldStatic, TraceParams))
	{
	DrawDebugLine(TheWorld, StartFVector, HitResult.Location, debugColor, false, 30.0f, 1.0f, 3.0f);
	if (HitResult.GetActor())
	{ 
		if (HitResult.GetActor()->ActorHasTag("Reflect"))
		{
			// Calculate and create ray for the reflected line trace
			StartFVector = HitResult.Location;
			EndFVector = StartFVector + !Direction Of New Ray! * RayLength;

			if (TheWorld->LineTraceSingle(HitResult, StartFVector, EndFVector, ECC_WorldStatic, TraceParams))
			{
				DrawDebugLine(TheWorld, StartFVector, HitResult.Location, FColor::Green, false, 30.0f, 1.0f, 3.0f);
			}
			else
			{
				DrawDebugLine(TheWorld, StartFVector, EndFVector, FColor::Blue, false, 30.0f, 1.0f, 3.0f);
			}
			//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Reflected"));
		}
	}
}


So i’m wondering if unreal engine 4 have some inbuilt function for this or do i need to do the math myself and if so, anyone have any suggestions or math examples for me to take a look at?
Thanks!
// Solicio

UE4 has the (confusingly named) FVector::MirrorByVector() function to reflect a vector in the direction of a normal.

So i have been trying out the FVector::MirrorByVector() function but i’m not really getting it to work the way it should.

This is my current code for the reflection of the ray.



// Calculate Reflection
StartFVector = HitResult.Location;
FVector Normal = HitResult.Normal;
FVector Direction = Direction.MirrorByVector(Normal);
Direction.Normalize();

EndFVector = StartFVector + (Direction* RayLength);

if (TheWorld->LineTraceSingle(HitResult, StartFVector, EndFVector, ECC_WorldStatic, TraceParams))
{

DrawDebugLine(TheWorld, StartFVector, HitResult.Location, FColor::Green, false, 30.0f, 1.0f, 3.0f);

}


And this is the result i’m getting
LineTrace.jpg
The red ray is the first line trace and the green one is the reflected one, it might be hard to see but the green is reflected in a wierd way into the wall in the background.

Am i doing the reflection calculation wrong is some way?

From that code snippet, you seem to be using the vector ‘Direction’ when it is uninitialized.

Looking at your last example, something more akin to:


FVector Direction = GetActorForwardVector().MirrorByVector(Normal);

would be more appropriate.

NVM, re-read post and its hitting the wall…