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:
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With