# Orientation woes

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, I am trying to orient a gun in the direction of the object all the time. The gun is at 0, 0, 0 and the position of object is at x, 5, z. However at values of x = 0 and z = 40 or -40 or something around 30 or more the angle it makes is so low that the angle it makes is about 3degrees and at times it becomes zero. This is the code I am using to calculate the angles
matrix44 CalculateOrientation(vector3 &pos)
{
// this will calculate orientation in x, y, z angles
// from the origin only
double alpha, beta, gamma;

// first get length of vectors
float len = pos.length();

/**************************************************************************************
* We should subtract 90 from angle because we are obtaining the angle that is made   *
* with respect to the axis - but what we require is how much we need for orienting   *
* the object.                                                                        *
*************************************************************************************/

// alpha is arc cosine of x/length

// beta is arc cosine of y/length

// gamma is arc cosine of z/length
// positive z here is away from the screen and not into so subtract from 90

fprintf(fp, "%.2f %.2f %.2f\n", alpha, beta, gamma);

//float test = (cos(alpha)*cos(alpha)) + (cos(beta)*cos(beta)) + (cos(gamma)*cos(gamma));

// our rotation matrix gives rotation around the x, y, z axis
// but we have the angle that the line makes with the origin

// rotation around z axis -> angle made with the Z axis

// rotation around y axis -> angle made with the Y axis

// rotation around z axis -> angle made with the x axis

return (RotAroundX * RotAroundY * RotAroundZ);
}


Is there anyway I can get around this problem ? Does the angle of zero mean that target is now out of range ? Should I set a limit to how much it can rotate ? Or am I doing the maths incorrectly ? Thanks

##### Share on other sites
First off, you only need two angles, not three. If you wanted to look at something, you would turn your head horizontally in that direction(heading), then tilt it up or down(pitch). Also, there is no reason to convert from rads to deg and then back.

The math:

float d=sqrt(x*x+z*z);
float pitch=atan2(y,d);

matrix44 RotPitch = RotateRadMatrix44(vector3(0, 1, 0), pitch);

• 18
• 29
• 11
• 21
• 16