UE4 三种旋转(二)

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());

参考

UE4 三种旋转(一)

UE4 三种旋转(二)

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值