FMath::Lerp and Pitch Rotation Problem

Hey guys,

I am pretty new to UE4 and am currently working on a project which requires a player being able to control a flying pawn through the use of the Rift’s HMD Head tracking. The way I’m able to accomplish this is by adding a cube in front of the camera and then lerping the player body’s rotation to face towards the box. This works well for Yaw but not so much for the Pitch. As soon as the player’s Pitch rotation nears the poles the camera starts jittering around and spins out of control.

Let me show the current code in use :



        FVector _CrosshairLocation = CrosshairComponent->GetComponentLocation();
	FVector _ParentLocation = RootComponent->GetComponentLocation();

	FVector _Dir = (_CrosshairLocation - _ParentLocation);
	_Dir.Normalize();
 	
 	FQuat _DirQuat = FQuat(_Dir.Rotation());
 	FQuat _RootQuat = RootComponent->GetComponentRotation().Quaternion();
 	FQuat _ResultantQuat = FMath::Lerp(_RootQuat, _DirQuat, RotationSpeed * DeltaSeconds);	

 	RootComponent->SetWorldRotation(_ResultantQuat.Rotator());

So does anyone have any idea on how I go about having a good 4 DOF flight mode?

One immediate thing that I noticed is the odd way you are using Lerp. The last argument of Lerp is supposed to be an alpha value (a value between 0 and 1) and you’re using it more as an interpolation speed.

If you want to use it more like this then you could try using FMath::RInterpto which takes delta time and a rotation speed. Note you’ll be working directly with rotators in this case instead of converting to a quaternion.

This may or may not be your actual problem, but the end result is that the way you’re using things, if your delta time is relatively stable then your body will stay at the same rotation.