Advertisement
NebulaGames

c++ to Blueprint 6DoF Fixing Gimbal Lock

Jul 30th, 2018 (edited)
7,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. public:            
  2.  
  3.     // Convert Euler Rotations To Quaternions
  4.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Euler To Quaternion", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  5.         static FQuat Euler_To_Quaternion(FRotator Current_Rotation);
  6.  
  7.     // Function to set world rotation of scene component to input quaternion rotation
  8.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set World Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  9.         static void SetWorldRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation);
  10.  
  11.     // Function to set relative rotation of scene component to input quaternion rotation
  12.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Relative Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  13.         static void SetRelativeRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation);
  14.  
  15.     // Function to add delta rotation to current local rotation of scene component
  16.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Add Local Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  17.         static void AddLocalRotationQuat(USceneComponent* SceneComponent, const FQuat& Delta_Rotation);
  18.  
  19.     // Function to set world rotation of Actor to input quaternion rotation
  20.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Actor World Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  21.         static void SetActorWorldRotationQuat(AActor* Actor, const FQuat& Desired_Rotation);
  22.  
  23.     // Function to set relative rotation of Actor to input quaternion rotation
  24.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Actor Relative Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  25.         static void SetActorRelativeRotationQuat(AActor* Actor, const FQuat& Desired_Rotation);
  26.  
  27.     // Function to add delta rotation to current local rotation of Actor
  28.     UFUNCTION(BlueprintCallable, meta = (DisplayName = "Add Actor Local Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation")
  29.         static void AddActorLocalRotationQuat(AActor* Actor, const FQuat& Delta_Rotation);
  30.        
  31.        
  32.        
  33. -------------------------------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35. // Formula to convert a Euler angle in degrees to a quaternion rotation
  36. FQuat UMyFunctionName::Euler_To_Quaternion(FRotator Current_Rotation)
  37. {
  38.     FQuat q;                                            // Declare output quaternion
  39.     float yaw = Current_Rotation.Yaw * PI / 180;        // Convert degrees to radians
  40.     float roll = Current_Rotation.Roll * PI / 180;
  41.     float pitch = Current_Rotation.Pitch * PI / 180;
  42.  
  43.     double cy = cos(yaw * 0.5);
  44.     double sy = sin(yaw * 0.5);
  45.     double cr = cos(roll * 0.5);
  46.     double sr = sin(roll * 0.5);
  47.     double cp = cos(pitch * 0.5);
  48.     double sp = sin(pitch * 0.5);
  49.  
  50.     q.W = cy * cr * cp + sy * sr * sp;
  51.     q.X = cy * sr * cp - sy * cr * sp;
  52.     q.Y = cy * cr * sp + sy * sr * cp;
  53.     q.Z = sy * cr * cp - cy * sr * sp;
  54.  
  55.     return q;                                           // Return the quaternion of the input Euler rotation
  56. }
  57.  
  58. // Set the scene component's world rotation to the input quaternion
  59. void UMyFunctionName::SetWorldRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation)
  60. {
  61.     if (SceneComponent)
  62.     {
  63.         SceneComponent->SetWorldRotation(Desired_Rotation);
  64.     }
  65. }
  66.  
  67. // Set the scene component's relative rotation to the input quaternion
  68. void UMyFunctionName::SetRelativeRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation)
  69. {
  70.     if (SceneComponent)
  71.     {
  72.         SceneComponent->SetRelativeRotation(Desired_Rotation);
  73.     }
  74. }
  75.  
  76. // Add the input delta rotation to the scene component's current local rotation
  77. void UMyFunctionName::AddLocalRotationQuat(USceneComponent* SceneComponent, const FQuat& Delta_Rotation)
  78. {
  79.     if (SceneComponent)
  80.     {
  81.         SceneComponent->AddLocalRotation(Delta_Rotation);
  82.     }
  83. }
  84.  
  85. // Set the Actor's world rotation to the input quaternion
  86. void UMyFunctionName::SetActorWorldRotationQuat(AActor* Actor, const FQuat& Desired_Rotation)
  87. {
  88.     if (Actor)
  89.     {
  90.         Actor->SetActorRotation(Desired_Rotation);
  91.     }
  92. }
  93.  
  94. // Set the Actor's relative rotation to the input quaternion
  95. void UMyFunctionName::SetActorRelativeRotationQuat(AActor* Actor, const FQuat& Desired_Rotation)
  96. {
  97.     if (Actor)
  98.     {
  99.         Actor->SetActorRelativeRotation(Desired_Rotation);
  100.     }
  101. }
  102.  
  103. // Add the input delta rotation to the Actor's current local rotation
  104. void UMyFunctionName::AddActorLocalRotationQuat(AActor* Actor, const FQuat& Delta_Rotation)
  105. {
  106.     if (Actor)
  107.     {
  108.         Actor->AddActorLocalRotation (Delta_Rotation);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement