Sign in to follow this  
X5-Programmer

Rotate to face a special point.

Recommended Posts

Hi, I have some questions about how to rotate to face an special point. in this case I have this.. X,Z is the current point I will face. Here is how I do it now. float rotY; //Current rotation I will use for the player. glRotatef( rotY-90.0f, 0.0f, 1.0f, 0.0f ); DrawModel(); My function that I use is like this.
void Face( float playerX, float playerZ, float pointX, float pointZ)
{
	float x = pointX - playerX;
	float z = pointZ - playerZ;
	if (x!=0 && z<0)
	{
		rotY=((atanf(x/z))/PIOVER180)-180;
	}
	if (x!=0 && z>0)
	{
		rotY=((atanf(x/z))/PIOVER180);
	}
	if (x==0)
	{
		if (z>0)
		{
			rotY=0;
		}
		else
		{
			rotY=180;
		}
	}
	if (z==0)
	{
		if (x>0)
		{
			rotY=90;
		}
		else
		{
			rotY=270;
		}
	}
}

It work but sometimes the player look beside the x,z point.. dont know why. if anyone know a better way plz share ;)

Share this post


Link to post
Share on other sites
You might try rotY = atan2(x, z) instead. I don't know what your conventions are, so you may have to negate the result and/or add some multiple of pi/2 (90 degrees).

Share this post


Link to post
Share on other sites
Got it working now.. I just diden't need this part.


if (x==0)
{
if (z>0)
{
rotY=0;
}
else
{
rotY=180;
}
}
if (z==0)
{
if (x>0)
{
rotY=90;
}
else
{
rotY=270;
}
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this