Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Pitch and Yaw between two unknown points

I've been working with points in a 3D space (X,Y,Z) and want to be able to calculate the pitch and yaw between two of these points.

My current code:

pitch = (float) (1/Math.tan((Y1 - Y2) / (Z1 - Z2)));
yaw = (float) (1/Math.tan((X1 - X2) / (Z1 - Z2)));

Where X1, X2, Y1, Y2, Z1, Z2 are all unknown until run time, at which point they are gathered from the two randomly generated points.

For some reason though, my results are drastically wrong, I've tried many different combinations and googled countless things but came up with nothing.

Some constraints are:

  1. Pitch can only be below or equal to 90, or above or equal to -90 (90 => pitch => -90)
  2. There is never a roll so that does not apply
  3. All co-ordinates are unknown until declared in the program
  4. Pitch starts along the Z axis, which is forward/backward, where upwards pitching is positive
  5. Y is up, Z is forward/backward, X is sidewards
  6. Yaw starts facing directly down the Z axis

It's my first time working with 3D angles, and I've read that pitch and yaw can be calculated separately, as in my example, on two different 2D planes, but that doesn't work.

Any help much appreciated.

like image 346
pathurs Avatar asked Mar 23 '23 19:03

pathurs


1 Answers

Maybe this will help you. It is a tutorial by LucasEmanuel it is a pitch, yaw calculation for Minecraft BUT it should work for yours too!

First you have to calculate delta x, y, z:

double dX = first_location.getX() - second_location.getX();
double dY = first_location.getY() - second_location.getY();
double dZ = first_location.getZ() - second_location.getZ();

After that you have to calculate the yaw:

double yaw = Math.atan2(dZ, dX);

and the pitch for it:

double pitch = Math.atan2(Math.sqrt(dZ * dZ + dX * dX), dY) + Math.PI;

If you need a vector do it like:

double X = Math.sin(pitch) * Math.cos(yaw);
double Y = Math.sin(pitch) * Math.sin(yaw);
double Z = Math.cos(pitch);

Vector vector = new Vector(X, Z, Y);

Original Tutorial

Basically the calculation should be the same. I am sorry if it dosn't help you!

like image 147
Gerret Avatar answered Mar 25 '23 19:03

Gerret