Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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
	alpha = RadToDeg(acos(pos.x/len)) - 90;

	// beta is arc cosine of y/length
	beta = RadToDeg(acos(pos.y/len)) - 90;

	// gamma is arc cosine of z/length
	// positive z here is away from the screen and not into so subtract from 90
	gamma = 90 - RadToDeg(acos(pos.z/len));

	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
	matrix44 RotAroundX = RotateRadMatrix44(vector3(1, 0, 0), DegToRad(gamma));
	// rotation around y axis -> angle made with the Y axis
	matrix44 RotAroundY = RotateRadMatrix44(vector3(0, 1, 0), DegToRad(beta));
	// rotation around z axis -> angle made with the x axis
	matrix44 RotAroundZ = RotateRadMatrix44(vector3(0, 0, 1), DegToRad(alpha));
	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 this post

Link to post
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 heading=atan2(z,x);
float d=sqrt(x*x+z*z);
float pitch=atan2(y,d);

matrix44 RotHead = RotateRadMatrix44(vector3(1, 0, 0), heading);

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

return (RotHead * RotPitch);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!