How to calculate the 'rotation' between 2 coordinates?

759 Views Asked by At

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 Image

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.

1

There are 1 best solutions below

3
On BEST ANSWER

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.

I hope this helps, if you have any further questions don't hesitate to leave a comment!


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.