How to get the Right of Up of a vector?

Hello,

lets say I have a vector that is a direction between 2 locations.


FVector direction = (Location1 - Location2).GetSafeNormal();

How can I get the right and Up of that vector? For example, if the vector were to strafe to the side while still looking forward.

(Note this is just with vectors, it isn’t using actors so I can’t just use GetActorRightVector())
Thanks!

1 Like

What do you mean with “strafe to the side while still looking forward”? Could you maybe draw an example?

Each vector has 3 components (x, y, z). If your x component is your “forward” then your y component is the strafing and the z component is your up.

Cross product of two normalized vectors gives you a vector which is perpendicular to both of them. In cases when you have just a single vector, forward vector in your case, you have to arbitrary define one intermediate vector, to be used as alignment.
Usually, if I need to find Up vector for example, I define intermediate Right vector as (0,1,0) as it doesn’t have to be perpendicular to forward vector, it just shouldn’t be pointing in the same direction as Up or Forward vectors. Cross-product of forward and intermediate right vector, gives you “true” up vector. Now to find “true” right vector (if necessary), take another cross product of forward and now Up vector that you’ve just calculated.
What I mean by intermediate vector being used as alignment is that its used to define in which plane your third vector will be. So if you define your intermediate right vector as normalize(0.0,0.5, 0.5), the Up vector will be tilted to 45 degrees from ground plane.

2 Likes

Not sure if this will work as I havent tried it but there is a function for FindLookatRotation which returns a rotator which you should be able to use the typical Get Right/Up Vector functions on :cool:

You can convert a vector into orientation quaternion and then use GetForwardVector/GetRightVector on that



**FVector** InitalVec;
**FVector** RightVec;
RightVec = InitalVec.**ToOrientationQuat()**.**GetRightVector(**);

^ That’s a pretty nifty method.

To get a ‘true’ Right vector though, you need a second vector too - one isn’t enough to ensure the correct orthagonility (is that a word?)

Whoops, completely forgot about this post. I ended up rotating my vector by 90* to get it’s right/up, which isn’t a great solution.

I’ll go back and fix it with your suggestions. Thanks guys!

Wouldn’t it be a lot more efficient to use


FVector Right = FVector::CrossProduct(InitialVec, FVector::UpVector);

?

1 Like