How would I format this C++ code into a function for my blueprint function library?

Hello, I’ve been trying to format this code into a function in my blueprints library. Can anyone help?




FQuat UQuaternionLibrary::Quaternion_LookRotation()
{
FRotator MyLookRotation(FVector lookAt, FVector upDirection)

FVector forward = lookAt - GetActorLocation();
FVector up = upDirection;


forward = forward.GetSafeNormal();
up = up - (forward * FVector::DotProduct(up, forward));
up = up.GetSafeNormal();

///////////////////////


FVector vector = forward.GetSafeNormal();
FVector vector2 = FVector::CrossProduct(up, vector);
FVector vector3 = FVector::CrossProduct(vector, vector2);
float m00 = vector.X;
float m01 = vector.Y;
float m02 = vector.Z;
float m10 = vector2.X;
float m11 = vector2.Y;
float m12 = vector2.Z;
float m20 = vector3.X;
float m21 = vector3.Y;
float m22 = vector3.Z;

float num8 = (m00 + m11) + m22;
FQuat quaternion = FQuat();

if (num8 > 0.0f)
{
float num = (float)FMath::Sqrt(num8 + 1.0f);
quaternion.W = num * 0.5f;
num = 0.5f / num;
quaternion.X = (m12 - m21) * num;
quaternion.Y = (m20 - m02) * num;
quaternion.Z = (m01 - m10) * num;
return FRotator(quaternion);
}

if ((m00 >= m11) && (m00 >= m22))
{
float num7 = (float)FMath::Sqrt(((1.0f + m00) - m11) - m22);
float num4 = 0.5f / num7;
quaternion.X = 0.5f * num7;
quaternion.Y = (m01 + m10) * num4;
quaternion.Z = (m02 + m20) * num4;
quaternion.W = (m12 - m21) * num4;
return FRotator(quaternion);
}

if (m11 > m22)
{
float num6 = (float)FMath::Sqrt(((1.0f + m11) - m00) - m22);
float num3 = 0.5f / num6;
quaternion.X = (m10 + m01) * num3;
quaternion.Y = 0.5f * num6;
quaternion.Z = (m21 + m12) * num3;
quaternion.W = (m20 - m02) * num3;
return FRotator(quaternion);
}

float num5 = (float)FMath::Sqrt(((1.0f + m22) - m00) - m11);
float num2 = 0.5f / num5;
quaternion.X = (m20 + m02) * num2;
quaternion.Y = (m21 + m12) * num2;
quaternion.Z = 0.5f * num5;
quaternion.W = (m01 - m10) * num2;


return FRotator(quaternion);

}



The code seems that’s already written as Unreal C++ format.
You can try to add a blueprint function library in your project, then create a blueprint function with the macro UFUNCTION(BlueprintCallable).
It’s also nessary for move Quaternion_LookRotation in the same class (or another class which could be seen).
(Quaternion_LookRotation may not the only function you need to move in, member function used should be also created such as GetActorLocation)
After that, in the cpp file, you may implement the blueprint function and call this Quaternion_LookRotation.

Thanks for the response! So I’m adding it to this. Whats my next step?




#include "QuaternionLibrary.h"

#include "../Launch/Resources/Version.h"

#define ENGINE_HAS_QINTERP_FUNCTION ENGINE_MINOR_VERSION >= 20

#include "Engine/EngineTypes.h"
#include "Engine/Engine.h"
#include "Math/Quat.h"
#include "Math/RotationMatrix.h"
#include "Math/Rotator.h"

#include "Components/ActorComponent.h"
#include "Components/SceneComponent.h"
#include "GameFramework/Actor.h"



FQuat UQuaternionLibrary::Quaternion_LookRotation()
{
FRotator MyLookRotation(FVector lookAt, FVector upDirection)

FVector forward = lookAt - GetActorLocation();
FVector up = upDirection;


forward = forward.GetSafeNormal();
up = up - (forward * FVector::DotProduct(up, forward));
up = up.GetSafeNormal();

///////////////////////


FVector vector = forward.GetSafeNormal();
FVector vector2 = FVector::CrossProduct(up, vector);
FVector vector3 = FVector::CrossProduct(vector, vector2);
float m00 = vector.X;
float m01 = vector.Y;
float m02 = vector.Z;
float m10 = vector2.X;
float m11 = vector2.Y;
float m12 = vector2.Z;
float m20 = vector3.X;
float m21 = vector3.Y;
float m22 = vector3.Z;

float num8 = (m00 + m11) + m22;
FQuat quaternion = FQuat();

if (num8 > 0.0f)
{
float num = (float)FMath::Sqrt(num8 + 1.0f);
quaternion.W = num * 0.5f;
num = 0.5f / num;
quaternion.X = (m12 - m21) * num;
quaternion.Y = (m20 - m02) * num;
quaternion.Z = (m01 - m10) * num;
return FRotator(quaternion);
}

if ((m00 >= m11) && (m00 >= m22))
{
float num7 = (float)FMath::Sqrt(((1.0f + m00) - m11) - m22);
float num4 = 0.5f / num7;
quaternion.X = 0.5f * num7;
quaternion.Y = (m01 + m10) * num4;
quaternion.Z = (m02 + m20) * num4;
quaternion.W = (m12 - m21) * num4;
return FRotator(quaternion);
}

if (m11 > m22)
{
float num6 = (float)FMath::Sqrt(((1.0f + m11) - m00) - m22);
float num3 = 0.5f / num6;
quaternion.X = (m10 + m01) * num3;
quaternion.Y = 0.5f * num6;
quaternion.Z = (m21 + m12) * num3;
quaternion.W = (m20 - m02) * num3;
return FRotator(quaternion);
}

float num5 = (float)FMath::Sqrt(((1.0f + m22) - m00) - m11);
float num2 = 0.5f / num5;
quaternion.X = (m20 + m02) * num2;
quaternion.Y = (m21 + m12) * num2;
quaternion.Z = 0.5f * num5;
quaternion.W = (m01 - m10) * num2;


return FRotator(quaternion);

}

There’s no need to do any of this, the functionality you need already exists in the engine. There’s also a good chance that the code you have won’t work because it’ll be written for a different coordinate system.



FQuat LookAtRotation = FRotationMatrix::MakeFromXZ(Direction, UpVector).ToQuat();


Thanks TheJamsh, I’ve been using Quaternions in BP already though. LookAtRotation causes gimble lock for what I’m doing. I’m averaging normals. The only thing I cant do in Blueprints is the Quaterniun.LookRotation. I come from c# and am learning c++. I just need to know how the .cpp should look. I think I can handle the header.

My code in c# looks like this. Works well, useless in c++. lol


q = Quaternion.Slerp(normal1, normal2, 0.5f);
normalAverage = q * Vector3.up;

//Hit 1
normal1 = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);

//Hit 2
normal2 = Quaternion.LookRotation(Vector3.ProjectOnPlane(transform.forward, hit.normal), hit.normal);

Honestly, I new it was a dumb question. I figured it out. Thanks. lol