UE4 三种旋转(二)
UE4 中旋转操作常用的方式:欧拉角、四元数和旋转矩阵。
欧拉角
//右手法则
struct FRotator
{
public:
/** Rotation around the right axis (around Y axis), Looking up and down (0=Straight Ahead, +Up, -Down) */
float Pitch;
/** Rotation around the up axis (around Z axis), Running in circles 0=East, +North, -South. */
float Yaw;
/** Rotation around the forward axis (around X axis), Tilting your head, 0=Straight, +Clockwise, -CCW. */
float Roll;
}
//左手法则
auto cameraRightVector = GetActorRotation().RotateVector(FVector::RightVector);
auto cameraUpVecotor = GetActorRotation().RotateVector(FVector::UpVector);
auto cameraForwardVector=(cameraRightVector ^ cameraUpVecotor);
四元数
struct FQuat
{
public:
/** The quaternion's X-component. */
float X;
/** The quaternion's Y-component. */
float Y;
/** The quaternion's Z-component. */
float Z;
/** The quaternion's W-component. */
float W;
}
利用四元数进行旋转操作。
FQuat axisRot(FVector::RightVector, FMath::DegreesToRadians(90));
SetActorRotation((GetActorRotation().Quaternion() * axisRot).Rotator());
FQuat axisRot(FVector::UpVector, FMath::DegreesToRadians(90);
SetActorRotation((axisRot * GetActorRotation().Quaternion()).Rotator());
旋转矩阵
class FRotationMatrix
: public FRotationTranslationMatrix :public FMatrix
{
FRotationMatrix(const FRotator& Rot)
: FRotationTranslationMatrix(Rot, FVector::ZeroVector)
//FRotationTranslationMatrix(const FRotator& Rot, const FVector& Origin)
{ }
M[0][0] = CP * CY;
M[0][1] = CP * SY;
M[0][2] = SP;
M[0][3] = 0.f;
M[1][0] = SR * SP * CY - CR * SY;
M[1][1] = SR * SP * SY + CR * CY;
M[1][2] = - SR * CP;
M[1][3] = 0.f;
M[2][0] = -( CR * SP * CY + SR * SY );
M[2][1] = CY * SR - CR * SP * SY;
M[2][2] = CR * CP;
M[2][3] = 0.f;
M[3][0] = 0;
M[3][1] = 0;
M[3][2] = 0;
M[3][3] = 1.f;
}
旋转矩阵的移动操作。
AddMovementInput(FRotationMatrix(GetActorRotation()).GetScaledAxis(EAxis::X), Value * GetCameraSpeed());
AddMovementInput(FRotationMatrix(GetActorRotation()).GetScaledAxis(EAxis::Y), Value * GetCameraSpeed());
AddMovementInput(FRotationMatrix(GetActorRotation()).GetScaledAxis(EAxis::Z), Value * GetCameraSpeed());