# 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.

## 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 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 on other sites
Thank you :)

I always thought the tan2 was for some sort of assembly optimalisation, but that it did the same. :P

##### 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))

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

• 12
• 9
• 31
• 16
• 9
• ### Forum Statistics

• Total Topics
632612
• Total Posts
3007423

×