Jump to content
  • Advertisement
Sign in to follow this  
Revelation60

Creating rotation matrix through the target point

This topic is 4061 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, 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!