Rotate Vector around a Point in Local Space

Hi,

I’m trying to modify the spring arm component for my Camera.
What Im trying to do is have a front trace so that if the character draws near to a wall, the camera will get closer behind him…

So I have a FrontResultLoc that I obtained via a trace in front of the character. and now I want to rotate this vector around the character 180deg, so i would like to do this in local space.
But I cant find how to do it…

In UDK i would just do something like MyVector << Pawn.Location and then rotate it, but there isn’t this kinda macro anymore in UE4

I see a lot of people talking a Matrices, but I dont see how they can help.

Anyone got an Idea?

Hello Rumplestilskin,

Rotation around the character 180 deg is very specific rotation. I propose to you next way:
You know character location (or point you want to rotate around) and character forward direction. With this info you can describe plane and mirror point.

FVector FrontResultLoc = …;

// Our plane
FVector pawnLoc = GetPawn()->GetActorLocation();
FVector pawnView = GetPawn()->GetActorRotation().Vector();

// distance to the plane
float d = (FrontResultLoc - pawnLoc) | pawnView;
	
// Mirror plane
FVector DesiredPoint = pawnLoc - 2*d*pawnView;

To rotate around character on other angle. You can try this:

FVector FrontResultLoc;

FVector pawnLoc = GetPawn()->GetActorLocation();
FQuat q(FVector(0, 0, 1), 0.2); // rotate around axis (0,0,1) on angle 0.2

FVector DesiredPoint = q.RotateVector(FrontResultLoc - pawnLoc) + pawnLoc;

Best regards,

Sorry I didnt anwser before but I managed to resolve my problem.

For those interested here it goes:

First, I had to bring the location of the camera in the characters local space, for this i substracted the characters position form the trace result. Since Im using a Spring component to achieve what Im doing, for me it looks like this:

FinalFrontResultLoc = (FrontResultLoc - ArmOrigin);

Then I create the Matrix and apply it to the vector, so:

FRotationMatrix		FirstFrontRotmatix(FRotator(180.0f, 0.0f, 180.0f));

FinalFrontResultLoc = FirstFrontRotmatix.TransformVector(FinalFrontResultLoc);
FinalFrontResultLoc.Z = -FinalFrontResultLoc.Z;

The last line is to have it at the actual correct position.
And then, you just put the camera position back in world space, so:

FinalFrontResultLoc = (FinalFrontResultLoc + ArmOrigin);

Now here is the funny part, I realised I didnt need to use a Matrix,

I only needed to multiply the camera location by -1 once in local space to get the job done :smiley:
So my final code is actually just:

FinalFrontResultLoc = (FrontResultLoc - ArmOrigin);
		
FinalFrontResultLoc = FinalFrontResultLoc * -1;

FinalFrontResultLoc = (FinalFrontResultLoc + ArmOrigin);

I knew it would end being 3 lines of code in the end… Hope it helps the next person!

If I understand correctly you’re basically trying to construct a “lookat” transform.

You can do this in a fairly intuitive manner with FTransform:

FVector ActorPos = Actor->GetActorLocation();

FVector Pos(100, 0, 0);
FQuat Rot(FVector(0, 1, 0), .2f);
Pos = Rot.RotateVector(Pos);

FTransform CameraTransform;
CameraTransform.SetLocation(ActorPos);
CameraTransform.AddToTranslation(-Pos);
CameraTransform.SetRotation(Rot);