Sign in to follow this  

Face an point

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

Hello, I have been trying to get the best way to make a function that rotates my character to face a specific point. I have done something like this:

#define PIOVER180  0.0174532925199432957692369076848861f

float rot;

void Face(float pointX, float pointZ)
{
	float x = pointX - PlayerX;
	float z = pointZ - PlayerZ;
	if (x!=0 && z<0)
	{
		rot=((atanf(x/z))/PIOVER180)-180;
	}
	if (x!=0 && z>0)
	{
		rot=((atanf(x/z))/PIOVER180);
	}
}



glRotatef( rot-90.0f, 0.0f, 1.0f, 0.0f ); -- Draw my character -- If someone have any better way to do this please feel free to post. Thanks.

Share this post


Link to post
Share on other sites
I would suggest looking into the atan2() function as an alternative to atan(); it's more stable, and will simplify the code (you won't have to determine the quadrant manually).

Share this post


Link to post
Share on other sites
To make it look better in game (your current algorithm appears to "snap" the player to the facing angle) you want to clamp the rotation to no more than some max value. i.e. no more than 5degrees per second or whatever number looks good.

You find the angle between the current facing direction and the desired facing direction. if that is 30degrees clockwise, then you'd only rotate Xdegrees clockwise this frame where X is calculated based on your clamping angle.

For extra credit make that max rotation rate tunable in data somewhere so you don't have to re-compile to tune the visual result. for super-extra credit, make it so that you don't have to restart the game to change the number. =)

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by X5-Programmer
About that you said, if you have time to make some example on how to do it? Thanks.


Sadly, no. What I wrote should be enough of an algorithm for you to code it. Was there a specific bit of what I described that was confusing?

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
I would suggest looking into the atan2() function as an alternative to atan(); it's more stable, and will simplify the code (you won't have to determine the quadrant manually).


Thank you for bringing this function to my attention. I've always used atan() myself. Yay new and better information!

Share this post


Link to post
Share on other sites

This topic is 4012 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.

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