UE4中万向锁问题

使用中,绕y轴旋转90度后必定遇到万向锁

FRotator Rotator = SpringArmComponent->GetRelativeRotation();
Rotator.Add(y, 0, 0);
SpringArmComponent->SetRelativeRotation(Rotator);
FRotator Rotator = SpringArmComponent->GetRelativeRotation();
Rotator.Pitch += y;
SpringArmComponent->SetRelativeRotation(Rotator);

这里本质上问题还是需要转换成欧拉角来计算角度,必定会导致万向锁。

尝试使用四元数进行解决

FQuat Quat = SpringArmComponent->GetRelativeRotation().Quaternion() * FQuat(FVector::LeftVector, y * PI / 180);
SpringArmComponent->SetRelativeRotation(Quat);
SpringArmComponent->AddRelativeRotation(FQuat(FVector::LeftVector,y*PI/180));

都没有出现万向锁的问题,因此,使用时,遇到万向锁问题就避免使用欧拉角。

posted @ 2022-04-06 21:57  LDnanchao  阅读(622)  评论(0编辑  收藏  举报