# Rotate to face a special point.

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

## 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 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 on other sites
ok, I will try that.

thanks.

##### 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;		}	}}

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 19
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632605
• Total Posts
3007372

×