Rotate a vector around another vector?

Miscellaneous Forums/General Discussion/Rotate a vector around another vector?

If I have vector A, and I want to rotate it n degrees around vector B, what is the equation for this?

Ax#,Ay#,Az# is pointing somewhere.

Bx#,By#,Bz# is an axis I want to rotate vector A around by n degrees.

heh, I thought I needed this the other day - luckily, I didn't.

Anyway, to rotate a vector around an arbitrary axis, I think will invovle constructing a rotation matrix which you multiply your vector by. Pretty sure you will be able to find out how to do it on this site:
http://www.euclideanspace.com/maths/

There is a plane which is perpendicular to vector B which contains the endpoint of Vector A.

Imagine a circle on a peice of paper with a point in the center of it.

The paper is the plane. The point is where vector B passes through the plane.

The circle itself contains of all points where Vector A's endopoint could be depending on how the plane is rotated around the center axis defined by Vector B.

There is no up or down here because you only have one vector. Luckilly, we don't need a fixed UP direction, because you only want to rotate the point clockwise or countsreclockwise around the center axis, and don't need to know what angle it is actually at. So we can pretend point A itself defines what direction UP is, and and stick it at the top of the circle.

Now that we know the direction of our X and Y axis in the plane, we can rotate the point in 2D like so:

NewX# = Cos(Angle#)*X# - Sin(Angle#)*Y#
NewY# = Cos(Angle#)*Y# + Sin(Angle#)*X#


To get to this point though, and to complete the process, you will need to figure out how rotate one vector into the space of another. I think you can use the above equation for that as well. You will need to do it in two steps. First you need to find out how far you need to rotate vector B on the XY plane to rest it on the Y axis. Then you need to rotate vector A by the same arouny. Then you need to do the same from overhead and align it to the Z axis maybe. Then Vector B will be aligned to one of the three axis, and Vector A will be in vector B's space. And that means that the plane in which you have to rotate A will be aligned to two axes, and you can then rotate A in 2D as outlined above.

There may be a simple eqation for rotating one vector into the space of another though. I would look for that.

Once you have that it is simply:


Calculate position of vector endpoint A in the space of vector B.

Rotate A's enpoint in the 2D plane perpendicular to B which contains it using this:

NewX# = Cos(Angle#)*X# - Sin(Angle#)*Y#
NewY# = Cos(Angle#)*Y# + Sin(Angle#)*X#

Transform resulting point in vector B space back into global space.


Sorry I can't make it any simpler, but I'm just flying by the seat of my pants here.

That sounds about right. I know you can do it without messing with a matrix, just using a lot of sines and cosines.

Thanks.

Heh. That is doing it with a matrix, it's just writing it out longhand. :)

Yeah, that's what I was thinking, teamonkey - not that I usually know what I'm talking about when it comes to maths. :P I figure using a matrix will be a more compact way of doing it, since you can transform the vector in a single hit?!

You don't need a 4x4 matrix.

teamonkey:
Matrices are one of my weak points in math. I know what they are. And I sort of know how you multiply one with another. And I know how they are commonly used in 3D graphics.

But I haven't got a clue how you go about making one that does what you want, or when is the right time to do that. Nor can I look at a matrix and make heads or tails of what it is supposed to do. I also don't understand why there's often blank spaces along the top and a one in the lower right hand corner. Well I might have an inkling of why that might be. But I'm not at all sure.

I think you only need a 3x3 matrix to do this, but whatever...

swifty: Did you look at that site I linked to? It's got some pretty good info on there.

You're right, for a simple rotation you don't need the full 4x4 matrix, only 3x3 is used for rotation, the other bits are used for scaling and movement and are padded with 0s and 1s.

To apply a rotation operation on a vector you do:

Vout = M * Vin

Where M is a 3x3 matrix that you've built up.

This explains how you might build one up: http://sjbaker.org/steve/omniv/matrices_can_be_your_friends.html

And check out the "rotations about the origin" section of this page: http://www.mines.edu/~gmurray/ArbitraryAxisRotation/ArbitraryAxisRotation.html

The second equation in that section is your answer, it's your new X,Y,Z and a 1 from top to bottom. It looks really nasty but it can be greatly simplified, for example if the vector you're rotating around is a unit vector (length = 1.0) then Sqr(u^2 + v^2 + w^2) = 1 and (u^2 + v^2 + w^2) = 1. Also, you can use temporary variables to store the results of commonly used bits so you only need to calculate them once: sin(t), cos(t), ux, vy, wz.

Most programmers are visual thinkers and don't take kindly to piles of abstract math.
Heh, that nicely sums up my problem with most math operations.

not me... i'm just dumb...

really... i hated math in high school, especially when it came to algebra... and i cut most of those classes...

... i'm paying for it now.

--Mike

TeaMonkey, thank you. Here is the function, translated to Blitz:

;Rotate the point (x,y,z) around the vector (u,v,w)
Function RotatePointAroundVector(x#,y#,z#,u#,v#,w#,a#)
ux#=u*x
uy#=u*y
uz#=u*z
vx#=v*x
vy#=v*y
vz#=v*z
wx#=w*x
wy#=w*y
wz#=w*z
sa#=Sin(a)
ca#=Cos(a)
x#=u*(ux+vy+wz)+(x*(v*v+w*w)-u*(vy+wz))*ca+(-wy+vz)*sa
y#=v*(ux+vy+wz)+(y*(u*u+w*w)-v*(ux+wz))*ca+(wx-uz)*sa
z#=w*(ux+vy+wz)+(z*(u*u+v*v)-w*(ux+vy))*ca+(-vx+uy)*sa
End Function


It might be possible to simplify it further, since this is for a point, and I just need to rotate the vector (a point with a magnitude of 1.0).

Beware the Gimbal Lock .

Gimbal lock doesn't matter if you're making something with a fixed camera or a normal camera [FPS, RPG, etc.]