Calculate Pitch and Yaw between two unknown points
angle, computational-geometry, java
Solution
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!
Problem
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: - Pitch can only be below or equal to 90, or above or equal to -90 (90 => pitch => -90) - There is never a roll so that does not apply - All co-ordinates are unknown until declared in the program - Pitch starts along the Z axis, which is forward/backward, where upwards pitching is positive - Y is up, Z is forward/backward, X is sidewards - 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.