# How to calculate the 'rotation' between 2 coordinates?

The title may have been slightly misleading, however I have got no idea how better to describe it.

I have got 2 coordinates.

Opens up paint.net

If you look at that diagram, there is a line at the bottom. This is the sea/water that the ship will be floating on.

Now, we have 2 coordinates. One is the back of the ship, one is the front of the ship (Fine, they are off the ground. I am a bad drawer).

As you can see, the front coordinate is higher than the back one. That is because the ship has hit a land or beach, and so the front is obviously higher (i.e. ship lands but back is still in the water).

However, to tip the ship backwards, my rendering engine/library (OpenGL) requires a set rotational value.

So, I need to get the rotation from the bottom coordinate to the top coordinate, the sea level being 0 degrees.

What is the mathematical formula to do this?

Thanks!

P.S. Sorry if I am missing something. I am 14 and do not know my rotation matrices excellently yet.

• Is the boat/sea 2D or 3D? – user7530 Jun 23 '14 at 20:18

We note that for two vectors $\vec{a}$ and $\vec{b}$ in 3-dimensional Euclidean space ($\mathbb{R}^{3}$) we can define the dot product:

$$\vec{a}\cdot\vec{b}=\|\vec{a}\|\|\vec{b}\|\cos(\theta)=a_{x}b_{x}+a_{y}b_{y}+a_{z}b_{z}$$

Where $\theta$ is the angle between the two vectors, we can therefore write:

$$\theta=\cos^{-1}\left(\frac{a_{x}b_{x}+a_{y}b_{y}+a_{z}b_{z}}{\|\vec{a}\|\|\vec{b}\|}\right)$$

Where $\|\vec{a}\|=\sqrt{a_{x}^{2}+a_{y}^{2}+a_{z}^{2}}$ is the standard Euclidean norm (or vector length). We can find our vector $\vec{a}$ by taking the co-ordinates of the upper point away from those of the lower point. We can then use the line on the ground as our $\vec{b}$ vector (which will probably be the x-axis) and then you can calculate $\theta$ as above.

Here is a brief pseduocode function that will compute the angle:

float BoatAngle( CPoint pointTop, CPoint pointBottom )
{
CVector vecOrientation = pointTop - pointBottom;
return acos( vecOrientation.m_x / vecOrientation.length() );
}


Where CVector::length() would be defined as follows:

float CVector::length()
{
return sqrt( m_x * m_x + m_y * m_y + m_z * m_z );
}


And m_x, m_y, m_z are the x-,y- and z-coordinates of the vector.

• Eh, how can I convert this into code format? As in, what is with all the big brackets and lines that I cannot seem to understand? – user2722083 Jun 23 '14 at 20:47
• @user2722083 See my edit for a brief pseudocode implementation (assuming your sea lies along the $x$-axis). – Thomas Russell Jun 23 '14 at 22:05
• @sharktal Err. The sea is just a height, the height being 50, which is the Y. There is nothing more to it. – user2722083 Jun 23 '14 at 22:07