FQuat: add 45 degrees to the current value

Hey. I cannot create the correct FQuat. I want to add to the current Relative Rotation for any axis for example 45 degrees.



FRotator Clear(0.0f, 0.0f, 0.0f);
FQuat Current(GetRelativeRotation());
FRotator Delta = SetAxisValueForwardType(Axis, Angle, Clear); // this creates Clear(45, 0, 0) or Clear(0, 45, 0) or Clear(0, 0, 45)
FQuat DeltaQuat(Delta);

FQuat AngleRotateQuat = Current * DeltaQuat; // local

SetRelativeRotation(AngleRotateQuat);



But this code does not work correctly, if the Y axis is not 0, then the Z axis shift is wrong.
Any ideas?

I cant find description of method SetAxisValueForwardType. Is it a custom method?

Anyway, is there a possibility that you’ve messed angle and radian? Or have you calculated correctly of roll\ pitch \yaw?(not x, y ,z)

the SetAxisValueForwardType method adds 45 degrees to any axis of the Clear vector (0,0,0).

i checked using UE_LOG and the angles are correct
Delta is equal to Delta (45, 0, 0) or Delta (0, 45, 0) or Delta (0, 0, 45)

Depends - do you want to add 45 degrees in the quaternions local-space or world-space?

From Quat.h:



Example: LocalToWorld = (LocalToWorld * DeltaRotation) will change rotation in local space by DeltaRotation.
Example: LocalToWorld = (DeltaRotation * LocalToWorld) will change rotation in world space by DeltaRotation.


Also, that function you created is not stricly neccesary and will result in more conversions. FQuat already has a constructor for creating a Quat from an axis and angle.



FQuat(FVector Axis, float AngleRad);


I need a Relative rotation relative to the Parent Component, I somehow have to indicate that the rotation around the component

AngleRotateQuat = Current * DeltaQuat; // local
AngleRotateQuat = DeltaQuat * Current; // world

both options are incorrect, if not Z, then the Y-axis rotation will be incorrect. This only works for World or Local Rotation

I still can’t solve the problem, maybe I don’t understand something in FQuat. If the Y angle of the component is greater than 0, then the X rotation is incorrect, the X rotation is as if I rotated the root component The following code gives different results:




FRotator delta (0, 0, 45);
AddRelativeRotation(delta);



And code 2




FRotator delta (0, 0, 45);
AddRelativeRotation(delta.Quaternion());



Is this a bug or am I creating FQuat incorrectly?

A quaternion encodes an angle rotation about a specific axis. If you want to increase the quaternion’s current rotation by 45 degrees, extract the axis and angle, add 45 degrees to the angle, then reconstruct the quaternion with your new axis/angle:


FQuat rotation(....);
FVector axis;
float angleRadians;
rotation.ToAxisAndAngle(axis, angleRadians);
angleRadians += 3.14 / 4; //add 45 degrees
rotation = FQuat(axis, angleRadians);


**@Teh_Masterer_ **this also does not work correctly, the rotation is done like World Rotation


FQuat q1;
if (Axis == EAxisType::X)
{
q1 = FQuat(FVector::ForwardVector, FMath::DegreesToRadians(Angle));
}
else if (Axis == EAxisType::Y)
{
q1 = FQuat(FVector::RightVector, FMath::DegreesToRadians(Angle *-1));
}
else if (Axis == EAxisType::Z)
{
q1 = FQuat(FVector::UpVector, FMath::DegreesToRadians(Angle));
}

AddRelativeRotation(q1);

This code is identical



FRotator delta (0, 0, 45);
AddWorldRotation(delta)



This code works correctly for X and Y, but does not work correctly for Z axis if X and Y are not 0



FQuat q1;
if (Axis == EAxisType::X)
{
q1 = FQuat(GetForwardVector(), FMath::DegreesToRadians(Angle));
}
else if (Axis == EAxisType::Y)
{
q1 = FQuat(GetRightVector(), FMath::DegreesToRadians(Angle *-1));
}
else if (Axis == EAxisType::Z)
{
q1 = FQuat(GetUpVector(), FMath::DegreesToRadians(Angle));


AddRelativeRotation(q1);
}

Can you describe how the Z axis rotation is wrong? I’m having trouble understanding.

Also, the Z axis rotates in a different direction as opposed to X and Y. Try it in the editor with any object, adjust the rotation values under the Transform section in the details pane. Does using (0, 0, -1) for your Z vector work better?

@Teh_Masterer_
original position

after applying FQuat


Not only rotate along the X axis, it rotates along the two axes X and Z.

on video up to 7 seconds this is use



FRotator delta (0, 0, 45);
AddRelativeRotation (delta);


This is the correct result, after 7 seconds this is using

Not only rotate along the X axis, it rotates along the two axes X and Z.

I think FQuat is not designed to use Relative Rotation, only Local Rotation or World Rotation

If


q1 = FQuat(FVector::ForwardVector, FMath::DegreesToRadians(Angle));
AngleRotateQuat = Current  * q1;
SetRelativeRotation(AngleRotateQuat);

Then for X and Y the result is correct, but the rotation along the Z axis will be incorrect

This also incorrect



q1 = FQuat(FVector::ForwardVector, FMath::DegreesToRadians(Angle));
AddRelativeRotation(AngleRotateQuat);