• 13
• 14
• 27
• 9
• 9

Turning my enemies

This topic is 4193 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have two angles in the range of 0 to 2pi. One is the current angle of the enemy, and the other is the angle it needs to turn to. Based on the angles, I need to know weather a left or right turn is faster. I also need some way of telling when to stop turning, when the enemies angle has overshot the goal angle. I don't see any simple way of doing this, any ideas? Note: I pulled the angle of my enemy out by using atan on its look vector. When I want to turn to something, I use an x,y point on my terrain, and the x,y of the enemy. I then come up with an angle from that in which I use as my goal angle. Is this a good system? Thank you Dev578

Share on other sites
This problem can be expressed in a way that eliminates the 'which direction should I turn' problem. Instead of thinking of it terms of a current angle and a target angle, just find the relative rotation (signed angle) that will align the object with the target. You can find this angle as follows:
Vector2 diff = target - position;float angle = atan2(perp_dot(forward,diff), dot(forward,diff));
The angle returned is an exact solution, as it represents both the direction you need to turn and how much.

If you don't want to 'snap' directly to the target, you can use the information in other ways. For example, you could just turn in the appropriate direction by a fixed amount, or by the magnitude of the angle, whichever is less, or you could interpolate to the goal orientation.

As for stopping rotation when aligned with the target, I would simply not change the object's orientation if the angle as computed above is below some small value.