Jump to content
  • Advertisement
Sign in to follow this  
X5-Programmer

Rotate to face a special point.

This topic is 4746 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

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!