View Single Post
Old 26th December 2016, 08:00 PM   #8
jentron
Senior Member
Professional user
 
Join Date: Nov 2007
Posts: 139
Default 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;
}
For posterity. In the function above, Y is up, X is yaw 0, Z is yaw 90.

In Brian's original code he did:
Code:
float len = (float)sqrt(x * x + y * y + z * z);
He said himself the vector is normalized, so that formula always returns 1. We need to exclude the value that is not multiplied by the cosine.

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.
jentron is offline   Reply With Quote