Announcement

Collapse
No announcement yet.

How to rotate a vector to another vector?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to rotate a vector to another vector?

    I'm sure there must be a simple solution to this but I can't wrap my head around it.

    Suppose I have a SceneComponent, and attached to it an ArrowComponent. I set the relative rotation of the arrow to be something arbitrary (eg. 45,45,45). Like this:






    (Note this is not what I am specifically trying to do, just a setup that shows the problem I need to solve.)

    My question is: How can I figure out the rotation necessary to put on the SceneComponent parent to bring the ArrowComponent back to pointing in its original direction (along the X axis: 1, 0, 0)?

    Simply setting the rotation of the SceneComponent to (-45, -45, -45) obviously doesn't work, and I'm assuming that might be something to do with Euler angles. But I have been trying all sorts of approaches using MakeRotFromX and MakeRotFromXY, and various other things, but nothing seems to work.

    Is this a solution that requires quaternions, and if so, how would I go about it? I'm trying to wrap my head around quaternions, but ****...

    Totally confused, all help massively appreciated!

    #2
    Because I'm the 1st to post, The trick that I use is called unrotate vector. Lets you skip a ton of quat and euler math that you're probably going to get answered with. So get the forward vector of your arrow component and then unrotate that vector. The resulting vector you can grab the rotation and apply it to the scene component to get you back to origin rotation on your arrow.

    https://docs.unrealengine.com/en-US/...tor/index.html

    [Edit] I'm at work and this is going off memory so i might be wrong here but im pretty sure this will work.
    Last edited by ViperG; 07-15-2020, 07:06 PM.

    Comment


      #3
      Try this

      Click image for larger version

Name:	rotscene.jpg
Views:	48
Size:	62.3 KB
ID:	1788953

      Comment


        #4
        I had wondered about UnrotateVector, but I must be using it wrong because it's not working for what I'm trying to do.

        For example:

        Code:
        FVector Forward = Arrow->GetForwardVector();
        FRotator Rotation = UKismetMathLibrary::MakeRotFromX(Forward);
        FVector UnrotatedVector = Rotation.UnrotateVector(Forward);
        This just gives a return FVector of {1, 0, 0}. This seems like a redundant and incorrect usage of UnrotateVector though, because no matter what forward vector you start with, it will always return that result. And that is also the same forward vector as the thing I need to rotate anyway. I need to rotate the scene component such that its child arrow component now points along {1,0,0}.

        But when I try to unrotate the starting vector of {1,0,0} using the rotation of the arrow component like this:

        Code:
        FVector SceneComponentForward = SceneComponent->GetForwardVector(); // this is {1,0,0}
        FVector Forward = Arrow->GetForwardVector();
        FRotator Rotation = UKismetMathLibrary::MakeRotFromX(Forward);
        FVector UnrotatedVector = Rotation.UnrotateVector(SceneComponentForward);
        That doesn't produce the intended result either. So either this function doesn't do what I need it to do, or I'm misunderstanding and using it wrong (fairly likely to be honest).

        Comment


          #5
          Originally posted by ThiloN1987 View Post
          Try this

          Click image for larger version

Name:	rotscene.jpg
Views:	48
Size:	62.3 KB
ID:	1788953
          AH! That does the trick! Thank you so, so much.

          SOLVED!

          Comment


            #6
            Originally posted by localstarlight View Post

            AH! That does the trick! Thank you so, so much.

            SOLVED!
            It's worth noting why this does what it does:

            Scene WorldRotation = Forwards.
            Arrow WorldRotation = Scene WorldRotation + AbitraryAngle
            Invert Arrow WorldRotation = (- Scene WorldRotation - ArbitraryAngle)

            CombineRotators
            Scene WorldRotation + Invert Arrow WorldRotation
            ==
            Scene WorldRotation + (- Scene WorldRotation - AbritraryAngle)
            ==
            Scene WorldRotation - Scene WorldRotation - AbitraryAngle = - ArbitraryAngle

            Scene AddWorldRotation ( - ArbitraryAngle)

            Comment

            Working...
            X