# Rotate relative to set point in a specified direction in a 3D space

I know there's a geometry formula to calculate the next point in rotation relative to another point in 2D but I'm working in 3D and I want to calculate the next position in specified direction as well. Think Spiderman or the Bloodstalkers from ARK. I'm working in a game engine that uses the basic {x = 1, y = 2, z = 1} for vectors. I'm pretty bad at math so I honestly can't figure this out myself.

• Can you clarify what rotation you want to perform? What point, needs to rotate around what axis, around what pivot, by what angle? Nov 19, 2020 at 21:51
• Are you asking how to make something "swing" (move in a circular path) around some anchor point in 3D, in a given "swing plane"? If so, it would help if you added the 2D formula you're using to your question, I think it can be converted to its 3D analogue in a relatively straightforward way, provided that you can find two perpendicular unit vectors that would define the swing plane. Nov 20, 2020 at 0:44

First of all, game engines and math go hand in hand so understanding the underlying theory should be high in your priority list. I will try to explain this at a high level, based on what I understood and will try to elaborate later when I get the time. I will also put in bold terms you should look at.

Taking the spiderman swinging example, this means you have 2 points, namely the building corner the spiderweb is attached to and spiderman himself. These 2 points will give you a vector which we will name spiderwebVector. There is also a second vector which is what you refer to as specified direction. I'm assuming you know which direction that is (the direction spiderman is going). Let's name that vector swingdirectionVector.

Your steps should be the following:

1. Execute a cross product between the 2 vectors. This will give you a 3rd vector which will be the axis of rotation (the vector around which you are rotating).
2. Execute a second cross product between the spiderwebVector and the axisVector. This will give you another vector. (note: the new vector will probably have the same or inverse direction as the swingdirectionVector).
3. Normalize the 2 new vectors and the spiderwebVector. These new unit vectors now form a new basis. Essentially they are a new set of $$\x',y',z'\$$ basis vectors but their coordinates are expressed in standard basis $$\x,y,z\$$ values. Assume $$\z\$$ is the up vector.
4. Any rotation of angle $$\θ\$$ around an up axis (in our case either $$\z\$$ or $$\z'\$$) is defined by the following rotation transformation matrix $$R=\begin{pmatrix} \cos\theta & -\sin\theta & 0 \\ \sin\theta & \cos\theta & 0 \\ 0 & 0 & 1 \end{pmatrix}.$$
5. Because the rotation is around $$\z'\$$ (remember, the new basis) this transformation will rotate the points by $$\θ\$$ assuming they are expressed in the new basis coordinates, which I assume is not your case since everything is expressed in the standard basis $$\{x,y,z}\$$.
6. In order to create the transformation that will rotate all the points expressed in standard basis coordinates, we execute a change of basis transformation first. This is another 3x3 matrix. Each column of that matrix represents $$\x',y'\$$ or $$\z'\$$ expressed in the standard basis. For example if $$\x'=(0.57,0.57,0.57)\$$ then the first column will be $$\begin{pmatrix} 0.57 \\ 0.57 \\ 0.57 \end{pmatrix}.$$ We now have a change of basis matrix $$C=\begin{pmatrix} a & d & g \\ b & e & h \\ c & f & i \end{pmatrix}.$$
7. Finally, to execute the whole rotation, we sandwich the $$\R\$$ rotation matrix with the change of basis matrix and its matrix inverse $$U=CRC^{-1}$$ It is important to note that the inverse comes on the right. The reason for this is a little more complicated but if you understand the change of basis concept it makes sense. More info can be found here.
8. You take the $$\U\$$ transformation matrix and you multiply it with the vector that needs to rotate, in this case the spiderwebVector. Note that the vector must come from the right-multiplication U * spiderwebVector. This will give you a new vector that points to the new location spiderman ends up at after a rotation of angle $$\θ\$$.

I believe this is the gist of it, however I'm also new at this and any input/edits/corrections from more experienced members is very welcome.