How to calculate number of flips/spins/rolls over time?

Hi guys, I was wondering if I could get some help with a rotational math issue. So I’m trying to calculate the number of flips, spins, and rolls my character (a car) does while it is in the air. I had been doing this previously by subtracting my previous Rotator from my current Rotator. This led to gimbal lock when axes went past 180 or pitch went past 90, which resulted in “360 degree roll” output when I did a flip using pitch.

I have since switched to trying to use Quaternions, but since they have 4 values instead of 3, I’m not sure how to map them to flips, yaw, and roll degrees in local space without gimbal lock or weird rotator issues. My goal is to be able to do 3 spins and have an output saying “900 yaw” or 2 flips and have an output of “720 pitch”

Here is my current code -

    FVector AMyVehiclePawn::CalcQuatDifference()
    {
    	FQuat CurrentQuat = GetTransform().GetRotation();
    
    	FQuat TempQuat = PrevQuat - CurrentQuat;
    
    	PrevQuat = CurrentQuat;
    
    	return TempQuat.Euler();
    }

So after digging around and learning some additional math, I figured out that I was doing quaternion math wrong and shouldn’t be subtracting but rather multiplying by inverse. The sample below seems to be working pretty reliably for me!

FVector AMyVehiclePawn::CalcQuatDifference()
{
	//Get the current rotation in quaternion
	FQuat CurrentQuat = GetTransform().GetRotation();

	//Get the quaternion rotational difference
	FQuat TempQuat = PrevQuat.Inverse() * CurrentQuat;
	
	//Update the previous rotation value
	PrevQuat = CurrentQuat;

	//Get the axis values
	float roll = FMath::Atan2(2 * TempQuat.Y * TempQuat.W - 2 * TempQuat.X * TempQuat.Z, 1 - 2 * TempQuat.Y * TempQuat.Y - 2 * TempQuat.Z * TempQuat.Z);
	float pitch = FMath::Atan2(2 * TempQuat.X * TempQuat.W - 2 * TempQuat.Y * TempQuat.Z, 1 - 2 * TempQuat.X * TempQuat.X - 2 * TempQuat.Z * TempQuat.Z);
	float yaw = FMath::Asin(2 * TempQuat.X * TempQuat.Y + 2 * TempQuat.Z * TempQuat.W);
	
	//Convert the values to degrees
	roll = FMath::RadiansToDegrees(roll);
	pitch = FMath::RadiansToDegrees(pitch);
	yaw = FMath::RadiansToDegrees(yaw);

	//Make sure the values are positive
	FVector DifferenceVector = FVector(FMath::Abs(pitch), FMath::Abs(roll), FMath::Abs(yaw));

	return FVector(DifferenceVector);
}

FYI, FQuat can directly give you the Yaw/Pitch/Roll angles in degrees without you needing to do all the math yourself. You might want to just try:

return TempQuat.Euler();

And see if that works for you.

Yeah I think the root of my issue was the Quat subtraction not the Euler conversion. I might condense it down later, thank you!

Hi There - any way to figure this out in blueprints? Except I just need the yaw… Thanks!

You could just add 1 to a variable every time the object hits a certain rotation.