# Coordinates of 3D vector in rotated coordinate system (without using a matrix)

The problem: There is a vector with coordinates X,Y,Z. This vector is in a coordinate sytem that has been rotated by A degrees along the X axis and B degrees along the Y axis. I would like to know the Z component of this vector in a non-rotated coordinate system.

The question: I can't seem to understand the usage of matrices and it seems that would be needed. Is their a series of operations after which I get the Z coordinate?

A little backstory for those interested: I'm writing a drone flight controller and I can calculate the rotation of the drone using an accelerometer and a gyroscope. Now I would like to use the accelerometer to help with altitude controls. The XYZ values of that change depending on the rotation of the drone, but I need the change in altitude relative to the ground.

• Welcome to Math.ME. I've attempted to answer. Besides the help you may get from here, you may consider to ask to move your question to physics. – Ertxiem Apr 22 at 18:39

## 1 Answer

The rotation matrices (taken from Wikipedia) are:

\begin{align} Q_{\mathbf{x}}(A) &= \begin{bmatrix} 1 & 0 & 0 \\ 0 & \cos A & \sin A \\ 0 & -\sin A & \cos A \end{bmatrix}, \\[8px] Q_{\mathbf{y}}(B) &= \begin{bmatrix} \cos B & 0 & -\sin B \\ 0 & 1 & 0 \\ \sin B & 0 & \cos B \end{bmatrix}, \\[8px] Q_{\mathbf{z}}(C) &= \begin{bmatrix} \cos C & \sin C & 0 \\ -\sin C & \cos C & 0 \\ 0 & 0 & 1 \end{bmatrix}, \end{align}

The way to use them is to multiply one matrix by the current position $$(x,y,z)$$ vector to get the rotated vector $$(x',y',z')$$ and then multiply the other matrix by the resulting vector to get the result $$(x'',y'',z'')$$. However, from the information you provided I can't tell the exact order ad the angles may have their sign exchanged.

$$\begin{pmatrix} x' \\ y' \\ z' \end{pmatrix} = \begin{pmatrix} 1 & 0 & 0 \\ 0 & \cos A & \sin A \\ 0 & -\sin A & \cos A \end{pmatrix} \cdot \begin{pmatrix} x \\ y \\ z \end{pmatrix}$$

And $$\begin{pmatrix} x'' \\ y'' \\ z'' \end{pmatrix} = \begin{pmatrix} \cos B & 0 & -\sin B \\ 0 & 1 & 0 \\ \sin B & 0 & \cos B \end{pmatrix} \cdot \begin{pmatrix} x' \\ y' \\ z' \end{pmatrix}$$

However, you may want to track your position and have the velocity vector in the drone coordinates and you may want to update your estimated position using numeric integration.

$$x (t+\Delta t) = x(t) + v_x(t) \cdot \Delta t \\ y (t+\Delta t) = y(t) + v_y(t) \cdot \Delta t \\ z (t+\Delta t) = z(t) + v_z(t) \cdot \Delta t$$

With the velocity vector $$(v_x(t),v_y(t),v_z(t))$$ being computed using the rotation matrices as before, starting with a vector like $$(v_{fw},0,v_{up})$$ or $$(v_{fw},v_{up},0)$$, where $$v_{fw}$$ is the velocity forward in the coordinate system of the drone and $$v_{up}$$ is the velocity upward in that coordinate system.

• So if I understand this correctly, the values differ depending on the order of the rotations. What should my order be if the rotations take place basically simultaniously or randomly? – IGClusterFck Apr 22 at 19:33
• The order of the rotations depends on the specifications of the drone. I'm sorry I can't be more helpful, but you might get there by trial and error. And note that the vertical axis might be $z$ or $y$ (or, in theory, $x$). – Ertxiem Apr 22 at 21:06
• Probably it won't help much but the drone starts to rotate along the x axis and like a microsecond later it starts rotating along the y axis so they basically happen at the same time. I'm glad that u pointed me in the right direction, I will ask other communities as well – IGClusterFck Apr 22 at 21:42
• If the rotations are very small in the time resolution of the sensors, it probably will not make a big difference the order, although it will be more precise to have the correct order. – Ertxiem Apr 22 at 21:43