# Rotate Vector around a Point in Local Space

**URL:** <https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300>\
**Category:** Platform & Builds\
**Tags:** matrix, unreal-engine, question\
**Created:** [December 1, 2014, 10:54pm UTC](https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300 "2014-12-01T22:54:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rumpelstilskin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rumpelstilskin](https://forums.unrealengine.com/u/Rumpelstilskin)\
**Post date:** [December 1, 2014, 10:54pm UTC](https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300/1 "2014-12-01T22:54:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [December 2, 2014, 10:45am UTC](https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300/2 "2014-12-02T10:45:20Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Rumpelstilskin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Rumpelstilskin](https://forums.unrealengine.com/u/Rumpelstilskin)\
**Post date:** [January 19, 2015, 8:50pm UTC](https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300/3 "2015-01-19T20:50:52Z")

</div>

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 😃  
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!

---

<div class="post-metadata">

**Author:** ![moof2k\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@moof2k\_1](https://forums.unrealengine.com/u/moof2k_1)\
**Post date:** [January 21, 2015, 6:41am UTC](https://forums.unrealengine.com/t/rotate-vector-around-a-point-in-local-space/300300/4 "2015-01-21T06:41:53Z")

</div>

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);

```
