Jump to content

Rotating a vector by another vector


Recommended Posts

My algebra is rusty as hell, basically I have two 3-dimensional vectors and I want to rotate one of them by the angle(s) the other one is rotated. For example, in this image, I want to rotate the orange vector by the blue vector.

1yBlrr3.png

How do I do this without computing calculating costly trigonometrics every frame? I know you have to use a matrix or quaternion but both are confusing to me.

Link to comment
Share on other sites

green = Quaternion.AngleAxis(180, blue) * orange

Which mean rotate orange by 180° arround the blue vector.

And yes, this is using a quaternion because that's how you rotate stuff without "costly trigonometrics every frame?"

Edited by sarbian
Link to comment
Share on other sites

green = Quaternion.AngleAxis(180, blue) * orange

Which mean rotate orange by 180° arround the blue vector.

And yes, this is using a quaternion because that's how you rotate stuff without "costly trigonometrics every frame?"

I'm not sure this is correct, I don't get the 180 degrees part, I'm trying to rotate the orange vector around the z-axis by the angle between the blue vector and the black line.

Also, can I do this in a shader?

Edit: Oh wait wait, you're correct, anyway, how to do it in CG?

Edited by blackrack
Link to comment
Share on other sites

What I gave work for the example but it may not me what you are looking for.

First let me state that what you ask is not really precise. Your example is obviously in 2D so I can only assume that your vector are in the same plane.

To get the angle between Orange and blue :

angle = Quaternion.Angle(orange, blue)

To get the axis you are turning around ( you called it Z so it may be just equal to Vector.front but I'll calculate it anyway)

axis = Vector3.Cross(orange, blue)

To get green :

green = Quaternion.AngleAxis(angle, axis) * orange

As for the shader part : no idea. I am bad at shader :)

Link to comment
Share on other sites

What I gave work for the example but it may not me what you are looking for.

First let me state that what you ask is not really precise. Your example is obviously in 2D so I can only assume that your vector are in the same plane.

To get the angle between Orange and blue :

angle = Quaternion.Angle(orange, blue)

To get the axis you are turning around ( you called it Z so it may be just equal to Vector.front but I'll calculate it anyway)

axis = Vector3.Cross(orange, blue)

To get green :

green = Quaternion.AngleAxis(angle, axis) * orange

As for the shader part : no idea. I am bad at shader :)

Well it's hard to explain without making a 2d analogy but let's just say I have 3 vectors A, B and C in three dimensional space.

Vector A and B are in a random orientation in space. Vector C has a known orientation relative to vector A. I want to find vector C' such as C' has the same relative orientation to B, that C has to A. Does this make sense?

In the example I posted, vector A would be the black line if it was a vector pointing up, vector B would be blue, vector C would be orange and C' would be green.

The way I understand it now, I think I have to create a quaternion from vector A to vector B, then multiply it by C.

Basically, C'=C* Quaternion.SetFromToRotation (A,B)

Someone correct me if I'm wrong.

Edited by blackrack
Link to comment
Share on other sites

Your example is obviously in 2D so I can only assume that your vector are in the same plane.

I think it's safe to assume that "the rotation takes place in the plane that contains both vectors A and B" making this automatically a rotation in a two-dimensional plane (but not necesarilly and in fact highly unlikely, a plane perpendicular to the x, y or z-axis.)

Link to comment
Share on other sites

And you point is ? The code I posted does just that. Without that assumption there is an infinite number of vector where the angle between blue and green is the same as green and orange.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...