26th December 2016, 08:00 PM | #8 |
Senior Member
Professional user
Join Date: Nov 2007
Posts: 139
|
Re: Determining roll, pitch, and yaw from a normalized vector
Code:
#include "math.h" #include "stdio.h" #include "stdlib.h" float Rad2Deg = 180 / M_PI; float Deg2Rad = M_PI / 180; int main(int argc, char* argv[]) { float pitch; float yaw; if (argc != 3){ fprintf(stderr, "USAGE:\n%s pitch yaw\n", argv[0]); return(-1); } pitch = atof(argv[1]); yaw = atof(argv[2]); pitch *= Deg2Rad; yaw *= Deg2Rad; float x, y, z; x = (float)(cos(pitch) * cos(yaw)); y = (float) sin(pitch); z = (float)(cos(pitch) * sin(yaw)); float len = (float)sqrt(x * x + z * z); float verify_pitch = (float)atan2(y, len); float verify_yaw = (float)atan2(z, x); printf(" verify_pitch = %0.5f , pitch = %0.5f\n", verify_pitch, pitch); printf(" verify_yaw = %0.5f , yaw = %0.5f\n", verify_yaw, yaw); return 0; } In Brian's original code he did: Code:
float len = (float)sqrt(x * x + y * y + z * z); atan2() selects the correct sector, but in most implantations it returns +/- 180 degrees, not 0-360 degrees. P.s. Sorry for the necropost, but I'm just reviewing my trig and vector math over the Christmas break. |
|
|