[Math] Vector operation question – rotate a 2d vector based on angle

euclidean-geometryvectors

I think this question is pretty basic to someone who has knowledge about vector operations. I am developing a game and I have ran into a problem where I have the following vectors. For simple understanding I made a colored explanation:

Vector problem
The purple vector is based on user input (so I will always know it). The blue vector is just pointing up (this is just for explanation). I also have the green vector which is fixed in length but is based on the player's rotation which can be anything, this is basically forward direction for my player.

What I am planning to do is to create the red vector which should be the same length as the purple one. Also, the red vector's angle should differ by the same angle from the green vector as the purple vector differs from the blue one. I believe this is called theta (sorry if I am incorrect I only learnt vectors in high school 🙂 )

So my main question is: what kind of transformation should I do to get the red vector from the other 3 input vectors?

My idea is to multiply the blue vector with the purple (Purple xGreen x,Purple yGreen y) but I have no idea about the angle "transformation". (I have even watched youtube for like two hours now, but this seems to be not a basic operation)

Thank you in advance for your kind help!
David

P.s.: If you can give me hints about places where I can learn more about this topic I would be happy as well, because I think I will need this kind of knowledge in the future too.

Best Answer

No need for trig, poorly conditioned calcs of $θ$, or matrices

You did correctly intuit that you need the "right" Vector Product(s)

the Vector Algebra taught in High School, early Undergrad today is Gibbs Vector Algebra which, though stuck in 3D, can be used for 2D calcs like this although some may never see or use the Vector Triple Product used in the below expression for rotating the Green vector to create the Red output vector
To use the Gibbs 3D Cross Product with the 2D vectors of the problem, the 2D vectors can be temporarily "promoted" to 3D vectors by augmenting each with a $z = 0$ component
then the Gibbs Vector Algebra formula for rotating g (3D vector derived from the 2D Green) by the angle between p, b (3D vectors derived from the 2D Purple, Blue, resp.) is:

$\left(\left(p\cdot b\right)g+(p\times b)\times g\right)/\begin{vmatrix}p\end{vmatrix}$

For coding the operation you may want to go down to the Vector Component level depending on your programing language, environment choices

1st a little help from GeoGebra's Computer Algebra System with the Triple Cross Product term: enter image description here

Still requires hand massaging (as is typical of CAS output) but shows the simplifications with the $z$ components zero in all of the input and output vectors
Adding in by hand the Dot Product term and the Normalization, the final by component rendering isn't that daunting:

(dropping the temporary z component to get a 2D output)

$(r_{x},\;r_{y}) =$ $\left[\left(p_{x}b_{x}+p_{y}b_{y}\right)g_{x}+(b_{x}p_{y}-b_{y}p_{x})g_{y},\;\left(p_{x}b_{x}+p_{y}b_{y}\right)g_{y}+(b_{y}p_{x}-b_{x}p_{y})g_{x}\right]/\sqrt{p_{x}^{2}+p_{y}^{2}}$

The equations could still have typos but the method is verified in a "live" dynamic geometry GeoGebra worksheet I uploaded to Geogebra:
https://www.geogebra.org/m/hASsFa5F

pic below is static, the Geogebra link should get you to the live worksheet where the spherical points at the heads of of the input vectors can be dragged with the result updating dynamically (although I did the full normalization of the Rotor there so the output magnitude is independent of both Purple, Blue magnitudes, only tracks Green)

The pink z axis vector is the $(p\times b)$ term, which, together with $\left(p\cdot b\right)$ are the components of a 2D Rotor expressed in Gibbs 3D Vector Algebra
The Rotor is a good object to encode the rotation with, particularly with the input given as a pair of vectors $p, b$
If you want to reverse the sense of the output angle rotation relative to the input vector pair ording, just change the sign in front of the $(p\times b)$ term

The Rotor equation is simpler in Geometric Algebra which I highly recommend even though the final calculation from components ends up the same
The wedge product of Geometric Algebra works in any dimension so the "promotion/demotion" step to use the Gibbs 3D Cross Product isn't required in GA, everything can stay in 2D

There are now many online (and dead tree) Geometric Algebra resources: Geometric Algebra for Computer Graphics may be especially relevant to this question

Related Question