-
-
Save sinanm89/566eefb7fbb8a761dc73ac1addb923c0 to your computer and use it in GitHub Desktop.
ue4 quat functions for blueprint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public: | |
// Convert Euler Rotations To Quaternions | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Euler To Quaternion", Keywords = "rotation, quaterion"), Category = "Quaternion Rotation") | |
static FQuat Euler_To_Quaternion(FRotator Current_Rotation); | |
// Function to set world rotation of scene component to input quaternion rotation | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set World Rotation (Quaterion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void SetWorldRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation); | |
// Function to set relative rotation of scene component to input quaternion rotation | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Relative Rotation (Quaterion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void SetRelativeRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation); | |
// Function to add delta rotation to current local rotation of scene component | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Add Local Rotation (Quaterion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void AddLocalRotationQuat(USceneComponent* SceneComponent, const FQuat& Delta_Rotation); | |
// Function to set world rotation of Actor to input quaternion rotation | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Actor World Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void SetActorWorldRotationQuat(AActor* Actor, const FQuat& Desired_Rotation); | |
// Function to set relative rotation of Actor to input quaternion rotation | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Set Actor Relative Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void SetActorRelativeRotationQuat(AActor* Actor, const FQuat& Desired_Rotation); | |
// Function to add delta rotation to current local rotation of Actor | |
UFUNCTION(BlueprintCallable, meta = (DisplayName = "Add Actor Local Rotation (Quaternion)", Keywords = "rotation, quaternion"), Category = "Quaternion Rotation") | |
static void AddActorLocalRotationQuat(AActor* Actor, const FQuat& Delta_Rotation); | |
------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
// Formula to convert a Euler angle in degrees to a quaternion rotation | |
FQuat UMyFunctionName::Euler_To_Quaternion(FRotator Current_Rotation) | |
{ | |
FQuat q; // Declare output quaternion | |
float yaw = Current_Rotation.Yaw * PI / 180; // Convert degrees to radians | |
float roll = Current_Rotation.Roll * PI / 180; | |
float pitch = Current_Rotation.Pitch * PI / 180; | |
double cy = cos(yaw * 0.5); | |
double sy = sin(yaw * 0.5); | |
double cr = cos(roll * 0.5); | |
double sr = sin(roll * 0.5); | |
double cp = cos(pitch * 0.5); | |
double sp = sin(pitch * 0.5); | |
q.W = cy * cr * cp + sy * sr * sp; | |
q.X = cy * sr * cp - sy * cr * sp; | |
q.Y = cy * cr * sp + sy * sr * cp; | |
q.Z = sy * cr * cp - cy * sr * sp; | |
return q; // Return the quaternion of the input Euler rotation | |
} | |
// Set the scene component's world rotation to the input quaternion | |
void UMyFunctionName::SetWorldRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation) | |
{ | |
if (SceneComponent) | |
{ | |
SceneComponent->SetWorldRotation(Desired_Rotation); | |
} | |
} | |
// Set the scene component's relative rotation to the input quaternion | |
void UMyFunctionName::SetRelativeRotationQuat(USceneComponent* SceneComponent, const FQuat& Desired_Rotation) | |
{ | |
if (SceneComponent) | |
{ | |
SceneComponent->SetRelativeRotation(Desired_Rotation); | |
} | |
} | |
// Add the input delta rotation to the scene component's current local rotation | |
void UMyFunctionName::AddLocalRotationQuat(USceneComponent* SceneComponent, const FQuat& Delta_Rotation) | |
{ | |
if (SceneComponent) | |
{ | |
SceneComponent->AddLocalRotation(Delta_Rotation); | |
} | |
} | |
// Set the Actor's world rotation to the input quaternion | |
void UMyFunctionName::SetActorWorldRotationQuat(AActor* Actor, const FQuat& Desired_Rotation) | |
{ | |
if (Actor) | |
{ | |
Actor->SetActorRotation(Desired_Rotation); | |
} | |
} | |
// Set the Actor's relative rotation to the input quaternion | |
void UMyFunctionName::SetActorRelativeRotationQuat(AActor* Actor, const FQuat& Desired_Rotation) | |
{ | |
if (Actor) | |
{ | |
Actor->SetActorRelativeRotation(Desired_Rotation); | |
} | |
} | |
// Add the input delta rotation to the Actor's current local rotation | |
void UMyFunctionName::AddActorLocalRotationQuat(AActor* Actor, const FQuat& Delta_Rotation) | |
{ | |
if (Actor) | |
{ | |
Actor->AddActorLocalRotation (Delta_Rotation); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment