Announcement

Collapse
No announcement yet.

Reflecting line trace of a surface.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    Code:
    	
    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

    #2
    UE4 has the (confusingly named) FVector::MirrorByVector() function to reflect a vector in the direction of a normal.
    Tom Sarkanen | Unreal Engine Developer | Epic Games UK

    Comment


      #3
      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.
      Code:
      // 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
      Click image for larger version

Name:	LineTrace.jpg
Views:	1
Size:	11.3 KB
ID:	1049919
      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?

      Comment


        #4
        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:

        Code:
        FVector Direction = GetActorForwardVector().MirrorByVector(Normal);
        would be more appropriate.
        Tom Sarkanen | Unreal Engine Developer | Epic Games UK

        Comment


          #5
          NVM, re-read post and its hitting the wall...
          Last edited by iLLo; 04-02-2014, 08:07 PM. Reason: cant read original post

          Comment

          Working...
          X