Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save braustin20/bf40eed0adef1b4ed577cce34ce419f0 to your computer and use it in GitHub Desktop.
Save braustin20/bf40eed0adef1b4ed577cce34ce419f0 to your computer and use it in GitHub Desktop.
Unreal 4 snippet which calculates difference in rotation over time for stunt detection.
FVector ATacopocalypsePawn::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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment