Sign in to follow this  
Revelation60

Creating rotation matrix through the target point

Recommended Posts

Hi, In the game I am creating the enemies should first look at the player before they start to shoot. Also, when the player runs, the enemies should always look at him. I use a 4*4 matrix for the enemy. So when I have the player pos I should be able to rotate that matrix to let them view the player. The rotation will be one on the y-axis. This was my theory:
Quote:
float angle = atanf((Enemy.pos.z - objCamera.mPos.z) / (Enemy.pos.x - objCamera.mPos.x)); Enemy.LocalMatrix[0] = cosf(angle); Enemy.LocalMatrix[2] = sinf(angle); Enemy.LocalMatrix[8] = -sinf(angle); Enemy.LocalMatrix[10] = cosf(angle);
But this only works if the player is in the first quadrant. Is there an easy way to fix this or should I check in which quadrant the player is?

Share this post


Link to post
Share on other sites
You can use atan2() instead of just atan() when you are getting the angle. atan2() takes two arguments and will correct for which quadrant the angle is in.

Share this post


Link to post
Share on other sites
Any time you apply a direct-inverse trig function pair, there's a mathematical identity you should probably be using instead.

cos(atan(x)) = sqrt(1 / (1 + x^2)); sin(atan(x)) = sqrt(x^2 / (1 + x^2))

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